0

I have this code and I want sample(1:3650, 365, replace=T) to generate the same numbers at both instances, but different values for each replication. So that for each replication i generate 365 random values and those values are used for both appending for both mydf1allt and mydf2allt, but different 365 values for each replication. Since there is correlation between them i want to capture that. This is my attempt at making a function for bivariate bootstrap. I know how to fix this with a for loop but it takes forever to run, so would be nice it could be made without.

listboot1sl = c()
listboot2sl = c()

pairbootstrap2 <- function(y) {

  .GlobalEnv$listboot1sl <-  replicate(10**y, rbind(listboot1sl, max(sum(mydf1allt[,2][sample(1:3650, 365, replace=T)])-17980405,0)))

  .GlobalEnv$listboot2sl <-  replicate(10**y, rbind(listboot2sl, max(sum(mydf2allt[,2][sample(1:3650, 365, replace=T)])-137376627,0)))
}

(mydf2allt is made out of two columns with numbers.)

This is with a foor loop doing what I want it to do without a foor loop:

pairbootstrap2 <- function(y) {

  for (i in 1:10**y){

   z <- sample(1:3650, 365, replace=T)

  .GlobalEnv$listboot1sl <-  rbind(listboot1sl, max(sum(mydf1allt[,2][z])-17980405,0))

  .GlobalEnv$listboot2sl <-  rbind(listboot2sl, max(sum(mydf2allt[,2][z])-137376627,0))

  }

}
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 1
    `?set.seed`? What do you mean by *'the same numbers, but different values'*? That seems to contradict each other. – Jaap Mar 04 '16 at 11:34
  • I tried to clarify now. In the first replication I want 365 random values used at two places. In the second replication I want 365 new random values used at the same place and so on. – Erik Lundberg Mar 04 '16 at 11:43

1 Answers1

0

I solved it using:

mydfboth <- cbind(mydf1allt[,2], mydf2allt[,2])

list <- replicate(10**6, colSums(mydfboth[sample(1:3650, 365, replace=T),]))
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116