我开始探索包boot()
中功能的局限性boot
。我刚刚弄清楚如何从一次调用中提取多个统计信息,但我不知道如何对它们进行索引。
bs <- function(formula, data, indices) {
d <- data[indices,] # allows boot to select sample
fit <- lm(formula, data=d)
return(c(coef(fit), summary(fit)$r.squared)) # four stats extracted
}
Run Code Online (Sandbox Code Playgroud)
现在引导 1000 次复制。
(results <- boot(data = mtcars, statistic = bs, R = 1000, formula = mpg ~ wt + disp))
Run Code Online (Sandbox Code Playgroud)
现在,如果我们绘制并尝试获得该对象的 95% CI,我们只能获得第一个统计数据,在本例中为模型的截距。
plot(results)
boot.ci(results, type="bca")
# BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
# Based on 1000 bootstrap replicates
#
# CALL :
# boot.ci(boot.out = results, type = "bca")
#
# Intervals :
# Level BCa
# 95% (30.27, 39.53 )
# Calculations and Intervals on Original Scale
Run Code Online (Sandbox Code Playgroud)
如何对其他四个参数进行索引(一一索引和/或全部索引)?
首先注意到:
> head(results$t)
[,1] [,2] [,3] [,4]
[1,] 35.94765 -4.358146 -0.010605142 0.7471584
[2,] 33.54691 -3.319141 -0.011821276 0.8463980
[3,] 34.01732 -3.627647 -0.009797742 0.6727989
[4,] 32.26678 -1.717800 -0.030443778 0.7466273
[5,] 35.78895 -3.944054 -0.016471864 0.8277447
[6,] 33.99407 -3.560855 -0.014129072 0.8412922
Run Code Online (Sandbox Code Playgroud)
所以你可以只使用result$t[,i]
To 访问第 i 个元素。看起来第一列是截距,第二列是重量系数,第三列是位移系数,最后一列是 R 平方。
您也可以使用boot.ci(results, type="bca", index=i)
访问第 i 列,同样适用于plot plot(results, index=i)
。
归档时间: |
|
查看次数: |
1899 次 |
最近记录: |