SachaEpskamp / qgraph

Developmental version of qgraph
GNU General Public License v2.0
68 stars 21 forks source link

Conflict with `psych::score.multiple.choice()` #85

Closed emstruong closed 4 months ago

emstruong commented 4 months ago

Hello,

I wasn't sure where to post this, but it is somewhat related to qgraph and psych doesn't seem to have a Github so,

This works:

library(psych)
library(psychTools)
data(iqitems, package = 'psychTools')
iq.keys <- c(4,4,4, 6,6,3,4,4,  5,2,2,4,  3,2,6,7)
psych::score.multiple.choice(iq.keys,psychTools::iqitems)
#> Call: psych::score.multiple.choice(key = iq.keys, data = psychTools::iqitems)
#> 
#> (Unstandardized) Alpha:
#> [1] 0.84
#> 
#> Average item correlation:
#> [1] 0.25
#> 
#> item statistics 
#>           key    0    1    2    3    4    5    6    7    8 miss    r    n mean
#> reason.4    4 0.05 0.05 0.11 0.10 0.64 0.03 0.02 0.00 0.00    0 0.59 1523 0.64
#> reason.16   4 0.04 0.06 0.08 0.10 0.70 0.01 0.00 0.00 0.00    0 0.53 1524 0.70
#> reason.17   4 0.05 0.03 0.05 0.03 0.70 0.03 0.11 0.00 0.00    0 0.59 1523 0.70
#> reason.19   6 0.04 0.02 0.13 0.03 0.06 0.10 0.62 0.00 0.00    0 0.56 1523 0.62
#> letter.7    6 0.05 0.01 0.05 0.03 0.11 0.14 0.60 0.00 0.00    0 0.58 1524 0.60
#> letter.33   3 0.06 0.10 0.13 0.57 0.04 0.09 0.02 0.00 0.00    0 0.56 1523 0.57
#> letter.34   4 0.04 0.09 0.07 0.11 0.61 0.05 0.02 0.00 0.00    0 0.59 1523 0.61
#> letter.58   4 0.06 0.14 0.09 0.09 0.44 0.16 0.01 0.00 0.00    0 0.58 1525 0.44
#> matrix.45   5 0.04 0.01 0.06 0.14 0.18 0.53 0.04 0.00 0.00    0 0.51 1523 0.53
#> matrix.46   2 0.04 0.12 0.55 0.07 0.11 0.06 0.05 0.00 0.00    0 0.52 1524 0.55
#> matrix.47   2 0.04 0.05 0.61 0.07 0.11 0.06 0.06 0.00 0.00    0 0.55 1523 0.61
#> matrix.55   4 0.04 0.02 0.18 0.14 0.37 0.07 0.18 0.00 0.00    0 0.45 1524 0.37
#> rotate.3    3 0.04 0.03 0.04 0.19 0.22 0.15 0.05 0.12 0.15    0 0.51 1523 0.19
#> rotate.4    2 0.04 0.03 0.21 0.05 0.18 0.04 0.04 0.25 0.15    0 0.56 1523 0.21
#> rotate.6    6 0.04 0.22 0.02 0.05 0.14 0.05 0.30 0.04 0.14    0 0.55 1523 0.30
#> rotate.8    7 0.04 0.03 0.21 0.07 0.16 0.05 0.13 0.19 0.13    0 0.48 1524 0.19
#>             sd
#> reason.4  0.48
#> reason.16 0.46
#> reason.17 0.46
#> reason.19 0.49
#> letter.7  0.49
#> letter.33 0.50
#> letter.34 0.49
#> letter.58 0.50
#> matrix.45 0.50
#> matrix.46 0.50
#> matrix.47 0.49
#> matrix.55 0.48
#> rotate.3  0.40
#> rotate.4  0.41
#> rotate.6  0.46
#> rotate.8  0.39

