计算均值的 95% 置信区间

Calculate 95% confidence interval on the mean

我有一个练习说

当 x2=2300、x7=56 和 x8=2100 时,求一支球队平均获胜场数的 95% 的置信区间。

R中有直接给出这样的置信区间的函数吗?

我考虑过使用该功能 confint(f),但是这个函数给出了关于一个或多个参数的结果,据我所知,我没有参数,而是一个像 beta0+beta1xi 这样的函数,其中已经估计了参数 beta 和点 xi 将是 x2、x7 和 x8。

另一种方法是 'manually' 但这很复杂,因为我必须计算标准误差、方差、t 值等

你能帮忙吗?

提前致谢

你需要看的不是 confint,而是 predict.lm:

Details

predict.lm produces predicted values, obtained by evaluating the regression function in the frame newdata (which defaults to model.frame(object)). If the logical se.fit is TRUE, standard errors of the predictions are calculated. If the numeric argument scale is set (with optional df), it is used as the residual standard deviation in the computation of the standard errors, otherwise this is extracted from the model fit. Setting intervals specifies computation of confidence or prediction (tolerance) intervals at the specified level, sometimes referred to as narrow vs. wide intervals.`

您需要设置一个数据框,其列名与模型拟合中使用的列名相同,其中包含您想要预测的设置值,用于 newdata 参数。

这里有一个例子展示了如何使用 newdata:

x1<-c(1,2,5,6); x2<-c(3,2,4,1); x3<-c(5,4,3,4); y<-c(21,21,27,23)
res<-lm(y~x1+x2+x3)
predict.lm(res,newdata=data.frame(x1=4,x2=4,x3=2),
            interval="confidence")

(即您需要 data.frame(x2= ..., x7=... 等形式的内容,但您可以在其中填写所需的值)

但是,您还需要告诉它您需要的间隔类型。

(predict 是通用的;如果您在 lm 对象上调用 predict,它会调用 predict.lm,但要获得正确的帮助,您需要具体功能直接看)

是的。

R 中有一个函数可以直接给出这样的置信区间。

只需输入

predict.lm(f,newdata=data.frame(x2=2300,x7=56,x8=2100),interval="confidence")

其中f是线性模型,即f<-lm(y~x2+x7+x8)

其中 y,x2,x7,x8 是您的特定向量。


作为旁注,注意这个函数也可以给出"prediction"区间,只需将"confidence"改为"prediction"。