papaja icon indicating copy to clipboard operation
papaja copied to clipboard

apa_num() options specified in apa_print() don't apply to the whole the output

Open jakub-jedrusiak opened this issue 2 years ago • 1 comments

Describe the bug apa_num() options specified in apa_print(), like decimal.mark and gt1, don't apply to the whole the output, especially to the p-values.

To Reproduce

library(papaja)
model <- lm(Sepal.Length ~ Species, data = iris)
anova(model) |> apa_print(decimal.mark = ",", gt1 = TRUE)

Output

$estimate
$estimate$Species
[1] "$\\hat{\\eta}^2_G = 0,401$, 90\\% CI $[0,300, 0,485]$"


$statistic
$statistic$Species
[1] "$F(2, 147) = 49.16$, $p < .001$"


$full_result
$full_result$Species
[1] "$F(2, 147) = 49.16$, $p < .001$, $\\hat{\\eta}^2_G = 0,401$, 90\\% CI $[0,300, 0,485]$"


$table
A data.frame with 7 labelled columns:

     term estimate       conf.int statistic df df.residual p.value
1 Species    0,401 [0,300, 0,485]     49.16  2         147  < .001

Expected behavior I want all leading zeros to be displayed and all decimal marks changed to a comma which isn't the case with e.g. p-values and F-ratios but works well in other parts of the output.

Additional context Makes apa_print() nonadjustable for different languages' implementations of the APA style.

jakub-jedrusiak avatar Apr 22 '22 16:04 jakub-jedrusiak

Hi Jakub, thanks for raising the issue and providing an example. This is related to https://github.com/crsh/papaja/issues/506.

In some sense, this is beyond the scope of papaja and so there is currently no good way to adjust the type setting of all statistics. We support changes to the estimates because their typesetting depends on the outcome variable (e.g., leading zero, number of digits). That said, there is a mid- to long-term aim of abstracting the typesetting away and allowing users to specify their own reporting style. I should note, though, that there are several other big milestones with higher priority.

It is, however, straight forward to do what you need with a little bit of regex and knowledge of papaja internals:

adjust_reporting_style <- function(x) {
  stopifnot(inherits(x, "apa_results"))
  
  x$table |> 
    dplyr::mutate(
      statistic = gsub("\\.", ",", statistic)
      , p.value = gsub("\\.", "0,", p.value)
    ) |> 
    (\(x) {
      glue_apa_results(
        x
        , est_glue = papaja:::construct_glue(x, "estimate")
        , stat_glue = papaja:::construct_glue(x, "statistic")
      )
    })()
}


my_print <- function(...) {
  papaja::apa_print(...) |> 
    adjust_reporting_style()
}

anova(model) |> 
  my_print(decimal.mark = ",", gt1 = TRUE)

See the vignette extending_apa_print for some additional information (vignette("extending_apa_print", package = "papaja"), note that you need to set build_vignettes = TRUE when installing papaja).

This may also be of interest to @JeffreyRStevens.

crsh avatar Apr 25 '22 08:04 crsh