# Idea
Second, the default contrast codes in R are such that model with categorical covariates (i.e., factors) produce parameter estimates that do not accurately represent lower-order effects (e.g., main effects) if higher-order effects (i.e., interactions) are present. This latter fact is the reason that some people recommend to transform factors into numerical covariates by hand. However, this is not necessary; R contains coding schemes that are orthogonal and do not have this problem. The easiest way to change the coding globally is via the afex function `set_sum_contrasts()`. See [[Singmann 2020 intro to linear mixed modeling in experimental psychology]] and [How are unbalanced designs dealt with? · Issue #63 · singmann/afex](https://github.com/singmann/afex/issues/63).
- `Note again: Using Type III test for unbalanced data without appropriate coding results in nonsensical results.`
```r
library(afex)
set_sum_contrasts() # required to ensure results are similar to car::Anova(model, type = 3) and that contrasts are correct
# between-subjects anova
aov_4(y ~ condition * model * strategy + (1 | responseid), data = d1) # note that id column is still needed
aov_ez("id", "value", obk.long, between = c("treatment", "gender"),
within = c("phase", "hour"), covariate = "age",
observed = c("gender", "age"), factorize = FALSE)
# ANCOVA: adding a covariate (necessary to set factorize = FALSE)
aov_car(value ~ treatment * gender + age + Error(id/(phase*hour)),
data = obk.long, observed = c("gender", "age"), factorize = FALSE)
aov_4(value ~ treatment * gender + age + (phase*hour|id),
data = obk.long, observed = c("gender", "age"), factorize = FALSE)
aov_ez("id", "value", obk.long, between = c("treatment", "gender"),
within = c("phase", "hour"), covariate = "age",
observed = c("gender", "age"), factorize = FALSE)
```
# References
- [README](https://cran.r-project.org/web/packages/afex/readme/README.html)
- https://github.com/singmann/afex