# [R] repeated measures logistic regression

Ben Bolker bbolker at gmail.com
Sat Jul 27 21:05:25 CEST 2013

```Stanislav Aggerwal <stan.aggerwal <at> gmail.com> writes:

>
> I have searched the r-help archive and saw only one
> to mine.

Take a look at the r-sig-mixed-models (@r-project.org)
mailing list and archive ...
>
> My design is as follows.
>
>    - y is Bernoulli response
>    - x1 is continuous variable
>    - x2 is categorical (factor) variable with two levels
>
> The experiment is completely within subjects. That is, each subject
> receives each combination of x1 and x2.
>
> This is a repeated measures logistic regression set-up.
> The experiment will
> give two ogives for p(y==1) vs x1, one for level1 and one
> for level2 of x2.
> The effect of x2 should be that for level2 compared to level1, the ogive
> should have a shallower slope and increased intercept.

> I am struggling with finding the model using lme4. Here is a guess at it:
>
> glmer(y~x1*x2 +(1|subject),family=binomial)

> So far as I understand it, the 1|subject part says
> that subject is a random
> effect. But I do not really understand the notation or
>  how to specify that x1 and x2 are repeated measures variables.
> In the end I want a model that
> includes a random effect for subjects, and gives estimated slopes and
> intercepts for level1 and level2.

I believe you want

glmer(y~x1*x2 +(x1*x2|subject),family=binomial,data=...)

(I strongly recommend including the data= argument in your call)

This will give a population-level estimate of

intercept (log-odds in group 1 at x1=0)
treatment effect on intercept (log-odds(level2,x1=0)-log-odds(level1,x=0))
log-odds slope in level 1
difference in slopes

as well as among-individual variances in all four of these parameters,
and covariances among all the parameters (i.e. a 4x4 variance-covariance
matrix for these parameters).

For binary data and estimating 4 fixed + 10 RE parameters
(i.e., variances and covariances), you're going to need a lot of data --
very conservatively, 140 total observations.

It may help to center your x1 variable.

see http://glmm.wikidot.com/faq
(especially http://glmm.wikidot.com/faq#modelspec),
and the r-sig-mixed-models mailing list.

```