I have one group ($n=40$) of subjects pre- and post-tested (time; coded $0$ and $1$) on a continuous variable (y). I also have a gender variable coded $0$ and $1$.

Using R, I was wondering how I could run a mixed effects model using time point and gender as the independent variables, controlling for clustering at id level?

P.S. I'm interested in detecting a meaningful difference among the average scores between the pre- and post-scores as a whole.

Here is my data in R:

dat <- {
 data.frame(id = rep(1:40, each = 2), y = rnorm(80, 20, 5), gender = rep(rbinom(40, 1, .5), each = 2), 
 time = rep(0:1, 40))

  • 1
    $\begingroup$ One question I'd have before providing any answer is are you more concerned with detecting a meaningful/statistically significant difference among the average scores between the pre- and post-scores as a whole, or are you more interested in identifying subgroups or individuals who displayed the largest difference between pre- and post-scores? $\endgroup$ – StatsStudent Jan 6 at 20:50
  • $\begingroup$ @StatsStudent, thanks, mainly I'm interested in the former; detecting a difference between the pre- and post-scores mean changes given gender. $\endgroup$ – rnorouzian Jan 6 at 20:55
  • $\begingroup$ Logically, you would want to treat time as a factor or change the coding from {1, 2} to {0, 1}. $\endgroup$ – Dimitris Rizopoulos Jan 7 at 5:46

This is a pretty standard specification.

  • Assuming that the conditional distribution of y (i.e., residuals from the model) are reasonably close to Gaussian, a linear mixed model is appropriate.
  • Interacting fixed effects of gender and time and a random intercept varying across id should do what you want.
    • It doesn't make sense to test for differences in the effect gender by id because [unless you're working with people who are gender transitioning, or certain species of sex-transitioning fish] each individual will only have a single id.
    • The variation in time effect by id is subsumed in the residual variation because there are only two time observations per person.
  • note that the interpretation of the main effects of gender and time will depend on the contrasts used; switch from the default treatment contrasts to sum-to-zero contrasts if you want average effects. (Or use the emmeans package to do this post hoc.)


## oldest, provides p-values by default
nlme::lme(y~gender*time, random= ~1|id, data=dat)
## faster, more flexible; load the lmerTest package
## instead of lme4 to get p-values
## recent, even more flexible


Since you used the [hierarchical-bayesian] tag:

MCMCglmm::MCMCglmm(y~gender*time, random=~id, data=dat)

Ben has provided a great answer. Another approach would be to use the gee or geepack packages for marginal model (using the default gaussian family for your data). You would have to pick an appropriate covariance structure and specify that in the corstr argument. In your case this would be:

geepack::geeglm(y~time*gender, id=id, data=dat, corstr="ar1", waves=time)


gee::gee(formula = y ~ time * gender, id = id, data = dat, corstr = "unstructured")
  • $\begingroup$ Thanks, could you possibly briefly provide an interpretation of your approach? $\endgroup$ – rnorouzian Jan 7 at 2:47

Your Answer

