[R] drop.terms and [.terms ignores intercept
Niels Richard Hansen
Niels.R.Hansen+lists at math.ku.dk
Wed Sep 29 11:35:59 CEST 2010
On 29/09/10 11.13, peter dalgaard wrote:
>
> On Sep 29, 2010, at 10:29 , Niels Richard Hansen wrote:
>
>> The functions drop.terms and [.terms ignores if the intercept has been
>> explicitly removed. Is that a deliberate feature?
>
> Perhaps rather an unimplemented one. The root cause is that both functions
> use reformulate() on the "term.labels" attribute, and there is no way to
> specify that you want to reformulate into a no-intercept formula. On the
> other hand, the modeling code will happily proceed with a no-intercept model
> even if there is no "-1" in formula part of a terms object, e.g.
>
>> x<- terms(y~a+b) attr(x,"intercept")<- 0 lm(x)
>
> Call: lm(formula = x)
>
> Coefficients: a b 0.2263 0.4178
>
>> formula(x)
> y ~ a + b
>
> so I suppose that there is no really good excuse not to carry the "intercept"
> attribute over. As usual, with code as old as this, there is always the risk
> that something actually relies on current behavior.
Thanks Peter. I expected something like that.
As I see it, the intercept (or more often, removal of the intercept)
is treated differently from other terms, and information on the presence
of an intercept is stored in an attribute of the terms object rather
than the formula. This attribute is not copied when using drop.terms
or [.terms. I believe it should be, but as you say Peter, some may rely
on the way the functions behave now.
So basically, you should use code like
x <- terms(y~a+b-1)
z <- x[2]
attr(z, "intercept") <- attr(x, "intercept")
to make sure that the intercept information is preserved.
- Niels
>
>
--
Niels Richard Hansen Web: www.math.ku.dk/~richard
Associate Professor Email: Niels.R.Hansen at math.ku.dk
Department of Mathematical Sciences nielsrichardhansen at gmail.com
University of Copenhagen Skype: nielsrichardhansen.dk
Universitetsparken 5 Phone: +45 353 20783 (office)
2100 Copenhagen Ø +45 2859 0765 (mobile)
Denmark
More information about the R-help
mailing list