Description
Hi @gndaskalova
Just thought I'd start a new issue thread here to ask for your input on the improvement for the second data vis tutorial. As per our previous discussion, I've left Hadyn's more complicated code to visualize the mixed effect models, but have just played around with using ggpredict from ggeffects to do the same thing, which I thought would be useful to present to anyone looking at the tutorial.
However, my biggest issue was that with Hadyn's original mixed effect model built with the function lme
instead of lmer
, I for some reason am unable to use the ggpredict function and this error comes up: Error in filter_impl(.data, quo) : Result must have length 6, not 42
.
# Original mixed effect model
lm_heights <- lme(Height ~ year, random = ~1|year/plot,
data = heights[heights$land == "Hogsmeade",])
# ggpredict does not work for this model
ggpred <- ggpredict(lm_heights, terms = c("year"))
I spent some time trying to see why that would be the case but couldnt figure it out so decided to try the same using lmer
, and I was then able to use the ggpredict function and come up with the graph in that way. The code as follows:
# Utilizing lmer to create the mixed effect model instead
with_lmer_hogs <- lmer(Height ~ year + (1|plot) + (1|year),
data = heights[heights$land == "Hogsmeade",])
ggpred_hogs <- ggpredict(with_lmer_hogs, terms = c("year"))
with_lmer_narn <- lmer(Height ~ year + (1|plot) + (1|year),
data = heights[heights$land == "Narnia",])
ggpred_narn <- ggpredict(with_lmer_narn, terms = c("year"))
# Creating the graph
heights$plot <- as.factor(heights$plot)
(new_graph <- ggplot() +
geom_point(data = heights,
aes(x = year, y = Height, colour = land)) +
geom_line(data = ggpred_hogs, aes(x = x, y = predicted),
colour = "#ff9999", size = 1) +
geom_ribbon(data = ggpred_hogs,
aes(ymin = conf.low, ymax = conf.high, x = x),
fill = "rosybrown1", alpha = 0.4) +
geom_line(data = ggpred_narn, aes(x = x, y = predicted),
colour = "#99b3ff", size = 1) +
geom_ribbon(data = ggpred_narn,
aes(ymin = conf.low, ymax = conf.high, x = x),
fill = "#deebf7", alpha = 0.4) +
scale_colour_manual(values = c("#ff9999", "#99b3ff"),
breaks = c("Hogsmeade","Narnia"),
name = "Land of magic",
labels = c("Hogsmeade","Narnia")) +
labs(y = "Mean canopy height",
x = "Year") +
theme_bw() +
theme(panel.grid = element_blank()))
This piece of code produces a similar graph to what Haydn's code produces:
Therefore, I just wanted to ask if you would like me to add in this piece of code to show the use of ggpredict or if I should just leave it, since an entirely different function to develop the mixed effect model is being utilized here. Also, I'm actually unsure if i've stated the random effects correctly (Haydn's original random = ~1|year/plot vs my lmer using (1|plot) + (1|year) instead).
Let me know what you think is the most appropriate and I'd be happy to make the changes.