我想要引导我的数据,以获得不同长度向量的平均值周围的置信区间。例如,使用下面的代码,我根据向量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的自举平均CI,比如3-4,4-5,5-6,6-7,7-8?
发布于 2021-03-10 19:32:43
这样如何:
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)https://stackoverflow.com/questions/66563091
复制相似问题