I've read the threads on some similar topics but I'm not positive that they address the issue that I'm having. I am conducting analysis on a data set investigating the factors that predict whether or not patients have had an exam (binary outcome variable, so I'm using logistic regression). One of the prediction factors is the implementation of an intervention (dichotomous, between-subjects predictor), and I want to see if this intervention was effective across pre-post measures (dichotomous, within-subjects predictor). Patients are nested within doctors, and it is actually the doctors that are either part of the intervention or not. Ultimately, I want to examine the interaction effect of the intervention x time, with the hypothesis that the change in proportion of exams completed (from pre to post) will be higher in the intervention group compared to the control group. I'm trying to run this analysis in R using the glmer package, but I'm not quite sure how to set it up correctly. If these are my variables:

  • outcome (dichotomous)
  • intervention (dichotomous, between subjects)
  • time (dichotomous, within-subjects)
  • Doctor (nominal, doctor name)
  • ID (participant ID)

I essentially want to do:

outcome ~ intervention + time + intervention:time

but I want time to be within ID, and I also want to allow random intercepts and slopes by Doctor.

I've tried: outcome ~ 1|Doctor + intervention + time|ID + intervention:time

But this gives me errors about a model frame, formula mismatch in R.

I can't share the data because of publicly identifiable information, but I'm stuck and was hoping somebody could help point me in the right direction.


  • $\begingroup$ How many participants and doctors do you have, and what do you mean by "I want time to be within ID" ? $\endgroup$ Commented Aug 24, 2016 at 15:59

1 Answer 1


You have participants nested within doctors, so the random structure of the model should reflect this by estimating random intercepts for doctor and participants within doctors, provided that you have sufficient numbers of both (10 is a typical rule of thumb). I would start with the model:

glmer(outcome ~ intervention*time + (1|Doctor/ID), data=mydata, family=binomial(link=logit)

where intervention*time is shorthand for intervention + time + intervention:time. I don't know what you mean by "I want time to be within ID". From your description, time is simply a pre/post indicator variable. You could allow for the effect of time to differ among participants (and/or doctors) by adding a random coefficient for time:

glmer(outcome ~ intervention*time + (time|Doctor/ID), data=mydata, family=binomial(link=logit)

In this formulation, the model will estimate time random slopes for both doctors and participants. If you wanted time random slopes for only participants you would use:

glmer(outcome ~ intervention + time + intervention:time + (1|Doctor) + (time|Doctor:ID), data=mydata, family=binomial(link=logit)

where this uses the fact that (1|Doctor/ID) is just shorthand notation for (1|Doctor) + (1|Doctor:ID

  • $\begingroup$ Thank you very much - I think this gets me where I want to go. To clarify, I have the data in long format (i.e., I have separate rows for the pre and post measures for each participant). so when I say 'I want time to be within ID', I'm just saying that I want to make sure R is recognizing that two rows with the same ID actually belong to the same participant, and should not be viewed as independent observations. $\endgroup$
    – drRussClay
    Commented Aug 24, 2016 at 16:39
  • $\begingroup$ I believe your first formulation (allowing the effect of time to differ among participants and doctors) is the appropriate way to control for the lack of independents due to patients as well as doctors over time. I guess one other thing to consider; doctors are technically nested within the intervention / control groups (i.e., all of a doctor's patients will either be intervention or control, there won't be a mix of both), so would that actually give me (time|intervention/Doctor/ID) ? $\endgroup$
    – drRussClay
    Commented Aug 24, 2016 at 16:39
  • $\begingroup$ @drRussClay, no, you don't have sufficient intervention levels to treat it as random, and it doesn't really meet the usual definition(s) for random vs fixed effects. Plus you already have it as a fixed effect so adding it as random would be questionable from that standpoint too. Any non-independence within intervention groups will be handled by including it as a fixed effect anyway. $\endgroup$ Commented Aug 24, 2016 at 18:16
  • $\begingroup$ "I want to make sure R is recognizing that two rows with the same ID actually belong to the same participant, and should not be viewed as independent observations".....Yes, that is how it works when you include ID as a grouping variable in the random structure. $\endgroup$ Commented Aug 24, 2016 at 18:22

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.