IvonLiu
IvonLiu

Reputation: 243

What is the meaning of "~ -1 + ". in R?

I am trying to understand a R script and I came upon this line:

train <- cbind(train[,c(1,2)],model.matrix(~ -1 + .,train[,-c(1,2)]))

train is a data.frame. I think it is trying to combine the first two columns of train with all the other columns after they have been through some sort of matrix manipulation. However, I cannot understand exactly what the model formula(?) seems to be doing. From the comment in the script it's purpose is to turn all the other columns in to 0's and 1's, but I'm not sure how. If someone could clarify that would be great. Thanks!

Upvotes: 2

Views: 3568

Answers (1)

jeremycg
jeremycg

Reputation: 24955

From ?formula:

The - operator removes the specified terms... [i]t can also used to remove the intercept term: when fitting a linear model y ~ x - 1 specifies a line through the origin.

Further:

There are two special interpretations of . in a formula. The usual one is in the context of a data argument of model fitting functions and means ‘all columns not otherwise in the formula’

So, you have a formula specifying the response is a function of all variables in train[,-c(1,2)], with an intercept at the origin.

Upvotes: 5

Related Questions