Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dotplot of random effects

Tags:

plot

r

lme4

I have an generalized mixed effects model such as the following:

d <- data.frame(
    g = sample(c("A","B","C","D","E"), 250, replace=TRUE),
    x = runif(250, max=100),
    y = sample(c(0,1), 250, replace=TRUE)
)

require(lme4)

fm <- lmer(y ~ x + (1 + x | g), data=d, family=binomial)

I'd like to plot the random effect of the intercepts using dotplot but without plotting the random slope component of x. My problem is that I can't seem to figure out how to access just the intercept component and not the random slopes.

For example, what I want is the left hand side of this plot:

dotplot(ranef(fm, postVar=TRUE))

enter image description here

Using dotplot(ranef(fm, postVar=TRUE)$g[,2]) doesn't give me what I want even though I think it should! I've looked at str(fm), but didn't see anything that helped me get any closer.

Any help and hints would be much appreciated!

like image 457
smillig Avatar asked Jan 17 '23 08:01

smillig


2 Answers

You were almost there in your original code:

dotplot(ranef(fm, postVar=TRUE))$g[1]

An additional tip to free the scales for each plot:

dotplot(ranef(fm, postVar=TRUE),
        scales = list(x =list(relation = 'free')))
like image 120
AzadA Avatar answered Jan 25 '23 18:01

AzadA


This should get you pretty close. I've been meaning to add support for lme4 objects to coefplot so maybe this be my catalyst.

theRan <- ranef(fm, postVar=TRUE)
pv <- attr(theRan$g, "postVar")
se <- pv[1, 1, ]
theIntercepts <- theRan$g[, 1, drop=F]
theFrame <- cbind(theIntercepts, se)
names(theFrame)[1] <- "Intercept"
theFrame$Low <- with(theFrame, Intercept - 2 * se)
theFrame$High <- with(theFrame, Intercept + 2 * se)
theFrame$Variable <- rownames(theFrame)
ggplot(theFrame, aes(y=Intercept, x=reorder(Variable, Intercept))) + geom_linerange(aes(ymin=Low, ymax=High), colour="black") + geom_point(, colour="blue") + coord_flip() + labs(y="Intercept", x=NULL)
like image 29
Jared Avatar answered Jan 25 '23 20:01

Jared