This content originally appeared on DEV Community and was authored by Jaye Ross
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
This content originally appeared on DEV Community and was authored by Jaye Ross
Jaye Ross | Sciencx (2024-10-15T17:29:37+00:00) Displaying a model matrix in R. Retrieved from https://www.scien.cx/2024/10/15/displaying-a-model-matrix-in-r/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.