Skip to content

parameters:::.extract_htest_correlation should always return CI info if the htest object provides it #1003

Open
@DarwinAwardWinner

Description

@DarwinAwardWinner

Currently, parameters:::.extract_htest_correlation only returns confidence intervals for method = "pearson", which makes sense since the default cor.test only returns CIs for this method. However, I implemented an alternative Spearman correlation test that does return CIs, but parameters::parameters does not return the CI in the table (see below reprex). It would be nice if instead of checking the method used, parameters:::.extract_htest_correlation would always check for a conf.int in the htest object and use it if it exists.

library(DescTools)
library(parameters)
library(assertthat)

spearman.test.with.ci <- function(x, y, ..., conf.level = 0.95) {
  res <- cor.test(x, y, method = "spearman", ...)
  scor <- DescTools::SpearmanRho(x,y, conf.level = conf.level)
  if (all(c("lwr.ci", "upr.ci") %in% names(scor))) {
    ci_vec <- unname(scor[c("lwr.ci", "upr.ci")])
    attr(ci_vec, "conf.level") <- conf.level
    res$conf.int <- ci_vec
  }
  res
}

set.seed(1986)
x <- rnorm(500)
y <- rnorm(500)

ht_pearson <- cor.test(x, y, method = "pearson")
ht_spearman <- cor.test(x, y, method = "spearman")
## Same as above, but computes a CI
ht_spearman_ci <- spearman.test.with.ci(x, y)
print(ht_spearman_ci)
#> 
#>  Spearman's rank correlation rho
#> 
#> data:  x and y
#> S = 20578916, p-value = 0.7853
#> alternative hypothesis: true rho is not equal to 0
#> 95 percent confidence interval:
#>  -0.07556340  0.09979184
#> sample estimates:
#>        rho 
#> 0.01220808
## Object indeed contains CI
assert_that("conf.int" %in% names(ht_spearman_ci))
#> [1] TRUE

ci_colnames <- c("CI", "CI_low", "CI_high")

## Pearson has CI columns
print(parameters(ht_pearson))
#> Pearson's product-moment correlation
#> 
#> Parameter1 | Parameter2 |        r |        95% CI | t(498) |     p
#> -------------------------------------------------------------------
#> x          |          y | 5.20e-03 | [-0.08, 0.09] |   0.12 | 0.908
#> 
#> Alternative hypothesis: true correlation is not equal to 0
assert_that(all(ci_colnames %in% colnames(parameters(ht_pearson))))
#> [1] TRUE

## Regular Spearman test does not have CI columns
print(parameters(ht_spearman))
#> Spearman's rank correlation rho
#> 
#> Parameter1 | Parameter2 |  rho |        S |     p
#> -------------------------------------------------
#> x          |          y | 0.01 | 2.06e+07 | 0.785
#> 
#> Alternative hypothesis: true rho is not equal to 0
assert_that(!any(ci_colnames %in% colnames(parameters(ht_spearman))))
#> [1] TRUE

## Modified Spearman test should have CI columns but doesn't
print(parameters(ht_spearman_ci))
#> Spearman's rank correlation rho
#> 
#> Parameter1 | Parameter2 |  rho |        S |     p
#> -------------------------------------------------
#> x          |          y | 0.01 | 2.06e+07 | 0.785
#> 
#> Alternative hypothesis: true rho is not equal to 0
assert_that(all(ci_colnames %in% colnames(parameters(ht_spearman_ci))))
#> Error: Elements 1, 2, 3 of ci_colnames %in% colnames(parameters(ht_spearman_ci)) are not true

Created on 2024-08-07 with reprex v2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions