我最近嘗試使用plotly庫繪製在RStudio迴歸窗格和閱讀這篇文章添加回歸平面中的R顯然不正確:使用Plotly
這裏是我的代碼,我評論的每一步:SM是我用
的data.framelibrary(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文件即可。
由於提前, rikojir
圖形看起來更好,如果你減少你點的大小,它會更容易看到飛機應該說謊。 – rnso
改變了點的大小,希望你現在可以看得更清楚 – rikojir