title: Extract raw model matrix of random effects from lmer objects (lme4, R)
body: <p>I have a question regarding the extraction of the (raw) model matrix of random effects from models fitted with <code>lmer</code> (<code>lme4</code>) in R.
  More specifically, I want to obtain a data frame or a matrix that contains all variables that are involved in random effects terms.
  The matter is further complicated because some entries of that matrix are zero.</p>
  
  <p>I usually extracted these matrices by accessing the sparse model matrix (<code>Zt</code>) via <code>getME</code>, after which I converted it to a regular matrix via its dimensions (see below).
  However, this leads to problems whenever the (raw) model matrix contains zeros because <code>Zt</code> only contains the nonzero elements.</p>
  
  <p>Here is an example, a simple mixed effects model where <code>x1</code> is normal and <code>x2</code> contains five values that are exactly zero:</p>
  
  <pre><code>id &lt;- rep(1:20,each=5)
  y &lt;- rnorm(100)
  x1 &lt;- rnorm(100)
  x2 &lt;- c(rep(0,5),rnorm(95))
  df &lt;- data.frame(id,x1,x2,y)
  </code></pre>
  
  <p>I fit two models using <code>lmer</code>, one with <code>x1</code> the other with <code>x2</code> as a predictor:</p>
  
  <pre><code>library(lme4)    
  m1 &lt;- lmer(y~1+x1+(1+x1|id), data=df)
  m2 &lt;- lmer(y~1+x2+(1+x2|id), data=df)
  </code></pre>
  
  <p>Here, I access the <code>Zt</code> slot of the fitted model object.
  The code below demonstrates that <code>Zt</code> doesn't contain the zero values in <code>x2</code>.
  As a result, my very simple conversion into a regular matrix throws an error.</p>
  
  <pre><code># length okay
  length(getME(m1,"Zt")@x)
  # model matrix okay
  mm1 &lt;- matrix(getME(m1,"Zt")@x, ncol=2, byrow=T)
  
  # too short
  length(getME(m2,"Zt")@x)
  # gives error on model matrix
  mm2 &lt;- matrix(getME(m2,"Zt")@x, ncol=2, byrow=T)
  </code></pre>
  
  <p>Here is what I thought I can do instead. <code>lmer</code> seems to save the raw matrices as well, which appears to work well as long as there is only one cluster variable.</p>
  
  <pre><code># seems to work okay
  mm3 &lt;- getME(m2,"mmList")[[1]]
  </code></pre>
  
  <p>However, the <code>mmList</code> slot is poorly documented online, and I barely find mentioning that people use it for programming.
  Accessing <code>Zt</code> seems by far the more common option.</p>
  
  <p>Is it possible to construct the model matrix of random effects from <code>Zt</code> even if the raw model matrix contains zeros?
  If not, then what should I expect from <code>mmList</code>?</p>