I want to graph response surface through scatterplot3d but the following code through an error.
library(rsm)
swiss2.lm <- lm(Fertility ~ poly(Agriculture, Education, degree = 2), data = swiss)
persp(swiss2.lm, Education ~ Agriculture, zlab = "Fertility")
library(scatterplot3d)
s3d <-
scatterplot3d(
swiss
# , type = "h"
, highlight.3d = TRUE
, angle = 55
, scale.y = 0.7
, pch = 16
)
s3d$plane3d(swiss2.lm, lty.box = "solid")
How can I figure out the issue?
Edit
Error in segments(x, z1, x + y.max * yx.f, z2 + yz.f * y.max, lty = ltya, :
cannot mix zero-length and non-zero-length coordinates
I’m using swiss data from rsm library.
How attached are you to using
scatterplot3d? If you’re willing to do it inrglit’s pretty easy. Following from your example:Set up evenly spaced grid and make predictions:
Plot points and add surface:
rglhas some advantages (hidden line removal, lighting effects, dynamic rotation and zooming) and some disadvantages (doesn’t fit well into base-package layouts etc.; harder to manipulate fonts, includeplotmathequations, etc.; harder to adjust label placement and plot style). Thescatter3dfunction in thecarpackage has some nice features for adding regression surfaces to anrglplot, but as far as I can see it does additive models, but doesn’t allow for quadratic polynomial models …As far as I can see, in order to do this in the
scatterplot3dframework you would have to construct the points corresponding to the quadrangles in the regression surface and usexyz.convertandsegmentsto draw them …