I want to plot a regression plane for my data:
structure(list(L = c(96.4155, 76.803, 71.5615, 68.193, 65.6975, 74.627, 67.82, 64.26, 62.06, 60.35, 68.284, 63.7, 61.04, 59.05, 57.56, 64.2695, 60.69, 58.47, 56.78, 55.42, 61.3715, 58.27, 56.42, 54.91, 53.65), a = c(-0.8375, -20.47, -22.1875, -22.6125, -22.5845, -2.2415, -12.91, -16.16, -17.9, -18.77, -2.4, -10.28, -13.64, -15.33, -16.37, -2.4495, -8.66, -11.74, -13.46, -14.55, -2.4645, -7.58, -10.38, -12.06, -13.09), b = c(1.437, -24.5915, -29.3275, -31.892, -33.524, -5.151, -15.22, -19.61, -22.65, -24.8, -6.239, -13.24, -17.11, -19.64, -21.55, -6.7845, -12.18, -15.45, -17.68, -19.47, -7.124, -11.53, -14.38, -16.41, -17.89)), .Names = c("L", "a", "b"), row.names = c(NA, 25L), class = "data.frame")
I tried an example from here. And that is my try:
L<-X[1:25,2]
a<-X[1:25,3]
b<-X[1:25,4]
mod2 = lm(L~a*b)
open3d()
plot3d(x=a, y=b, z=L, type="s", col="red", size=1)
grd <- expand.grid(x1=a, x2=b )
grd$pred <-predict(mod2, newdata=grd)
persp3d(x=unique(grd[,1]), y=unique(grd[,2]),
z=matrix(grd[,3]), add=TRUE, alpha=0.5)
The 3d-Plot looks fine, but for the grid I get this error:
Error in persp3d.default(x = grd[, 1], y = grd[, 2], z = matrix(grd[, :
increasing 'x' and 'y' values expected
Where is the problem in my code and is there maybe another way to solve my problem? Thx for your help!
Update:
I've tried it with plane3d() but got this error:
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
However, after searching for other solution I figured out that my intention was wrong. I'm searching for something like this ...
...but have no clue how to attain it or if it is even possible with my actual knowledge of R. Someone any idea?
Update2:
I've tried the scatter3d() function with this code:
scatter3d(a,b,L,fit="smooth")
but got this error:
Error in smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) :
A term has fewer unique covariate combinations than specified maximum degrees of freedom
I already found, that I have to use choose.k to adjust the degrees of freedom, but it is always explained for the gam functions and I have no clue how to use it in the scatter3d function. Any idea?