[R] Help on plotting a regression plane?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Sun Jul 15 12:13:52 CEST 2001


John Williams <jwilliams at business.otago.ac.nz> writes:

> I'm a bit embarrassed to ask this, but can anyone tell me (or point me
> to a reference) on how to plot a regression plane in R?
> 
> I have a regression model with two independent variables and have
> created a nice 3d scatterplot with scatterplot3d (thanks Uwe!) and now
> would like to overlay the regression plane (gridded, preferably.)  Ay
> pointers would be appreciated.

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...]
-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list