R 中的引导包简单帮助

发布于 2024-12-10 16:51:56 字数 287 浏览 3 评论 0原文

如果我想使用 R 的 boot 包中的 boot() 函数来计算两个向量之间的皮尔逊相关系数的显着性,我应该这样做

boot(re1, cor, R = 1000)

re1 是这两个观察向量的两列矩阵?我似乎无法正确理解这一点,因为这些向量的 cor0.8,但上面的函数返回 -0.2 作为 t0

If I want to use the the boot() function from R's boot package for calculating the significance of the Pearson correlation coefficient between two vectors, should I do it like this:

boot(re1, cor, R = 1000)

where re1 is a two column matrix for these two observation vectors? I can't seem to get this right because cor of these vectors is 0.8, but the above function returns -0.2 as t0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2024-12-17 16:51:56

只是为了强调 R 中引导的总体思路,尽管@caracal 已经通过他的评论回答了你的问题。使用boot时,需要有一个可以按行采样的数据结构(通常是矩阵)。统计数据的计算通常在一个函数中完成,该函数接收该数据矩阵并返回重采样后计算出的感兴趣的统计数据。然后,您调用 boot() 来负责将此函数应用于 R 复制并以结构化格式收集结果。这些结果可以依次使用 boot.ci() 进行评估。

以下是 MASS 包中的低出生婴儿研究的两个工作示例。

require(MASS)
data(birthwt)
# compute CIs for correlation between mother's weight and birth weight
cor.boot <- function(data, k) cor(data[k,])[1,2]
cor.res <- boot(data=with(birthwt, cbind(lwt, bwt)), 
                statistic=cor.boot, R=500)
cor.res
boot.ci(cor.res, type="bca")
# compute CI for a particular regression coefficient, e.g. bwt ~ smoke + ht
fm <- bwt ~ smoke + ht
reg.boot <- function(formula, data, k) coef(lm(formula, data[k,]))
reg.res <- boot(data=birthwt, statistic=reg.boot, 
                R=500, formula=fm)
boot.ci(reg.res, type="bca", index=2) # smoke

Just to emphasize the general idea on bootstrapping in R, although @caracal already answered your question through his comment. When using boot, you need to have a data structure (usually, a matrix) that can be sampled by row. The computation of your statistic is usually done in a function that receives this data matrix and returns the statistic of interest computed after resampling. Then, you call the boot() that takes care of applying this function to R replicates and collecting results in a structured format. Those results can be assessed using boot.ci() in turn.

Here are two working examples with the low birth baby study in the MASS package.

require(MASS)
data(birthwt)
# compute CIs for correlation between mother's weight and birth weight
cor.boot <- function(data, k) cor(data[k,])[1,2]
cor.res <- boot(data=with(birthwt, cbind(lwt, bwt)), 
                statistic=cor.boot, R=500)
cor.res
boot.ci(cor.res, type="bca")
# compute CI for a particular regression coefficient, e.g. bwt ~ smoke + ht
fm <- bwt ~ smoke + ht
reg.boot <- function(formula, data, k) coef(lm(formula, data[k,]))
reg.res <- boot(data=birthwt, statistic=reg.boot, 
                R=500, formula=fm)
boot.ci(reg.res, type="bca", index=2) # smoke
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文