title: Multiple glm in for loop
body: <p>I have an R dataframe, strongly simplified as:</p>
  
  <pre><code>id &lt;- rep(1:2, c(6,8))
  correct &lt;- sample(0:1,14,TRUE)
  phase &lt;- c(rep("discr",3),rep("rev",3), rep("discr",4),rep("rev",4))
  dat &lt;- data.frame(id,correct,phase)
  </code></pre>
  
  <p>with <code>id</code> as my subjects (in reality I have a lot more than 2), <code>correct</code> = responses coded as incorrect (0) or correct (1), and the <code>phases</code> Discrimination and Reversal (within-subjects factor). </p>
  
  <p>I want to perform a logistic regression in the form of</p>
  
  <pre><code>glm(correct~phase, dat, family="binomial")
  </code></pre>
  
  <p>later possibly adding additional predictors. 
  However, since I have a varying amount of data for each subject, I would like to perform <code>glm()</code> seperately for each subject and later compare the coefficients with ANOVA for group effects. 
  I would like to do this in a for loop in the form of</p>
  
  <pre><code>for(i in seq_along(dat$id)){
     my_glm[i] &lt;- glm(correct~list,dat[dat$id==i,],family="binomial")
  }
  </code></pre>
  
  <p>but keep receiving the error message </p>
  
  <pre><code>&gt;Error in `contrasts&lt;-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
   contrasts can be applied only to factors with 2 or more levels.
  </code></pre>
  
  <p>I have checked my data and there is no factor which contains only one level. All subjects gave at least one incorrect and one correct response, and all took part in Discrimination and Reversal. The function works outside the loop when I specify a particular subject. </p>