mviljamaa
2016-Sep-21 14:19 UTC
[R] Why removing the (Intercept) from lm is done by adding -1?
So I found out that to remove the (Intercept) term from lm's model one can add -1 to the predictors. I.e. do lm(resp ~ x1 + x2 - 1) Another way is to add 0, e.g. lm(resp ~ 0 + x1 + x2). Adding (or setting the (Intercept) term) zero seems more logical than subtracting one, but why is there the method of subtracting one? Why does subtracting one mean that the (Intercept) term disappears?
Sarah Goslee
2016-Sep-21 14:34 UTC
[R] Why removing the (Intercept) from lm is done by adding -1?
Linear regression is of the form y = mx + b right? And in R, - means omit, as in mydataframe[, -1] right? But when you specify a formula within lm(), the intercept is implicit. That is, you write: y ~ x and m and b are fitted. So if you want to omit the intercept, you use 1 as a placeholder rather than leaving the - dangling somewhere. y ~ x - 1 But as you say, there are other ways, so use the one you like. Note that if you really wanted to subtract 1 from x before fitting the model, you'd need to make that clear to R: y ~ I(x - 1) This is all in the help for formula, where it says "The - operator removes the specified terms". Sarah On Wed, Sep 21, 2016 at 10:19 AM, mviljamaa <mviljamaa at kapsi.fi> wrote:> So I found out that to remove the (Intercept) term from lm's model one can > add -1 to the predictors. I.e. do lm(resp ~ x1 + x2 - 1) > > Another way is to add 0, e.g. lm(resp ~ 0 + x1 + x2). > > Adding (or setting the (Intercept) term) zero seems more logical than > subtracting one, but why is there the method of subtracting one? Why does > subtracting one mean that the (Intercept) term disappears? >-- Sarah Goslee http://www.functionaldiversity.org
S Ellison
2016-Sep-21 15:34 UTC
[R] FW: Why removing the (Intercept) from lm is done by adding -1?
> Subject: Re: [R] Why removing the (Intercept) from lm is done by adding -1? > > And in R, - means omit, as in > mydataframe[, -1] > right?Not really, no. In the specific interpretation of an R model formula, '-' means 'remove the _term_ following '-' ...'. As below:> This is all in the help for formula, where it says "The - operator removes the > specified terms".Almost everywhere else, '-' it means negate as a unary operator and subtract as a binary operator. In '[', '-' still means negate, not remove. It's just that '[' uses negative _numbers_ as a special case to denote omission. As a recent post noted, [-"601",] does not work.> > Adding (or setting the (Intercept) term) zero seems more logical than > > subtracting one, but why is there the method of subtracting one? Why > > does subtracting one mean that the (Intercept) term disappears?See above; '-' _in a formula_ means 'remove the following term' Following that consistently, if there's a weirdness there, it's that ~0+x works to omit the intercept, not that ~x-1 does. But that arises from a slightly different, but still fairly reasonable, perspective on describing the model. S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}