2017-03-07 66 views
0

我試圖在嵌套數據框as described here內運行迴歸。爲了達到我的目的,我使用lfe包中的felm,因爲我有很多級別的固定效果。felm不適用掃帚::增強/咕嚕聲,但與整齊

如果我使用felm而不是lm重新進行上述鏈接中的示例,它在大多數情況下都可以使用,直到我嘗試使用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) 
    ) 

一切正常了這一點,除了我不得不在purrr::map在代碼的最後一行,可能是另一個felm怪癖使用的功能,而不是一個公式。

現在,如果我嘗試使用broom來提取模型輸出,它適用於glancetidy,但不適用於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)) 

試圖在以下錯誤消息以使用augment結果:

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 

回答

1

它看起來像augment是很難找到的數據爲data參數,它通常是用於擬合數據集。

如果使用這些模型中的單個模型而不是同時使用所有模型,問題會更容易看出。

這是不行的,你給出的錯誤:

augment(by_country$model[[1]]) 

但數據明確傳遞給data說法有:

augment(by_country$model[[1]], data = by_country$data[[1]]) 

因此通過DataSet中的解決辦法是作爲第二個參數到augment。這可以通過purrr:map2完成,同時在modeldata列之間循環。

by_country %>% 
    unnest(model %>% purrr::map2(., data, augment))