Draw a line after persp using rsm in R

2k views Asked by At

I used the following code to draw a 3d response surface with contour projection on the x-y plane in R using the persp function of the RSM package.

x <- seq(-3,3,by=0.25) 
y <- seq(-3,3,by=0.25) 
d <- expand.grid(x=x,y=y)
z <- c(data=NA,1089)
b0 = 5.628; b1 = 0; b2 = 0; b3 = -.1 ; b4 =.1; b5 = -.1
k=1
for (i in 1:25) {
for (j in 1:25) {
z[k]=b0+b1*x[i]+b2*y[j]+b3*x[i]*x[i]+b4*x[i]*y[j]+ b5*y[j]*y[j]
k=k+1
}  }
library(rsm)
data.lm <- lm(z~poly(x,y,degree=2),data=d)
persp(data.lm,x~y, zlim=c(0,max(z)),contour=list(z="bottom",col="colors"),theta=-55,phi=25)

enter image description here after drawing this diagram, is there a way that I can:

(1) draw an additional line, e.g, y=5x+8 on the persp x-y plane?

(2) project a scatter plot of z on the x-y plane?

1

There are 1 answers

0
Robert On

The line could be added with trans3d:

res1 <- persp(data.lm,x~y, zlim=c(0,max(z)),contour=list(z="bottom",col="colors"),theta=-55,phi=25)    
xy <- matrix(c((-3-8)/5,-3,(3-8)/5,3),ncol=2,byrow = T)
lines(trans3d(xy[,2], xy[,1], 0, pmat = res1$`y ~ x`$transf), col = 3)

enter image description here