dikip
dikip

Reputation: 43

Can we not pass pipe chain output into linear model lm() dot placeholder?

I wanted to know how to pass the output of pipe operation directly into lm().

For example, I can pass this following yay vector into lm() directly.

set.seed(40)
yay = c(rnorm(15), exp(rnorm(15)), runif(20, min = -3, max = 0))
lm(yay~1)

#> Call:
#> lm(formula = yay ~ 1)

#> Coefficients:
#> (Intercept)  
#>    -0.09522  

But when I tried something like this, it threw an error.

library(tidyverse)
library(palmerpenguins)

data("penguins")

filter_penguins <- penguins %>% filter(species == "Adelie") 

filter_penguins %>% 
  filter(island == "Torgersen") %>% 
  select(bill_length_mm) %>%
  pull() %>% 
  lm(. ~ 1)

#> Error in formula.default(object, env = baseenv()) : invalid formula

I have also tried to save the pull() output into object and later feed it into lm(), it works. But why the dot placeholder doesnot work this way?

Thank you very much.

Upvotes: 1

Views: 666

Answers (2)

MonJeanJean
MonJeanJean

Reputation: 2906

This issu is that lm() inside a pipeline consider the data given as formula argument. Therefore, the data is missplaced. Try:

filter_penguins %>% 
  filter(island == "Torgersen") %>% 
  select(bill_length_mm) %>%
  lm(data = ., pull(.) ~ 1)

Upvotes: 3

heds1
heds1

Reputation: 3438

Edit: I realise that I misread the question, and thought OP wanted to pass in a variable name as part of a formula, rather than pass in the dataset itself. I'll leave this post in for the former reason, anyway.

It won't work because the first argument to lm will be whatever is piped in, which is not a proper formula (as the error suggests).

Using your example, and pretending that the piped value is "var", then

"var" %>% 
    lm(. ~ 1)

would be evaluated as

lm(formula = "var", . ~ 1)

so the . ~ 1 portion isn't part of the formula argument. You could construct the formula with paste0 or similar, though. For example, this would work:

"mpg" %>%
    paste0(" ~ .") %>%
    lm(data = mtcars)

Upvotes: 1

Related Questions