如何计算R汇总函数的置信区间?

xriantvc  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(167)

如果给我一个线性回归模型的输出,如下所示:

  1. Call:
  2. lm(formula = Cost ~ Age + I(Age^2))
  3. Residuals:
  4. Min 1Q Median 3Q Max
  5. -371.76 -218.77 -70.16 141.97 541.08
  6. Coefficients:
  7. Estimate Std. Error t value Pr(>|t|)
  8. (Intercept) 348.088 214.816 1.620 0.127
  9. Age 103.003 181.969 0.566 0.580
  10. I(Age^2) 4.713 29.248 0.161 0.874
  11. Residual standard error: 293.2 on 14 degrees of freedom
  12. Multiple R-squared: 0.478,Adjusted R-squared: 0.4035
  13. F-statistic: 6.411 on 2 and 14 DF, p-value: 0.01056

我如何计算基于此的置信区间?
基本上,我希望手动计算以下内容:

  1. > confint(model.fit, level = 0.90)
  2. 5 % 95 %
  3. (Intercept) -30.26946 726.44545
  4. Age -217.50106 423.50653
  5. I(Age^2) -46.80263 56.22808
8zzbczxx

8zzbczxx1#

以下是使用Wald置信区间手动计算CI的函数:
样本数据

  1. df <- structure(list(income = c(5.42, 3.47, 1.58, 3.51, 3.38, 3.53,
  2. 5.7, 4.67, 3.66, 5.61, 6.28, 6.5, 2.34, 2.69, 1.53, 6.83, 4.06,
  3. 7.08, 2.07, 1.64, 2.52, 5.22, 6.84, 5.47, 2), happiness = c(4.5,
  4. 2.54, 1.98, 3.03, 1.42, 2.67, 4.04, 4.55, 2.86, 4.59, 4.23, 5.34,
  5. 2.34, 2.2, 2.42, 3.84, 3.57, 4.12, 2.19, 1.8, 1.53, 5.77, 5.92,
  6. 4.66, 1.82)), row.names = c(428L, 495L, 287L, 151L, 117L, 112L,
  7. 376L, 62L, 199L, 264L, 445L, 466L, 267L, 328L, 297L, 259L, 135L,
  8. 128L, 35L, 421L, 375L, 36L, 435L, 216L, 256L), class = "data.frame")

型号和手动CI

  1. model <- lm(income ~ happiness, data = df)
  2. manualCI <- function(model_object, ci = 0.95){
  3. a <- coef(summary(model_object))
  4. mult <- qnorm((1 + ci) / 2)
  5. restab <- with(as.data.frame(a),
  6. cbind(est = Estimate,
  7. lwr = Estimate - mult*`Std. Error`,
  8. upr = Estimate + mult*`Std. Error`))
  9. rownames(restab) <- rownames(a)
  10. return(data.frame(restab))
  11. }
  12. manualCI(model)
展开查看全部

相关问题