使用Plotly在R中添加回归平面 [英] Add regression plane in R using Plotly

查看:299
本文介绍了使用Plotly在R中添加回归平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试使用plotly库在RStudio中绘制回归窗格,并阅读以下文章:

我用str函数检查x1.seq和x2.seq是否具有相同的条目数,并且它们中都具有231个数字值.飞机得到了计算并显示了出来,但显然仍然是错误的.

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

预先感谢, rikojir

解决方案

此处是一个说明性示例,该示例显示了如何在使用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图:

I recently tried to plot a regression pane in RStudio using the plotly library and read this post: Add Regression Plane to 3d Scatter Plot in Plotly

I followed the exact same procedure and ended up with a regression plane, which is obviously not correct:

EDIT: I followed the proposal in the first answer and my result looks like this:

Here is my code, I commented every step: sm is the data.frame I used

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

I checked with the str-function, if the x1.seq and x2.seq have the same number of entries, and they both have 231 number values in them. The plane gets calculated and is shown, but it obviously still wrong.

PS: If you want to run the code, just download the file Supermodel.dat from Andy Fields website (https://studysites.uk.sagepub.com/dsur/study/articles.htm) under Regression.

Thanks in advance, rikojir

解决方案

Here is an illustrative example that shows how the observed points and the regression plane can be plotted together in a 3D plot generated using the plotlty package.
Hope it can help you.

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

Here is the 3D plot generated by the above code:

这篇关于使用Plotly在R中添加回归平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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