Mixed effects logistic regression with repeated measures predictor 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.
Thanks
 A: 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
