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)
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?
The line could be added with
trans3d
: