0

I came across a solution for how to map non factorial data into a correlation matrix in which the solution uses an argument ~0+.. For reference, here's the code :

model.matrix(~0+., data=df) %>% 
  cor(use="pairwise.complete.obs") %>% 
  ggcorrplot(show.diag = F, type="lower", lab=TRUE, lab_size=2)

What does the object ~0+. do in this scenario?

I tried looking up the meaning of the operator ~ and tried to make sense of the formula but I cannot understand how it this formula helping us create a mapping of factors.

Ahan
  • 73
  • 5

1 Answers1

1

It is a formula object (see help("formula")for details).

. means all variables (columns), 0 means exclude the intercept.

You pass the formula to model.matrix, which creates a design matrix. It does automatic dummy encoding of factor variables (using treatment contrasts by default). Excluding the intercept ensures there is a column for each factor level in the design matrix.

Roland
  • 127,288
  • 10
  • 191
  • 288