Tips
Use the |>
operator to add multiple statistical tests to a list
I designed the add_stats()
function such that you can use the |>
operator to add multiple tests to a list, like so:
statistics <- statistics |>
add_stats(sleep_t_test, type = "primary", preregistered = TRUE) |>
add_stats(D9_lm) |>
add_stats(npk_aov, notes = "An ANOVA example")
Alternatively, you can also do it without the operator:
statistics <- add_stats(
statistics,
sleep_t_test, type = "primary", preregistered = TRUE
)
statistics <- add_stats(statistics, lm_D9)
statistics <- add_stats(statistics, npk_aov, notes = "An ANOVA example")
In the latter case, you have to repeat the assignment and the first argument each time you call add_stats()
. You do not need to do this when you use the |>
operator, saving you some time and making your code easier to read.
Use the custom_stats()
and custom_stat()
functions from the R package to store statistics from unsupported functions
The normal tidystats workflow consists of running statistical functions such as lm()
, saving the output into variables, and then using the add_stats()
function to add the statistics to a list. This works as long as tidystats has built-in support for the statistics function.
If there is no support for a particular function you want to save the statistics of, you can use the custom_stats()
and custom_stat()
functions to still store the statistics. Below is an example of how to use these functions.
library(tidystats)
# Run a regression and calculate the AIC and BIC
lm1 <- lm(Fertility ~ ., data = swiss)
aic <- AIC(lm1)
bic <- BIC(lm1)
# Create a custom stats variable containing the AIC and BIC values
aic_bic <- custom_stats(
method = "AIC and BIC",
statistics = c(
custom_stat(name = "AIC", value = aic),
custom_stat(name = "BIC", value = bic)
)
)
# Create an empty statistics list and add the AIC and BIC statistics
statistics <- list()
statistics <- add_stats(statistics, aic_bic)
write_stats(statistics, "custom-stats.json")