简体   繁体   English

R:使用预测功能将标准误差和置信区间添加到预测中

[英]R: Using the predict function to add standard error and confidence intervals to predictions

I've made this model: 我做了这个模型:

model <- lm(mpg ~ wt, mtcars)

I now want to made prediction for new data, and I can do this with the effects package 现在,我要对新数据进行预测,可以使用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

I can made the same predictions with expand.grid like this: 我可以像这样用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

How can I add columns for the standard error and upper/lower confidence intervals for fit to expand_grid_df , as in effect_df ? 如何添加列的标准误差和上/下的置信区间为fitexpand_grid_df ,如effect_df

This will do it: 可以做到这一点:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算 predict() R function 的预测差异的置信区间? - How to calculate the confidence interval for the difference in predictions of the predict() R function? ggplot2图中的置信区间与使用R中的预测函数获得的值不同 - Confidence intervals in a ggplot2 graph and values obtained using predict function in R are not the same 在多个模型中使用 predict() 在 R 中生成置信区间 - Using predict() across multiple models to generate confidence intervals in R 如何使用预测 function 为 glmmTMB 拟合置信区间 - How to fit confidence intervals using predict function for glmmTMB 如何使用 R 中的 data.table 包计算汇总统计量(标准误差、上下置信区间) - How to Calculate Summary Statistics (Standard Error, and Upper and Lower Confidence intervals) using the package data.table in R R v 3.6.1中的lmer错误和预测函数/从lmer模型获得给定自变量的置信区间 - lmer errors and predict function in R v 3.6.1 / obtain confidence intervals from lmer model for a given independent variable R - 对每组数据使用不同的模型进行预测和置信区间 - R - Making predictions and confidence intervals with different models for each group of data 使用rq函数计算R中分位数回归的95%置信区间 - Calculating 95% confidence intervals in quantile regression in R using rq function 置信区间错误代码的函数{ - Function for confidence intervals error code { 将 t 值和置信区间添加到 R 中的条形图 - Add t values and confidence intervals to barplot in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM