0

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 ...

data
intention

...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?

Community
  • 1
  • 1
haPpy85
  • 1
  • 1

1 Answers1

0

I guess I've solved my problem with a very nasty solution:

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")

L<-data[1:25,1]
a<-data[1:25,2]
b<-data[1:25,3]
plot3d(a,b,L, col="red", size="3", xlab="a*", ylab="b*", zlab="L*", main=NULL)

#Generate order for quadruplets

a[c(1:64)] <- a[c(1,2,7,6,2,3,8,7,3,4,9,8,4,5,10,9,
                  6,7,12,11,7,8,13,12,8,9,14,13,9,10,15,14,
                  11,12,17,16,12,13,18,17,13,14,19,18,14,15,20,19,
                  16,17,22,21,17,18,23,22,18,19,24,23,19,20,25,24)] 
b[c(1:64)] <- b[c(1,2,7,6,2,3,8,7,3,4,9,8,4,5,10,9,
                  6,7,12,11,7,8,13,12,8,9,14,13,9,10,15,14,
                  11,12,17,16,12,13,18,17,13,14,19,18,14,15,20,19,
                  16,17,22,21,17,18,23,22,18,19,24,23,19,20,25,24)] 
L[c(1:64)] <- L[c(1,2,7,6,2,3,8,7,3,4,9,8,4,5,10,9,
                  6,7,12,11,7,8,13,12,8,9,14,13,9,10,15,14,
                  11,12,17,16,12,13,18,17,13,14,19,18,14,15,20,19,
                  16,17,22,21,17,18,23,22,18,19,24,23,19,20,25,24)] 

quads3d(a[1:64],b[1:64],L[1:64],col="red",alpha=0.5)

Is there any function who does exactly the same?

haPpy85
  • 1
  • 1