Created on 2024-02-25 with reprex v2.1.0

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.2 (2023-10-31) #> os Ubuntu 22.04.4 LTS #> system x86_64, linux-gnu #> ui X11 #> language en_CA:en #> collate en_CA.UTF-8 #> ctype en_CA.UTF-8 #> tz CENSORED #> date 2024-02-25 #> pandoc 3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> ! package * version date (UTC) lib source #> cli 3.6.2 2023-12-11 [1] RSPM (R 4.3.0) #> P digest 0.6.33 2023-07-07 [?] CRAN (R 4.3.1) #> P evaluate 0.23 2023-11-01 [?] RSPM (R 4.3.0) #> P fastmap 1.1.1 2023-02-24 [?] CRAN (R 4.3.0) #> P foreign 0.8-85 2023-09-09 [3] CRAN (R 4.3.2) #> P fs 1.6.2 2023-04-25 [?] CRAN (R 4.3.0) #> glue 1.7.0 2024-01-09 [1] RSPM (R 4.3.0) #> P htmltools 0.5.7 2023-11-03 [?] RSPM (R 4.3.0) #> P knitr 1.45 2023-10-30 [?] RSPM (R 4.3.0) #> P lattice 0.21-9 2023-10-01 [?] CRAN (R 4.3.2) #> lifecycle 1.0.4 2023-11-07 [1] RSPM (R 4.3.0) #> P magrittr 2.0.3 2022-03-30 [?] CRAN (R 4.3.0) #> P mnormt 2.1.1 2022-09-26 [?] CRAN (R 4.3.0) #> P nlme 3.1-162 2023-01-31 [?] CRAN (R 4.3.0) #> P psych * 2.4.1 2024-01-18 [?] RSPM (R 4.3.2) #> P psychTools * 2.4.2 2024-01-18 [?] RSPM (R 4.3.0) #> P purrr 1.0.1 2023-01-10 [?] CRAN (R 4.3.0) #> P R.cache 0.16.0 2022-07-21 [?] CRAN (R 4.3.0) #> P R.methodsS3 1.8.2 2022-06-13 [?] CRAN (R 4.3.0) #> P R.oo 1.26.0 2024-01-24 [?] RSPM (R 4.3.0) #> P R.utils 2.12.3 2023-11-18 [?] RSPM (R 4.3.0) #> P reprex 2.1.0 2024-01-11 [?] RSPM (R 4.3.0) #> rlang 1.1.3 2024-01-10 [1] RSPM (R 4.3.0) #> P rmarkdown 2.25 2023-09-18 [?] RSPM (R 4.3.0) #> P rstudioapi 0.15.0 2023-07-07 [?] CRAN (R 4.3.1) #> P sessioninfo 1.2.2 2021-12-06 [?] CRAN (R 4.3.0) #> P styler 1.10.2 2023-08-29 [?] RSPM (R 4.3.0) #> vctrs 0.6.5 2023-12-01 [1] RSPM (R 4.3.0) #> withr 3.0.0 2024-01-16 [1] RSPM (R 4.3.0) #> P xfun 0.42 2024-02-08 [?] RSPM (R 4.3.0) #> P yaml 2.3.8 2023-12-11 [?] RSPM (R 4.3.0) #> #> [1] /home/CENSORED/renv/library/R-4.3/x86_64-pc-linux-gnu #> [2] /home/CENSORED/R/renv/sandbox/R-4.3/x86_64-pc-linux-gnu/0b2508b5 #> [3] /home/CENSORED/installs/r/4.3.2/lib/R/library #> #> P ── Loaded and on-disk path mismatch. #> #> ────────────────────────────────────────────────────────────────────────────── ```

But this doesn't

library(qgraph)
library(psych)
library(psychTools)
data(iqitems, package = 'psychTools')
iq.keys <- c(4,4,4, 6,6,3,4,4,  5,2,2,4,  3,2,6,7)
psych::score.multiple.choice(iq.keys,psychTools::iqitems)
#> Warning in cbind(key, response.freq, item.cor, item.stats): number of rows of
#> result is not a multiple of vector length (arg 4)
#> Error in round(item.stats, digits): non-numeric argument to mathematical function

