title: R Pass colname into glht function as variable in loop
body: <p>I'm sure I'm missing something obvious here; am trying to use a loop to change the inputs to a formula, then generate a <code>glht</code> involving that formula. (<code>glht</code> = general linear hypothesis test). Thanks all!</p>
  
  <pre><code>df2 &lt;- data.frame(f1=seq(1:5),f2=seq(4,8), y=sample(x=c(0,1),size=5,replace=TRUE))
  df2 &lt;- data.frame(lapply(df2,factor)) # convert to factors
  
  library(multcomp)
  
  for (i in 1:(length(df2)-1)){
    fmla &lt;- as.formula( paste("y~",colnames(df2)[i],sep=""))
    fm1 &lt;- glm (fmla, family = binomial("logit"), data=df2)
   }
  </code></pre>
  
  <p>This much works; now I'm trying to dynamically pass in <code>f1</code> to make the line below:</p>
  
  <pre><code>  tuk &lt;- glht(fm1, linfct=mcp(f1 ="Tukey"))
  </code></pre>
  
  <p>I have tried:</p>
  
  <pre><code>tuk &lt;- glht(fm1,linfct=mcp(colnames(df2)[i] ="Tukey"))
  </code></pre>
  
  <p>but this gives:</p>
  
  <pre><code>Error: unexpected '=' in " tuk &lt;- glht(fm1,linfct=mcp(colnames(df2)[i] ="
  </code></pre>
  
  <p>I thought this would be something to do with <code>parse/eval</code>.
  So I tried:</p>
  
  <pre><code>e1 &lt;- paste("fm1,linfct=mcp(",colnames(df2)[i],"=Tukey)",sep="")
  glht(e1)
  </code></pre>
  
  <p>Gives:</p>
  
  <pre><code>Error in object$coefficients : $ operator is invalid for atomic vectors
  Error in modelparm.default(model, ...) :  no 'coef' method for 'model' found!
  </code></pre>
  
  <p>Then tried:</p>
  
  <pre><code>e1 &lt;- parse(text=paste("fm1,linfct=mcp(",colnames(df2)[i],"=Tukey)",sep="") )
  glht(e1)
  </code></pre>
  
  <p>Gives:</p>
  
  <pre><code>Error in parse.default(text = paste("fm1,linfct=mcp(", colnames(df2)[i],  : 
   &lt;text&gt;:1:4: unexpected ','
  1: fm1,
        ^
  </code></pre>
  
  <p>Finally:</p>
  
  <pre><code>e1 &lt;- as.expression(paste("fm1,linfct=mcp(",colnames(df2)[i],"=Tukey)",sep="") )
  glht(e1)
  </code></pre>
  
  <p>Gives:</p>
  
  <pre><code>Error in UseMethod("vcov") : no applicable method for 'vcov' applied to an object of class "expression"
  Error in modelparm.default(model, ...) :  no 'vcov' method for 'model' found!
  </code></pre>