Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Didzis' answer is great! Just to wrap it up a little bit, I put it into its own function that behaves a lot like <code>qqmath.ranef.mer()</code> and <code>dotplot.ranef.mer()</code>. In addition to Didzis' answer, it also handles models with multiple correlated random effects (like <code>qqmath()</code> and <code>dotplot()</code> do). Comparison to <code>qqmath()</code>:</p> <pre><code>require(lme4) ## for lmer(), sleepstudy require(lattice) ## for dotplot() fit &lt;- lmer(Reaction ~ Days + (Days|Subject), sleepstudy) ggCaterpillar(ranef(fit, condVar=TRUE)) ## using ggplot2 qqmath(ranef(fit, condVar=TRUE)) ## for comparison </code></pre> <p><img src="https://i.stack.imgur.com/3Zutl.jpg" alt="enter image description here"></p> <p>Comparison to <code>dotplot()</code>:</p> <pre><code>ggCaterpillar(ranef(fit, condVar=TRUE), QQ=FALSE) dotplot(ranef(fit, condVar=TRUE)) </code></pre> <p><img src="https://i.stack.imgur.com/1B2Rx.jpg" alt="enter image description here"></p> <p>Sometimes, it might be useful to have different scales for the random effects - something which <code>dotplot()</code> enforces. When I tried to relax this, I had to change the facetting (see this <a href="https://stackoverflow.com/a/3685496">answer</a>).</p> <pre><code>ggCaterpillar(ranef(fit, condVar=TRUE), QQ=FALSE, likeDotplot=FALSE) </code></pre> <p><img src="https://i.stack.imgur.com/HLmV8.jpg" alt="enter image description here"></p> <pre><code>## re = object of class ranef.mer ggCaterpillar &lt;- function(re, QQ=TRUE, likeDotplot=TRUE) { require(ggplot2) f &lt;- function(x) { pv &lt;- attr(x, "postVar") cols &lt;- 1:(dim(pv)[1]) se &lt;- unlist(lapply(cols, function(i) sqrt(pv[i, i, ]))) ord &lt;- unlist(lapply(x, order)) + rep((0:(ncol(x) - 1)) * nrow(x), each=nrow(x)) pDf &lt;- data.frame(y=unlist(x)[ord], ci=1.96*se[ord], nQQ=rep(qnorm(ppoints(nrow(x))), ncol(x)), ID=factor(rep(rownames(x), ncol(x))[ord], levels=rownames(x)[ord]), ind=gl(ncol(x), nrow(x), labels=names(x))) if(QQ) { ## normal QQ-plot p &lt;- ggplot(pDf, aes(nQQ, y)) p &lt;- p + facet_wrap(~ ind, scales="free") p &lt;- p + xlab("Standard normal quantiles") + ylab("Random effect quantiles") } else { ## caterpillar dotplot p &lt;- ggplot(pDf, aes(ID, y)) + coord_flip() if(likeDotplot) { ## imitate dotplot() -&gt; same scales for random effects p &lt;- p + facet_wrap(~ ind) } else { ## different scales for random effects p &lt;- p + facet_grid(ind ~ ., scales="free_y") } p &lt;- p + xlab("Levels") + ylab("Random effects") } p &lt;- p + theme(legend.position="none") p &lt;- p + geom_hline(yintercept=0) p &lt;- p + geom_errorbar(aes(ymin=y-ci, ymax=y+ci), width=0, colour="black") p &lt;- p + geom_point(aes(size=1.2), colour="blue") return(p) } lapply(re, f) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload