[R] truncated regressor
Emmanuel Paradis
paradis at isem.univ-montp2.fr
Mon May 15 12:57:58 CEST 2000
At 12:29 15/05/00 +0200, Peter Dalgaard wrote:
>> which is clear to me: doing lm(y ~ a + I(b+c)) is similar to
>>
>> d <- b + c
>> lm(y ~ a + d)
>>
>> But what if the expression in I() is a logical? After playing a moment, I
>> found out that a formula can include a logical variable, and treats it as a
>> factor:
>>
>> > x <- runif(50, 0, 50)
>> > y <- 2*x + rnorm(50, 0, 5)
>> > lm(y ~ x < 25)
>>
>> Call:
>> lm(formula = y ~ x < 25)
>>
>> Coefficients:
>> (Intercept) x < 25
>> 78.24 -53.92
>>
>
>I should hope that it is being converted to *numeric* (TRUE --> 1),
>not factor, otherwise there'd be all sorts of trouble with contrasts.
>This is a general feature: TRUE + 2 == 3 for instance, and (x>0)-(x<0)
>is the sign function.
>
>[Easiest way of checking is to compare
>
>lm(y ~ I(x < 25) - 1)
>
>with
>
>lm(y ~ factor(x < 25) - 1)
>
>]
You are right. I was misled by the fact that
lm(y ~ as.factor(x < 25))
and
lm(y ~ x < 25)
give the same result.
Emmanuel Paradis
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list