简体   繁体   中英

lm() in R and linear regression equation

I have two variables for which I used the following lm() formula:

lm(y ~ x1*x2)

I used this instead of

lm(y ~ x1 + x2)

because I wanted to see the main effect of each IV (x1, x2), and their interaction.

Is the formula I used the same as y = b0 + b1 x1 + b2 x2?

Or, y = b0 + (b1 x1) (b2*x2)?

Using the builtin anscombe data set these two have the same model matrix so they are fitting the same model. Examine the model matrix to understand exactly what model is being used.

fm1 <- lm(y1 ~ x1 + x2 + x1:x2, anscombe)
fm2 <- lm(y1 ~ x1 * x2, anscombe)

m1 <- model.matrix(fm1)
m2 <- model.matrix(fm2)
identical(m1, m2)
## [1] TRUE

# since m1 and m2 are identical we can just display one of them
m1
##    (Intercept) x1 x2 x1:x2
## 1            1 10 10   100
## 2            1  8  8    64
## 3            1 13 13   169
## 4            1  9  9    81
## 5            1 11 11   121
## 6            1 14 14   196
## 7            1  6  6    36
## 8            1  4  4    16
## 9            1 12 12   144
## 10           1  7  7    49
## 11           1  5  5    25
## attr(,"assign")
## [1] 0 1 2 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM