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

Two ways:


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:

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


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

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


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")
  dummy2.lm <- lm(dist ~ speed + as.factor(dummy2), data = new.cars)

Hope that helps,


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