【问题标题】:R: Using the predict function to add standard error and confidence intervals to predictionsR:使用预测函数将标准误差和置信区间添加到预测中
【发布时间】: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


    【解决方案1】:

    这样就可以了:

    expand_grid_df <- expand.grid(wt  = 1:5)
    expand_grid_df$fit <- predict(model, expand_grid_df, se.fit=TRUE, interval="confidence")$fit
    expand_grid_df$se.fit <- predict(model, expand_grid_df, se.fit=TRUE)$se.fit
    expand_grid_df
    
      wt  fit.fit  fit.lwr  fit.upr    se.fit
    1  1 31.94065 29.18042 34.70089 1.3515519
    2  2 26.59618 24.82389 28.36848 0.8678067
    3  3 21.25171 20.12444 22.37899 0.5519713
    4  4 15.90724 14.49018 17.32429 0.6938618
    5  5 10.56277  8.24913 12.87641 1.1328743
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-22
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 2013-07-07
      相关资源
      最近更新 更多