[英]How to Calculate Summary Statistics (Standard Error, and Upper and Lower Confidence intervals) using the package data.table in R
[英]How to calculate confidence intervals from R summary function?
如果给我一个 output 用于线性回归 model 如下:
Call:
lm(formula = Cost ~ Age + I(Age^2))
Residuals:
Min 1Q Median 3Q Max
-371.76 -218.77 -70.16 141.97 541.08
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 348.088 214.816 1.620 0.127
Age 103.003 181.969 0.566 0.580
I(Age^2) 4.713 29.248 0.161 0.874
Residual standard error: 293.2 on 14 degrees of freedom
Multiple R-squared: 0.478,Adjusted R-squared: 0.4035
F-statistic: 6.411 on 2 and 14 DF, p-value: 0.01056
我将如何仅基于此计算置信区间?
本质上,我希望手动计算以下内容:
> confint(model.fit, level = 0.90)
5 % 95 %
(Intercept) -30.26946 726.44545
Age -217.50106 423.50653
I(Age^2) -46.80263 56.22808
下面是一个 function,它将使用 Wald 置信区间手动计算 CI:
样本数据
df <- structure(list(income = c(5.42, 3.47, 1.58, 3.51, 3.38, 3.53,
5.7, 4.67, 3.66, 5.61, 6.28, 6.5, 2.34, 2.69, 1.53, 6.83, 4.06,
7.08, 2.07, 1.64, 2.52, 5.22, 6.84, 5.47, 2), happiness = c(4.5,
2.54, 1.98, 3.03, 1.42, 2.67, 4.04, 4.55, 2.86, 4.59, 4.23, 5.34,
2.34, 2.2, 2.42, 3.84, 3.57, 4.12, 2.19, 1.8, 1.53, 5.77, 5.92,
4.66, 1.82)), row.names = c(428L, 495L, 287L, 151L, 117L, 112L,
376L, 62L, 199L, 264L, 445L, 466L, 267L, 328L, 297L, 259L, 135L,
128L, 35L, 421L, 375L, 36L, 435L, 216L, 256L), class = "data.frame")
Model和手动CI
model <- lm(income ~ happiness, data = df)
manualCI <- function(model_object, ci = 0.95){
a <- coef(summary(model_object))
mult <- qnorm((1 + ci) / 2)
restab <- with(as.data.frame(a),
cbind(est = Estimate,
lwr = Estimate - mult*`Std. Error`,
upr = Estimate + mult*`Std. Error`))
rownames(restab) <- rownames(a)
return(data.frame(restab))
}
manualCI(model)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.