Created on 2024-02-25 with reprex v2.1.0

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.2 (2023-10-31) #> os Ubuntu 22.04.4 LTS #> system x86_64, linux-gnu #> ui X11 #> language en_CA:en #> collate en_CA.UTF-8 #> ctype en_CA.UTF-8 #> tz CENSORED #> date 2024-02-25 #> pandoc 3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> ! package * version date (UTC) lib source #> P abind 1.4-5 2016-07-21 [?] CRAN (R 4.3.0) #> P backports 1.4.1 2021-12-13 [?] CRAN (R 4.3.0) #> P base64enc 0.1-3 2015-07-28 [?] CRAN (R 4.3.0) #> P checkmate 2.3.1 2023-12-04 [?] RSPM (R 4.3.0) #> cli 3.6.2 2023-12-11 [1] RSPM (R 4.3.0) #> P cluster 2.1.4 2022-08-22 [3] CRAN (R 4.3.2) #> P colorspace 2.1-0 2023-01-23 [?] CRAN (R 4.3.0) #> P corpcor 1.6.10 2021-09-16 [?] CRAN (R 4.3.0) #> data.table 1.15.0 2024-01-30 [1] RSPM (R 4.3.0) #> P digest 0.6.33 2023-07-07 [?] CRAN (R 4.3.1) #> P dplyr 1.1.2 2023-04-20 [?] CRAN (R 4.3.0) #> P evaluate 0.23 2023-11-01 [?] RSPM (R 4.3.0) #> fansi 1.0.6 2023-12-08 [1] RSPM (R 4.3.0) #> P fastmap 1.1.1 2023-02-24 [?] CRAN (R 4.3.0) #> P fdrtool 1.2.17 2021-11-13 [?] CRAN (R 4.3.0) #> P foreign 0.8-85 2023-09-09 [3] CRAN (R 4.3.2) #> P Formula 1.2-5 2023-02-24 [?] CRAN (R 4.3.0) #> P fs 1.6.2 2023-04-25 [?] CRAN (R 4.3.0) #> P generics 0.1.3 2022-07-05 [?] CRAN (R 4.3.0) #> ggplot2 3.4.4 2023-10-12 [1] RSPM (R 4.3.0) #> P glasso 1.11 2019-10-01 [?] CRAN (R 4.3.0) #> glue 1.7.0 2024-01-09 [1] RSPM (R 4.3.0) #> P gridExtra 2.3 2017-09-09 [?] CRAN (R 4.3.0) #> gtable 0.3.4 2023-08-21 [1] RSPM (R 4.3.0) #> P gtools 3.9.5 2023-11-20 [?] CRAN (R 4.3.1) #> P Hmisc 5.1-1 2023-09-12 [?] CRAN (R 4.3.1) #> P htmlTable 2.4.2 2023-10-29 [?] RSPM (R 4.3.0) #> P htmltools 0.5.7 2023-11-03 [?] RSPM (R 4.3.0) #> P htmlwidgets 1.6.4 2023-12-06 [?] RSPM (R 4.3.0) #> P igraph 2.0.2 2024-02-17 [?] RSPM (R 4.3.0) #> P jpeg 0.1-10 2022-11-29 [?] CRAN (R 4.3.0) #> P knitr 1.45 2023-10-30 [?] RSPM (R 4.3.0) #> P lattice 0.21-9 2023-10-01 [?] CRAN (R 4.3.2) #> P lavaan 0.6-17 2023-12-20 [?] RSPM (R 4.3.0) #> lifecycle 1.0.4 2023-11-07 [1] RSPM (R 4.3.0) #> P magrittr 2.0.3 2022-03-30 [?] CRAN (R 4.3.0) #> P Matrix 1.6-5 2024-01-11 [?] CRAN (R 4.3.2) #> P mnormt 2.1.1 2022-09-26 [?] CRAN (R 4.3.0) #> P munsell 0.5.0 2018-06-12 [?] CRAN (R 4.3.0) #> P nlme 3.1-162 2023-01-31 [?] CRAN (R 4.3.0) #> P nnet 7.3-19 2023-05-03 [?] CRAN (R 4.3.0) #> P pbapply 1.7-2 2023-06-27 [?] CRAN (R 4.3.1) #> P pbivnorm 0.6.0 2015-01-23 [?] CRAN (R 4.3.0) #> P pillar 1.9.0 2023-03-22 [?] CRAN (R 4.3.0) #> P pkgconfig 2.0.3 2019-09-22 [?] CRAN (R 4.3.0) #> P plyr 1.8.9 2023-10-02 [?] CRAN (R 4.3.1) #> P png 0.1-8 2022-11-29 [?] CRAN (R 4.3.0) #> P psych * 2.4.1 2024-01-18 [?] RSPM (R 4.3.2) #> P psychTools * 2.4.2 2024-01-18 [?] RSPM (R 4.3.0) #> P purrr 1.0.1 2023-01-10 [?] CRAN (R 4.3.0) #> P qgraph * 1.9.8 2023-11-03 [?] CRAN (R 4.3.1) #> P quadprog 1.5-8 2019-11-20 [?] CRAN (R 4.3.0) #> P R.cache 0.16.0 2022-07-21 [?] CRAN (R 4.3.0) #> P R.methodsS3 1.8.2 2022-06-13 [?] CRAN (R 4.3.0) #> P R.oo 1.26.0 2024-01-24 [?] RSPM (R 4.3.0) #> P R.utils 2.12.3 2023-11-18 [?] RSPM (R 4.3.0) #> P R6 2.5.1 2021-08-19 [?] CRAN (R 4.3.0) #> P Rcpp 1.0.10 2023-01-22 [?] CRAN (R 4.3.0) #> P reprex 2.1.0 2024-01-11 [?] RSPM (R 4.3.0) #> P reshape2 1.4.4 2020-04-09 [?] CRAN (R 4.3.0) #> rlang 1.1.3 2024-01-10 [1] RSPM (R 4.3.0) #> P rmarkdown 2.25 2023-09-18 [?] RSPM (R 4.3.0) #> P rpart 4.1.21 2023-10-09 [3] CRAN (R 4.3.2) #> P rstudioapi 0.15.0 2023-07-07 [?] CRAN (R 4.3.1) #> scales 1.3.0 2023-11-28 [1] RSPM (R 4.3.0) #> P sessioninfo 1.2.2 2021-12-06 [?] CRAN (R 4.3.0) #> P stringi 1.7.12 2023-01-11 [?] CRAN (R 4.3.0) #> P stringr 1.5.1 2023-11-14 [?] CRAN (R 4.3.1) #> P styler 1.10.2 2023-08-29 [?] RSPM (R 4.3.0) #> P tibble 3.2.1 2023-03-20 [?] CRAN (R 4.3.0) #> P tidyselect 1.2.0 2022-10-10 [?] CRAN (R 4.3.0) #> utf8 1.2.4 2023-10-22 [1] RSPM (R 4.3.0) #> vctrs 0.6.5 2023-12-01 [1] RSPM (R 4.3.0) #> withr 3.0.0 2024-01-16 [1] RSPM (R 4.3.0) #> P xfun 0.42 2024-02-08 [?] RSPM (R 4.3.0) #> P yaml 2.3.8 2023-12-11 [?] RSPM (R 4.3.0) #> #> [1] /home/CENSORED/renv/library/R-4.3/x86_64-pc-linux-gnu #> [2] /home/CENSORED/R/renv/sandbox/R-4.3/x86_64-pc-linux-gnu/0b2508b5 #> [3] /home/CENSORED/installs/r/4.3.2/lib/R/library #> #> P ── Loaded and on-disk path mismatch. #> #> ────────────────────────────────────────────────────────────────────────────── ```

