0

Below I have code that finds the relative standard deviation of a bootstrap population that were bootstrapped from sample sizes ranging between 2 and 30.

I would like to create a loop that runs this loop for 10 iterations, finding the mean standard deviation for each sample size (2->30), and puts it into a data frame, so instead of the output being n 2:30 with the subsequent standard deviation, the standard deviation is instead a mean standard deviation (from 10 loops). I hope that makes sense.

n_range <- 2:29
bResultsRan <- vector("double", 28)
set.seed(30)
for (b in n_range) { 
  bRowsRan<-Random[sample(nrow(Random), b), ]
  base <- read.table("base.csv", header=T, sep="," )
  base$area<-5036821
  base$quadrea <- base$area * 16
  bootRan <- boot(data=bRowsRan$count, average, R=1000)
  base$data<- bootRan$t
  base$popsize<-(base$data*base$quadrea)
  bValue <- sd(base$popsize)/mean(base$popsize)
  bResultsRan[[b - 1]] <- bValue}

BRRan <- data.frame(n = n_range, bResultsRan)

plot(BRRan)
PeterLL
  • 3
  • 3
  • What is `Random` Try wtih `replicate(10, yourrloopfunc, simplify = FALSE)` – akrun Nov 28 '22 at 00:46
  • Hi, thank you for answering! Random is the data set which case count data. I have tried your suggestion, and it returns: [[1]] NULL [[2]] NULL [[3]] NULL [[4]] NULL [[5]] NULL [[6]] NULL [[7]] NULL [[8]] NULL [[9]] NULL [[10]] NULL. – PeterLL Nov 28 '22 at 00:54
  • 2
    If you can post a reproducible example, I can test it. thanks – akrun Nov 28 '22 at 00:56
  • Thank you. What do you mean by a reproducible example? Random contains data of counts that range between 0 and 4. "base.csv" is a simple excel file with the first column titled "n" and then couting upwards from 1. I'm basically trying to find how the standard deviation of the bootstrapped population changes as sample size increases – PeterLL Nov 28 '22 at 01:00
  • This is how I implemented your suggestion: replicate(10, for (b in n_range) { bRowsRan<-Random[sample(nrow(Random), b), ] base <- read.table("base.csv", header=T, sep="," ) base$area<-5036821 base$quadrea <- base$area * 16 bootRan <- boot(data=bRowsRan$count, average, R=1000) base$data<- bootRan$t base$popsize<-(base$data*base$quadrea) bValue <- sd(base$popsize)/mean(base$popsize) bResultsRan[[b - 1]] <- bValue} , simplify = TRUE) – PeterLL Nov 28 '22 at 01:00
  • 2
    How to make a great R reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. In general, the question should have everything needed to run the code and produce the issue. – Jon Spring Nov 28 '22 at 02:06

0 Answers0