O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Forcing lm to use a specific factor level as the reference

By default, lm uses the lowest factor as the reference level. To use a different one as reference, we can use the relevel function. Our original model uses 3cyl as the reference level. Perform the following steps to force lm to use 4cyl as the reference:

> auto <- within(auto, cylinders <- relevel(cylinders,  ref = "4cyl") ) 
> mod <- lm(mpg ~., data = auto[t.idx, -c(1, 8, 9)]) 

The resulting model will not have a coefficient for 4cyl.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required