Skip to content
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

Error: object of type 'symbol' is not subsettable #4

Open
slowkow opened this issue Oct 14, 2021 · 3 comments
Open

Error: object of type 'symbol' is not subsettable #4

slowkow opened this issue Oct 14, 2021 · 3 comments

Comments

@slowkow
Copy link

slowkow commented Oct 14, 2021

Dear @maiermarco,

Thank you for creating and sharing DirichletReg with the world.

I'm writing to let you know that there is an issue with how your DirichReg() function handles formulas.

See the reprex below for details.

This throws an error:

x <- "Disease"
my_formula <- as.formula(sprintf("Smp ~ %s", x))
dir1 <- DirichReg(
  formula = my_formula,
  data = Bld
)
#> Error: object of type 'symbol' is not subsettable

We can avoid the error like this:

dir1 <- do.call(
  DirichReg,
  args = list(
    formula = my_formula,
    data = Bld
  )
)

I learned about this weird workaround thanks to kassambara/survminer#252


Setup the example

library(DirichletReg)
#> Loading required package: Formula
library(modelsummary)

Bld <- DirichletReg::BloodSamples
Bld <- Bld[!is.na(Bld$Disease),]
Bld$Smp <- DR_data(Bld[, 1:4])
#> Warning in DR_data(Bld[, 1:4]): not all rows sum up to 1 => normalization forced

Demonstrate that the stats::lm() function works as expected.

lm1 <- lm(
  formula = Albumin ~ Disease,
  data = Bld
)

lm1 <- lm(
  formula = as.formula("Albumin ~ Disease"),
  data = Bld
)

lm1 <- lm(
  formula = as.formula(sprintf("Albumin ~ Disease")),
  data = Bld
)

x <- "Disease"
lm1 <- lm(
  formula = as.formula(sprintf("Albumin ~ %s", x)),
  data = Bld
)

For these 3 examples, DirichletReg::DirichReg() works as expected:

dir1 <- DirichReg(
  formula = Smp ~ Disease,
  data = Bld
)

dir1 <- DirichReg(
  formula = as.formula("Smp ~ Disease"),
  data = Bld
)

dir1 <- DirichReg(
  formula = as.formula(sprintf("Smp ~ Disease")),
  data = Bld
)

For these two examples, we get an issue:

x <- "Disease"
dir1 <- DirichReg(
  formula = as.formula(sprintf("Smp ~ %s", x)),
  data = Bld
)
#> Error in sprintf("Smp ~ %s", x): invalid type of argument[1]: 'language'

x <- "Disease"
my_formula <- as.formula(sprintf("Smp ~ %s", x))
dir1 <- DirichReg(
  formula = my_formula,
  data = Bld
)
#> Error: object of type 'symbol' is not subsettable

Here is the workaround:

dir1 <- do.call(
  DirichReg,
  args = list(
    formula = my_formula,
    data = Bld
  )
)

Here is how we can easily get the estimates:

get_estimates(dir1)
#>          term    estimate std.error conf.level   conf.low conf.high  statistic
#> 1 (Intercept)  3.48400630 0.2212193       0.95  3.0504245 3.9175881 15.7491078
#> 2    DiseaseB -0.31929827 0.3035356       0.95 -0.9142171 0.2756205 -1.0519303
#> 3 (Intercept)  2.91476627 0.2223808       0.95  2.4789079 3.3506246 13.1070950
#> 4    DiseaseB -0.37307387 0.3054933       0.95 -0.9718297 0.2256819 -1.2212180
#> 5 (Intercept)  2.36260672 0.2242571       0.95  1.9230709 2.8021426 10.5352588
#> 6    DiseaseB -0.24013259 0.3080348       0.95 -0.8438697 0.3636045 -0.7795632
#> 7 (Intercept)  2.85174707 0.2225512       0.95  2.4155548 3.2879394 12.8138930
#> 8    DiseaseB -0.06418743 0.3051030       0.95 -0.6621784 0.5338035 -0.2103795
#>   df.error      p.value    response
#> 1      Inf 6.965716e-56     Albumin
#> 2      Inf 2.928315e-01     Albumin
#> 3      Inf 2.998659e-39 Pre.Albumin
#> 4      Inf 2.220035e-01 Pre.Albumin
#> 5      Inf 5.941959e-26  Globulin.A
#> 6      Inf 4.356480e-01  Globulin.A
#> 7      Inf 1.370807e-37  Globulin.B
#> 8      Inf 8.333715e-01  Globulin.B

