Skip to content
Please note that GitHub no longer supports your web browser.

We recommend upgrading to the latest Google Chrome or Firefox.

Learn more
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vce = "bootstrap" and vce = "simulation" error/fail when variables has a length of 1 #112

Closed
jacob-long opened this issue Feb 17, 2019 · 0 comments · Fixed by #113
Closed

vce = "bootstrap" and vce = "simulation" error/fail when variables has a length of 1 #112

jacob-long opened this issue Feb 17, 2019 · 0 comments · Fixed by #113
Labels
bug

Comments

@jacob-long
Copy link
Contributor

@jacob-long jacob-long commented Feb 17, 2019

Please specify whether your issue is about:

  • a possible bug
  • a question about package functionality
  • a suggested code or documentation change, improvement to the code, or feature request
library("margins")
fit <- lm(mpg ~ wt + hp * cyl, data = mtcars)
summary(margins(fit, vce = "simulation", variables = "hp"))
#> factor     AME SE  z  p lower upper
#>     hp -0.0463 NA NA NA    NA    NA

Compare to

summary(margins(fit, vce = "simulation"))
#> factor     AME     SE       z      p   lower   upper
#>    cyl  0.0381 0.5768  0.0661 0.9473 -1.0925  1.1687
#>     hp -0.0463 0.0124 -3.7336 0.0002 -0.0706 -0.0220
#>     wt -3.1198 0.7771 -4.0147 0.0001 -4.6429 -1.5967

Similar problem with vce = "bootstrap"

summary(margins(fit, vce = "bootstrap", variables = "hp"))
#> Error in `[[<-.data.frame`(`*tmp*`, "z", value = c(Var_dydx_hp = NA_real_,  : 
#>   replacement has 50 rows, data has 1

Again, omitting variables or making variables a vector of length greater than 1 works fine.

summary(margins(fit, vce = "bootstrap"))
#> factor     AME     SE       z      p   lower   upper
#>    cyl  0.0381 0.7275  0.0524 0.9582 -1.3877  1.4640
#>     hp -0.0463 0.0198 -2.3441 0.0191 -0.0850 -0.0076
#>     wt -3.1198 0.8892 -3.5086 0.0005 -4.8626 -1.3770

Traceback gives a clue about the problem:

6: stop(sprintf(ngettext(N, "replacement has %d row, data has %d", 
       "replacement has %d rows, data has %d"), N, nrows), domain = NA)
5: `[[<-.data.frame`(`*tmp*`, "z", value = c(Var_dydx_hp = NA_real_, 
   Var_dydx_hp.1 = NA_real_, Var_dydx_hp.2 = NA_real_, Var_dydx_hp.3 = NA_real_, 
   Var_dydx_hp.4 = NA_real_, Var_dydx_hp.5 = NA_real_, Var_dydx_hp.6 = NA_real_, 
   Var_dydx_hp.7 = NA_real_, Var_dydx_hp.8 = NA_real_, Var_dydx_hp.9 = NA_real_, 
   Var_dydx_hp.10 = NA_real_, Var_dydx_hp.11 = NA_real_, Var_dydx_hp.12 = NA_real_, 
   Var_dydx_hp.13 = NA_real_, Var_dydx_hp.14 = NA_real_, Var_dydx_hp.15 = NA_real_, 
   Var_dydx_hp.16 = NA_real_, Var_dydx_hp.17 = NA_real_, Var_dydx_hp.18 = NA_real_, 
   Var_dydx_hp.19 = NA_real_, Var_dydx_hp.20 = NA_real_, Var_dydx_hp.21 = NA_real_, 
   Var_dydx_hp.22 = NA_real_, Var_dydx_hp.23 = NA_real_, Var_dydx_hp.24 = NA_real_, 
   Var_dydx_hp.25 = NA_real_, Var_dydx_hp.26 = NA_real_, Var_dydx_hp.27 = NA_real_, 
   Var_dydx_hp.28 = NA_real_, Var_dydx_hp.29 = NA_real_, Var_dydx_hp.30 = NA_real_, 
   Var_dydx_hp.31 = NA_real_, Var_dydx_hp.32 = NA_real_, Var_dydx_hp.33 = NA_real_, 
   Var_dydx_hp.34 = NA_real_, Var_dydx_hp.35 = NA_real_, Var_dydx_hp.36 = NA_real_, 
   Var_dydx_hp.37 = NA_real_, Var_dydx_hp.38 = NA_real_, Var_dydx_hp.39 = NA_real_, 
   Var_dydx_hp.40 = NA_real_, Var_dydx_hp.41 = NA_real_, Var_dydx_hp.42 = NA_real_, 
   Var_dydx_hp.43 = NA_real_, Var_dydx_hp.44 = NA_real_, Var_dydx_hp.45 = NA_real_, 
   Var_dydx_hp.46 = NA_real_, Var_dydx_hp.47 = NA_real_, Var_dydx_hp.48 = NA_real_, 
   Var_dydx_hp.49 = NA_real_))
