How do I split and modify the dotplot of a multiple mixed effects model in R

50 Views Asked by At

I have the following script in R 4.3.1, this isnt the complete script but just the relevant parts:

library(lme4)
library(lmerTest)
library(ggplot2)
library(lattice)
library(RColorBrewer)

MEDE.fin <- lmer(Relative_Value ~ Dilution + (Dilution|Paper) + (1|Paper:DataSet), data=DE_data)

dotplot(ranef(MEDE.fin), condVar = TRUE)

This gives me the following 2 graphs: Not the important graph Important graph

the second graph shows both the random effect on the intercept and slope, however i only want to use the part showing the slope. How do i solve this? (bonus points if you also can tell me how to change the colour of the graph layout)

I tried running this code (dotplot(ranef(MEDE.fin, condVar=TRUE))$Paper)$g[1] which is a variation on what i found here on stackoverflow but it doesnt work.

2

There are 2 best solutions below

1
DaveArmstrong On BEST ANSWER

Can't test because I don't have your data, but something like this should work:

dd <- as.data.frame(ranef(MEDE.fin), condVar = TRUE))
dd <- subset(dd, term == "Dilution")
library(ggplot2)
ggplot(dd, aes(y=grp,x=condval)) +
  geom_point() + facet_wrap(~term,scales="free_x") +
  geom_errorbarh(aes(xmin=condval -2*condsd,
                     xmax=condval +2*condsd), height=0)

Not sure what you meant by "change the color of the graph layout.

0
medium-dimensional On

IIUC, you can extract each panel using [.]. Here is a reproducible example using sleepstudy data from lme4:

library(lme4)
library(lattice)
fit <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)
x <- dotplot(ranef(fit))$Subject 
x[2] 

# To reduce the whitespace along x-axis
# update(x[2], scales=list(relation = "free")) 

Output:

enter image description here