Skip to content
Prev 6567 / 20628 Next

corSymm in lme model returns error

I have added more detail as follows in order for my problem to be quickly reproducible, in the hope that somebody might be able to help me more easily:

I have given the code with which to reproduce my dataframe, as well as the correlation matrix and correlation structure that I would like to incorporate into my lme model. 

The main issue I struggle to understand is the following: I have 22 observations on the variable "avg", where these observations fall into 4 groups (geographical districts, the factor being called "Level3Name"). I believe that the 22x22 correlation matrix "mycorrII" roughly describes the (spatial) correlation between my 22 observations. I succeed in fitting an lme model which models "avg" as the sum of a grand mean (i.e. intercept), a random effect at district level (i.e. at "Level3Name") and a residual random effect at observation level. But as soon as I introduce my correlation structure "mycorrstrucII" into the model, I obtain an error saying that the "Covariate must have unique values within groups for corSymm objects".

I would be extremely grateful for any advice!

Thank you!


# data: ------------------------------------------------------------------------------------------------

regIIsp <- cbind(Level3Name, avg)

where:

Level3Name <- c(rep("DI4",2), rep("DI5",3), rep("DI6",5), rep("DI7",12))
Level3Name <- as.factor(Level3Name)
avg <- c(2943, 2267, 3027, 2515, 3178, 2301, 2398, 2503, 2843, 2508, 2373, 1384, 1805, 2783, 
2205, 1717, 1338, 1971, 2733, 2666, 2268, 1603)


# correlation matrix: ---------------------------------------------------------------------------------

mycorrII <- diag(x=1,nrow=22,ncol=22)
mycorrII[cbind(col1,col2)] <- 0.28

where:

col1 <- c( 1 , 1,  1,  2,  2,  2,  3,  3,  3,  4,  4,  4,  5,  5,  5,  6,  6,  6,  7,  7,  7,  8,  8,
8,  9,  9,  9, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16,
16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22,  2,  3,  3,
 4,  4,  5,  5,  5 , 5 , 6,  6,  7,  7,  8,  8,  9,  9,  9,  9, 10, 10, 11, 11, 12, 12, 12,
12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 17, 17, 18, 18, 18,
18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22)

col2 <- c(2,  7,  8,  1,  7,  8,  4,  5, 19,  3,  5, 19,  3,  4, 19,  7,  9, 10 , 6,  8,  9,  6,  7,
9,  6,  7,  8,  6,  9, 11, 13, 18, 20, 15, 21, 22, 11, 18, 20, 15, 19, 20, 14, 16, 21, 15,
21, 22, 12, 13, 18, 11, 13, 17,  5, 14, 20, 13, 14, 19, 15, 16, 22, 12, 16, 21,  1,  4,  5,
3,  5,  3,  4, 14, 19,  9, 10,  8,  9,  7,  9,  6, 7,  8, 10,  6,  9, 13, 18, 14, 15, 17,
18, 21, 22, 11, 18, 20,  5, 12, 15, 19, 20, 12, 14, 16, 21, 15, 21, 22, 12, 18, 11, 12, 13,
17,  5, 14, 20, 13, 14, 19, 12, 15, 16, 22, 12, 16, 21)


# correlation structure: ------------------------------------------------------------------------------

mycorrstrucII <- corSymm(value=mycorrII[col(mycorrII)<row(mycorrII)], form=~coordsII, fixed=TRUE)


# model to be fitted: ---------------------------------------------------------------------------------

lme(avg ~ 1, data=regIIsp, random= ~1 | Level3Name, correlation=mycorrstrucII)