R中具有Plotly的3D散点图-无法获得回归平面以准确拟合

时间:2019-01-13 04:44:18

标签: r plotly linear-regression r-plotly

使用Plotly和R将某些回归平面准确地拟合到3D散点图中时,会有一些问题。

我能够拟合一个平面,但是该平面显然与我数据中的数据点/回归不相似。

有一些相关的文章处理类似的问题-我试图遵循这些文章中概述的模板,但是不幸的是,我离找到解决方案还很近。

相关文章: 1. How do I add surfaces such a planes as traces generated mathematical formulas in a 3D scatter plot in plotly in r? 2. Add Regression Plane to 3d Scatter Plot in Plotly

我的代码可以在下面找到:

# Load packages    

library(readxl)
library(plotly)
library(dplyr)
library(data.table)    

# Generate Data Frame    

Test_Score = c(99, 54, 26, 63, 48, 75, 26, 54, 38, 28, 81, 35, 91, 72, 51, 23, 96, 36, 33, 98, 28)
Time_Studying = c(10, 4, 2, 6, 5, 8, 2, 3, 2, 1, 9, 3, 9, 7, 5, 1, 10, 3, 3, 8, 2)
Time_PlayingDiablo = c(1, 4, 7, 3, 3, 2, 7, 4, 5, 7, 3, 6, 1, 2, 5, 9, 3, 9, 8, 1, 9)    

raw_data <- data.frame(cbind(Test_Score,Time_Studying,Time_PlayingDiablo))    


# Construct initial 3D scatter plot using plotly (this part works)    


p <- plot_ly(raw_data,x=raw_data$Time_Studying,
                       y=raw_data$Test_Score,
                       z=raw_data$Time_PlayingDiablo,type='scatter3d',mode='markers') %>% 

    layout(
      title = "Test Score Regression",
      scene = list(
        xaxis = list(title = "Time Studying"),
        yaxis = list(title = "Study Score"),
        zaxis = list(title = "Time Playing Games")
    ))

p    

# Fit a linear regression model    

model2 <- lm(Test_Score ~ Time_PlayingDiablo + Time_Studying, raw_data)
summary(model2)    



# Fit a 3D plane to the plot showing the regression model, using the following stackoverflow QA 
# as a template     

# --> (https://stackoverflow.com/questions/38331198/add-regression-plane-to-3d-scatter-plot-in-plotly)
# and this too 
# --> https://stackoverflow.com/questions/46326005/how-do-i-add-surfaces-such-a-planes-as-traces-generated-mathematical-formulas-in    

# Set graph resolution    

graph_reso <- 1    

# Setup axis
axis_x <- seq(min(raw_data$Time_Studying), max(raw_data$Time_Studying), graph_reso)
axis_y <- seq(min(raw_data$Time_PlayingDiablo), max(raw_data$Time_PlayingDiablo), graph_reso)    

# Sample points
reg_surface <- expand.grid(Time_Studying = axis_x, Time_PlayingDiablo= axis_y,KEEP.OUT.ATTRS = F)
reg_surface$Model2_Predict <- round(predict(model2, newdata = reg_surface),0)
reg_surface2 <- acast(reg_surface, Time_Studying ~ Time_PlayingDiablo, value.var = "Model2_Predict")    


######################################################
#  ---- THIS IS THE PART THAT DOESN"T WORK ----
# Add surface/plane to our scatterplot 
######################################################    

p2 <- add_trace(p,
                x = axis_x,
                y = axis_y,
                z = reg_surface2,
                type = "surface", inherit=FALSE)    

p2

渲染p2时,会得到以下3D图表: enter image description here

显然不正确。查看``reg_surface2''表可以看到该模型的合理预测值(即,学习多个小时而玩暗黑破坏神几个小时的学生可以获得很好的结果),因此数据本身并不是问题。

为什么这些数字不转换为3D平面图?...

0 个答案:

没有答案