Adjusting for age and gender in ANOVA I am performing an ANOVA to compare the means of three groups. However, I need to adjust for the effects of age and gender in the ANOVA. I'm not quite sure how to go about it in R.
Conventionally, I would use this command in R
model <- aov(y ~ x1) 

x1 is categorical with 3 levels.
 A: You would do regression with your categorical variables coded $(0,0)$ for category $1$, $(1,0)$ for category $2$, and $(0,1)$ for category $3$, plus the age and gender variables, so m_full <- lm(y ~ c2 + c3 + age + gender), with c1 omitted since it is all $0$s. You then compare to the model that lacks your categorical features: m_reduced <- lm(y ~ age + gender) via anova(m_full, m_reduced).
This does an F-test of the two models the same way that an ANOVA F-test compares the model with the categorical features to a model that only has an intercept (which is what ANOVA does).
ANOVA is just a particular type of regression. If you want to add covariates, feel free.
You can condense the categorical variable into one lm input by calling it with as.factor, such as m_full <- lm(y ~ as.factor(categorical_variable) + age + gender).
A: You might find this rundown helpful: https://conjugateprior.org/2013/01/formulae-in-r-anova/
It covers some subtleties / annoyances when it comes to expressing nesting and error terms in formulae and offers some further reading.
