I cannot understand how to analyse a really simple randomised controlled trial in R.

I have two groups: Control and Intervention. Say there are 10 subjects in each group. These two groups are followed for 100 days, and X is measured every 10th day. I want to analyse if X response differently to time between the two groups. This is a very common design in medical science.

Lets for simplicity construct the following random intercept linear mixed model:
fit <- lme(X ~ Group * Time, random = ~ 1|ID, data)

The variable "Time" is categorical with levels: 1, 10, 20, 30, 40...100.

My question is: I am interested in the Group by Time interaction effects, between each category of Time, how can I get these results?

The main effects may be obtained like this:
library(car) Anova(fit)

For the next step I want to compare the time response between the two groups between each category of time, i.e. Time*Group from day 0 to day 10, from day 10 to day 20 etc.

I know I can use Lsmeans, but I only get within group time responses. Thats not what I want, I want between groups time responses!

  • $\begingroup$ Are you interested in the group differences at 11 different time points, i.e., difference between groups at time 1, difference between groups at time 10, difference between groups at time 20,...? $\endgroup$
    – user158565
    Nov 9, 2018 at 22:03
  • $\begingroup$ No. I want the differences in change: change in the intervention group vs. the change in the control group. Anova(fit) tells me if this is significant regarding all time points. However, I also want these tests from each time point to the next. $\endgroup$ Nov 9, 2018 at 22:27

2 Answers 2


Try something like this:

emm <- emmeans(fit, ~ Group*Time)
contrast(emm, interaction = “pairwise”)

This will compute pairwise comparisons of pairwise comparisons

See vignette(“interactions”, “emmeans”) for several examples related to this type of analysis.

  • $\begingroup$ Perfect! Thats it. I have been struggling with lsmeans, but emmeas is the new package. $\endgroup$ Nov 9, 2018 at 23:55

If I understand what you have in mind, in Stata, this would correspond to:

xtset id time    
xtreg x i.group##i.time, re cluster(ID)

You can follow that up with:

margins, dydx(group) at(time=(1 10(10)100))

Then you can get a nice plot of the effects by time with:

  • $\begingroup$ Seems very nice! I know STATA is much better than R for this.. However, I would very much like to do the same in R $\endgroup$ Nov 9, 2018 at 23:53
  • $\begingroup$ @user2862862 You had the Stata tag on the question, so I produced a Stata answer. This is a bit harder in R, but certainly doable. $\endgroup$
    – dimitriy
    Nov 9, 2018 at 23:56
  • $\begingroup$ Yes, Its because I know STATA produced what I want very easily, and also, I like STATA very much. More userfriendly. However, 99% of the time I use R. $\endgroup$ Nov 10, 2018 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.