# r mixed model unstructured and ar1 covariance matrices

My goal is to specify two different covariance matrices for two different random intercepts.

Briefly, this is my dataset.

Outcome is continuous (school test scores)

13 Schools in my study. Random students selected for testing. All students who were selected were tested. Students nested within Schools.

I am interested in estimating the random intercept for each School and each Student.

So this is my model.

lme( y ~ SchoolName + Age + Sex, random = ~1 | SchoolName/StudentID, data= df)

I am interested in

• Schools following unstructured covariance matrix
• Students , ar1.

I have another variable time, which is a discreate variable that takes values either 10,11,12,13 . So I specified the model like this

lme( y ~ SchoolName + Age + Sex,
random = ~1 | SchoolName/StudentID,
correlation = corAR1(form = ~1 | time/StudentID,
data= df)


I am seeing an error, "Incompatible formulas in random and correlation", How to resolve this error. Please help.

• Do you have two or more measurements of each student?
– BenP
Commented Apr 5 at 6:03
• In my understanding, in lme you can only specify correlation structures for the exact random effects that are in the model. So you could only specify a correlation structure to the ~1|SchoolName/StudentID term. Commented Apr 5 at 7:34
• @BenP, Yes, 40% of the students have more than one observation. Commented Apr 5 at 14:53

Sointu is right. But it seems that it is possible to use something like this, where I use an example of my own:

HAR <- lme(read ~ 1+occf, data=da, random= ~1|school,
correlation=corAR1(form = ~ occasion |school/id),
weights = varIdent(form = ~1|occasion))


This way you get a random intercept across schools, and a heterogeneous AR1 structure across time points (occasion) within student id.

But you say you'd like an unstructured covariance matrix across schools. The above would give a compound symmetry structure. I cannot easily understand why you would like an unstructured pattern for schools. Suppose you have maximally 20 students in a school. Then the unstructured pattern would try to estimate 1/22021=210 (co)variances. That would be very hard to estimate. Also, there is no logical order for the students in a school, they are exchangeable I guess, and thus the compound symmetry structure seems more plausible.

• Ben I followed this approach and I get an error that says Error in Initialize.corAR1(X[[i]],...) : covariate must have unique values within groups for "corAR1" objects Commented Apr 5 at 15:56
• I'm not sure. Sounds as if for one or more "id" the "occasion" variable is not unique, like having observed a person twice at the same time point. Did you check that?
– BenP
Commented Apr 5 at 16:06
• good point Ben, that is a possibility. Some students are tested more than once, twice, during age 10 for example Commented Apr 5 at 19:51
• Maybe you can combine the two values somehow into one new value.
– BenP
Commented Apr 5 at 20:20
• That is the compound symmetry structure e.g. for "random = ~1 | school".
– BenP
Commented Apr 6 at 7:36