lm 使用管道 %>% 运算符的交互项

lm with an interaction term using the pipe %>% operator

我有一个包含三列的标题,yx1x2。我可以通过简单地执行

使用 %>% 进行常规回归 lm(y ~ x1 + x2)
dat %>%
 select(y, x1, x2) %>%
 lm()

但是,如果我想 lm(y ~ x1*x2),我该怎么做呢?我想到的唯一方法是

dat %>%
 mutate(x1x2 = x1 * x2) %>%
 select(y, x1, x2, x1x2) %>%
 lm()

但我不喜欢这个解决方案,想要更简单的东西。

你可以传递你想要的公式lm()

dat %>%
  select(y, x1, x2) %>%
  lm(formula = (y ~x1*x2))