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
Top comments (0)