confint
不会返回任何内容,因为不支持“传销”方法:
methods(confint)
#[1] confint.default confint.glm* confint.lm confint.nls*
正如您所说,我们只需加/减一些标准误差的倍数即可获得置信区间的上限/下限。您可能会通过以下方式执行此操作coef(summary(mod))
,然后使用一些*apply
提取标准误差的方法。但我的答案 https://stackoverflow.com/a/39922928/4891738 to 获取“mlm”对象返回的回归系数的标准误差lm() https://stackoverflow.com/q/19740292/4891738为您提供了一种超级有效的方法来获取标准错误,而无需经过summary
。正在申请std_mlm
您的示例模型给出:
se <- std_mlm(mod)
# income prestige
#(Intercept) 1162.299027 3.54212524
#education 103.731410 0.31612316
#women 8.921229 0.02718759
现在,我们定义另一个小函数来计算下限和上限:
## add "mlm" method to generic function "confint"
confint.mlm <- function (model, level = 0.95) {
beta <- coef(model)
se <- std_mlm (model)
alpha <- qt((1 - level) / 2, df = model$df.residual)
list(lower = beta + alpha * se, upper = beta - alpha * se)
}
## call "confint"
confint(mod)
#$lower
# income prestige
#(Intercept) -3798.25140 -15.7825086
#education 739.05564 4.8005390
#women -81.75738 -0.1469923
#
#$upper
# income prestige
#(Intercept) 814.25546 -1.72581876
#education 1150.70689 6.05505285
#women -46.35407 -0.03910015
解释这一点很容易。例如,对于响应income
,所有变量的 95% 置信区间为
#(intercept) (-3798.25140, 814.25546)
# education (739.05564, 1150.70689)
# women (-81.75738, -46.35407)