bootstrap samples by row of a data frame in r

懵懂的女人 提交于 2019-12-25 04:24:11

问题


I am trying to run a simple bootstrap on the rows of a data frame in r. Here is what I have worked up so far, but I'm hitting a dead end.

x1 <- c(1:5)
x2 <- c(6:10)
y <- runif(5)
z <- as.data.frame(rbind(x1, x2, y))

trial <- 10
avg <- rep(0, trial)
for(i in 1:trial){
  ind <- sample(ncol(z), size = ncol(z), replace = TRUE)
  z.boot <- z[ind, ]
  mean[i] <- mean(z.boot)
}
mean

Ideally, what I would like to do is to get a bootstrap weighted mean for the first and second rows with the weights in the third row but I can't even get my loop to work. There has to be a better way to do this. Any help is appreciated


回答1:


try this... I don't quite get your point about weighted mean... but you can maybe work it out from here:

n= seq( 100, 500, 50)    
bootdata=list()
for (i in 1:length(n)) {
   bootdata[[i]]=data[sample(nrow(data), n[i], replace=TRUE), ]
}
bootdata
str(bootdata[[1]])



回答2:


Here is how a non-parametric bootstrap could be done. (This seems to be the type you are trying to do, based on your code.) Please note that nrow() and not the ncol() is the proper function. Bootstraps which are stored as items of the list "bootResult" could be retrieved via their index, like bootResult[[2]] and go through the next steps:

  nBoots<-10 #number of bootstraps
  bootResult<-list()
  for (i in seq_len(nBoots)){
    bootResult[[i]]<-z[sample(seq_len(nrow(z)), nrow(z), replace=TRUE), ]
  }


来源:https://stackoverflow.com/questions/40233902/bootstrap-samples-by-row-of-a-data-frame-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!