rtime-seriesstandard-errormodelsummary

How can I use Newey-West Standard Errors in modelplot(), in R?


apologies if I write anything wrong - this is my first post. I am doing some time series regressions on the different determinants of house prices for different countries, and would like to use modelplot() to showcase the different coefficients for one variable across all the countries. This is the easy part, and I have it done. However, when I try to run this code to try and include Newey-West std. errors:

modelplot(mods_TS_graph,
      coef_map= varnames,
      vcov = c(vcovAUS, vcovBEL, vcovEST, vcovFIN, vcovFRA, vcovGER, vcovGRE, vcovIRE, vcovITA, vcovLAT, vcovLIT, vcovLUX, vcovNET, vcovPOR, vcovSLK, vcovSLE, vcovSPA),
      coef_omit="[^ECB]",
      draw=T, size=1)

Where the vcovAUS arguments etc are just sandwich::NeweyWest(OLS_AUS) etc I get the following error:

Error in get_vcov.default(model, vcov = vcov, conf_level = conf_level, : Unable to extract a variance-covariance matrix from model of class lm. The variance-covariance matrix is required to adjust the standard errors. The vcov argument accepts a variance-covariance matrix, a vector of standard errors, or a function that returns one of these, such as stats::vcov.

I used pretty much the same method for an earlier modelsummary(), which computed Newey-West standard errors no problem. Is there something I am doing wrong? I have also tried to just use "NeweyWest" in the vcov() argument for modelplot(), just like I did in modelsummary(). Very confused. Thanks for reading, hope somebody can help.


Solution

  • The modelplot documentation notes that the vcov argument can be either a list of functions, or a list of matrices, or a vector of strings. This means that both of these commands should work:

    library(modelsummary)
    library(sandwich)
    
    mod <- lm(mpg ~ hp, mtcars)
    
    modelplot(mod, vcov = c("HC3", "NeweyWest"))
    

    Alternatively,

    modelplot(mod, vcov = list(vcovHC, NeweyWest))
    modelplot(mod, vcov = list(vcovHC(mod), NeweyWest(mod)))
    

    The error message you get suggests that the specific model you are trying to summarize is unsupported by the sandwich package. You can check if it is supported by calling:

    sandwich::NeweyWest(mod)
    #>             (Intercept)           hp
    #> (Intercept)  5.93124548 -0.032929737
    #> hp          -0.03292974  0.000221307
    

    In my example, mod was a lm model, but you did not supply a MINIMAL REPRODUCIBLE EXAMPLE, so your problem is impossible to diagnose conclusively.