atiq marwat
atiq marwat

Reputation: 11

Alternative of summarise() function in dplyr

food_consumption %>% 
   group_by(food_category) %>% 
   summarise(mod= lm(co2_emmission ~ consumption))

After runing this code I get the folowing error

Error: Column mod must be length 1 (a summary value), not 12

How can I make it right, and get the regression results for each catagory?

Upvotes: 1

Views: 1391

Answers (1)

akrun
akrun

Reputation: 886938

We can wrap the output of lm model in a list as it have many components and summarise wants to return with a length of 1 for each group

library(dplyr)
food_consumption %>% 
     group_by(food_category) %>%
     summarise(mod= list(lm(co2_emmission ~ consumption)))

In the devel version of dplyr, can use condense, which would automatically return a list

food_consumption %>% 
     group_by(food_category) %>%
     condense(mod= lm(co2_emmission ~ consumption))

Using a reproducible example

mtcars %>% 
     group_by(cyl) %>%
     summarise(mod = list(lm(mpg ~ gear)))
# A tibble: 3 x 2
#    cyl mod   
#  <dbl> <list>
#1     4 <lm>  
#2     6 <lm>  
#3     8 <lm>  

Or with condense

mtcars %>% 
   group_by(cyl) %>%
   condense(mod = lm(mpg ~ gear))
# A tibble: 3 x 2
# Rowwise:  cyl
#    cyl mod   
#  <dbl> <list>
#1     4 <lm>  
#2     6 <lm>  
#3     8 <lm>  

In order to get the coefficients

mtcars %>% 
   group_by(cyl) %>%
    condense(mod = lm(mpg ~ gear), Coef = coef(mod))
# A tibble: 3 x 3
# Rowwise:  cyl
#    cyl mod    Coef     
#  <dbl> <list> <list>   
#1     4 <lm>   <dbl [2]>
#2     6 <lm>   <dbl [2]>
#3     8 <lm>   <dbl [2]>

Or with mutate and map

mtcars %>% 
    group_by(cyl) %>%
    summarise(mod = list(lm(mpg ~ gear))) %>% 
    mutate(Coef = map(mod, coef))
# A tibble: 3 x 3
#    cyl mod    Coef     
#  <dbl> <list> <list>   
#1     4 <lm>   <dbl [2]>
#2     6 <lm>   <dbl [2]>
#3     8 <lm>   <dbl [2]>

Or another option is nest and then map over the list

library(purrr)
mtcars %>% 
    group_by(cyl) %>% 
    nest %>% 
    transmute(mod = map(data, ~ lm(mpg ~ gear, data = .x)))
# A tibble: 3 x 2
# Groups:   cyl [3]
#    cyl mod   
#  <dbl> <list>
#1     6 <lm>  
#2     4 <lm>  
#3     8 <lm>  

Upvotes: 5

Related Questions