user3545999
user3545999

Reputation: 73

Options for caterpillar plots in lme4, grouping by factor to visually identify temporal trends

I'm analyzing a large and complex data set using the lmer function in lme4. I'm using lattice and dotplot to generate caterpillar plots of my random effects. Is there a way to color code my caterpillar plot by either a factor or continuous variable that's in my data set? Have also used qqmath, I might just need help understanding how to use the "groups" argument. It would aid in a discussion point.

library("lme4")
data(package = "lme4")

summary(grouseticks)

fit1<-lmer(TICKS~1+(1|LOCATION), grouseticks)
rr1<-ranef(fit1, condVar = TRUE)
dotplot(rr1)
#We get a nice caterpillar plot of intecepts and variances by location, is there any way to color code those
#blue intercept points by another factor, such as year? 

Upvotes: 4

Views: 4302

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226761

The problem with doing what you want easily is that the ranef() results don't include the information that you want, and the dotplot.ranef.merMod() method is a bit too hard-coded to modify easily ... I'm going to show a ggplot solution instead. If you insist on a lattice solution, try examining lme4:::dotplot.ranef.merMod and seeing if you can adapt it following the solution below.

library("lme4")

fit1 <- lmer(TICKS~1+(1|LOCATION), grouseticks)
rr1 <- ranef(fit1, condVar = TRUE)
lattice::dotplot(rr1)

The question doesn't make a huge amount of sense for this particular data set, since the locations were sampled irregularly across years:

yrtab <- with(grouseticks,table(LOCATION,YEAR))
head(yrtab)
    YEAR
## LOCATION 95 96 97
##        1  0  5  3
##        2  0  0  3
##        3  0  7  0
##        4  3  6 11
##        5  0  3  0
##        6  0  9  0

... but just to continue the example, let's compute the modal sampling year for each location (i.e., the year in which the maximum number of samples were taken -- for ties we'll take the first year since it's easiest and this is just an example)

yrvec <- 95:97
yrmode <- with(grouseticks,yrvec[apply(yrtab,1,which.max)])
dd <- data.frame(LOCATION=rownames(yrtab),yrmode)

Now we need to get the random effects data in the right shape, and extract the standard errors:

## extract conditional mode and square root
## (c() works on simple attr(.,"postVar") -- would have
##  to be more careful with vector-valued random effects
rr2 <- data.frame(LOCATION=rownames(rr1[[1]]),
                  int=unname(rr1[[1]]),
                  se=sqrt(c(attr(rr1[[1]],"postVar"))))
## combine with other variables
rr3 <- merge(rr2,dd)
## prepare for caterpillar by ordering locations by est. value
rr4 <- transform(rr3,LOCATION=reorder(LOCATION,int))
library("ggplot2"); theme_set(theme_bw())
ggplot(rr4,aes(LOCATION,int,ymin=int-1.96*se,ymax=int+1.96*se))+
    geom_pointrange(aes(colour=factor(yrmode)))+coord_flip()+
        scale_colour_discrete(name="year")

enter image description here

This would have to be generalized a bit to deal with vector-valued random effects ...

Upvotes: 5

Related Questions