Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "/" mean in R when writing a regression formula in lm()

Tags:

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)))
like image 861
Roy Avatar asked Jun 11 '18 09:06

Roy


People also ask

What does lm () do in R?

The lm() function is used to fit linear models to data frames in the R Language. It can be used to carry out regression, single stratum analysis of variance, and analysis of covariance to predict the value corresponding to data that is not in the data frame.

What does R mean in linear regression?

In the context of simple linear regression: R: The correlation between the predictor variable, x, and the response variable, y. R2: The proportion of the variance in the response variable that can be explained by the predictor variable in the regression model.

What does intercept mean in lm R?

The intercept (sometimes called the “constant”) in a regression model represents the mean value of the response variable when all of the predictor variables in the model are equal to zero. This tutorial explains how to interpret the intercept value in both simple linear regression and multiple linear regression models.

What does lm mean in regression?

Description. lm is used to fit linear models. It can be used to carry out regression, single stratum analysis of variance and analysis of covariance (although aov may provide a more convenient interface for these).


1 Answers

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.

like image 137
Lennyy Avatar answered Oct 06 '22 12:10

Lennyy