broom icon indicating copy to clipboard operation
broom copied to clipboard

`augment`: special case plm's first-difference models

Open tappek opened this issue 3 years ago • 1 comments

broom::augment errors for plm's first-difference (FD) models as these models are somewhat special (they compress data for estimation, thus the residuals are shorter then number of observations). Please see https://stackoverflow.com/q/72266256/4640346 (and its answer with hints) for details. The culprit seems to be broom::augment_columns.

tappek avatar May 20 '22 10:05 tappek

Thanks for the issue! Dropping a reprex here for posterity:

library(AER)
#> Loading required package: car
#> Loading required package: carData
#> Loading required package: lmtest
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: sandwich
#> Loading required package: survival
data(Fatalities)
Fatalities$fatality <- Fatalities$fatal / Fatalities$pop * 10000

library(plm)
library(broom)


plm.fd <- plm(fatality ~ beertax, data = Fatalities, 
              index = c("state", "year"), 
              model = "fd")

tidy(plm.fd)
#> # A tibble: 2 × 5
#>   term        estimate std.error statistic p.value
#>   <chr>          <dbl>     <dbl>     <dbl>   <dbl>
#> 1 (Intercept) -0.00314    0.0119   -0.263    0.792
#> 2 beertax      0.0137     0.285     0.0480   0.962

augment(plm.fd)
#> Warning in get(.Generic)(e1, e2): longer object length is not a multiple of
#> shorter object length
#> Error in `$<-.data.frame`(`*tmp*`, ".resid", value = c(`2` = 0.219840293582125, : replacement has 288 rows, data has 336

Created on 2022-06-24 by the reprex package (v2.0.1)

As you note, residuals are not well-defined for some observations in the training data in this implementation of a first-differences model. This is a situation in which broom expects residuals methods to encode those missing values as NA in order to return a vector with the same length as the input data.

Would it be possible for yall to update the residuals.plm method to satisfy this condition?

simonpcouch avatar Jun 24 '22 17:06 simonpcouch

Given lack of activity here, I'm going to go ahead and close. Thanks again!

simonpcouch avatar Mar 07 '23 17:03 simonpcouch

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

github-actions[bot] avatar Mar 22 '23 00:03 github-actions[bot]