如何在绘图中为3d图生成多重回归平面

时间:2019-04-21 08:26:45

标签: r

我想为我的图形创建3d回归平面。我不确定为什么它不起作用,但是我认为这与我对预测函数(和相应数据)的使用有关。

ESS3 <- ESS2 %>%
  select(agertr, agea, eduyrs)
ESS3 <- na.omit(ESS3)

m5 <- lm(agertr ~ agea + eduyrs, data = ESS3)

# predict over sensible grid of values

ageas <- unique(ESS3$agea)
eduyrs1 <- unique(ESS3$eduyrs)
agertrs <- unique(ESS3$agertr)
grid <- with(ESS3, expand.grid(ageas, eduyrs1, agertrs))
d <- setNames(data.frame(grid), c("agea", "eduyrs", "agertrs"))


vals <- predict(model, newdata = d)

# form matrix and give to plotly
m <- matrix(vals, nrow = length(unique(d$agea)), ncol = length(unique(d$eduyrs)))

p <- plot_ly(m5, x = ~agea, y = ~agertr, z = ~eduyrs)
layout(scene = list(xaxis = list(title = 'Age'),
                    yaxis = list(title = 'Age of Retirement'),
                    zaxis = list(title = 'Years of Education')))

p %>% add_surface(x = ~ageas, y = ~eduyrs1, z = ~m)

产生的两个错误是:

  

predict(model,newdata = d)中的错误:找不到对象'model'错误   在矩阵中(vals,nrow = length(unique(d $ agea)),ncol =   length(unique(d $ eduyrs))):找不到对象“ vals”

此代码有点超出我的能力,如果我的代码到处都是,请对不起。感谢您的帮助和建议。

0 个答案:

没有答案
相关问题