title: Loop mixed models and tukey comparison
body: <p>I would like to loop a mixed model and Tukey test.
  All I want to do is to repeat the fitting and the comparison for 3 columns (each one containing a response) and for 4 subgroups (total = 12).</p>
  
  <p>A similar dataframe is available here: <a href="https://drive.google.com/open?id=0Bwrsa11LAnrgTXMzWk1fYXR1MHM" rel="nofollow noreferrer">https://drive.google.com/open?id=0Bwrsa11LAnrgTXMzWk1fYXR1MHM</a>.  The 3 resposes are the columns "RESP_1","RESP_2" and "RESP_3", the subgroups are the variables of column "layer".</p>
  
  <p>I obtain the model and the adjustament for a single response and unique layer by:</p>
  
  <pre><code>#mixed model    
  Mlm_RESP_1 &lt;-lme(RESP_1~clay+till, random=~1|strata/point, data=loop_lm_tukey)
  
  #tukey
  ls_RSP_1 &lt;- lsmeans(Mlm_RESP_1,pairwise~till,adjust="tukey")
  ls_RSP_1$contrasts
  cld(ls_RSP_1)
  </code></pre>
  
  <p>Then, I try to loop the model for each column by:</p>
  
  <pre><code>#loop model    
  mlm_RESP &lt;- lapply(c("RESP_1", "RESP_2","RESP_3"), function(k) {
        lme(eval(substitute(j ~ clay+till, list(j = as.name(k)))), random = ~1|strata/point, data = loop_lm_tukey)})
  </code></pre>
  
  <p>From now, I am not able to loop the Tukey comparison using lsmeans package because <code>lapply</code> returns a list and this package can't handle this kind of class.</p>
  
  <p>Furthermore, how can I loop this for every layer?</p>
  
  <p>Any help to do loop of Tukey's comparison would be much appreciated.</p>