这个问题在这里已有答案:
我无法匹配R的线性模型置信度和点估计的预测间隔:
set.seed(3)
x <- rnorm(392, 20, 5)
y <- 2*x + 3 + rnorm(392, sd=3)
lm.fit <- lm(y~x)
(est <- as.vector(matrix(c(1,20),nrow=1,ncol=2) %*% lm.fit$coefficients))
(ci <- est + c(-1,1) * qt(.975, df=lm.fit$df.residual)*sqrt(var(lm.fit$residuals)/(lm.fit$df.residual)))
(pred <- predict(lm.fit,data.frame(x=(c(20))), interval="confidence", se.fit = TRUE))
sqrt(var(lm.fit$residuals)/(lm.fit$df.residual))
pred$se.fit
如果您运行上面的代码,您将看到点估计值相同,但我得到的标准误差略有不同,因此置信区间略有偏差 . R如何匹配标准错误?
我在Documentation.lm()(下面)的文档中看到R在predict.lm函数中使用pred.var,但pred.var使用res.var,并没有说res.var来自何处 . res.var也与残差的方差不匹配:
var(lm.fit$residuals)
(http://127.0.0.1:25345/library/stats/html/predict.lm.html)
谢谢!
1 回答
您将预测区间标准误差(对于新观察值)与置信区间标准误差(对于平均响应)混淆 .
具体而言,对于预测,SE在标准误差的表达式中具有额外的1 . 我在下面显示完整的计算 .
请参阅以下代码:
If this is a new observation you need a 1 in the scaling factor (see the first term
If you are predicting the mean response (which is what linear regression usually does) you don't need that term.
注意他们现在如何完美匹配 . 要进行数学分析,请看一下这篇伟大的文章:
Linear Regression for the mean response and for an individual response
具体来说,对于平均响应:
但是,对于个人观察:
额外的"1"是给你差异的东西 . 基本思想是,当您预测个体响应时,会有额外的随机性,因为您对MSE的估计会计算出平均响应值附近的可变性,而不是单个响应值 .
直观地说,这很有道理 . 随着您的观察次数 - >无穷大,1使标准误差保持为0(因为任何一个人总是存在一些变化) .
希望有所帮助!