leeper/margins

at = list() does not find variable in the model fit

Ellesaere opened this issue · 0 comments

  • a possible bug

https://stackoverflow.com/questions/67138433/how-to-use-margins-package-to-evaluate-marginal-affects-at-different-values-of-t

## load package
library("margins")
library("MASS")

## The following example does not work. But somehow the reproducible example I tried to make does work
## See https://stackoverflow.com/questions/67138433/how-to-use-margins-package-to-evaluate-marginal-affects-at-different-values-of-t
fit.polr2 <- polr(ordinal_dep_var ~ Dummy + Continuous + Dummy2 + as.factor(industry) + Urbanisation_Dummy + Size_Dummy, data = df2, method = "probit", Hess=TRUE)
summary(margins(fit.polr2))

polr_1st_margins <- summary(margins(fit.polr2, at = list(ordinal_dep_var= 0:3)))

Error in dat[, not_numeric, drop = FALSE] : 
  incorrect number of dimensions


df2 <- structure(list(ordinal_dep_var = structure(c(4L, 3L, 4L, 1L, 
3L, 1L, 1L, 3L, 1L, 4L, 4L, 4L, 1L, 3L, 4L, 2L, 4L, 2L, 2L, 1L, 
2L, 1L, 1L, 1L, 3L, 4L, 3L, 2L, 2L, 1L, 1L, 2L, 4L, 2L, 1L, 4L, 
3L, 1L, 2L, 3L, 4L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 2L, 4L, 2L, 1L, 
4L, 1L, 1L, 3L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 3L, 
2L, 3L, 3L, 1L, 4L, 4L, 4L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 2L, 4L, 3L, 3L, 1L, 1L, 1L, 3L, 2L, 3L, 2L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 4L, 3L, 2L, 2L, 3L, 1L, 1L, 1L, 
1L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", 
"1", "2", "3"), class = c("ordered", "factor")), Dummy = c(0, 
0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 
0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 
0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 
0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0), Continuous = c(15.6862745098039, 
41.6666666666667, 26.9230769230769, 14.8514851485149, 32.1428571428571, 
20, 43.75, 0, 0, 0, 80, 100, 21.4285714285714, 23.8095238095238, 
28.125, 0, 30.3030303030303, 25, 100, 100, 100, 13.3333333333333, 
66.6666666666667, 33.3333333333333, 55.5555555555556, 72.2222222222222, 
57.3033707865169, 17.7777777777778, 47.6190476190476, 17.7777777777778, 
41.6666666666667, 40, 20, 8.33333333333333, 16.6666666666667, 
40, 100, 0, 50, 0, 0, 7.69230769230769, 0, 0, 0, 0, 0, 0, 0, 
33.3333333333333, 20, 1.84089414858646, 1.84089414858646, 1.84089414858646, 
30, 20, 0, 33.3333333333333, 33.3333333333333, 0, 0, 0, 100, 
50, 22.2222222222222, 0, 0, 50, 50, 46.1538461538462, 44.4444444444444, 
0, 5.55555555555556, 0, 0, 47.3684210526316, 43.75, 18.1818181818182, 
0, 42.8571428571429, 14.2857142857143, 0, 50, 0, 0, 50, 20, 50, 
100, 0, 42.8571428571429, 20, 25, 33.3333333333333, 0, 0, 0, 
66.6666666666667, 0, 25.9259259259259, 0, 33.3333333333333, 0, 
100, 25, 0, 0, 10, 100, 50, 33.3333333333333, 75, 0, 0, 40, 0, 
33.3333333333333, 28.5714285714286, 0, 0, 28.5714285714286, 0, 
28.5714285714286, 0, 0, 9.09090909090909, 30, 66.6666666666667, 
50, 0, 20, 50, 0, 33.3333333333333, 0, 66.6666666666667, 18.1818181818182, 
28.5714285714286, 36.9230769230769, 14.2857142857143, 36.3636363636364, 
0, 0, 7.69230769230769), Dummy2 = structure(c(0, 0, 0, 0, 1, 
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 
0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 
1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 
0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 
0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 
1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0), label = "Gift/informal payment requested: tax inspectorate?", format.stata = "%14.0f", class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(Yes = 1, No = 2)), industry = structure(c(3, 
7, 2, 7, 19, 17, 9, 7, 9, 11, 12, 10, 5, 5, 3, 12, 3, 5, 4, 1, 
1, 11, 5, 7, 9, 9, 7, 9, 5, 9, 4, 7, 9, 11, 11, 22, 12, 21, 19, 
11, 10, 7, 19, 23, 20, 20, 21, 24, 19, 1, 21, 21, 21, 21, 21, 
21, 6, 6, 21, 3, 17, 19, 20, 12, 21, 20, 12, 10, 10, 21, 21, 
19, 3, 21, 21, 10, 10, 21, 5, 20, 21, 19, 22, 15, 6, 21, 21, 
10, 19, 21, 20, 3, 6, 10, 24, 24, 21, 23, 21, 21, 11, 21, 3, 
3, 21, 24, 21, 1, 10, 22, 19, 17, 3, 20, 23, 1, 22, 21, 21, 23, 
21, 23, 21, 22, 22, 21, 10, 13, 10, 15, 10, 24, 24, 7, 10, 10, 
22, 21, 21, 23, 21, 22, 7, 21), label = "Industry", format.stata = "%34.0g", class = c("haven_labelled", 
"vctrs_vctr", "double"), labels = c(Textiles = 1, Leather = 2, 
Garments = 3, Agroindustry = 4, Food = 5, Beverages = 6, `Metals and machinery` = 7, 
Electronics = 8, `Chemicals and pharmaceutics` = 9, Construction = 10, 
`Wood and furniture` = 11, `Non-metallic and plastic materials` = 12, 
Paper = 13, `Sport goods` = 14, `IT services` = 15, `Other manufacturing` = 16, 
Telecommunications = 17, `Accounting and finance` = 18, `Advertising and marketing` = 19, 
`Other services` = 20, `Retail and wholesale trade` = 21, `Hotels and restaurants` = 22, 
Transport = 23, `Real estate and rental services` = 24, `Mining and quarrying` = 25, 
`Auto and auto components` = 26, `Other transport equipment` = 27, 
`Other unclassified` = 99)), Urbanisation_Dummy = structure(c(2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 
1L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 2L, 3L, 
3L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 2L, 2L, 1L, 1L, 1L, 2L, 3L, 2L, 
3L, 3L, 3L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 1L, 2L, 3L, 2L, 2L, 3L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 
3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 2L, 
2L, 1L, 1L, 3L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 1L, 3L, 1L, 1L, 3L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 1L), .Label = c("City > 250", 
"50k-250k", "< 50k"), class = "factor"), Size_Dummy = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 
3L, 1L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 
3L, 3L, 3L, 3L, 3L, 2L, 3L, 1L, 2L, 3L, 3L, 2L, 1L, 1L, 1L, 3L, 
3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 3L, 3L, 1L, 3L, 3L, 1L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 
1L, 3L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 3L, 1L, 3L, 
1L, 1L, 1L, 3L, 3L, 2L, 3L, 1L, 2L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 1L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 3L, 3L, 
2L, 3L, 3L, 1L, 3L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("Employees: < 10", 
"Employees: 10-19", "Employees: 20+"), class = "factor")), row.names = c(NA, 
-144L), class = c("data.table", "data.frame"))


