22

The formula is just like this. I don't quite understand the usage of the notion "/". It seems that "/" usually be used in dummy variables. But I am not sure about its usage.

lm(y~x/z,data = data.frame(x = rnorm(6), y = rnorm(6), z = rep(0:1,each=3)))
Cœur
  • 37,241
  • 25
  • 195
  • 267
Roy
  • 539
  • 1
  • 4
  • 12
  • 3
    I think you are suggesting here at first the mathemical operation occurs of x being divided by z, and that the result of that calculation is used as a predictor variable in the regression model. That is not correct though. – Lennyy Jun 11 '18 at 09:32

1 Answers1

20

lm(y ~ x/z, data) is just a shortcut for lm(y ~ x + x:z, data)

These two give the same results:

lm(mpg ~ disp/hp,data = mtcars)

Call:
lm(formula = mpg ~ disp/hp, data = df)

Coefficients:
(Intercept)         disp      disp:hp  
  2.932e+01   -3.751e-02   -1.433e-05  


lm(mpg ~ disp + disp:hp, data = mtcars)

Call:
lm(formula = mpg ~ disp + disp:hp, data = mtcars)

Coefficients:
(Intercept)         disp      disp:hp  
  2.932e+01   -3.751e-02   -1.433e-05  

So, what your doing is modelling mpg based on disp alone and on an interaction between disp and hp.

DJV
  • 4,743
  • 3
  • 19
  • 34
Lennyy
  • 5,932
  • 2
  • 10
  • 23
  • Link to the official documentation [Defining statistical models; formulae](https://cran.r-project.org/doc/manuals/R-intro.html#Formulae-for-statistical-models) – phiver Jun 24 '18 at 10:07