# How is conditioning on a variable different than specifying nested random effects

How is conditioning on a variable similar to or different from using a mixed effect model?

I am a biologist. I'm analyzing my data. I have body sites nested within individuals. We're interested in studying the relationship between our predictor variables and the microbiome. My ecology textbook suggests I can use a redundancy analysis with 3 matrices, X, Y, and Z where Z is a matrix of variables I want to "condition on". I've specified X = microbiome, Y=predictors, Z= Subject, Site.

At the same time, I recently implemented my first linear mixed effects model. In that analysis, my model was: microbiome ~ predictors + (1 | Subject / Site).

I'm trying to make sense of these two approaches.

Whether the math is the same, in practice is there a difference between conditioning on a variable and using a nested mixed effect structure? I'm a biologist trying to wrap my head around the methods people in the field use while applying these models to my own data.

Welcome to the site, Sue. For this answer, I'm assuming you used R. Given that Subject and Site are categorical factors, you could include them in an OLS model that would "condition" on them, e.g.,

ols <- lm(microbiome ~ predictors + as.factor(Subject) + as.factor(Site), data=df)

In this case, the conditioning is analogous to running the regression of microbiome on predictors within each Subject and Site, and then the coefficients on the predictors are a kind of average effect of all these within-Subjects and within-Sites regressions. Obviously I am playing it fast and loose with my language.

This can be contrasted with the multilevel or mixed effects model that you ran:

mlm <- lmer(microbiome ~ predictors + (1 | Subject / Site), data=df)

In this model, Subject and Site are treated as nested random effects that adjust for correlations in the outcome within body sites of the same Subject and the same Site. Each of these random effects are given their own distribution ($$N$$ ~ $$(0, \sigma^2)$$) and instead of estimating their effect directly in the model as you did in OLS, you get a variance estimate ($$\sigma^2$$) in the random effects reported by lmer. Depending on the level at which predictors were measured (body site-specific, Subject-specific, or Site-specific), the coefficients in mlm are going to be a blend of within and between Subject and within and between Site estimates of the association with that predictor and the outcome. The degree to which they reflect more of a within or between effect depend on whether there is more variation within or between Subjects and Sites.

Why might you use one model or another? Here's a few reasons:

1. mlm is more parsimonious than ols. Say you had 50 Subjects and 20 Sites. ols will estimate a total of 49+19 regression coefficients for these two factors alone. mlm only estimates two parameters - the variance for each factor.

2. If you believe your Subjects are drawn from a larger population of subjects that you wish to generalize to, then mlm is more consistent with such an interpretation (and likewise for Sites).

3. mlm also allows you to parsimoniously estimate varying associations between predictors and the outcome across Subjects and Sites through the use of random slopes, which get their own variance estimate. To do something similar in ols you would have to interact the predictor of interest with the 49 Subject dummy variables and/or 19 Site variables.

4. Finally, if you use the ols approach, any predictors that were measured at the Subject or Site level, could not be included in your regression model. The dummy variables absorb all variation in Subject or Site, including variation attributable to your predictors. mlm handles these predictors without a problem, but does make the assumption that those predictors are uncorrelated with the random effect at that level. It's important to consider whether this is a valid assumption in your data.