使用 Plotly 在 R 中添加回归平面
Add regression plane in R using Plotly
我最近尝试使用 plotly 库在 RStudio 中绘制回归窗格并阅读此 post:
我按照完全相同的步骤得到了一个回归平面,这显然是不正确的:
编辑:我遵循了第一个答案中的建议,我的结果如下所示:
这是我的代码,我对每一步都做了注释: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:如果你想 运行 代码,只需从 Andy Fields 网站 (https://studysites.uk.sagepub.com/dsur/study/articles.htm) 下载文件 Supermodel.dat 下的回归。
提前致谢,
里科吉尔
这里是一个说明性示例,展示了如何在使用 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 图:
我最近尝试使用 plotly 库在 RStudio 中绘制回归窗格并阅读此 post:
我按照完全相同的步骤得到了一个回归平面,这显然是不正确的:
编辑:我遵循了第一个答案中的建议,我的结果如下所示:
这是我的代码,我对每一步都做了注释: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:如果你想 运行 代码,只需从 Andy Fields 网站 (https://studysites.uk.sagepub.com/dsur/study/articles.htm) 下载文件 Supermodel.dat 下的回归。
提前致谢, 里科吉尔
这里是一个说明性示例,展示了如何在使用 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 图: