Cette page appartient aux archives web de l'EPFL et n'est plus tenue à jour.
This page belongs to EPFL's web archive and is no longer updated.

Practical 1 - R misunderstanding


We do not understand why

y <- 100/cars[,7]

x1 <- cars[,25]

x2 <- cars[,13] / x1


lm(y ~ x1 + x2) 


Produces a different model fit than 

lm(100/CityMPG ~ Weight + (Horsepower/Weight) , data=cars)


Can you explain that behaviour ? 

Thanks in advance,

Posted by Jeremy Gotteland on Wednesday 13 November 2013 at 14:08
This is because lm does not understand Horsepower/Weight in the formula as division but instead as nesting of variables. Similarly Horsepower*Weight would not be interpreted as multiplication (see e.g. http://ww2.coastal.edu/kingw/statistics/R-tutorials/formulae.html). You should always first transform the variables and then use the transformed variables in lm as you've done in the first code snippet you provided.
Posted by Mikael Kuusela on Wednesday 13 November 2013 at 16:27
So does cars[,13]/cars[,25] works ?
Posted by Clément Genetet on Wednesday 13 November 2013 at 16:39
If you do it the way you wrote in your message, then yes, it works.
Posted by Mikael Kuusela on Wednesday 13 November 2013 at 16:45