easystats / parameters

:bar_chart: Computation and processing of models' parameters
https://easystats.github.io/parameters/
GNU General Public License v3.0
427 stars 36 forks source link

FR: Add group medians for Wilcoxon rank sum test in `model_parameters` #492

Open iago-pssjd opened 3 years ago

iago-pssjd commented 3 years ago

As model_parameters shows group means for Welch two-sample test, it could also show group medians for Wilcoxon rank sum test.

IndrajeetPatil commented 3 years ago

Note that parameters only returns details contained in the underlying object.

It returns group means for Welch two-sample test because the underlying object contains them.

library(parameters)

x <- c(1.83,  0.50,  1.62,  2.48, 1.68, 1.88, 1.55, 3.06, 1.30)
y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29)

mod1 <- t.test(x, y, paired = TRUE)

mod1
#> 
#>  Paired t-test
#> 
#> data:  x and y
#> t = 3.0354, df = 8, p-value = 0.01618
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#>  0.1037787 0.7599991
#> sample estimates:
#> mean of the differences 
#>               0.4318889
parameters(mod1)
#> Paired t-test
#> 
#> Parameter | Group | Difference | t(8) |     p |       95% CI
#> ------------------------------------------------------------
#> x         |     y |       0.43 | 3.04 | 0.016 | [0.10, 0.76]

But it doesn't do so for Wilcoxon rank sum test because the object doesn't contain them:

mod2 <- wilcox.test(x, y, paired = TRUE)

mod2
#> 
#>  Wilcoxon signed rank exact test
#> 
#> data:  x and y
#> V = 40, p-value = 0.03906
#> alternative hypothesis: true location shift is not equal to 0
parameters(mod2)
#> Wilcoxon signed rank exact test
#> 
#> Parameter1 | Parameter2 |     W |     p
#> ---------------------------------------
#> x          |          y | 40.00 | 0.039

You can always add effect size information for these object though:

parameters(mod1, hedges_g = TRUE)
#> Paired t-test
#> 
#> Parameter | Group | Difference |       95% CI | t(8) | Hedges_g |     g 95% CI |     p
#> --------------------------------------------------------------------------------------
#> x         |     y |       0.43 | [0.10, 0.76] | 3.04 |     0.91 | [0.17, 1.73] | 0.016
parameters(mod2, rank_biserial = TRUE)
#> Wilcoxon signed rank exact test
#> 
#> Parameter1 | Parameter2 |     W | r_rank_biserial | rank_biserial 95% CI |     p
#> --------------------------------------------------------------------------------
#> x          |          y | 40.00 |            0.78 |         [0.33, 1.00] | 0.039

Created on 2021-05-13 by the reprex package (v2.0.0)

IndrajeetPatil commented 3 years ago

Reopening in light of https://github.com/easystats/report/issues/189

This will depend on whether we can recover data from the htest object, which is not always guaranteed.

mattansb commented 3 years ago

Something along these lines:

x <- c(0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46)
y <- c(1.15, 0.88, 0.90, 0.74, 1.21)

## wilcox.test ----

model <- wilcox.test(x, y)

data <- insight::get_data(model)
desc <- NULL
if (!is.null(data)) {
  desc <- as.data.frame(t(sapply(data, function(d) {
    data.frame(Median = median(d, na.rm = TRUE),
               MAD = mad(d, na.rm = TRUE))
  })))
}
desc
#>   Median      MAD
#> x  1.415 0.630105
#> y    0.9 0.237216

## wilcox.test ----

model <- t.test(x, y)

data <- insight::get_data(model)
desc <- NULL
if (!is.null(data)) {
  desc <- as.data.frame(t(sapply(data, function(d) {
    data.frame(Mean = mean(d, na.rm = TRUE),
               SD = sd(d, na.rm = TRUE))
  })))
}
desc
#>    Mean        SD
#> x 1.313 0.4412117
#> y 0.976 0.1973069

Created on 2021-06-17 by the reprex package (v2.0.0)