Quick debugging seems to imply that something about qgraph is interfering with the internal functions of psych::score.multiple.choice(), but I thought that was supposed to be impossible...

revelle commented 4 months ago

Very strange. The failure happens when I try to pick 3 columns from the output from describe.

With qgraph loaded, the data.frame returned from describe is not subsetable.

test <- describe(iqitems)
test
          vars    n mean   sd median trimmed  mad min max range  skew kurtosis   se
reason.4     1 1523 3.39 1.24      4    3.59 0.00   0   6     6 -1.28     1.25 0.03
reason.16    2 1524 3.39 1.14      4    3.65 0.00   0   6     6 -1.61     1.70 0.03
reason.17    3 1523 3.80 1.34      4    3.93 0.00   0   6     6 -1.17     2.08 0.03
reason.19    4 1523 4.79 1.84      6    5.12 0.00   0   6     6 -1.28     0.24 0.05
letter.7     5 1524 4.95 1.70      6    5.34 0.00   0   6     6 -1.72     1.98 0.04
letter.33    6 1523 2.79 1.25      3    2.78 0.00   0   6     6 -0.06     0.64 0.03
letter.34    7 1523 3.38 1.30      4    3.54 0.00   0   6     6 -1.15     0.65 0.03
letter.58    8 1525 3.26 1.52      4    3.39 1.48   0   6     6 -0.71    -0.61 0.04
matrix.45    9 1523 4.14 1.36      5    4.37 0.00   0   6     6 -1.44     1.71 0.03
matrix.46   10 1524 2.47 1.36      2    2.32 0.00   0   6     6  0.97     0.56 0.03
matrix.47   11 1523 2.59 1.37      2    2.44 0.00   0   6     6  1.01     0.63 0.04
matrix.55   12 1524 3.69 1.56      4    3.75 1.48   0   6     6 -0.31    -0.30 0.04
rotate.3    13 1523 4.65 2.16      4    4.71 1.48   0   8     8 -0.04    -0.64 0.06
rotate.4    14 1523 4.77 2.47      4    4.84 2.97   0   8     8 -0.18    -1.32 0.06
rotate.6    15 1523 4.40 2.56      5    4.43 2.97   0   8     8 -0.26    -1.26 0.07
rotate.8    16 1524 4.63 2.40      4    4.68 2.97   0   8     8 -0.14    -1.25 0.06
> test[,2:4]

 NA  Variables      13  Observations

show the class of this

class(test)
[1] "psych"      "describe"   "data.frame"

but, if I force class of test to be data.frame, it works.

class(test) <- "data.frame"
> test[,2:4]
             n     mean       sd
reason.4  1523 3.385424 1.235895
reason.16 1524 3.394357 1.144226
reason.17 1523 3.804334 1.340193
reason.19 1523 4.789888 1.841050
letter.7  1524 4.946194 1.695769
letter.33 1523 2.785292 1.247544
letter.34 1523 3.376231 1.295250
letter.58 1525 3.264918 1.519552
matrix.45 1523 4.137886 1.355149
matrix.46 1524 2.468504 1.360615
matrix.47 1523 2.588969 1.372887
matrix.55 1524 3.692257 1.557535
rotate.3  1523 4.647406 2.163437
rotate.4  1523 4.766251 2.471225
rotate.6  1523 4.400525 2.559885
rotate.8  1524 4.627953 2.401309

I have no idea why this is happening, but I just added a patch to change the class of the internal object to be data.frame.

This is in version 2.4.2 which you can download from my server at

install.packages("psych", repos="https://personality-project.org/r/",type="source")

emstruong commented 4 months ago

Continued here https://github.com/SachaEpskamp/qgraph/issues/86#issue-2152969969