report icon indicating copy to clipboard operation
report copied to clipboard

Cannot use custom rule when interpreting the effect size of a t-test

Open fkohrt opened this issue 6 months ago • 0 comments

Describe the bug

A t-test cannot be reported with a custom rule to interpret the effect size.

To Reproduce

When reporting a t-test and supplying a custom rule...

t.test(formula = extra ~ group, data = sleep) |>
    report::report(rules = effectsize::rules(1, c("tiny", "yeah okay")))

...the following error appears:

Error:
! 'rules' must be 'cohen1988', 'sawilowsky2009', 'lovakov2021', 'gignac2016' or an object of type
  'rules'.
Backtrace:
     ▆
  1. ├─report::report(...)
  2. └─report:::report.htest(...)
  3.   ├─report::report_table(x, model_info = model_info, ...)
  4.   └─report:::report_table.htest(x, model_info = model_info, ...)
  5.     ├─base::do.call(report_effectsize, args)
  6.     ├─report (local) `<fn>`(`<htest>`, model_info = `<named list>`, rules = `<rules>`)
  7.     └─report:::report_effectsize.htest(...)
  8.       └─report:::.report_effectsize_ttest(x, table, dot_args)
  9.         ├─base::do.call(effectsize::interpret, es_args)
 10.         ├─effectsize (local) `<fn>`(`<effctsz_[,4]>`, rules = `<list>`)
 11.         └─effectsize:::interpret.effectsize_table(`<effctsz_[,4]>`, rules = `<list>`)
 12.           └─effectsize::interpret_cohens_d(value, rules = rules)
 13.             └─effectsize:::.match.rules(...)
 14.               └─insight::format_error(...)
 15.                 └─insight::format_alert(..., type = "error")

More specifically, the following call to .report_effectsize_ttest() elicits the error message:

report:::.report_effectsize_ttest(
  x = t.test(
    formula = extra ~ group,
    data = sleep
  ),
  table = table,
  dot_args = list(
    data = sleep,
    rules = effectsize::rules(1, c("tiny", "yeah okay"))
  )
)

Expected behaviour

The custom rule being used without any problem.

Additional information

There is something wrong with the assignment to dot_args$rules within .report_effectsize_ttest, where the custom rule gets erroneously overwritten:

https://github.com/easystats/report/blob/69e8c5ffb9ad494683c769f46974fbff794f2fc4/R/report_htest_ttest.R#L46-L51

Indeed, running...

ifelse(FALSE, "cohen1988", effectsize::rules(1, c("tiny", "yeah okay")))

...gets

[[1]]
[1] 1

The problem is that test and yes have length 1, but no has length 2 in this case. ifelse() silently only returns the first element of no. Using a safer variant of ifelse() would have thrown an error message, e.g., compare

data.table::fifelse(FALSE, "cohen1988", effectsize::rules(1, c("tiny", "yeah okay")))
Error in `data.table::fifelse()`:
! Length of 'no' is 2 but must be 1 or length of 'test' (1).
Backtrace:
    ▆
 1. └─data.table::fifelse(...)

fkohrt avatar Aug 20 '24 23:08 fkohrt