Repeating a vector within a multilevel simulation

Hi, I am pretty new to R. I am performing a multilevel simulation in R but I am stuck on how to generate appropriate values for a variable. The value should be different for each year and state but the same for individuals within the state. The code is below. Any help is appreciated. Thank you


seed <-  476;
n_state <-  length(state.name[1:4]); 
years <- 2007:2012
n_time <-  length(years);
n_id   <-  3;
state <- rep(state.name[1:n_state], each=n_time*n_id)
time <- rep(years, time=n_state*n_id)
id <- rep(1:n_id, each=n_time, time=n_state)
idvar <- data.frame(state, time, id)
head(idvar, n=18)
#>      state time id
#> 1  Alabama 2007  1
#> 2  Alabama 2008  1
#> 3  Alabama 2009  1
#> 4  Alabama 2010  1
#> 5  Alabama 2011  1
#> 6  Alabama 2012  1
#> 7  Alabama 2007  2
#> 8  Alabama 2008  2
#> 9  Alabama 2009  2
#> 10 Alabama 2010  2
#> 11 Alabama 2011  2
#> 12 Alabama 2012  2
#> 13 Alabama 2007  3
#> 14 Alabama 2008  3
#> 15 Alabama 2009  3
#> 16 Alabama 2010  3
#> 17 Alabama 2011  3
#> 18 Alabama 2012  3
#first three years, the value of prepost_id (intervention) = 0
prepost_id <- c(rep(0,3), rep(NA,(n_time - 3))) 


#a function to generate whether or not there was an intervention
# within state at a certain year

prepost <- function(prepost_val = prepost_id, n=1, prob =0.5, 
                    start=4, end =n_time){
  for (i in start: end) {
    
    prepost_val[i] <- ifelse(
      prepost_val[i-1] == 0,
      rbinom(n,1,prob=prob),
      prepost_val[i-1])
  }
  
  return(prepost_val)
}
prepost(prepost_val = prepost_id)
#> [1] 0 0 0 0 0 0

# repeating the process for all states
# Note that the value of the prepost function should be somewhat different
# for the different state but the same for individuals within that state

prepostdata <- rep(replicate(n_state, prepost(prepost_val = prepost_id)), 
                   time=n_id)
dta <- data.frame(state,time, id, prepostdata)

#This is what I got
head(dta, n=18)
#>      state time id prepostdata
#> 1  Alabama 2007  1           0
#> 2  Alabama 2008  1           0
#> 3  Alabama 2009  1           0
#> 4  Alabama 2010  1           1 
#> 5  Alabama 2011  1           1
#> 6  Alabama 2012  1           1
#> 7  Alabama 2007  2           0
#> 8  Alabama 2008  2           0
#> 9  Alabama 2009  2           0
#> 10 Alabama 2010  2           1
#> 11 Alabama 2011  2           1
#> 12 Alabama 2012  2           1
#> 13 Alabama 2007  3           0
#> 14 Alabama 2008  3           0
#> 15 Alabama 2009  3           0
#> 16 Alabama 2010  3           0 <---The problem is here: should be the same for all ids
#> 17 Alabama 2011  3           1
#> 18 Alabama 2012  3           1

#But this is what I want
#>      state time id prepostdata
#> 1  Alabama 2007  1           0
#> 2  Alabama 2008  1           0
#> 3  Alabama 2009  1           0
#> 4  Alabama 2010  1           1
#> 5  Alabama 2011  1           1
#> 6  Alabama 2012  1           1
#> 7  Alabama 2007  2           0
#> 8  Alabama 2008  2           0
#> 9  Alabama 2009  2           0
#> 10 Alabama 2010  2           1
#> 11 Alabama 2011  2           1
#> 12 Alabama 2012  2           1
#> 13 Alabama 2007  3           0
#> 14 Alabama 2008  3           0
#> 15 Alabama 2009  3           0
#> 16 Alabama 2010  3           1
#> 17 Alabama 2011  3           1
#> 18 Alabama 2012  3           1

Created on 2020-01-28 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.