How to get a report for each group using Wilcoxon test

I am using the reportpackage. I had no problems when doing a t-test, only with Wilcoxon I have problem.
This code gives a report independently of the variable Session. How can I obtain two reports, one for each Session, using a Wilcoxon paired test?
I tried group_by without success and I don't know what to do after split(df, df$Session).

library(tidyverse)
library(report)

df <- tibble(Session = c(rep("1", 10), rep("2", 10)),
             scores = sample(20:70, 20),
             group = c(rep("before", 5), rep("after",5),
                       rep("before", 5), rep("after",5)))

wilcox.test(df$scores ~ df$group, paired = TRUE) |> report()
#> Effect sizes were labelled following Funder's (2019) recommendations.
#> 
#> The Wilcoxon signed rank exact test testing the difference in ranks between
#> df$scores and df$group suggests that the effect is negative, statistically not
#> significant, and small (W = 24.00, p = 0.770; r (rank biserial) = -0.13, 95% CI
#> [-0.68, 0.52])
library(report)

set.seed(42)
d <- data.frame(Session = c(rep("1", 10), rep("2", 10)),
       scores = sample(20:70, 20),
       group = c(rep("before", 5), rep("after",5),
                 rep("before", 5), rep("after",5)))

s1 <- d[which(d$Session == "1"),]
wilcox.test(s1$scores ~s1$group, paired = TRUE) |> report()
#> Effect sizes were labelled following Funder's (2019) recommendations.
#> 
#> The Wilcoxon signed rank exact test testing the difference in ranks between
#> s1$scores and s1$group suggests that the effect is positive, statistically not
#> significant, and small (W = 9.00, p = 0.812; r (rank biserial) = 0.20, 95% CI
#> [-0.64, 0.82])

s2 <- d[which(d$Session == "2"),]
wilcox.test(s2$scores ~s2$group, paired = TRUE) |> report()
#> Effect sizes were labelled following Funder's (2019) recommendations.
#> 
#> The Wilcoxon signed rank exact test testing the difference in ranks between
#> s2$scores and s2$group suggests that the effect is negative, statistically not
#> significant, and very small (W = 7.00, p > .999; r (rank biserial) = -0.07, 95%
#> CI [-0.78, 0.72])

Created on 2023-11-27 with reprex v2.0.2

1 Like

Suppose that I have 10 Sessions. Is it possible not to repeat the code and use purr::map?

Probably. For some reason, I ran into a problem trying to write a function to do that, but I didn’t give it much effort. Why don’t you have a go and report back.

1 Like

Thank you. Maybe something like this:

library(tidyverse)
library(report)


set.seed(42)
df <- tibble(Session = c(rep("1", 10), rep("2", 10)),
             scores = sample(20:70, 20),
             group = c(rep("before", 5), rep("after",5),
                       rep("before", 5), rep("after",5)))

wil <- function(sess) {
  sess <- as.character(sess)
  s1 <- df[which(df$Session == sess),]
  wilcox.test(s1$scores ~ s1$group, paired = TRUE) |> report()
  }

results <- unique(df$Session) |> map(wil) |> unlist()
results

Looks good, subject to tweaking for whatever the next step is.

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.