Created on 2021-10-14 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.3 (2020-10-10)
#>  os       macOS Catalina 10.15.7      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2021-10-14                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date       lib source                             
#>  assertthat     0.2.1      2019-03-21 [2] CRAN (R 4.0.2)                     
#>  backports      1.2.1      2020-12-09 [1] CRAN (R 4.0.2)                     
#>  bayestestR     0.11.0     2021-09-03 [1] CRAN (R 4.0.2)                     
#>  broom          0.7.9      2021-07-27 [1] CRAN (R 4.0.2)                     
#>  checkmate      2.0.0      2020-02-06 [2] CRAN (R 4.0.2)                     
#>  cli            3.0.1      2021-07-17 [1] CRAN (R 4.0.2)                     
#>  coda           0.19-4     2020-09-30 [1] CRAN (R 4.0.2)                     
#>  codetools      0.2-18     2020-11-04 [1] CRAN (R 4.0.2)                     
#>  crayon         1.4.1      2021-02-08 [1] CRAN (R 4.0.2)                     
#>  datawizard     0.2.1      2021-10-04 [1] CRAN (R 4.0.2)                     
#>  DBI            1.1.1      2021-01-15 [1] CRAN (R 4.0.2)                     
#>  digest         0.6.28     2021-09-23 [1] CRAN (R 4.0.2)                     
#>  DirichletReg * 0.7-1      2021-05-18 [1] CRAN (R 4.0.2)                     
#>  dplyr          1.0.7      2021-06-18 [1] CRAN (R 4.0.2)                     
#>  ellipsis       0.3.2      2021-04-29 [1] CRAN (R 4.0.2)                     
#>  emmeans        1.7.0      2021-09-29 [1] CRAN (R 4.0.2)                     
#>  estimability   1.3        2018-02-11 [2] CRAN (R 4.0.2)                     
#>  evaluate       0.14       2019-05-28 [2] CRAN (R 4.0.1)                     
#>  fansi          0.5.0      2021-05-25 [1] CRAN (R 4.0.2)                     
#>  fastmap        1.1.0      2021-01-25 [1] CRAN (R 4.0.2)                     
#>  Formula      * 1.2-4      2020-10-16 [1] CRAN (R 4.0.2)                     
#>  fs             1.5.0      2020-07-31 [1] CRAN (R 4.0.2)                     
#>  generics       0.1.0      2020-10-31 [2] CRAN (R 4.0.2)                     
#>  glue           1.4.2      2020-08-27 [2] CRAN (R 4.0.2)                     
#>  highr          0.9        2021-04-16 [1] CRAN (R 4.0.2)                     
#>  htmltools      0.5.2      2021-08-25 [1] CRAN (R 4.0.2)                     
#>  insight        0.14.4     2021-09-02 [1] CRAN (R 4.0.2)                     
#>  knitr          1.36       2021-09-29 [1] CRAN (R 4.0.2)                     
#>  lattice        0.20-45    2021-09-22 [1] CRAN (R 4.0.2)                     
#>  lifecycle      1.0.1      2021-09-24 [1] CRAN (R 4.0.2)                     
#>  magrittr       2.0.1.9000 2020-12-15 [1] Github (tidyverse/magrittr@bb1c86a)
#>  MASS           7.3-54     2021-05-03 [1] CRAN (R 4.0.2)                     
#>  Matrix         1.3-4      2021-06-01 [1] CRAN (R 4.0.2)                     
#>  maxLik         1.5-2      2021-07-26 [1] CRAN (R 4.0.2)                     
#>  miscTools      0.6-26     2019-12-08 [1] CRAN (R 4.0.2)                     
#>  modelsummary * 0.9.2      2021-09-29 [1] CRAN (R 4.0.2)                     
#>  multcomp       1.4-17     2021-04-29 [1] CRAN (R 4.0.2)                     
#>  mvtnorm        1.1-3      2021-10-08 [1] CRAN (R 4.0.2)                     
#>  parameters     0.14.0     2021-05-29 [1] CRAN (R 4.0.2)                     
#>  pillar         1.6.3      2021-09-26 [1] CRAN (R 4.0.2)                     
#>  pkgconfig      2.0.3      2019-09-22 [2] CRAN (R 4.0.2)                     
#>  purrr          0.3.4      2020-04-17 [2] CRAN (R 4.0.2)                     
#>  R.cache        0.15.0     2021-04-30 [1] CRAN (R 4.0.2)                     
#>  R.methodsS3    1.8.1      2020-08-26 [1] CRAN (R 4.0.2)                     
#>  R.oo           1.24.0     2020-08-26 [1] CRAN (R 4.0.2)                     
#>  R.utils        2.11.0     2021-09-26 [1] CRAN (R 4.0.2)                     
#>  R6             2.5.1      2021-08-19 [1] CRAN (R 4.0.2)                     
#>  reprex         2.0.1      2021-08-05 [1] CRAN (R 4.0.2)                     
#>  rlang          0.4.11     2021-04-30 [1] CRAN (R 4.0.2)                     
#>  rmarkdown      2.11       2021-09-14 [1] CRAN (R 4.0.2)                     
#>  rstudioapi     0.13       2020-11-12 [2] CRAN (R 4.0.2)                     
#>  sandwich       3.0-1      2021-05-18 [1] CRAN (R 4.0.2)                     
#>  sessioninfo    1.1.1      2018-11-05 [2] CRAN (R 4.0.2)                     
#>  stringi        1.7.5      2021-10-04 [1] CRAN (R 4.0.2)                     
#>  stringr        1.4.0      2019-02-10 [2] CRAN (R 4.0.2)                     
#>  styler         1.6.2      2021-09-23 [1] CRAN (R 4.0.2)                     
#>  survival       3.2-13     2021-08-24 [1] CRAN (R 4.0.2)                     
#>  tables         0.9.6      2020-09-22 [1] CRAN (R 4.0.2)                     
#>  TH.data        1.1-0      2021-09-27 [1] CRAN (R 4.0.2)                     
#>  tibble         3.1.5      2021-09-30 [1] CRAN (R 4.0.2)                     
#>  tidyr          1.1.4      2021-09-27 [1] CRAN (R 4.0.2)                     
#>  tidyselect     1.1.1      2021-04-30 [1] CRAN (R 4.0.2)                     
#>  utf8           1.2.2      2021-07-24 [1] CRAN (R 4.0.2)                     
#>  vctrs          0.3.8      2021-04-29 [1] CRAN (R 4.0.2)                     
#>  withr          2.4.2      2021-04-18 [1] CRAN (R 4.0.2)                     
#>  xfun           0.26       2021-09-14 [1] CRAN (R 4.0.2)                     
#>  xtable         1.8-4      2019-04-21 [2] CRAN (R 4.0.2)                     
#>  yaml           2.2.1      2020-02-01 [2] CRAN (R 4.0.2)                     
#>  zoo            1.8-9      2021-03-09 [1] CRAN (R 4.0.2)                     
#> 
#> [1] /Users/kamil/Library/R/4.0/library
#> [2] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
@maiermarco
Copy link
Owner

