# Specifying variable levels in multilevel repeated measures in R using lme4

I am trying to analyze a dataset in which there are three measures on patients within areal units, however I am having trouble in how I am thinking about random/fixed effects and including covariates at different levels of the model.

Dataset

Dependent variable:

• Presence of disease (1=Yes, 0=No)

Independent variables:

• Disease indicator (1=Disease 1, 2=Disease 2, 3=Disease 3)
• Patient age
• Patient race/ethnicity
• Patient sex
• Areal unit exposure

Example of my data:

ArealID | PatientID | DV | DV_Indicator | Patient_Age | Patient_Race | Patient_Sex | ArealExposure
A |      0001 |  1 |            1 |          57 |        White |        Male | 5
A |      0001 |  0 |            2 |          57 |        White |        Male | 5
A |      0001 |  0 |            3 |          57 |        White |        Male | 5
A |      0002 |  1 |            1 |          43 |        White |      Female | 5
A |      0002 |  1 |            2 |          43 |        White |      Female | 5
A |      0002 |  0 |            3 |          43 |        White |      Female | 5
A |      0003 |  0 |            1 |          60 |        Black |        Male | 5
A |      0003 |  0 |            2 |          60 |        Black |        Male | 5
A |      0003 |  0 |            3 |          60 |        Black |        Male | 5
... |       ... | ...|          ... |         ... |          ... |         ...
Z |      5678 |  1 |            1 |          77 |        Black |      Female | 12
Z |      5678 |  1 |            2 |          77 |        Black |      Female | 12
Z |      5678 |  1 |            3 |          77 |        Black |      Female | 12
Z |      5679 |  1 |            1 |          70 |        White |      Female | 12
Z |      5679 |  0 |            2 |          70 |        White |      Female | 12
Z |      5679 |  1 |            3 |          70 |        White |      Female | 12
Z |      5680 |  0 |            1 |          64 |     Hispanic |        Male | 12
Z |      5680 |  1 |            2 |          64 |     Hispanic |        Male | 12
Z |      5680 |  0 |            3 |          64 |     Hispanic |        Male | 12


Note that the areal exposure is the same within each areal unit and that patient age, race, and sex are the same within each patient.

Model specification

I am trying to specify my model using the glmer function in the lme4 package in R.

Since I have multiple observations per patient and multiple patients in each areal unit, I am specifying my model as:

m1 <- glmer(DV ~ DV_Indicator + Patient_Age + Patient_Race + Patient_Sex + ArealExposure + (1 | ArealID/PatientID), data=mydata, family=binomial)


If I understand the syntax correctly, this model should have random intercepts for both patients and areal units, but I am not sure that I have included the variables correctly since they all seem to be included at the disease level (rather than patient or areal unit).

The way I thought to indicate which level each variable should be included at was like this:

m2 <- glmer(DV ~ DV_Indicator + (1 + Patient_Age + Patient_Race + Patient_Sex | PatientID) + (1 + Areal_Exposure | ArealID), data=mydata, family=binomial)


but I think this is actually including these terms as random slopes(?). I have had trouble finding clear examples of this since it seems many people mix using other packages with lme4

Starting from the formulation of the model, we have measurements $i$ nested in patients $j$ nested in areas $k$. The dataset can be described in three-level formulation:

• Level-1 (measurement): DV, DV_Indicator
• Level-2 (patient): Patient_Age, Patient_Race, Patient_Sex
• Level-3 (area): ArealExposure

The model is then:

$$\text{logit}\{\text{Pr}(\text{DV}_{ijk}=1 | a_{jk},\text{DV_Ind}_{ijk})\} = a_{jk} + \beta \text{DV_Ind}_{ijk}$$

with the varying intercept

$$a_{jk} = b_{0k} + b_1\text{Patient_Age}_{jk} + b_2 \text{Patient_Race}_{jk} + b_3 \text{Patient_Sex} + \zeta_{jk}^{(2)}$$

and this varying intercept

$$b_{0k} = \delta_0 + \delta_1 \text{ArealExposure}_{k} + \zeta_k^{(3)}$$

In the above case you only have random intercepts as random effect, and you can incorporate that in the glmer function as you correctly stated:

m1 <- glmer(DV ~ DV_Indicator + Patient_Age + Patient_Race +
Patient_Sex + ArealExposure + (1 | ArealID/PatientID), data=mydata,
family=binomial)


however I would suggest you to think of it in its alternative expression:

m1 <- glmer(DV ~ DV_Indicator + Patient_Age + Patient_Race +
Patient_Sex + ArealExposure + (1 | ArealID)
+ (1 | ArealID:PatientID), data=mydata,family=binomial)


Both of them, give you the same result, and in the random part you will get only random intercepts. You don't have to worry whether you have included the variables correctly at the 'right' level as you say, since for the computational part it doesn't make any difference. Patient-level variables have the same value for each measurement within each patient, so there is no variability at all.

However, if at any level you suspect that there is any variability at higher level, e.g. Patient_Age at areal-level, or something measure-specific at patient-level, you can include a random slope for each case in the respective level. If you require to add varying slopes to the 1 intercepts, it's easier to do so using the second expression as you have a distinction on the levels.

This is something different from what you have written for the second model m2. This is the case of non-nested grouping factors.