Hi all,
I am a PhD student using brms to run mixed models to understand the effect of parental factors on the fertility of eggs. My model currently looks like this - understanding the effect ion maternal and paternal ages, ages as quadratic effects, inbreeding coefficients and interactions between age and inbreeding coefficient for each parent.
model_dev <- brm(Fertile ~
#linear fixed effects
Pair.Inbreeding +
#quadratic fixed effects
I(Dam.age.years^2) + I(Sire.age.years^2) +
#interactive fixed effects
Sire.Inbreeding*Sire.age.years + Dam.Inbreeding*Dam.age.years +
#random effects
(1|Sire.Studbook.ID) + (1|Dam.Studbook.ID)+
(1|Lay.Year) + (1|Location),
data = data_dev_sc,
family = "bernoulli",
prior = priors_dev,
iter = 30000,
control=list(adapt_delta=0.99),
cores = 8,
sample_prior = TRUE,
seed = 13579)
Based on other research and with the help of other forum members, I have now successfully set my priors are as follows:
priors_dev <- c(set_prior("normal(0,5)", class = "b", coef = "Dam.age.years"),
set_prior("normal(-5,5)", class = "b", coef = "Sire.age.years"),
set_prior("normal(0,10)", class = "b", coef = "Sire.Inbreeding"),
set_prior("normal(0,10)", class = "b", coef = "Dam.Inbreeding"),
set_prior("normal(-40,20)", class = "b", coef = "Pair.Inbreeding"),
set_prior("normal(0,5)", class = "b", coef = "IDam.age.yearsE2"),
set_prior("normal(0.5,5)", class = "b", coef = āISire.age.yearsE2"))
After running the model, I am wanting to do variable selection using projpredās cv_varsel, but have been hitting the following error:
Error in updateGlmerDevfun(devfun, glmod$reTrms, nAGQ = nAGQ) :
nAGQ > 1 is only available for models with a single, scalar random-effects term
I am following the methods of a paper which uses āsearch_termsā argument of cv_varsel to ensure that the fixed effects are explored before the random effects and are included in every model. As a result my search_terms currently look like this:
search_terms<-c(
"1",
"Pair.Inbreeding",
"Dam.age.years",
"Sire.age.years",
"Dam.Inbreeding",
"Sire.Inbreeding",
"Dam.age.years+Dam.Inbreeding+Dam.age.years:Dam.Inbreeding",
"Sire.age.years+Sire.Inbreeding+Sire.age.years:Sire.Inbreeding",
paste0(
"Sire.age.years+I(Sire.age.years^2)+Dam.age.years+I(Dam.age.years^2)+Dam.age.years:Dam.Inbreeding+",
"Dam.Inbreeding+Sire.Inbreeding+Pair.Inbreeding+Sire.age.years:Sire.Inbreeding+",
"(1|Sire.Studbook.ID)"
),
paste0(
"Sire.age.years+I(Sire.age.years^2)+Dam.age.years+I(Dam.age.years^2)+Dam.age.years:Dam.Inbreeding+",
"Dam.Inbreeding+Sire.Inbreeding+Pair.Inbreeding+Sire.age.years:Sire.Inbreeding+",
"(1|Dam.Studbook.ID)"
),
paste0(
"Sire.age.years+I(Sire.age.years^2)+Dam.age.years+I(Dam.age.years^2)+Dam.age.years:Dam.Inbreeding+",
"Dam.Inbreeding+Sire.Inbreeding+Pair.Inbreeding+Sire.age.years:Sire.Inbreeding+",
"(1|Lay.Year)"
),
paste0(
"Sire.age.years+I(Sire.age.years^2)+Dam.age.years+I(Dam.age.years^2)+Dam.age.years:Dam.Inbreeding+",
"Dam.Inbreeding+Sire.Inbreeding+Pair.Inbreeding+Sire.age.years:Sire.Inbreeding+",
"(1|Location)"
)
)
Is there a better way to do this? My aim is simply to prioritise the fixed terms over the random terms when the variable selection is happening.
My cv_varsel code is as follows:
cvvs_dev <- cv_varsel(
ref_dev,
validate_search = TRUE,
### prioritise fixed effects first
search_terms = search_terms,
refit_prj=FALSE,
nclusters_pred = 20,
seed = 13579
)
Please see full code below to simulate equivalent data to recreate the error.
Many thanks!
library(tidyverse)
library(brms)
library(projpred)
set.seed(13579)
predictor_mean <- 0
predictor_sd <- .5
Pair.Inbreeding <- Dam.age.years <- Sire.age.years <- Dam.Inbreeding <- Sire.Inbreeding <- rnorm(539, mean = predictor_mean, sd = predictor_sd)
Fertile <- as.factor(sample(c("Y","N"), size = 539, replace = T))
Dam.Studbook.ID <- as.factor(sample(c(1:52), size=539, replace = T))
Sire.Studbook.ID <- as.factor(sample(c(53:104), size=539, replace = T))
Lay.Year <- as.factor(sample(c(1988:2019), size = 539, replace = T))
Location <- as.factor(sample(c(1:12), size = 539, replace = T))
data_dev_sc <- tibble(
Fertile,
Dam.age.years,
Sire.age.years,
Dam.Inbreeding,
Sire.Inbreeding,
Pair.Inbreeding,
Sire.Studbook.ID,
Dam.Studbook.ID,
Lay.Year,
Location
)
# Set egg_dev priors
priors_dev <- c(
set_prior("normal(0,5)", class = "b", coef = "Dam.age.years"),
set_prior("normal(-5,5)", class = "b", coef = "Sire.age.years"),
set_prior("normal(0,10)", class = "b", coef = "Sire.Inbreeding"),
set_prior("normal(0,10)", class = "b", coef = "Dam.Inbreeding"),
set_prior("normal(-40,20)", class = "b", coef = "Pair.Inbreeding"),
set_prior("normal(0,5)", class = "b", coef = "IDam.age.yearsE2"),
set_prior("normal(0.5,5)", class = "b", coef = "ISire.age.yearsE2")
)
model_dev <- brm(Fertile ~
Pair.Inbreeding +
I(Dam.age.years^2) + I(Sire.age.years^2) +
Sire.Inbreeding*Sire.age.years + Dam.Inbreeding*Dam.age.years +
(1|Sire.Studbook.ID) + (1|Dam.Studbook.ID)+
(1|Lay.Year) + (1|Location),
data = data_dev_sc,
family = "bernoulli",
prior = priors_dev,
iter = 30000,
control=list(adapt_delta=0.99),
cores = 8,
sample_prior = TRUE,
seed = 13579)
ref_dev <- brms:::get_refmodel.brmsfit(model_dev, seed = 13579)
search_terms <- c(
"1",
"Pair.Inbreeding",
"Dam.age.years",
"Sire.age.years",
"Dam.Inbreeding",
"Sire.Inbreeding",
"Dam.age.years+Dam.Inbreeding+Dam.age.years:Dam.Inbreeding",
"Sire.age.years+Sire.Inbreeding+Sire.age.years:Sire.Inbreeding",
paste0(
"Sire.age.years+I(Sire.age.years^2)+Dam.age.years+I(Dam.age.years^2)+Dam.age.years:Dam.Inbreeding+",
"Dam.Inbreeding+Sire.Inbreeding+Pair.Inbreeding+Sire.age.years:Sire.Inbreeding+",
"(1|Sire.Studbook.ID)"
),
paste0(
"Sire.age.years+I(Sire.age.years^2)+Dam.age.years+I(Dam.age.years^2)+Dam.age.years:Dam.Inbreeding+",
"Dam.Inbreeding+Sire.Inbreeding+Pair.Inbreeding+Sire.age.years:Sire.Inbreeding+",
"(1|Dam.Studbook.ID)"
),
paste0(
"Sire.age.years+I(Sire.age.years^2)+Dam.age.years+I(Dam.age.years^2)+Dam.age.years:Dam.Inbreeding+",
"Dam.Inbreeding+Sire.Inbreeding+Pair.Inbreeding+Sire.age.years:Sire.Inbreeding+",
"(1|Lay.Year)"
),
paste0(
"Sire.age.years+I(Sire.age.years^2)+Dam.age.years+I(Dam.age.years^2)+Dam.age.years:Dam.Inbreeding+",
"Dam.Inbreeding+Sire.Inbreeding+Pair.Inbreeding+Sire.age.years:Sire.Inbreeding+",
"(1|Location)"
)
)
cvvs_dev <- cv_varsel(
ref_dev,
### Only for the sake of speed (not recommended in general):
validate_search = TRUE,
### prioritise fixed effects first
search_terms = search_terms,
nclusters_pred = 20,
### nterms_max = 12,
seed = 13579
)