首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对不同长度的值进行引导

对不同长度的值进行引导
EN

Stack Overflow用户
提问于 2021-03-10 18:44:12
回答 1查看 47关注 0票数 0

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

代码语言:javascript
复制
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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-10 19:32:43

这样如何:

代码语言:javascript
复制
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间隔,请使用:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66563091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档