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

polyserial correlations not working in correlation() #261

Open
shirdekel opened this issue Sep 12, 2022 · 3 comments
Open

polyserial correlations not working in correlation() #261

shirdekel opened this issue Sep 12, 2022 · 3 comments

Comments

@shirdekel
Copy link

library(correlation)

d <- data.frame(
  x = sample(1:5, 20, TRUE),
  y = as.ordered(sample(letters[1:5], 20, TRUE))
)

# Works

cor_test(d, "x", "y", method = "polychoric")
#> Parameter1 | Parameter2 |  rho |        95% CI | t(18) |     p
#> --------------------------------------------------------------
#> x          |          y | 0.11 | [-0.35, 0.53] |  0.48 | 0.640
#> 
#> Observations: 20

# Doesn't work

correlation(d, method = "polychoric")
#> Warning: It seems like there is not enough continuous variables in your data.
#> Maybe you want to include the factors? We're setting `include_factors=TRUE` for
#> you.
#> Error: No random effects terms specified in formula

Created on 2022-09-12 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.1 (2022-06-23)
#>  os       macOS Big Sur ... 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_AU.UTF-8
#>  ctype    en_AU.UTF-8
#>  tz       Australia/Sydney
#>  date     2022-09-12
#>  pandoc   2.19 @ /usr/local/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  admisc        0.29    2022-06-20 [1] CRAN (R 4.2.0)
#>  bayestestR    0.12.1  2022-05-02 [1] CRAN (R 4.2.0)
#>  boot          1.3-28  2021-05-03 [1] CRAN (R 4.2.1)
#>  cli           3.3.0   2022-04-25 [1] CRAN (R 4.2.0)
#>  correlation * 0.8.2   2022-08-09 [1] CRAN (R 4.2.0)
#>  datawizard    0.5.1   2022-08-17 [1] CRAN (R 4.2.0)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.2.0)
#>  evaluate      0.16    2022-08-09 [1] CRAN (R 4.2.0)
#>  fansi         1.0.3   2022-03-24 [1] CRAN (R 4.2.0)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.0)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.2.0)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.2.0)
#>  htmltools     0.5.3   2022-07-18 [1] CRAN (R 4.2.0)
#>  insight       0.18.2  2022-08-10 [1] CRAN (R 4.2.0)
#>  knitr         1.39    2022-04-26 [1] CRAN (R 4.2.0)
#>  lattice       0.20-45 2021-09-22 [1] CRAN (R 4.2.1)
#>  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.2.0)
#>  lme4          1.1-30  2022-07-08 [1] CRAN (R 4.2.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
#>  MASS          7.3-57  2022-04-22 [1] CRAN (R 4.2.1)
#>  Matrix        1.4-1   2022-03-23 [1] CRAN (R 4.2.1)
#>  minqa         1.2.4   2014-10-09 [1] CRAN (R 4.2.0)
#>  mnormt        2.1.0   2022-06-07 [1] CRAN (R 4.2.0)
#>  nlme          3.1-157 2022-03-25 [1] CRAN (R 4.2.1)
#>  nloptr        2.0.3   2022-05-26 [1] CRAN (R 4.2.0)
#>  pillar        1.8.1   2022-08-19 [1] CRAN (R 4.2.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
#>  polycor       0.8-1   2022-01-11 [1] CRAN (R 4.2.0)
#>  psych         2.2.5   2022-05-10 [1] CRAN (R 4.2.0)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.2.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.2.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.0  2022-06-28 [1] CRAN (R 4.2.0)
#>  Rcpp          1.0.9   2022-07-08 [1] CRAN (R 4.2.0)
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.2.0)
#>  rlang         1.0.4   2022-07-12 [1] CRAN (R 4.2.0)
#>  rmarkdown     2.14    2022-04-25 [1] CRAN (R 4.2.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
#>  stringi       1.7.8   2022-07-11 [1] CRAN (R 4.2.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.2.0)
#>  styler        1.7.0   2022-03-13 [1] CRAN (R 4.2.0)
#>  tibble        3.1.8   2022-07-22 [1] CRAN (R 4.2.0)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.2.0)
#>  vctrs         0.4.1   2022-04-13 [1] CRAN (R 4.2.0)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
#>  xfun          0.31    2022-05-10 [1] CRAN (R 4.2.0)
#>  yaml          2.3.5   2022-02-21 [1] CRAN (R 4.2.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.2/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Less important but perhaps related, why do I need to set include_factors=TRUE for correlation() and not for cor_test()?

@briana-new
Copy link

Has this issue been addressed? I am also experiencing issues with the polyserial calculation using correlation(). Not sure if it is a me issue or a bug

@mattansb
Copy link
Member

mattansb commented Sep 5, 2023

This issue has not yet been addressed - a major update to the package is in the works for this coming year (that will also address this issue) and all efforts are going towards that.

@jmgirard
Copy link

Would be great to revisit this.

set.seed(2024)

o <-
  sample(
    LETTERS[1:4],
    size = 100,
    replace = TRUE
  ) |>
  factor(
    levels = LETTERS[1:4],
    ordered = TRUE
  )

x <- rnorm(100)

d <- data.frame(o, x)

correlation::correlation(d, include_factors = TRUE, method = "auto")
#> For i = 2 j = 1  A cell entry of 0 was replaced with correct =  0.5.  Check your data!
#> For i = 2 j = 1  A cell entry of 0 was replaced with correct =  0.5.  Check your data!
#> For i = 2 j = 1  A cell entry of 0 was replaced with correct =  0.5.  Check your data!
#> For i = 2 j = 1  A cell entry of 0 was replaced with correct =  0.5.  Check your data!
#> For i = 2 j = 1  A cell entry of 0 was replaced with correct =  0.5.  Check your data!
#> For i = 2 j = 1  A cell entry of 0 was replaced with correct =  0.5.  Check your data!
#> # Correlation Matrix (auto-method)
#> 
#> Parameter1 | Parameter2 |       rho |         95% CI |  t(98) |         p
#> -------------------------------------------------------------------------
#> o.A        |        o.B |     -0.75 | [-0.82, -0.64] | -11.09 | < .001***
#> o.A        |        o.C |     -0.72 | [-0.80, -0.61] | -10.32 | < .001***
#> o.A        |        o.D |     -0.81 | [-0.87, -0.72] | -13.50 | < .001***
#> o.A        |          x |      0.02 | [-0.17,  0.22] |   0.24 | > .999   
#> o.B        |        o.C |     -0.59 | [-0.71, -0.45] |  -7.28 | < .001***
#> o.B        |        o.D |     -0.69 | [-0.78, -0.57] |  -9.41 | < .001***
#> o.B        |          x | -9.54e-03 | [-0.21,  0.19] |  -0.09 | > .999   
#> o.C        |        o.D |     -0.66 | [-0.76, -0.54] |  -8.78 | < .001***
#> o.C        |          x |     -0.01 | [-0.21,  0.18] |  -0.12 | > .999   
#> o.D        |          x | -6.68e-03 | [-0.20,  0.19] |  -0.07 | > .999   
#> 
#> p-value adjustment method: Holm (1979)
#> Observations: 100

polycor::polyserial(x, o)
#> [1] -0.02025863

Created on 2024-08-15 with reprex v2.1.1

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

4 participants