title: R: simulating 2-level model
body: <p>I am trying to simulate the unequal sample size in the multilevel model.I have four groups, the sample size is 100,200,300,and 400, respectively.
  So, the total sample size is 1000. w, u0,u1 variables are in  the level 2 ; x , r0 are in the level 1. y is an outcome</p>
  
  <pre><code>nSubWithinGroup &lt;- c(100,200,300,400)###the sample size in each group 
  nGroup &lt;-4 ## 4 groups
  gamma00 &lt;- 1 
  gamma01 &lt;- 1 ## b0 = gamma00+gamma01*w+u0
  gamma10 &lt;- 1 ## b1 = gamma10+gamma11*w+u1
  gamma11 &lt;- 1
  dataLevel1 &lt;- mat.or.vec(sum(nSubWithinGroup),4)
  colnames(dataLevel1) &lt;- c("Group","X","W","Y")
  rowIndex &lt;- 0
  for (group in 1:nGroup) {
    u0 &lt;- rnorm(1,mean=0,sd=1)
    u1 &lt;- rnorm(1,mean=0,sd=1)
    w &lt;- rnorm(1,mean=0,sd=1)
    for(i in 1:length(nSubWithinGroup)){
      for (j in 1:nSubWithinGroup[i]){
        r0 &lt;- rnorm(1,mean=0,sd=1)
        x &lt;- rnorm(1,mean=0,sd=1)
        y &lt;- (gamma00+gamma01*w+u0)+(gamma10+gamma11*w+u1)*x+r0
        rowIndex &lt;- rowIndex + 1
        dataLevel1[rowIndex,] &lt;- c(group,x,w,y)
      }
    }
  }
  </code></pre>
  
  <p>I ran the codes, and it showed me the value in the "Group" column is 1 , no 2,3, or 4. Also, it has errors, which is:</p>
  
  <blockquote>
    <p>"Error in <code>[&lt;-</code>(<code>*tmp*</code>, rowIndex, , value = c(2, -1.94476463667851, -0.153516782293473,  : 
     subscript out of bounds" </p>
  </blockquote>