首先,假设我运行一个形式为
lm(formula = cbind(ci, ct) ~ psy + ext + neu, data=DF)
的多元回归模型
其次,我使用上面的 lm 模型运行了 5000 个样本的引导程序,并获得了
dim = (4, 2, 5000); 4 = intercept + 3 predictors; 2 = outcomes; 5000 = resamples of the data
的 3D 数组
我的数据示例输出如下
output = structure(c(14.8350211817628, 2.7195602061234, 0.0655118922985072,
0.650501820265356, 17.2883184094025, 0.716181596905375, -0.687662353838093,
1.35609847901603, 15.2080112604409, 2.05153257169287, 0.136932171871498,
1.00930059512869, 16.2365679933569, 0.906978073894866, -0.471661601619115,
1.34989437227286), dim = c(4L, 2L, 2L), dimnames = list(c("(Icept)",
"psy", "ext", "neu"), c("ct", "ci"), NULL))
我想做的是得到这些数据的 95% 置信区间。我知道我可以通过这种方式获得单独的分位数间隔:
quantile(output["psy", "ct", ], probs = c(0.25, 0.975))
25% 97.5%
2.218539 2.702860
但我正在寻找一种方法来一次获取所有内容。所以我使用的策略是
confidence interval of the mean approach
,像这样:
bootci <- function(.data){
# confidence interval for the mean
n <- length(.data)
dof <- n-1
m <- apply(.data, c(1,2), mean)
s <- apply(.data, c(1,2), mean)
t <- qt(p=.05/2, df=dof, lower.tail=FALSE)
e <- (t * s / sqrt(n))
lwr <- data.frame(lwr = m - e)
upr <- data.frame(upr = m + e)
lu <- cbind(lwr, upr)
cn.order <- c("lwr.ct", "upr.ct", "lwr.ci", "upr.ci")
lu2 <- lu[, cn.order]
lu2
}
bootci(output)
我的问题,这是一种合法的方法吗?