Based on this stackoverflow topic, I would like to extract coefficient.

From 'diamonds' dataset, I used nest() function to split diamonds dataset with respect to two categorical variables: color and cut. Then for each model compute coefficients and r_square and store them as data frames.

I successfully did it by these code:

df_dia <- diamonds %>%
  group_by(color, cut) %>% nest() %>% 

  # generate summary
  mutate(fit = map(data, ~lm(price ~ carat, data=.)), summary= map(fit, glance)) %>%
  unnest(summary) %>%

  # generate coef
  mutate(fit = map(data, ~lm(price ~ carat, data=.) %>%coef %>% 
                                        as.list %>% as_tibble)) %>%
  unnest(fit) %>% unnest(data) %>%
  select(color, cut, `(Intercept)`, carat, r.squared)

However, it is not efficient, since it has to do regression 2 times. Is there anybetter way?

1 Answers

0
Robert On Best Solutions

Why not using sapply:

fa = split(diamonds,f = list(diamonds$color,diamonds$cut) )
df_d=data.frame(t(sapply(fa,function(dft){  ## dft=fa[[1]]
  m1=lm(price ~ carat,dft)
  c(coef(m1),r.squared=summary(m1)$r.squared)
  })))
df_dn=do.call("rbind",strsplit(row.names(df_d),".",fixed = T))
df_d = cbind(df_dn,df_d)
colnames(df_d)[1:2]= c("color", "cut")
head(df_d)

# head(df_d)
#        color  cut  X.Intercept.  carat r.squared
# D.Fair     D Fair    -2255.690 7115.086 0.7705523
# E.Fair     E Fair    -2612.316 7348.326 0.8100614
# F.Fair     F Fair    -2214.511 6677.834 0.7531280
# G.Fair     G Fair    -2199.924 6289.356 0.7370402
# H.Fair     H Fair    -2297.343 6096.767 0.7396489
# I.Fair     I Fair    -3097.742 6496.508 0.8263858