dlinzer / poLCA

Polytomous Variable Latent Class Analysis (R package)
https://dlinzer.github.io/poLCA/
48 stars 17 forks source link

`poLCA.entropy()` crashes when observable variables have different number of categories #20

Closed DaniMori closed 2 years ago

DaniMori commented 3 years ago

When a LCA model is fit to variables with different number of categories, poLCA.entropy() crashes when trying to create a data frame of response probabilities:

library(poLCA)
#> Loading required package: scatterplot3d
#> Loading required package: MASS

set.seed(786154)

toy_data <- data.frame(
  i1 = sample.int(4, size = 100, replace = TRUE),
  i2 = sample.int(3, size = 100, replace = TRUE)
)

lca_fit <- poLCA(
  cbind(i1, i2) ~ 1,
  data = toy_data
)
#> Conditional item response (column) probabilities,
#>  by outcome variable, for each class (row) 
#>  
#> $i1
#>            Pr(1)  Pr(2)  Pr(3)  Pr(4)
#> class 1:  0.2458 0.0649 0.3840 0.3053
#> class 2:  0.2891 0.4139 0.0191 0.2779
#> 
#> $i2
#>            Pr(1)  Pr(2)  Pr(3)
#> class 1:  0.3984 0.1454 0.4562
#> class 2:  0.3119 0.4934 0.1947
#> 
#> Estimated class population shares 
#>  0.4409 0.5591 
#>  
#> Predicted class memberships (by modal posterior prob.) 
#>  0.44 0.56 
#>  
#> ========================================================= 
#> Fit for 2 latent classes: 
#> ========================================================= 
#> number of observations: 100 
#> number of estimated parameters: 11 
#> residual degrees of freedom: 0 
#> maximum log-likelihood: -244.5202 
#>  
#> AIC(2): 511.0404
#> BIC(2): 539.6973
#> G^2(2): 0.7217094 (Likelihood ratio/deviance statistic) 
#> X^2(2): 0.7257384 (Chi-square goodness of fit) 
#> 

poLCA.entropy(lca_fit)
#> Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 4, 3

Created on 2021-03-22 by the reprex package (v1.0.0)

Session info ``` r sessioninfo::session_info() #> - Session info --------------------------------------------------------------- #> setting value #> version R version 4.0.4 (2021-02-15) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate Spanish_Spain.1252 #> ctype Spanish_Spain.1252 #> tz Europe/Paris #> date 2021-03-22 #> #> - Packages ------------------------------------------------------------------- #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.3) #> cli 2.3.1 2021-02-23 [1] CRAN (R 4.0.4) #> digest 0.6.27 2020-10-24 [1] CRAN (R 4.0.3) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.3) #> fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.3) #> glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.3) #> highr 0.8 2019-03-20 [1] CRAN (R 4.0.3) #> htmltools 0.5.1.1 2021-01-22 [1] CRAN (R 4.0.3) #> knitr 1.31 2021-01-27 [1] CRAN (R 4.0.3) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.3) #> MASS * 7.3-53 2020-09-09 [2] CRAN (R 4.0.4) #> poLCA * 1.4.1 2014-01-10 [1] CRAN (R 4.0.4) #> ps 1.6.0 2021-02-28 [1] CRAN (R 4.0.4) #> reprex 1.0.0 2021-01-27 [1] CRAN (R 4.0.3) #> rlang 0.4.10 2020-12-30 [1] CRAN (R 4.0.3) #> rmarkdown 2.7 2021-02-19 [1] CRAN (R 4.0.4) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.0.3) #> scatterplot3d * 0.3-41 2018-03-14 [1] CRAN (R 4.0.3) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.3) #> stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.3) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.3) #> withr 2.4.1 2021-01-26 [1] CRAN (R 4.0.3) #> xfun 0.21 2021-02-10 [1] CRAN (R 4.0.3) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.3) #> #> [1] C:/Users/Mori.P16/Documents/R/win-library/4.0 #> [2] C:/Program Files/R/R-4.0.4/library ```
matt5mitchell commented 3 years ago

I just pulled down the latest version of poLCA from CRAN and the entropy function is different from what's here on Github. Specifically, this issue was addressed in a commit on January 30, 2016: https://github.com/dlinzer/poLCA/commit/51ea0f91e52dbdb3de4026cf7c340afbb41d79f7

Would you please update the package on CRAN with this fix?

JeffreyBLewis commented 2 years ago

Addressed on CRAN in Version 1.6.