Skip to content
Prev 12337 / 398502 Next

Help on plotting a regression plane?

Peter Dalgaard BSA wrote:
Thank you, Peter. If I have got some more time, I will do (see below).
Both ideas are very nice. I think predict() has to be used slightly
different.

Nevertheless, in the morning I begun writing a (now tested) example,
also using xyz.convert(). Here is the modified example 5 from the help:

 library(scatterplot3d)
 data(trees)
 attach(trees)
 s3d <- scatterplot3d(trees, type = "h", highlight.3d = TRUE,
     pch=16, main="scatterplot3d -- example 5", angle=60, scale.y=0.7)
 # I want to draw this regression plane as a grid:
 my.lm <- lm(Volume ~ Girth + Height)   
 my.pred <- function(x, y, model){
    my.coef <- coef(model)
    return(my.coef[1] + x * my.coef[2] + y * my.coef[3])
 }
 
 # Now drawing segments from (x1, y1, z1) to (x2, y2, z2). 
 # Looking at the existing plot helps to find the points at first:
 
 x1 <- seq(8, 22, 2)  # Girth
 y1 <- rep(60, 8)     # Height
 z1 <- my.pred(x1, y1, my.lm)
 x2 <- seq(8, 22, 2)
 y2 <- rep(90, 8)
 z2 <- my.pred(x2, y2, my.lm)

 # Now calculating the 3d -> 2d projection for the existing plot
 seg1 <- s3d$xyz.convert(x1, y1, z1)
 seg2 <- s3d$xyz.convert(x2, y2, z2)
 segments(seg1$x, seg1$y, seg2$x, seg2$y, lty="dashed")

 # As above, but the orthogonal lines of the grid:
 x1 <- rep(8, 7)        # Girth
 y1 <- seq(60, 90, 5)   # Height
 z1 <- my.pred(x1, y1, my.lm)
 x2 <- rep(22, 7)
 y2 <- seq(60, 90, 5)
 z2 <- my.pred(x2, y2, my.lm)
 seg1 <- s3d$xyz.convert(x1, y1, z1)
 seg2 <- s3d$xyz.convert(x2, y2, z2)
 segments(seg1$x, seg1$y, seg2$x, seg2$y, lty="dashed")



That's a workaround if you only want to plot it once, but surely
automating it would be nicer. One can also think about drawing
non-linear planes. Maybe this will become a additional function for the
package.

If there a any questions remaining, feel free to send a private mail.

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._