Dear Kamil,

thank you for your interest in my R-package and pointing out this problem.

It seems like the formula-argument is evaluated too late; I will fix this in an upcoming release.

In the meantime, your solution using do.call() is a great workaround.
If you prefer to keep the DirichReg(...) function call, you can insert a placeholder (substitute_formula) that is replaced by substitute() and the resulting expression is then evalued (eval()), like so:

x <- "Disease"

dir1 <- eval(substitute(
      DirichReg(formula = substitute_formula, data = Bld)
    , list(
      "substitute_formula" = as.formula(sprintf("Smp ~ %s", x))
    )
  ))

dir1

#Call:
#DirichReg(formula = Smp ~ Disease, data = Bld)
#using the common parametrization
#
#Log-likelihood: 152.3 on 8 df (44 BFGS + 2 NR Iterations)
#
#-----------------------------------------
#Coefficients for variable no. 1: Albumin
#(Intercept)     DiseaseB  
#     3.4840      -0.3193  
#-----------------------------------------
#Coefficients for variable no. 2: Pre.Albumin
#(Intercept)     DiseaseB  
#     2.9148      -0.3731  
#-----------------------------------------
#Coefficients for variable no. 3: Globulin.A
#(Intercept)     DiseaseB  
#     2.3626      -0.2401  
#-----------------------------------------
#Coefficients for variable no. 4: Globulin.B
#(Intercept)     DiseaseB  
#    2.85175     -0.06419  
#-----------------------------------------

Hope this helps for now.
I'll let you know when the fix is implemented.

@maiermarco
Copy link
Owner

Update: The changes are now implemented in the development-version (0.7-1.9000) in the devel branch. If you have devtools and the R-toolchain, you can install and test it via:

devtools::install_github("maiermarco/DirichletReg@devel")

Please let me know if the issues have been resolved.

@slowkow
Copy link
Author

slowkow commented Oct 18, 2021

Thanks for fixing this so quickly!

I confirmed that the 2 examples with errors no longer throw errors using version 06c44ad

The other examples also seem to work as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants