5

I have a dataframe Z looking like

t  x  y  d
0  1  2  1
1  2  3  1
2  3  4  1
0  1  2  2
1  2  3  2
2  3  4  2

with d being a factor column. I know want to fit a linear model with lm to y over t for both factors in d and add it as a new column to the dataframe.

I tried

Z %>%
  filter(d == 1) %>%
  lm(y ~ t)

but this gives me an error saying "Error in as.data.frame.default(data) : cannot coerce class ""formula"" to a data.frame". But

lm(y ~ t, data = Z)

works fine. Any help would be appreciated.

Pascal
  • 563
  • 1
  • 3
  • 15
  • Related: [Using the %>% pipe, and dot (.) notation](https://stackoverflow.com/questions/42385010/using-the-pipe-and-dot-notation) – Henrik Mar 21 '18 at 09:40

1 Answers1

5

We need to extract the data and . represents the data object

Z %>% 
  filter(d == 1) %>% 
  lm(y ~ t, data = .)
#Call:
#lm(formula = y ~ t, data = .)

#Coefficients:
#(Intercept)            t  
#          2            1  

Within the summarise/mutate/group_by and other tidyverse functions, we can simply pass the column name. Here, either we need to get the columns from within the environment of data or create a list output in summarise

library(magrittr)    
Z %>%
  filter(d ==1 ) %>%
  summarise(lmout = list(lm(y ~ t))) %>%
  pull(lmout) %>%
  extract2(1)
#Call:
#lm(formula = y ~ t)

#Coefficients:
#(Intercept)            t  
#          2            1  
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks! What are the last two lines doing? – Pascal Mar 21 '18 at 09:54
  • @Pascal In the `summarise`, I wrapped it with `list`, so it is a `list` column. with `pull`, the column is extracted as a list of length 1, with `extract2` from `magrittr`, the list element is extracted. `summarise` returns a single element as output but the `lm` output is a specialized structure and that is the reason to wrap it in `list` – akrun Mar 21 '18 at 09:56
  • Thus, I can extract the `fitted.values` by `extract2(5)`? – Pascal Mar 21 '18 at 09:59
  • @Pascal The `extract2` is to extract the model object that is in the list. Now, you may have to extract again the values from the model object – akrun Mar 21 '18 at 10:04