ropensci / dynamite

Bayesian Inference of Complex Panel Data
https://docs.ropensci.org/dynamite/
GNU General Public License v3.0
27 stars 1 forks source link

Error when summarizing categorical predictions with type = "mean" #73

Closed mirojantti closed 11 months ago

mirojantti commented 12 months ago

Hello!

In case of a dynamite model, I understand that one can summarize the predictions over individuals by specifying the funs argument. When the model is categorical and type = "response" this appears to be working as expected.

predict(
  dynamite::categorical_example_fit,
  type = "response",
  funs = list(y = list(n = length))
)$simulated |> head()
  n_y time .draw
1  NA    1     1
2 100    2     1
3 100    3     1
4 100    4     1
5 100    5     1
6 100    6     1

However, attempting to summarize predictions with type = "mean" results in an error.

predict(
  dynamite::categorical_example_fit,
  type = "mean",
  funs = list(y = list(n = length))
)$simulated |> head()
Error in `[.data.table`(simulated, idx, lapply(.SD, f$fun), by = ".draw",  : 
  Some items of .SDcols are not column names: [y_mean]
Session info R version 4.2.2 Patched (2022-11-10 r83330) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 23.04 Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.11.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.11.0 locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=fi_FI.UTF-8 [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=fi_FI.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=fi_FI.UTF-8 LC_NAME=C LC_ADDRESS=C [10] LC_TELEPHONE=C LC_MEASUREMENT=fi_FI.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] rstan_2.21.7 tidyselect_1.2.0 mitools_2.4 splines_4.2.2 [5] lattice_0.20-45 carData_3.0-5 colorspace_2.1-0 vctrs_0.6.4 [9] generics_0.1.3 stats4_4.2.2 loo_2.5.1 utf8_1.2.4 [13] survival_3.5-3 rlang_1.1.2 pkgbuild_1.4.2 nloptr_2.0.3 [17] pillar_1.9.0 glue_1.6.2 DBI_1.1.3 distributional_0.3.2 [21] matrixStats_0.63.0 lifecycle_1.0.4 effects_4.2-2 posterior_1.5.0 [25] munsell_0.5.0 gtable_0.3.1 codetools_0.2-19 inline_0.3.19 [29] callr_3.7.3 ps_1.7.5 gradu_0.1.0 dynamite_1.4.7 [33] parallel_4.2.2 fansi_1.0.5 Rcpp_1.0.11 scales_1.2.1 [37] backports_1.4.1 checkmate_2.1.0 StanHeaders_2.32.2 RcppParallel_5.1.6 [41] abind_1.4-5 farver_2.1.1 lme4_1.1-31 gridExtra_2.3 [45] tensorA_0.36.2 ggplot2_3.4.1 processx_3.8.2 insight_0.19.0 [49] dplyr_1.0.10 survey_4.1-1 grid_4.2.2 cli_3.6.1 [53] tools_4.2.2 magrittr_2.0.3 patchwork_1.1.3 tibble_3.2.1 [57] crayon_1.5.2 pkgconfig_2.0.3 MASS_7.3-58.2 Matrix_1.5-3 [61] prettyunits_1.2.0 data.table_1.14.6 assertthat_0.2.1 minqa_1.2.5 [65] rstudioapi_0.15.0 R6_2.5.1 boot_1.3-28.1 nnet_7.3-19 [69] nlme_3.1-162 compiler_4.2.2
santikka commented 12 months ago

Hi @joakim219 !

Thanks for reporting, should be fixed now. With type = "mean" and a categorical response, the function(s) in funs will now be applied to each category:

predict(
  dynamite::categorical_example_fit,
  type = "mean",
  funs = list(y = list(n = length))
)$simulated |> head()
  n_y_a n_y_b n_y_c time .draw
1    NA    NA    NA    1     1
2   100   100   100    2     1
3   100   100   100    3     1
4   100   100   100    4     1
5   100   100   100    5     1
6   100   100   100    6     1