Reputation: 298
What is the difference between Y ~ X and Y ~ X+1 in R?
Upvotes: 1
Views: 170
Reputation: 49660
What is the context of your question?
As has been mentioned, in lm
and other functions that use model.matrix
internally they are the same. But there are other cases where they differ, consider the following code:
plot.new()
text( .5, .1, y ~ x )
text( .5, .3, y ~ x + 1 )
Here they are different (and running the code shows the difference).
For any other functions or contexts it will depend on the implementation.
These 2 lines give the same results:
plot( Petal.Length ~ Species, data=iris )
plot( Petal.Length ~ Species + 1, data=iris )
But these don't:
library(lattice)
bwplot( Petal.Length ~ Species, data=iris )
bwplot( Petal.Length ~ Species + 1, data=iris )
I remember seeing one time (though it may have been in S-Plus rather than R and may not be possible in R) a formula that included a 0+
or -1
at the beginning and a +1
later in the formula. It constructed the main effects without an intercept (fit mean for each level of 1st factor) but the +1
at the right place changed the way the interactions were coded.
In theory there could be modeling functions (I can't think of any, but they could exist or be written in the future) that take a formula but do not include an intercept by default and so the +1
would be needed to specify an intercept.
So, what context are you asking your question in?
Upvotes: 5
Reputation: 500743
In the context of lm()
, they are exactly the same. Both models include the intercept.
To remove the intercept, you could write Y ~ X - 1
or Y ~ X + 0
.
Upvotes: 7