class: center, middle, inverse, title-slide .title[ # lecture 7: Random Effects ] .subtitle[ ## FANR 8370 ] .author[ ###
Spring 2025 ] --- ## Readings > Kéry & Schaub 73-82 --- class: inverse, center, middle # What are random effects? --- ## What are random effects? <br/> -- - Fixed effects are constant across observational units, random effects vary across units <br/> -- - Fixed effects are used when you are interested in the specific factor levels, random effects are used when you are interested in the underlying population <br/> -- - When factors levels are exhaustive, they are fixed. When they are a sample of the possible levels, they are random <br/> -- - Random effects are the realized values of a random variable <br/> -- - Fixed effects are estimated by maximum likelihood, random effects are estimated with shrinkage ??? Definitions from Gelman & Hill (2007) pg. 245 --- ## A simple linear model `$$\Large y_{ij} = \beta_{[j]} + \epsilon_i$$` `$$\Large \epsilon_i \sim Normal(0, \sigma^2)$$` -- - If `\(\beta_{[1]} = \beta_{[2]} = \beta_{[3]} = ...=\beta_{[J]}\)` -- ``` nimbleCode({ # Priors beta0 ~ dnorm(0, 1) sd ~ dgamma(0.25, 0.25) # Likelihood for (i in 1:N){ y[i] ~ dnorm(mu[i], sd = sd) mu[i] <- beta0 } #i }) ``` --- ## A simple linear model `$$\Large y_{ij} = \beta_{[j]} + \epsilon_i$$` `$$\Large \epsilon_i \sim Normal(0, \sigma^2)$$` -- - If `\(\beta_{[1]} \perp \beta_{[2]} \perp \beta_{[3]} \perp ...\perp \beta_{[J]}\)` -- ``` nimbleCode({ # Priors for(j in 1:J){ beta0[j] ~ dnorm(0, 1) } sd ~ dgamma(0.25, 0.25) # Likelihood for (i in 1:N){ y[i] ~ dnorm(mu[i], sd = sd) mu[i] <- beta0[group[i]] } #i }) ``` ??? nb `\(\perp\)` means "independent of" This should look familiar - it's the means parameterization of the ANOVA model In this case, since 'group' is not defined as a random variable, it must be provided as a constant to Nimble --- ## A simple linear model `$$\Large y_{ij} = \beta_{[j]} + \epsilon_i$$` `$$\Large \epsilon_i \sim Normal(0, \sigma^2)$$` -- - If `\(\beta_{[j]} \sim Normal(\mu_{\beta}, \sigma^2_{\beta})\)` <img src="07_random_effects_files/figure-html/unnamed-chunk-1-1.png" width="576" style="display: block; margin: auto;" /> --- ## A simple linear model `$$\Large y_{ij} = \beta_{[j]} + \epsilon_i$$` `$$\Large \epsilon_i \sim Normal(0, \sigma^2)$$` -- - If `\(\beta_{[j]} \sim Normal(\mu_{\beta}, \sigma^2_{\beta})\)` -- ``` nimbleCode({ # Priors for(j in 1:J){ beta0[j] ~ dnorm(mu.beta, sd = sd.beta) } mu.beta ~ dnorm(0, 0.33) sd.beta ~ dgamma(0.25, 0.25) sd.mu ~ dgamma(0.25, 0.25) # Likelihood for (i in 1:N){ y[i] ~ dnorm(mu[i], sd = sd.mu) mu[i] <- beta0[group[i]] } #i }) ``` --- ## Random effects <br/> -- - Only apply to factors <br/> -- - Imply grouped effects <br/> -- - Can include intercept, slope, and variance parameters <br/> -- - Assume exchangeability <br/> -- - Represent a compromise between total pooling `\((\beta_{[1]}=\beta_{[2]}=...=\beta_{[J]})\)` and no pooling `\((\beta_{[1]} \perp \beta_{[2]} \perp ...\perp \beta_{[J]})\)` <br/> -- - Typically require `\(>5-10\)` factor levels --- ## Random effects = hierarchical model <br/> <br/> `$$\Large [\beta_{[j]}, \mu_{\beta}, \sigma^2_{\beta}, \sigma^2|y_{ij}] = [y_{ij}|\beta_{[j]}, \sigma^2][\beta_{[j]}|\mu_{\beta}, \sigma^2_{\beta}][\sigma^2][\mu_{\beta}][\sigma^2_{\beta}]$$` <br/> -- - Can include multiple random effects - Can include fixed and random effects (mixed-effect models, multi-level models) - Can include multiple levels of hierarchy --- ## Why use random effects? 1) Scope of inference - learn about `\(\beta_{[j]}\)` **and** `\(\mu_{\beta}\)`, `\(\sigma^2_{\beta}\)` - prediction to unsampled groups (in space or time) --- ## Why use random effects? 1) Scope of inference 2) Partitioning of variance - Account for variability among groups vs. among observational units --- ## Why use random effects? 1) Scope of inference 2) Partitioning of variance 3) Accounting for uncertainty - modeling `\(\sigma^2_{\beta}\)` recognizes uncertainty from sampling groups --- ## Why use random effects? 1) Scope of inference 2) Partitioning of variance 3) Accounting for uncertainty 4) Avoiding psuedo-replication - Account for non-independence within groups --- ## Why use random effects? 1) Scope of inference 2) Partitioning of variance 3) Accounting for uncertainty 4) Avoiding psuedo-replication 5) Borrowing strength - `\(\beta_{[j]}\)` estimating from group `\(j\)` observations + all other groups - "shrinkage"" towards mean + degree of shrinkage inversely proportional to precision --- ## Why not use random effects? #### Always use random effects (Gelman & Hill 2007) #### but... -- - Assumption of exchangeability <br/> -- - Requires 5-10 levels <br/> -- - Computationally intensive <br/> -- - Harder to interpret --- ## Example - Neonate feral hogs at SREL <img src="pig_study1.png" width="1084" style="display: block; margin: auto;" /> --- ## Example - Neonate feral hogs at SREL -- - Neonate pig survival is based on many factors <br/> -- - Neonate survival seems to vary between sows. Some are better parents than others. <br/> -- - Should sow be treated as a fixed or random effect? <br/> -- - Sow should be fixed. The success of one parent is unrelated to the success of another <br/> -- - Sow should be a random effect. Probably most pigs aren't that different from each other; What if we want to estimate piglet survival for a sow that's not in our dataset? <br/> -- - Sow should be ignored. Differences in survival between neonates of different sows can be absorbed in the uncertainty for the process model <br/> --- ## Example - Neonate feral hogs at SREL - Neonate survival might also vary by pelt color pattern <br/> -- - Should color be treated as a fixed or random effect? <br/> -- - Color should be fixed. No specific reason to believe that survival is similar between pelt colors. Only 6 color types. <br/> -- - Color should be a random effect - Some colors are less common, so we have less data; Potentially easier to estimate (shrinkage). <br/> --- ## Example - Neonate feral hogs at SREL The authors chose to use a random effect for sow. How would we expect these estimates to change if they had modeled these as fixed effects? <img src="Fig2_pigs.png" width="457" style="display: block; margin: auto;" />