Reputation: 98
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
Upvotes: 0
Views: 299
Reputation: 36086
It looks like augment
is having trouble finding the data for the data
argument, which is generally the dataset used for fitting.
The problem is easier to see if working with a single one of these models rather than all of them at once.
This doesn't work, with your given error:
augment(by_country$model[[1]])
But explicitly passing the data to the data
argument does:
augment(by_country$model[[1]], data = by_country$data[[1]])
A work-around is therefore to pass the dataset to augment
as the second argument. This can be done via purrr:map2
, looping through both the model
and data
columns at the same time.
by_country %>%
unnest(model %>% purrr::map2(., data, augment))
Upvotes: 1