使用引导包进行引导
Bootstraoping using the boot package
尝试使用 R 中的 boot
包,它没有完成我想要的,它只是 returns 相同的值。我已经阅读了文档并检查了第一个似乎相同的示例,但我没有得到任何引导结果,只返回了原始值。我的例子:
dat <- data.frame(A = rnorm(100), B = runif(100))
booty <- function(x, ind) sum(x$A)/sum(x$B)
boot_out <- boot(dat, booty, R = 50, stype = "w")
就像我在评论中所说的那样,您必须使用索引(在本例中为 ind
)来对传递给 bootstrap 统计函数 sum(x$A[ind])/sum(x$B[ind])
的数据集进行子集化。
完整的功能将变成
booty <- function(x, ind) sum(x$A[ind])/sum(x$B[ind])
两个音符。
第一,stype = "i"
参数声明 booty
的第二个参数是一个索引向量,我相信这是你想要的,而不是权重向量,stype = "w"
.
此外,为了获得可重现的结果,应在调用 boot
或 RNG 函数之前设置 RNG 种子。类似下面的内容。
set.seed(4237)
dat <- data.frame(A = rnorm(100), B = runif(100))
boot_out <- boot(dat, booty, R = 50, stype = "i")
尝试使用 R 中的 boot
包,它没有完成我想要的,它只是 returns 相同的值。我已经阅读了文档并检查了第一个似乎相同的示例,但我没有得到任何引导结果,只返回了原始值。我的例子:
dat <- data.frame(A = rnorm(100), B = runif(100))
booty <- function(x, ind) sum(x$A)/sum(x$B)
boot_out <- boot(dat, booty, R = 50, stype = "w")
就像我在评论中所说的那样,您必须使用索引(在本例中为 ind
)来对传递给 bootstrap 统计函数 sum(x$A[ind])/sum(x$B[ind])
的数据集进行子集化。
完整的功能将变成
booty <- function(x, ind) sum(x$A[ind])/sum(x$B[ind])
两个音符。
第一,stype = "i"
参数声明 booty
的第二个参数是一个索引向量,我相信这是你想要的,而不是权重向量,stype = "w"
.
此外,为了获得可重现的结果,应在调用 boot
或 RNG 函数之前设置 RNG 种子。类似下面的内容。
set.seed(4237)
dat <- data.frame(A = rnorm(100), B = runif(100))
boot_out <- boot(dat, booty, R = 50, stype = "i")