bootstrap 不同长度的值
bootstrap for different length of values
我想 bootstrap 我的数据以获得不同长度向量均值附近的置信区间。例如下面的代码我根据我的向量 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"))
我的问题是如何计算 bootstrapped 平均值 CI 对于 x
的不同大小,例如 3-4、4-5、5-6、6- 7、7-8?
这个怎么样:
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)
我想 bootstrap 我的数据以获得不同长度向量均值附近的置信区间。例如下面的代码我根据我的向量 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"))
我的问题是如何计算 bootstrapped 平均值 CI 对于 x
的不同大小,例如 3-4、4-5、5-6、6- 7、7-8?
这个怎么样:
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)