Despite many efforts, I cannot run a linear mixed model because of the autocorrelation term. In fact, I can't manage to code for both a nested design and random slopes within it.
For example, let's imagine some monthly captures of wild rabbits in kilograms in 5 sites during 21 years:
site<- rep(rep(c("Golden Cave","Ringo's place","Damned Dam","Knockampton","Easy Fuzzy"),each=12),21)
year <- rep(2000:2020, each=12*5)
month <- rep(seq(1,12),21*5)
rabbit_captures <- rnorm(12*21*5, 50, 10)
dataset <- as.data.frame(cbind(site,year,month,rabbit_captures))
dataset$rabbit_captures <- as.numeric(dataset$rabbit_captures)
Then is the modeling part that I can't succeed:
library(nlme)
library(MASS)
model_lme <- lme(fixed = log(rabbit_captures) ~ site,
random = ~ site|year/month,
correlation = corAR1(value = 0.9, form = ~ site|year/month),
data = dataset, method = "ML",
control = lmeControl(opt = 'optim'))
I desperately get an error that I suppose is for the "site" variable within the autocorrelation structure term:
Error in as.character.factor(X, ...) : malformed factor
I can run the model without autocorrelation:
model_lme_wo_autocorrelation <- lme(fixed = log(rabbit_captures) ~ site,
random = ~ site|year/month,
data = dataset, method = "ML",
control = lmeControl(opt = 'optim'))
And I am really interested in including autocorrelation to compare both models.