Julia Piaskowski
March 3, 2026
\[Y_i = \beta_0 + \beta_1 X_i + \epsilon_i\]
\(Y_i\) = dependent variable (there is only 1)
\(X_i\) = independent variable(s) (there may be many)
\(B_0\) = model intercept, the overall mean of \(Y\)
\(B_1\) = how \(Y\) changes with \(X\)
\(\epsilon_i\) = model residual, the gap between the predicted value for \(Y_i\) and its observed value
\[ \epsilon_i \sim N(0, \sigma)\]
The residuals are normally distributed with a mean of zero and some standard deviation that we will estimate during the model-fitting process.
All model residuals are independent and identically distributed, “i.i.d.” – they are uncorrelated with each other and drawn from the same distribution (that has exactly one variance)
For every observation, there is a residual: \(\{n_1, n_2,...,n_j\}\), \(\{e_1, e_2,...,e_j\}\)
imagine we have a data set of 3 observations and hence 3 residuals: \(\{e_1, e_2, e_3\}\)
there is a variance-covariance matrix for the residuals:
\[ \epsilon_i \sim N (0, \begin{bmatrix} \sigma & 0 & 0 \\ 0 & \sigma & 0 \\ 0 & 0 & \sigma \end{bmatrix} ) \]
In the “general” linear model days, when a premium was placed on the i.i.d. error paradigm, if we did reject \(H_0\), it would set off a minor crisis in the form of a hunt for a variance stabilizing transformation. In contemporary modeling, we simply proceed with inference on estimable functions using the unequal variance model.
– Walt Stroup, Marina Ptukhiuna and Julie Garai (2024), Generalized Linear Mixed Models, 2nd Ed, Section 7.2.3.1
Solution: model variance for each site with varIdent(), an ‘nlme’ function
library(emmeans)
emmeans(m2, ~ variety*site, at = list(variety = "variety_1", site = c("site_1", "site_8", "site_7"))) variety site emmean SE df lower.CL upper.CL
variety_1 site_1 1713 126 8 1423 2002
variety_1 site_8 1327 126 8 1038 1617
variety_1 site_7 147 126 8 -142 437
Degrees-of-freedom method: containment
Confidence level used: 0.95
emmeans(m2_new, ~ variety*site, at = list(variety = "variety_1", site = c("site_1", "site_8", "site_7"))) variety site emmean SE df lower.CL upper.CL
variety_1 site_1 1713 186.0 8 1283.8 2142
variety_1 site_8 1327 124.0 8 1040.6 1614
variety_1 site_7 147 98.8 8 -80.5 375
Degrees-of-freedom method: containment
Confidence level used: 0.95
numDF denDF F-value p-value
(Intercept) 1 977 185.95512 <.0001
variety 37 977 28.28370 <.0001
site:variety 304 977 7.80575 <.0001
numDF denDF F-value p-value
(Intercept) 1 977 84.79352 <.0001
variety 37 977 10.75473 <.0001
site:variety 304 977 10.19020 <.0001
Solution: model variance as a function of the model fitted values using varPower(), an ‘nlme’ function
variety emmean SE df lower.CL upper.CL
variety_22 28.5 59.8 3 -161.9 219
variety_3 168.1 59.8 3 -22.3 359
variety_26 725.3 59.8 3 534.9 916
Degrees-of-freedom method: containment
Confidence level used: 0.95
variety emmean SE df lower.CL upper.CL
variety_22 28.5 6.43 3 8.0 48.9
variety_3 168.1 27.70 3 79.9 256.3
variety_26 725.3 92.30 3 431.5 1019.1
Degrees-of-freedom method: containment
Confidence level used: 0.95
numDF denDF F-value p-value
(Intercept) 1 111 33.3863 <.0001
variety 37 111 9.6935 <.0001
numDF denDF F-value p-value
(Intercept) 1 111 47.47258 <.0001
variety 37 111 29.91474 <.0001
Note: LSD (least significant difference) calculations rely on a single variance term, so LSD cannot be used on these models (and is ill defined for mixed models)
Tests for unequal variance by groups
bartlett.test()car::leveneTest(..., center = "mean")car::leveneTest(..., center = "median")Test for unequal variance for regression: lmtest::bptest()
Log likelihood ratio tests between models 🤩
- Log likelihood ratio test (\(H_0\): the models are equivalent)
- “Better” model: lower AIC, lower BIC, higher log likelihood
- Test to determine if change in log likelihood is meaningful
Almost normal, just take the (natural) log
Earthquake acceleration
Quake emmean SE df lower.CL upper.CL
20 0.1630 0.0312 159 0.1014 0.225
10 0.0300 0.1250 159 -0.2164 0.276
5 0.0275 0.0376 159 -0.0468 0.102
Degrees-of-freedom method: df.error
Confidence level used: 0.95
Quake response SE df lower.CL upper.CL
20 0.1439 0.0335 159 0.09086 0.2281
10 0.0300 0.0280 159 0.00476 0.1890
5 0.0156 0.0044 159 0.00898 0.0272
Degrees-of-freedom method: df.error
Confidence level used: 0.95
Intervals are back-transformed from the log scale
In standard linear models, there is an ‘iid’ assumption (independent and identically distributed): we assume that model residuals shares a single common variance and are uncorrelated with each other
Violating these assumptions can impact the power of hypothesis testing and the width of confidence intervals
Failure to address these violations invalidates the analysis and inference from it
We can model variance by categorical variable or by a covariate using tools in the R package ‘nlme’
Some variables by their nature violate the iid assumption and required generalized linear models to analyze
Heteroscedasticity Examples: more details on the data and analysis
Mixed-Effects Models in S and S-PLUS thee book for nlme, by José C. Pinheiro and Douglas M. Bates (2000). We used this book extensively for developing this guide. Sadly, it’s out of print. However, there are affordable used copies available; some school libraries also carry this.
Zero Inflated Models and Generalized Linear Mixed Models with R (2012) by Alain Zuur, Anatoly Saveliev, and Elena Ieno
GLMM FAQs by Ben Bolker (more resources on mixed models from Dr. Bolker)