Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does "~0" mean in the R model matrix

Tags:

r

model.matrix

I am trying to understand model matrix in R (model.matrix), to convert categorical variables to dummy variables and came across the following code

# Option 2: use model.matrix() to convert all categorical variables in the data frame into a set of dummy variables. We must then turn the resulting data matrix back into 
# a data frame for further work.
xtotal <- model.matrix(~ 0 + REMODEL, data = df)
xtotal <- as.data.frame(xtotal)

Can someone please help me understand what "~0" here mean ? And what the code is trying to do ?

like image 861
archana reddy Avatar asked Oct 22 '25 01:10

archana reddy


1 Answers

+ 0 means that the model will not have an intercept, i.e., a column of 1s. In the presence of a factor variable, when an intercept is present, one of the levels will be removed in order to ensure that the model matrix is full rank, which is required in ordinary least squares regression. When the intercept is absent, all levels of the factor can remain.

So, this code is a way to convert a factor to a matrix where dummy variables for all the levels are present. Omitting + 0 would replace one of the dummies with a column of 1s, which may not be useful for your purpose.

like image 97
Noah Avatar answered Oct 24 '25 15:10

Noah