[英]Linear regression: calculate confidence and prediction intervals with the standard errors of the fitted parameters the correlation coefficient
在自然科學的許多領域,通常的做法是將線性回歸分析的結果報告為y = (a1 +- u(a1)) + (a2 +- u(a2)) * x
,包括 R2 和 p,但是不是原始數據。 u(a1) 和 u(a2) 是 a1 和 a2 的不確定度(標准誤差)。 我怎樣才能用這些信息計算置信區間和預測區間,或者有一個“合理”的估計?
讓我用一個例子來澄清。 這是一個虛擬數據集,具有一條斜率為 1 和高斯噪聲為 10 的線:
set.seed(1961)
npoints <- 1e2
(x <- 1:npoints)
(y <-1:npoints + rnorm(npoints, 0, npoints/10))
現在我執行線性回歸:
par(mar = c(4, 4, 1, 1))
xy.model <- lm(y ~ x)
plot(x, y, pch = 16)
abline(xy.model, col = "orange", lwd = 2)
(xy.sum <- summary(xy.model))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) -1.28106 1.94918 -0.657 0.513
# x 1.00484 0.03351 29.987 <2e-16 ***
# Residual standard error: 9.673 on 98 degrees of freedom
# Multiple R-squared: 0.9017, Adjusted R-squared: 0.9007
# F-statistic: 899.2 on 1 and 98 DF, p-value: < 2.2e-16
我計算置信區間和預測區間:
x.new <- data.frame(x = 1:npoints)
xy.conf <- predict.lm(xy.model, se.fit = TRUE, interval = "confidence", newdata = x.new)
xy.pred <- predict.lm(xy.model, se.fit = TRUE, interval = "prediction", newdata = x.new)
比如第一個點的置信區間和預測區間是:
xy.conf$fit[1, ]
# fit lwr upr
# -0.2762127 -4.0867009 3.5342755
xy.pred$fit[1, ]
# fit lwr upr
# -0.2762127 -19.8462821 19.2938568
如果回歸方程報告為 y = (-1.28106 +- 1.94918) + (1.00484 +- 0.03351) * x, R2 = 0.9017, p < 0.05,但沒有提供原始數據,我如何重現(至少大約) 置信區間和預測區間的值?
如果沒有原始數據,您還需要一條信息:兩個變量的均值。 您提供的統計數據允許構建線性回歸線,但置信和預測帶在均值 (x)、均值 (y) 處最窄,因此如果沒有這些,您將無法計算它們。
一個簡單的例子可能會使這更清楚。 從一些數據開始:
z <- structure(list(x = c(5, 5.1, 5.4, 5.8, 4.7, 5.7, 4.8, 5.1, 4.6,
5.4, 5.2, 5, 5, 5.5, 5.2, 5.1, 4.7, 5.2, 4.8, 5.4, 4.8, 5.1,
5, 4.6, 4.8), y = c(3.4, 3.7, 3.4, 4, 3.2, 3.8, 3, 3.5, 3.1,
3.7, 4.1, 3.4, 3.6, 4.2, 3.5, 3.3, 3.2, 3.4, 3, 3.9, 3.1, 3.5,
3.5, 3.4, 3.4)), row.names = c(NA, -25L), class = "data.frame")
計算回歸線並將其與數據一起繪制:
z.lm <- lm(y~x, z)
z.lm
#
# Call:
# lm(formula = y ~ x, data = z)
#
# Coefficients:
# (Intercept) x
# -0.4510 0.7762
#
plot(y~x, z, xlim=c(0, 20), ylim=c(0, 20))
abline(z.lm)
現在從原始數據創建一個新數據集並計算回歸:
x2 <- z$x + 10
y2 <- z$y+(10 * coef(z.lm)[2])
z2 <- data.frame(x=x2, y=y2)
points(y~x, z2, col="red")
z2.lm <- lm(y~x, z2)
z2.lm
#
# Call:
# lm(formula = y ~ x, data = z2)
#
# Coefficients:
# (Intercept) x
# -0.4510 0.7762
注意回歸系數與原始數據相同。 事實上,將 10 更改為任何其他值將產生另一組具有相同回歸結果的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.