[R] help

Mark Myatt mark at myatt.demon.co.uk
Fri Jul 5 11:10:32 CEST 2002


elizabeth pino <epino21 at yahoo.com> writes:
>Hello: 
>    Can you tell me how to do a regression
>   that include a binary variable in the function lm
>or some other?. 
> thank you for any help.
>
>Elizabeth 


Two ways:

(1)

Make sure your binary variable is coded 0 / 1 and specify it in the model formula.

As example data, add a binary (0/1) dummy variable to the example 'cars' data:


  data(cars)
  new.cars <- transform(x = cars, 
                        dummy = round(runif(n = nrow(cars), min = 0, max = 1)))

  new.cars


Then us lm() specifying the dummy variable in the model formula:

  dummy.lm <- lm(dist ~ speed + dummy, data = new.cars)
  summary(dummy.lm)


(2)

Have a look at factors in the help system. R handles these in model formula and
there is no need for the dummy variable to be coded 0 / 1. In this example the
dummy variable is non-numeric:

  new.cars$dummy2 <- ifelse(new.cars$dummy == 0, "ZERO", "NON-ZERO")
  new.cars
  dummy2.lm <- lm(dist ~ speed + as.factor(dummy2), data = new.cars)
  summary(dummy2.lm)


Hope that helps,

Mark

--
Mark Myatt


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