I have run a linear mixed model in R using lmer. I am attempting to visualise the random effect structure. To produce a graph I have used print(dotplot(ranef(RT.model.4, condVar=T))[['part_no']])
where part_no
is the random effect from the mixed model. It creates something like this:
This is great. However I want to be able to visually tell the difference between my two groups of participants (the random effect being discussed) in the graph. I have group A and group B. In my dataset I have a column for participant type and for each row it gives a value of A or B.
I would like to either colour code the graph to show participants from groups A and B. Or perhaps better would be to create two separate panels, one for each group.
Any suggestions on how to do this would be very much appreciated.
This is a way using ggplot
rather than lattice
(just because I am more familiar with it) using code from the examples in ?dotplot.ranef.mer
. You need to match your treatment group in the data to the random effects grouping variables returned by ranef
. I don't see how this can be done automatically within dotplot.ranef.mer
.
Create a small example with a treatment group; each subject is assigned to one treatment group.
library(lme4)
library(ggplot2)
sleepstudy$trt = as.integer(sleepstudy$Subject %in% 308:340)
m = lmer(Reaction ~ trt + (1|Subject), sleepstudy)
Convert the random effects to a dataframe and match in the treatment groups
dd = as.data.frame(ranef(m, condVar=TRUE), "Subject")
dd$trt = with(sleepstudy, trt[match(dd$grp, Subject)])
You can then plot how you want, say using facet_
's or assigning a colour to each group, or...
ggplot(dd, aes(y=grp,x=condval, colour=factor(trt))) +
geom_point() + facet_wrap(~term,scales="free_x") +
geom_errorbarh(aes(xmin=condval -2*condsd,
xmax=condval +2*condsd), height=0)
ggplot(dd, aes(y=grp,x=condval)) +
geom_point() +
geom_errorbarh(aes(xmin=condval -2*condsd,
xmax=condval +2*condsd), height=0)+
facet_wrap(~trt)
You should be able to use the groups=
option in dotplot()
. Assuming your data is in a dataframe called df
with the group variable being in group
, you could use
print(dotplot(ranef(RT.model.4, condVar=T), groups=df$group)[['part_no']])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.