Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:使用预测函数为预测添加标准误差和置信区间_R_Linear Regression_Prediction_Predict - Fatal编程技术网

R:使用预测函数为预测添加标准误差和置信区间

R:使用预测函数为预测添加标准误差和置信区间,r,linear-regression,prediction,predict,R,Linear Regression,Prediction,Predict,我制作了这个模型: model <- lm(mpg ~ wt, mtcars) 我可以用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 1

我制作了这个模型:

model <- lm(mpg ~ wt, mtcars)
我可以用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
如何为标准误差和拟合的上/下置信区间添加列以扩展网格df,就像实际的df一样?

这样做:

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
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