I am trying to fit a linear mixed model in the R
package nlme
, using the lme
function. There are a number of commonly-used correlation structures available to be used as values for the correlation
argument. However, I would like to define a custom correlation structure. Based on the documentation, I believe that this is possible, but I don't understand their instructions well enough to implement it. As an example, suppose I want to use the following correlation structure:
How would I set the correlation
argument of lme
to this structure?
I provide a toy data set below, in case setting the correlation
argument requires it.
# independent variable
x <- rnorm(3)
# effect of independent variable on dependent variable
slope <- 2
# error term
error <- rnorm(3, sd = 1/4)
# making successive independent variable observations correlated
alpha <- 0.5
y <- c()
y[1] <- slope * x[1] + error[1]
for (i in 2:length(x)){
y[i] <- (1 - alpha) * (slope * x[i] + error[i]) + alpha * y[i-1]
}