Search code examples
rscatter-plotscatter3d

scatterplot3d: regression plane with residuals


Using scatterplot3d in R, I'm trying to draw red lines from the observations to the regression plane:

wh <- iris$Species != "setosa"
x  <- iris$Sepal.Width[wh]
y  <- iris$Sepal.Length[wh]
z  <- iris$Petal.Width[wh]
df <- data.frame(x, y, z)

LM <- lm(y ~ x + z, df)
library(scatterplot3d)
G  <- scatterplot3d(x, z, y, highlight.3d = FALSE, type = "p")
G$plane3d(LM, draw_polygon = TRUE, draw_lines = FALSE)

Regression Plane

To obtain the 3D equivalent of the following picture:

enter image description here

In 2D, I could just use segments:

pred  <- predict(model) 
segments(x, y, x, pred, col = 2)

But in 3D I got confused with the coordinates.


Solution

  • Using the advertising dataset from An Introduction to Statistical Learning, you can do

    advertising_fit1 <- lm(sales~TV+radio, data = advertising)
    sp <- scatterplot3d::scatterplot3d(advertising$TV, 
                                       advertising$radio, 
                                       advertising$sales, 
                                       angle = 45)
    sp$plane3d(advertising_fit1, lty.box = "solid")#,
               # polygon_args = list(col = rgb(.1, .2, .7, .5)) # Fill color
    orig <- sp$xyz.convert(advertising$TV, 
                           advertising$radio, 
                           advertising$sales)
    plane <- sp$xyz.convert(advertising$TV, 
                            advertising$radio,  fitted(advertising_fit1))
    i.negpos <- 1 + (resid(advertising_fit1) > 0)
    segments(orig$x, orig$y, plane$x, plane$y,
             col = c("blue", "red")[i.negpos], 
             lty = 1) # (2:1)[i.negpos]
    sp <- FactoClass::addgrids3d(advertising$TV, 
                                 advertising$radio, 
                                 advertising$sales,
                                 angle = 45,
                                 grid = c("xy", "xz", "yz"))
    

    enter image description here

    And another interactive version using rgl package

    rgl::plot3d(advertising$TV, 
                 advertising$radio, 
                 advertising$sales, type = "p", 
                 xlab = "TV", 
                 ylab = "radio", 
                 zlab = "Sales", site = 5, lwd = 15)
    rgl::planes3d(advertising_fit1$coefficients["TV"], 
                  advertising_fit1$coefficients["radio"], -1, 
                  advertising_fit1$coefficients["(Intercept)"], alpha = 0.3, front = "line")
    rgl::segments3d(rep(advertising$TV, each = 2), 
                    rep(advertising$radio, each = 2),
                    matrix(t(cbind(advertising$sales, predict(advertising_fit1))), nc = 1),
                    col = c("blue", "red")[i.negpos], 
                    lty = 1) # (2:1)[i.negpos]
    rgl::rgl.postscript("./pics/plot-advertising-rgl.pdf","pdf") # does not really work...
    

    enter image description here