leeper / margins

An R Port of Stata's 'margins' Command
https://cloud.r-project.org/package=margins
Other
263 stars 40 forks source link

Custom vcov in cplot(..., what = "prediction") not working #139

Open 18kimn opened 4 years ago

18kimn commented 4 years ago

I'm a huge fan of the ability to handle custom variance-covariance matrix in margins() and cplot(), but it doesn't seem to work with the "prediction" setting for cplot():

library("margins")
library(plm)

iris_mod <- lm(Sepal.Length ~ Sepal.Width + Petal.Length, data = iris)
iris_vcov <- plm::vcovHC(iris_mod)
identical(iris_vcov, stats::vcov(iris_mod))
## FALSE
pred1 <- cplot(iris_mod, what = "prediction", draw = F)
pred2 <- cplot(iris_mod, what = "prediction", draw = F,  vcov = iris_vcov)

identical(pred1, pred2)
## TRUE

sessionInfo()
# R version 3.6.3 (2020-02-29)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 17763)
# 
# Matrix products: default
# 
# locale:
#   [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
# [5] LC_TIME=English_United States.1252    
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] margins_0.3.23
# 
# loaded via a namespace (and not attached):
#   [1] bdsmatrix_1.3-4   lattice_0.20-38   zoo_1.8-7         digest_0.6.25     lmtest_0.9-37     MASS_7.3-51.5     grid_3.6.3        nlme_3.1-144      magrittr_1.5     
# [10] bibtex_0.4.2.2    Rdpack_0.11-1     stringi_1.4.5     miscTools_0.6-26  plm_2.2-3         data.table_1.12.8 sandwich_2.5-1    Formula_1.2-3     prediction_0.3.14
# [19] tools_3.6.3       stringr_1.4.0     compiler_3.6.3    gbRd_0.4-11       maxLik_1.3-8    

I took a brief look at the source code and believe a call to prediction() in cplot() is just missing a "vcov = vcov" argument, but I'm not 100% sure on this.

18kimn commented 4 years ago

Circling back to this, I believe it may have been left out intentionally as it's not implemented in prediction() either. Carry-over from issue 31 from the prediction package. Feel free to close this.