I am currently trying to help a colleague and I simply cannot find a solution. So I am hoping that someone else might be able to help us.
I have a dataset containing weight data assessed with different study designs, for different species in different studies (a study included multiple designs and multiple species). I want to investigate the relation between the weight and the study design, using study and species as nested random effect.
The model looks like this and runs fine:
m <- lmer(weight ~ design +(1|study/species), data=dataset)
I tried to make predictions for the different species but with a generic study: I made a new data.table new.dt that contains the unique design-species-combinations of the original dataset and added a column for the report.
new.dt <- unique(dataset[,.(design, species))
new.dt$study <- "xyz"
Then I used the predict-function and allowed new levels.
new.dt$p <- predict(m, newdata=new.dt, re.form= NULL, allow.new.levels=TRUE)
I don't get an error, but I get the same prediction for every species in a design.
Is there a way to keep the original levels of one part of the nested random effect and make the other part a new level?
Thank you in advance!
UPDATE - working example: This issue is not dependent on the dataset.
library(data.table)
library(lme4)
dt <- data.table(expand.grid(design=c("a", "b"), species=c("x", "y", "z"), report=c("1", "2", "3"), count=seq(1, 10, 1)))
dt$weight <- 0
dt[species=="x"]$weight <- rnorm(60, 70, 10)
dt[species=="y"]$weight <- rnorm(60, 80, 15)
dt[species=="z"]$weight <- rnorm(60, 90, 20)
dt[design=="a"]$weight <- dt[design=="a"]$weight- 0.1*dt[design=="a"]$weight
dt[report=="1"]$weight <- dt[report=="1"]$weight+0.15*dt[report=="1"]$weight
dt[report=="2"]$weight <- dt[report=="2"]$weight-0.15*dt[report=="1"]$weight
m <-lmer(weight~design+(1|report/species), data=dt)
dt.pred <- unique(dt[,c(1:2)])
dt.pred$report<- "xyz"
dt.pred$pred<-predict(m, newdata=dt.pred, re.form= NULL, allow.new.levels=TRUE)