knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(statstring)

Using statstring to format ANOVA outputs

statstring is a package to facilitate formatting ANOVA outputs in APA style.

You can pass a summary object to the format_anova_string() function to extract and format an ANOVA result.

For instance, using the stats::aov() function:

data("warpbreaks")
res <- summary(stats::aov(breaks~wool*tension, data = warpbreaks))
str_out <- format_anova_string(res)

There was a significant interaction between tension and wool on breaks, r str_out.

Using rstatix::get_anova_table() function:

res <- rstatix::get_anova_table(rstatix::anova_test(breaks~wool*tension, 
                                                    data = warpbreaks, type = 3,
                                                    effect.size = "pes"))
str_out <- format_anova_string(res)

There was a significant interaction between tension and wool on breaks, r str_out.

Using apaTables::apa.aov.table() function (also specifying a particular independent variable):

res <- apaTables::apa.aov.table(lm(breaks~wool*tension, data = warpbreaks), type = 3)
interaction_out <- format_anova_string(res)

wool_out <- format_anova_string(res, predictor = "wool") #Specify the name of the predictor
tension_out <- format_anova_string(res, predictor = 3) #Can also specify the row of the predictor

There was a significant interaction between tension and wool on breaks, r interaction_out. There was a main effect of wool on breaks, r wool_out. There was a main effect of tension on breaks, r tension_out.

Using statstring to format t-test outputs

You can use statstring to format t-test results as well.

indep_test <- stats::t.test(warpbreaks$breaks~warpbreaks$wool)
indep_str <- format_tstat_apa(indep_test)

onesample_test <- stats::t.test(warpbreaks$breaks)
onesample_str <- format_tstat_apa(onesample_test)

There was not a significant difference in breaks by wool type, r indep_str. The number of breaks differed from zero, r onesample_str.

You can also use the outputs of a pairwise comparison directly when formatting a t-statistic

mod <- lm(breaks~wool*tension, warpbreaks)
marginal_means <- emmeans::emmeans(mod, c("wool", "tension"))
pairwise_comps <- data.frame(pairs(marginal_means, adjust="tukey"))
cis <- data.frame(confint(pairs(marginal_means, adjust="tukey")))
cis <- cis[,c("contrast", "lower.CL", "upper.CL")]
pairwise_comps <- dplyr::left_join(pairwise_comps, cis, by = "contrast")

pairwise_comps["stat_string"] <- mapply(format_pairwise_comparison,
                                        t_stat = pairwise_comps$t.ratio,
                                        df = pairwise_comps$df,
                                        p_val= pairwise_comps$p.value,
                                        mdiff = pairwise_comps$estimate,
                                        lci=pairwise_comps$lower.CL,
                                        uci = pairwise_comps$upper.CL)

Example output: r pairwise_comps$stat_string[1]



silverer/statstring documentation built on Aug. 6, 2023, 11:48 a.m.