Skip to content
Prev 12336 / 398502 Next

Help on plotting a regression plane?

John Williams <jwilliams at business.otago.ac.nz> writes:
Doesn't look like something to be embarrassed about... (Uwe, you might
want to add this to one of the examples).

As far as I can see the key would be to use the points3d() function
returned by scatterplot3d(). Something like:

model <- lm(....)
s <- scatterplot3d(....)

for (x in seq(...)) {
    line.x <- rep(x,2)
    line.y <- c(ymin, ymax)
    line.z <- predict(model, new = data.frame(x=line.x, y=line.y)
    s$points3d(line.x, line.y, line.z, type="l", lty="dotted")
}
for (y in seq(....)) {
    ...
}

Alternatively, you might generate the endpoints for a segments() call 
by first producing the coordinates in 3d using predict and then
convert them with s$xyz.convert(). That'd be

d1<-rbind(data.frame(x=xmin,y=seq(...)), data.frame(x=seq(...),y=ymin)
d2<-rbind(data.frame(x=xmax,y=seq(...)), data.frame(x=seq(...),y=ymax)
p1 <- predict(model, new=d1)
p2 <- predict(model, new=d2)

line.begin <- s$xyz.convert(d1$x,d1$y,p1)
line.end <- s$xyz.convert(d2$x,d2$y,p2)

segments(line.begin$x,line.begin$y,line.end$x,line.end$y, lty="dotted")


[As you may have gathered, none of this is actually tested...]