title: (MuMIn) Dredge when global mixed-effects model is rank deficient
body: <p>I am trying to run variable selection on Poisson mixed-effect models using <code>glmer()</code> and <code>dredge()</code>. Since several variables are collinear I use the subsetting function of <code>dredge</code> to avoid correlated variables. However, to use <code>dredge()</code> effectively one needs to have a full model including all terms - which can lead to full model to be rank-deficient.</p>
  
  <p>[edited Feb 15 2016] To give a reproducible example, let's generate a random data set:</p>
  
  <pre><code>dfdat&lt;-data.frame(replicate(6, round(rnorm(6),2)))
  dfdat$group&lt;-factor(sample(1:2,nrow(dfdat),replace=T))
  dfdat$Y&lt;-rpois(nrow(dfdat),10)+rpois(nrow(dfdat),as.numeric(dfdat$group))
  dfdat
       X1    X2    X3    X4    X5    X6 group  Y
  1 -0.88  0.05  1.33 -1.51  0.61 -0.09     2  8
  2 -0.12 -0.57  0.05 -1.12  0.60 -0.41     1  7
  3  0.14 -0.97 -1.04  0.40  0.87  0.27     1  9
  4 -1.04 -0.26 -1.33  0.77 -1.84  1.67     1 11
  5 -1.06  1.10 -0.09  0.50 -2.62  2.15     1 10
  6 -1.74 -0.61  0.72 -0.29 -0.30 -0.93     1  8
  </code></pre>
  
  <p>Trying to run a model with all 6 terms does not work as the model is rank-deficient:</p>
  
  <pre><code> #library(MuMIn) # not run
   #library(lme4) # not run
   vars&lt;-names(dfdat)[1:6]
   form&lt;-formula(paste0('Y~',paste0(vars,collapse='+'),'+(1|group)'))
   fmod&lt;-glmer(form,data=dfdat,family='poisson')
   fixed-effect model matrix is rank deficient so dropping 1 column / coefficient
   Error: pwrssUpdate did not converge in (maxit) iterations
  </code></pre>
  
  <p>Using <code>dredge</code> on <code>fmod</code> would lead to the one variable dropped by <code>glmer</code> being always excluded. </p>
  
  <p>The solution, <a href="https://stackoverflow.com/questions/28744959/dredge-with-the-global-model-failing-to-converge">suggested here</a> seems to 1. run a model that converges, and 2.trick dredge into considering the full list of variables by changing the formula in the converged model.</p>
  
  <pre><code>## full model is rank deficient, so use smaller subset
  vars.red&lt;-vars[1:3]
  form.red&lt;-formula(paste0('Y~',paste0(vars.red,collapse='+'),'+(1|group)'))
  fmod.red&lt;-glmer(form.red,data=dfdat,family='poisson')
  </code></pre>
  
  <p>This new model <code>fmod.red</code> converges, but only includes variables X1,X2 and X3. </p>
  
  <p>Now to the "tricking dredge" part. The solution proposed on the page linked above didn't work as such with <code>glmer</code> as the structure of <code>mermod</code> is different from gamms. So I tried to use:</p>
  
  <pre><code>fom.red@call$formula&lt;-form
  </code></pre>
  
  <p>where <code>form</code> has all my covariates (to be subsetted).
  This didn't work, but using the formula in the frame element, as suggested by Kamil Bartoń below, did work:</p>
  
  <pre><code># replace formula in the frame element of fmod.red
  attr(fmod.red@frame,"formula")&lt;-form
  # check
  formula(fmod.red)
  # now apply dredge function with covariates
  # exclude variable combinations (randomly chosen for the sake of example)
  sexpr&lt;-expression(!((X1 &amp;&amp; X3) || (X1&amp;&amp;X6) || (X4 &amp;&amp; X6) || (X4 &amp;&amp; X5)))
  # run dredge()
  options(na.action = na.fail)
  ms&lt;-dredge(fmod.red,subset=sexpr)
  </code></pre>
  
  <h2>UPDATE</h2>
  
  <p>While <code>ms</code> seemed to include all variables, as shown by:</p>
  
  <pre><code>names(ms)
  [1] "(Intercept)" "X1"          "X2"          "X3"          "X4"          "X5"          "X6"         
  [8] "df"          "logLik"      "AICc"        "delta"       "weight"  
  </code></pre>
  
  <p>the new variables (X4,X5,X6) were never actually included (NAs everywhere):</p>
  
  <pre><code>summary(ms)
    (Intercept)          X1                X2                X3              X4        X5        X6    
   Min.   :2.407   Min.   :0.09698   Min.   :-0.4026   Min.   :-0.42078   +   : 0   +   : 0   +   : 0  
   1st Qu.:2.443   1st Qu.:0.22688   1st Qu.:-0.3204   1st Qu.:-0.35303   NA's:26   NA's:26   NA's:26  
   Median :2.474   Median :0.27361   Median :-0.2980   Median :-0.22444                                
   Mean   :2.535   Mean   :0.27539   Mean   :-0.3059   Mean   :-0.23517                                
   3rd Qu.:2.515   3rd Qu.:0.32357   3rd Qu.:-0.2718   3rd Qu.:-0.17472                                
   Max.   :3.009   Max.   :0.45664   Max.   :-0.2177   Max.   : 0.08802                                
               NA's   :20        NA's   :13        NA's   :16                                      
  </code></pre>
  
  <p>What is happening?</p>