I would like to know how to structure a mixed effect model that allows for a) a fixed factor or b) a covariate to influence residual variance.
I have a dataset of repeated observations from individuals (id) with regards to a behaviour (y). Individuals vary randomly in their mean y value. Additionally, individuals have phenotypic strategies (factoral, Ex1 below) or varying degrees of a personality trait (covariate, Ex2). The phenotype/personality trait does not influence the mean y value of an individual, but does influence the variability an individual shows for trait y.
Consider the following 2 examples:
library(tidyverse)
set.seed(123)
#Ex1: fixed effect is a factor
# -- Data simulation
id <- rep(letters[1:9], each=10) #10 individuals with 10 observations
strat <- rep(c("a","b", "c"), each = 30) # 3 individuals use each strategy
id.int <- rep(rnorm(9,0,5), each=10) #individuals vary in intercept
strat.e <- c(rnorm(30,0,1), rnorm(30,0,5), rnorm(30,0,10)) ## strategy b is more variable than strategy a
y <- id.int + strat.e
data.f <- data.frame(id,y, strat)
head(data.f)
ggplot(data.f, aes(y=y, x = id, fill = strat)) + geom_boxplot()
#Ex2: fixed effect is a covariate
# -- Data simulation
id <- rep(letters[1:10], each=10) #10 individuals with 10 observations
strat <- rep(1:10, each = 10) # individuals are linearly ranked on a personality trait
id.int <- rep(rnorm(10,0,5), each=10) #individuals vary in intercept
strat.e <- sapply(strat, function(x) rnorm(1,0,3*x)) ## variation increases with the personality trait
y <- id.int + strat.e
data.cov <- data.frame(id,y, strat)
head(data.cov)
ggplot(data.cov, aes(y=y, x = id, fill = strat)) + geom_boxplot()
If the residual variance were homoscedastic, this can simply be modeled with a linear mixed effect model, where individual is included as a random effect:
lme4::lmer(y ~ (1|id), data = data.f)
However, for the examples above, I would like to test whether there is additional structure in the residual variance as a result of different strategies, but I am unsure how to specify this structure.
I suspect I need a glmm with structured dispersion or an hglm, but am not confident in my ability to properly specify the matrices. Any suggestions would be much appreciated.