wrahool
wrahool

Reputation: 1141

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

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.

Upvotes: 0

Views: 127

Answers (1)

Fino
Fino

Reputation: 1784

You can pass the formula you want to lm()

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

Upvotes: 1

Related Questions