【发布时间】:2014-07-26 07:38:43
【问题描述】:
我做了这个模型:
model <- lm(mpg ~ wt, mtcars)
我现在想对新数据进行预测,我可以使用 effects 包来做到这一点
library(effects)
effect_df <- as.data.frame(effect(c("wt"), model, list(wt = 1:5)))
effect_df
wt fit se lower upper
1 1 31.94065 1.3515519 29.18042 34.70089
2 2 26.59618 0.8678067 24.82389 28.36848
3 3 21.25171 0.5519713 20.12444 22.37899
4 4 15.90724 0.6938618 14.49018 17.32429
5 5 10.56277 1.1328743 8.24913 12.87641
我可以像这样使用expand.grid 做出同样的预测:
expand_grid_df <- expand.grid(wt = 1:5)
expand_grid_df$fit <- predict(model, expand_grid_df)
expand_grid_df
wt fit
1 1 31.94065
2 2 26.59618
3 3 21.25171
4 4 15.90724
5 5 10.56277
如何将fit 的标准误差和上/下置信区间的列添加到expand_grid_df,如effect_df 中一样?
【问题讨论】:
标签: r linear-regression prediction predict