4: `[[<-`(`*tmp*`, "z", value = c(Var_dydx_hp = NA_real_, Var_dydx_hp.1 = NA_real_, 
   Var_dydx_hp.2 = NA_real_, Var_dydx_hp.3 = NA_real_, Var_dydx_hp.4 = NA_real_, 
   Var_dydx_hp.5 = NA_real_, Var_dydx_hp.6 = NA_real_, Var_dydx_hp.7 = NA_real_, 
   Var_dydx_hp.8 = NA_real_, Var_dydx_hp.9 = NA_real_, Var_dydx_hp.10 = NA_real_, 
   Var_dydx_hp.11 = NA_real_, Var_dydx_hp.12 = NA_real_, Var_dydx_hp.13 = NA_real_, 
   Var_dydx_hp.14 = NA_real_, Var_dydx_hp.15 = NA_real_, Var_dydx_hp.16 = NA_real_, 
   Var_dydx_hp.17 = NA_real_, Var_dydx_hp.18 = NA_real_, Var_dydx_hp.19 = NA_real_, 
   Var_dydx_hp.20 = NA_real_, Var_dydx_hp.21 = NA_real_, Var_dydx_hp.22 = NA_real_, 
   Var_dydx_hp.23 = NA_real_, Var_dydx_hp.24 = NA_real_, Var_dydx_hp.25 = NA_real_, 
   Var_dydx_hp.26 = NA_real_, Var_dydx_hp.27 = NA_real_, Var_dydx_hp.28 = NA_real_, 
   Var_dydx_hp.29 = NA_real_, Var_dydx_hp.30 = NA_real_, Var_dydx_hp.31 = NA_real_, 
   Var_dydx_hp.32 = NA_real_, Var_dydx_hp.33 = NA_real_, Var_dydx_hp.34 = NA_real_, 
   Var_dydx_hp.35 = NA_real_, Var_dydx_hp.36 = NA_real_, Var_dydx_hp.37 = NA_real_, 
   Var_dydx_hp.38 = NA_real_, Var_dydx_hp.39 = NA_real_, Var_dydx_hp.40 = NA_real_, 
   Var_dydx_hp.41 = NA_real_, Var_dydx_hp.42 = NA_real_, Var_dydx_hp.43 = NA_real_, 
   Var_dydx_hp.44 = NA_real_, Var_dydx_hp.45 = NA_real_, Var_dydx_hp.46 = NA_real_, 
   Var_dydx_hp.47 = NA_real_, Var_dydx_hp.48 = NA_real_, Var_dydx_hp.49 = NA_real_
   ))
3: summarize_one(object, level = level, is_weighted = is_weighted, 
       ...)
2: summary.margins(margins(fit, vce = "bootstrap", variables = "hp"))
1: summary(margins(fit, vce = "bootstrap", variables = "hp"))

The margins object has a bunch of weird NA_real_-filled columns called Var_dydx_hp. plus a number. Something similar is going on with vce = "simulation" but the names are slightly different and therefore so is the error (or lack thereof in this case)

names(margins(fit, vce = "simulation", variables = "hp"))
#>  [1] "mpg"        "cyl"        "disp"       "hp"         "drat"      
#>  [6] "wt"         "qsec"       "vs"         "am"         "gear"      
#> [11] "carb"       "fitted"     "se.fitted"  "dydx_hp"    "Var_"      
#> [16] "NA"         "NA.1"       "NA.2"       "NA.3"       "NA.4"      
#> [21] "NA.5"       "NA.6"       "NA.7"       "NA.8"       "NA.9"      
#> [26] "NA.10"      "NA.11"      "NA.12"      "NA.13"      "NA.14"     
#> [31] "NA.15"      "NA.16"      "NA.17"      "NA.18"      "NA.19"     
#> [36] "NA.20"      "NA.21"      "NA.22"      "NA.23"      "NA.24"     
#> [41] "NA.25"      "NA.26"      "NA.27"      "NA.28"      "NA.29"     
#> [46] "NA.30"      "NA.31"      "NA.32"      "NA.33"      "NA.34"     
#> [51] "NA.35"      "NA.36"      "NA.37"      "NA.38"      "NA.39"     
#> [56] "NA.40"      "NA.41"      "NA.42"      "NA.43"      "NA.44"     
#> [61] "NA.45"      "NA.46"      "NA.47"      "NA.48"      "_weights"  
#> [66] "_at_number"

These are actually separate errors, but have a related cause in that there are internal functions that expect a a matrix of certain dimensions and names but are getting something else because we are isolating one variable.

PR incoming.

@leeper leeper added the bug label Feb 17, 2019
@leeper leeper closed this in #113 Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.