I'm trying to run regressions within a nested data frame as described here. For my purposes, I'm using felm
from the lfe
package because I have many levels of fixed effects.
If I re-do the example in the link above using felm
instead of lm
, it works for the most part until I try to use broom::augment
.
library(tidyverse)
library(broom)
library(gapminder)
library(lfe)
by_country <- gapminder %>%
group_by(continent, country) %>%
nest()
country_felm <- function(data){
felm(lifeExp ~ year, data = data)
}
by_country <- by_country %>%
mutate(model = purrr::map(data, country_felm)
)
Everything works up to this point except that I had to use a function instead of a formula in purrr::map
in the last line of code, possibly another felm
quirk.
Now if I try to use broom
to extract the model output, it works for glance
and tidy
, but not for augment
.
by_country %>% unnest(model %>% purrr::map(broom::glance))
by_country %>% unnest(model %>% purrr::map(broom::tidy))
by_country %>% unnest(model %>% purrr::map(broom::augment))
Trying to use augment
results in the following error message:
Error in mutate_impl(.data, dots) :
argument must be coercible to non-negative integer
In addition: Warning message:
In seq_len(nrow(x)) : first element used of 'length.out' argument