Plotting GLMMs with ggplot2

You can use ggplot2 to plot your model fit and data. The example is based on mod1 that we fitted in previous page. First, create a new dataframe and add model predictions to the new samples.

#create a new dataframe: names should match those of the originary dataframe
newdfr = expand.grid(X = seq(from = 30, to = 130, by = 1),
                     condition = c("A", "B"),
                     Subject =  c("S1", "S2", "S3", "S4", "S5", "S6"))

#add preditions
newdfr$predict.mod1 = predict(mod1, type = "response", newdata = newdfr)

Now, use ggplot{ggplot2} to plot the model:

ggplot(datafr, aes(x = X, y = Longer/Total, color = condition)) +
  ylab("Probability") + xlab("X") +
  geom_point() +
  geom_line(data = newdfr, aes(x = X, y = predict.mod1)) +
  facet_wrap(~ Subject, ncol = 3) +
  scale_x_continuous(breaks= seq(30, 130, by = 30))

Here is how the plot looks like. Just as a comment, probably the model is a bit overfitting, a simpler model with fewer random predictors might work better here. You can test this yourself by fitting nested models and comparing them using anova(), AIC()etc.

Example of model plot using ggplot{ggplot2}

Example of model plot using ggplot{ggplot2}

The pdf of the image is available here:example

Now, some extra functions for MERpsychophysics.0/lme4.0 only. You can run the example from the MERdemo.r file. First, do you want to highlight a subject? Use palette!

palette(value = c("gray1", "gray2","red","gray3","gray4"))
MERplot(mod2b, col = T, p05line = T, x.from = 300, = 1300)

In order to export a plot use a function such as pdf(), postscript(), ect. Use par() to modify graphical parameters such as line width, character expansion, etc.

pdf(file = "Figure1.pdf")
par(mar = c(4,4,1,1), cex = 2, lwd = 2, cex.axis = 0.8, cex.lab = 0.8)
MERplot(mod2b, col = T, p05line = T, x.from = 300, = 1300)

Plot partial effects (requires the package languageR)

 plotLMER.fnc(mod1b, xlabel = "Stimulus Intensity",
 ylabel = "Predicted Response", ylimit = c(0,1))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: