scatterplot3d:带残差的回归平面 [英] scatterplot3d: regression plane with residuals

查看:72
本文介绍了scatterplot3d:带残差的回归平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 R 中使用 scatterplot3d,我试图从观察到回归平面绘制红线:

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)图书馆(散点图3d)G <- scatterplot3d(x, z, y, highlight.3d = FALSE, type = "p")G$plane3d(LM, draw_polygon = TRUE, draw_lines = FALSE)

获得下图的3D等价物:

在 2D 中,我可以只使用 segments:

pred <- 预测(模型)段(x,y,x,pred,col = 2)

但在 3D 中,我对坐标感到困惑.

解决方案

使用来自

另一个交互式版本使用rgl

rgl::plot3d(advertising$TV,广告$电台,广告$销售,类型=p",xlab = "电视",ylab =收音机",zlab = 销售",站点 = 5,lwd = 15)rgl::planes3d(advertising_fit1$coefficients[TV"],ads_fit1$coefficients[radio"], -1,ads_fit1$coefficients["(Intercept)"], alpha = 0.3, front = "line")rgl::segments3d(rep(advertising$TV, each = 2),代表(广告$电台,每个= 2),矩阵(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") # 真的不起作用...

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)

To obtain the 3D equivalent of the following picture:

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.

解决方案

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"))

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...

这篇关于scatterplot3d:带残差的回归平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