## session info for your system
sessionInfo()

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 
 
locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] splines   stats4    stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] margins_0.3.26      forcats_0.5.1       purrr_0.3.4         readr_1.4.0         tidyr_1.1.3         tibble_3.1.0        tidyverse_1.3.0     dplyr_1.0.5        
 [9] data.table_1.14.0   lavaan_0.6-8        plm_2.4-1           modelr_0.1.8        magrittr_2.0.1      bbmle_1.0.23.1      openxlsx_4.2.3      AER_1.2-9          
[17] sandwich_3.0-0      lmtest_0.9-38       car_3.0-10          carData_3.0-4       ff_4.0.4            bit_4.0.4           pryr_0.1.4          jomo_2.7-2         
[25] mice_3.13.0         GGally_2.1.1        visdat_0.5.3        dummies_1.5.6       checkmate_2.0.0     randomForest_4.6-14 zoo_1.8-9           sjlabelled_1.1.7   
[33] glmnet_4.1-1        Matrix_1.2-17       stringr_1.4.0       foreign_0.8-71      readxl_1.3.1        countrycode_1.2.0   logspline_2.1.16    fitdistrplus_1.1-3 
[41] MASS_7.3-51.4       doBy_4.6.9          ivmodel_1.9.0       censReg_0.5-32      maxLik_1.4-6        miscTools_0.6-26    VGAM_1.1-5          stargazer_5.2.2    
[49] growthrates_0.8.2   deSolve_1.28        rms_6.2-0           SparseM_1.81        Hmisc_4.5-0         ggplot2_3.3.3       Formula_1.2-4       survival_3.2-7     
[57] lattice_0.20-38     sure_0.2.0          haven_2.3.1         session_1.0.3      

