Displaying a model matrix in R
Jaye Ross
Posted on October 15, 2024
If you want to see how R will represent a factor in a model, you can use the model.matrix
function.
For example, with unordered factors the model produces dummy variables.
> p = letters[1:4] |> factor()
> model.matrix(~p)
(Intercept) pb pc pd
1 1 0 0 0
2 1 1 0 0
3 1 0 1 0
4 1 0 0 1
For ordered factors, it produces polynomial contrasts.
> p = letters[1:4] |> factor(ordered = TRUE)
> model.matrix(~p)
(Intercept) p.L p.Q p.C
1 1 -0.6708204 0.5 -0.2236068
2 1 -0.2236068 -0.5 0.6708204
3 1 0.2236068 -0.5 -0.6708204
4 1 0.6708204 0.5 0.2236068
💖 💪 🙅 🚩
Jaye Ross
Posted on October 15, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.