【问题标题】:bootstrap for different length of values引导不同长度的值
【发布时间】:2021-06-08 07:09:32
【问题描述】:

我想引导我的数据以获得围绕不同长度向量平均值的置信区间。例如,使用下面的代码,我根据我的向量 x 计算 y,然后应用 bootstrap 来获取 CI。

set.seed(001)

x <- rnorm(length(iris$Sepal.Length),iris$Sepal.Length,0.05*iris$Sepal.Length)

mydata<-data.frame(x=x,y=pi*x^2)

library(boot)
myboot<-boot(mydata$y, function(u,i) mean(u[i]), R = 999)

boot.ci(myboot, type = c("perc"))

我的问题是如何计算 x 的不同大小(如 3-4、4-5、5-6、6-7、7-8)的引导平均 CI?

【问题讨论】:

  • x = "3-4" 或 "4-5" 的大小到底是什么意思?

标签: r statistics-bootstrap


【解决方案1】:

这个怎么样:

set.seed(001)
x <- rnorm(length(iris$Sepal.Length),iris$Sepal.Length,0.05*iris$Sepal.Length)

mydata<-data.frame(x=x,y=pi*x^2) 
mydata$x_cut <- cut(x, breaks=c(3,5,6,7,9))

boot_fun <- function(data, inds){
  tmp <- data[inds, ]
  tapply(tmp$y, tmp$x_cut, mean)
}
library(boot)
myboot<-boot(mydata, boot_fun, R = 999, strata=mydata$x_cut)

boot.ci(myboot, type = c("perc"))
cis <- sapply(1:4, function(i)boot.ci(myboot, type="perc", index=i)$percent)
colnames(cis) <- names(myboot$t0)
cis <- cis[4:5, ]
rownames(cis) <- c("Lower", "Upper")
cis <- t(cis)

cis
#           Lower     Upper
# (3,5]  67.79231  72.81593
# (5,6]  92.25999  97.25919
# (6,7] 124.65315 130.88061
# (7,9] 167.58324 183.88702

对于 BCa 间隔,使用:

boot.ci(myboot, type = c("bca"))
cis <- sapply(1:4, function(i)boot.ci(myboot, type="bca", index=i)$bca)
colnames(cis) <- names(myboot$t0)
cis <- cis[4:5, ]
rownames(cis) <- c("Lower", "Upper")
cis <- t(cis)

【讨论】:

  • 我添加了一个块来展示如何获得 BCa 间隔。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多