0

I have a tibble with three columns, y, x1, and x2. I can do a regular regression lm(y ~ x1 + x2) using %>% by simply doing

dat %>%
 select(y, x1, x2) %>%
 lm()

However if I want to do lm(y ~ x1*x2), how do I go about doing it? The only way that comes to my mind is

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

but I don't like this solution and would like something simpler.

wrahool
  • 1,101
  • 4
  • 18
  • 42
  • 2
    `dat %>% lm(y ~ x1 + x2 + x1*x2, data = .)` (you don't need the `select` if you use a formula). – r2evans Dec 02 '19 at 16:44

1 Answers1

1

You can pass the formula you want to lm()

dat %>%
  select(y, x1, x2) %>%
  lm(formula = (y ~x1*x2))
Fino
  • 1,774
  • 11
  • 21