loaded via a namespace (and not attached):
 [1] backports_1.2.1     plyr_1.8.6          TH.data_1.0-10      digest_0.6.27       foreach_1.5.1       htmltools_0.5.1.1   fansi_0.4.2         cluster_2.1.0      
 [9] matrixStats_0.58.0  bdsmatrix_1.3-4     jpeg_0.1-8.1        colorspace_2.0-0    rvest_1.0.0         rbibutils_2.0       xfun_0.22           jsonlite_1.7.2     
[17] crayon_1.4.1        lme4_1.1-26         iterators_1.0.13    glue_1.4.2          FME_1.3.6.1         gtable_0.3.0        MatrixModels_0.5-0  shape_1.4.5        
[25] abind_1.4-5         scales_1.1.1        mvtnorm_1.1-1       DBI_1.1.1           Rcpp_1.0.6          htmlTable_2.1.0     tmvnsim_1.0-2       prediction_0.3.14  
[33] httr_1.4.2          htmlwidgets_1.5.3   RColorBrewer_1.1-2  ellipsis_0.3.1      farver_2.1.0        pkgconfig_2.0.3     reshape_0.8.8       dbplyr_2.1.0       
[41] nnet_7.3-12         utf8_1.2.1          labeling_0.4.2      tidyselect_1.1.0    rlang_0.4.10        reshape2_1.4.4      munsell_0.5.0       cellranger_1.1.0   
[49] tools_3.6.1         cli_2.3.1           generics_0.1.0      broom_0.7.5         glmmML_1.1.1        fs_1.5.0            knitr_1.31          zip_2.1.1          
[57] rootSolve_1.8.2.1   nlme_3.1-140        quantreg_5.85       xml2_1.3.2          compiler_3.6.1      rstudioapi_0.13     curl_4.3            png_0.1-7          
[65] reprex_1.0.0        statmod_1.4.35      pbivnorm_0.6.0      stringi_1.5.3       nloptr_1.2.2.2      vctrs_0.3.6         pillar_1.5.1        lifecycle_1.0.0    
[73] Rdpack_2.1.1        insight_0.13.1      conquer_1.0.2       R6_2.5.0            latticeExtra_0.6-29 gridExtra_2.3       rio_0.5.26          codetools_0.2-16   
[81] polspline_1.1.19    boot_1.3-22         assertthat_0.2.1    minpack.lm_1.2-1    withr_2.4.1         mnormt_2.0.2        Deriv_4.1.3         multcomp_1.4-16    
[89] parallel_3.6.1      hms_1.0.0           grid_3.6.1          rpart_4.1-15        coda_0.19-4         minqa_1.2.4         lubridate_1.7.10    numDeriv_2016.8-1.1
[97] base64enc_0.1-3