【问题标题】:Add regression plane in R using Plotly使用 Plotly 在 R 中添加回归平面
【发布时间】:2017-11-03 11:26:33
【问题描述】:

我最近尝试使用 plotly 库在 RStudio 中绘制回归窗格并阅读了这篇文章:Add Regression Plane to 3d Scatter Plot in Plotly

我遵循了完全相同的程序,最终得到了一个回归平面,这显然是不正确的:

编辑:我按照第一个答案中的建议进行操作,结果如下所示:

这是我的代码,我对每一步都进行了注释:sm 是我使用的data.frame

library(reshape2);
sm <- read.delim("Supermodel.dat", header = TRUE);
x1 <- sm$age
x2 <- sm$years
y <- sm$salary
df <- data.frame(x1, x2, y);


### Estimation of the regression plane
mod <- lm(y ~ x1+x2, data = df, na.action =     
        na.omit);

cf.mod <- coef(mod)

### Calculate z on a grid of x-y values
x1.seq <- seq(min(x1),max(x1),length.out=231)
x2.seq <- seq(min(x2),max(x2),length.out=231)
z.mtx <- t(outer(x1.seq, x2.seq, function(x1,x2) 
  cf.mod[1]+cf.mod[2]*x1+cf.mod[3]*x2))


#### Draw the plane with "plot_ly" and add points with "add_trace"
library(plotly) 

# Draw plane with plotly surface plot
plane <- plot_ly(x=~x1.seq, y=~x2.seq, z=~z.mtx, colors = 
c("#f5cb11", #b31d83"),type="surface") %>%
  add_trace(data=df, x=x1, y=x2, z=y, mode="markers", 
        type="scatter3d", 
        marker = list(color="black", opacity=0.7, symbol=105)) %>%
  layout(scene = list(aspectmode = "manual", aspectratio = list(x=1, 
    y=1, z=1), xaxis = list(title = "Age", range = c(12,24)), yaxis = 
    list(title = "Work experience (years)", range = c(0,10)), zaxis = 
list(title = "Salary p.a. (k)", range = c(0,90) )))

plane

我检查了 str 函数,如果 x1.seq 和 x2.seq 具有相同数量的条目,并且它们都有 231 个数值。飞机被计算出来并显示出来,但它显然仍然是错误的。

PS:如果要运行代码,只需在 Regression 下从 Andy Fields 网站 (https://studysites.uk.sagepub.com/dsur/study/articles.htm) 下载文件 Supermodel.dat。

提前致谢, ありり

【问题讨论】:

  • 如果您减小点大小,图形可能会更好看,并且更容易看到平面应该位于的位置。
  • 更改了磅值,希望您现在能看得更清楚

标签: r regression plotly pane


【解决方案1】:

这是一个说明性示例,显示了如何在使用 plotlty 包生成的 3D 图中将观察点和回归平面绘制在一起。
希望对你有帮助。

### Data generating process
set.seed(1234)
n <- 50
x1 <- runif(n); x2 <- runif(n)
x3 <- rnorm(n)>0.5
y <- 2*x1-x2+rnorm(n, sd=0.25)
df <- data.frame(y, x1, x2, x3)

### Estimation of the regression plane
mod <- lm(y ~ x1+x2)
cf.mod <- coef(mod)

### Calculate z on a grid of x-y values
x1.seq <- seq(min(x1),max(x1),length.out=25)
x2.seq <- seq(min(x2),max(x2),length.out=25)
z <- t(outer(x1.seq, x2.seq, function(x,y) cf.mod[1]+cf.mod[2]*x+cf.mod[3]*y))

#### Draw the plane with "plot_ly" and add points with "add_trace"
cols <- c("#f5cb11", "#b31d83")
cols <- cols[x3+1] 
library(plotly)
p <- plot_ly(x=~x1.seq, y=~x2.seq, z=~z,
  colors = c("#f5cb11", "#b31d83"),type="surface") %>%
  add_trace(data=df, x=x1, y=x2, z=y, mode="markers", type="scatter3d",
  marker = list(color=cols, opacity=0.7, symbol=105)) %>%
  layout(scene = list(
    aspectmode = "manual", aspectratio = list(x=1, y=1, z=1),
    xaxis = list(title = "X1", range = c(0,1)),
    yaxis = list(title = "X2", range = c(0,1)),
    zaxis = list(title = "Y", range = pretty(z)[c(1,8)])))
print(p)

这是上面代码生成的3D图:

【讨论】:

  • 感谢您的帮助,我尝试按照您的方法,现在我看到了飞机,但它仍然在点内放置错误。我编辑了我的帖子,希望您知道还有什么问题。
  • 我相信你知道你不应该编辑有问题的错误代码来更正一个。然后其他读者不知道错误在哪里。您应该指出错误或在自己的答案中输入正确的代码。
  • @rnso 我同意。对不起。我们将把问题改写成原来的形式。
  • 我试过你的解决方案 Marco,现在飞机已经改变(更适合数据),但它仍然是错误的。我编辑了我的帖子并更改了代码。
  • @rikojir 我没有发现您的 3D 绘图有任何问题。回归平面不适合观察到的点不是因为 R 代码的问题,而是因为线性回归不是数据的好模型。参见mod 的 R 平方:0.178。它相当低。
猜你喜欢
  • 2016-11-14
  • 1970-01-01
  • 2020-11-07
  • 2016-05-12
  • 2018-11-17
  • 1970-01-01
  • 2023-04-01
  • 2018-06-24
  • 2021-05-31
相关资源
最近更新 更多