[R] truncated regressor

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon May 15 12:29:12 CEST 2000


Emmanuel Paradis <paradis at isem.univ-montp2.fr> writes:


> 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)   

]

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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