title: using as.formula() in glmer and dredging it with MuMIn
body: <p>I am trying to use the <code>dredge</code> function from the <code>MuMIn</code> package, as follows. Since I want to run it for different datasets, I am using a function, that takes in the vector of covariates and the formula is created from this vector within the function. </p>
  
  <pre><code>require(lme4);require(MuMIn)
  x1 &lt;- rnorm(100)
  x2 &lt;- rnorm(100)
  zrand &lt;- sample(letters, 100, replace =T)
  yind &lt;- sample(1:1000, size=100, replace=T)
  mydata &lt;- data.frame(yind, x1, x2, zrand)
  vars &lt;- c('x1', 'x2')
  </code></pre>
  
  <p>When I run it this way, I get the error that symbol is not subsettable. I think it has to do with using <code>vars</code> in the formula, though the lmer runs fine</p>
  
  <pre><code>myformula &lt;- as.formula(paste('yind~',paste(vars,collapse='+'),'+(1|zrand)'))
  
  mylmer1 &lt;- lmer(myformula, mydata, gaussian)
  
  dredge(mylmer1)
  </code></pre>
  
  <p>Error in terms(as.formula(formula(x))) : 
    error in evaluating the argument 'x' in selecting a method for function 'terms': Error: object of type 'symbol' is not subsettable</p>
  
  <p>But this works fine, for a reason I do not understand fully, but has to do with the scope of the dredge function. (I may be totally off the mark here)</p>
  
  <pre><code>mylmer2 &lt;- lmer(as.formula(paste('yind~',paste(vars,collapse='+'),'+(1|zrand)')), 
                mydata, gaussian)
  
  dredge(mylmer2)
  </code></pre>
  
  <p>What I doing in my analysis is more akin to this, where lmer and dredge are run inside a function</p>
  
  <pre><code>myfn &lt;- function(fnd,fnvar){
    myformula &lt;- as.formula(paste('yind~',paste(fnvar,collapse='+'),'+(1|zrand)'))
      print(myformula)
  
  mylmer1 &lt;- lmer(myformula, fnd, gaussian)
      print(mylmer1@call)
    print(dredge(mylmer1))
  
    mylmer2 &lt;- lmer(as.formula(paste('yind~',paste(fnvar,collapse='+'),'+(1|zrand)')), 
                  fnd, gaussian)
  
    print(mylmer2@call)
    print(dredge(mylmer2))
    }
  
  myfn(fnd=mydata, fnvar=vars)
  </code></pre>
  
  <p>Dredgin only lmer1 gives this error:</p>
  
  <pre><code>Error in print(dredge(mylmer1)) : 
    error in evaluating the argument 'x' in selecting a method for function 'print': Error in terms(as.formula(formula(x))) : 
    error in evaluating the argument 'x' in selecting a method for function 'terms': Error: object of type 'symbol' is not subsettable
  </code></pre>
  
  <p>Dredging only lmer2, gives the following error</p>
  
  <pre><code>Error in print(dredge(mylmer2)) : 
    error in evaluating the argument 'x' in selecting a method for function 'print': Error in terms(as.formula(formula(x))) : 
    error in evaluating the argument 'x' in selecting a method for function 'terms': Error in paste(fnvar, collapse = "+") : object 'fnvar' not found.
  </code></pre>
  
  <p>Is there a way to get around this? Is this a necessary limitation of the dredge function? </p>