Displaying a model matrix in R

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 …


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

Original source.


This content originally appeared on DEV Community and was authored by Jaye Ross


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Displaying a model matrix in R." Jaye Ross | Sciencx - Tuesday October 15, 2024, https://www.scien.cx/2024/10/15/displaying-a-model-matrix-in-r/
HARVARD
Jaye Ross | Sciencx Tuesday October 15, 2024 » Displaying a model matrix in R., viewed ,<https://www.scien.cx/2024/10/15/displaying-a-model-matrix-in-r/>
VANCOUVER
Jaye Ross | Sciencx - » Displaying a model matrix in R. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/15/displaying-a-model-matrix-in-r/
CHICAGO
" » Displaying a model matrix in R." Jaye Ross | Sciencx - Accessed . https://www.scien.cx/2024/10/15/displaying-a-model-matrix-in-r/
IEEE
" » Displaying a model matrix in R." Jaye Ross | Sciencx [Online]. Available: https://www.scien.cx/2024/10/15/displaying-a-model-matrix-in-r/. [Accessed: ]
rf:citation
» Displaying a model matrix in R | Jaye Ross | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.