# R package

tidystats is an R package designed to enable researchers to combine the output of their statistical analyses into a single file that can be shared with others or used to report statistics. This means that to use tidystats, you need to know R. There are many different places to learn R, such as this introduction or this book on using R for Data Science.

Below I explain how the tidystats R package works and provide some example code to help you get started.

## Installation

tidystats is available on CRAN. This means the package can be installed using the following code:

`install.packages("tidystats")`

## Usage

tidystats is designed to have a minimal impact on the way you would normally conduct analyses in R. The idea is that you run the analyses and store the output of each analysis in a variable. You then add these variables to a list and save the list to a file.

The main functions are `add_stats()`

and `write_stats()`

.

`add_stats()`

has two necessary arguments:

`list`

: A list you want to add the statistical output to. You can create an empty list with the`list()`

function.`output`

: The output of a statistical analysis you want to add to the list (e.g., the output of`t.test()`

or`lm()`

)

Optionally, you can also add meta-information about the analysis:

`identifier`

: A name used to identify the output (e.g., "weight_height_correlation"). If you do not provide one, one is automatically created for you based on the name of the variable containing the output.`type`

: A character string specifying the type of analysis as primary, secondary, or exploratory.`preregistered`

: A boolean (`TRUE`

/`FALSE`

) used to indicate whether the analysis was preregistered or not.`notes`

: A character string to add additional information which you may find useful.

`write_stats()`

is the function to save the statistics to a file on your computer. It requires a list containing the output of the analyses as its first argument and a path name to a file on your computer as its second argument (with ".json" as the file extension).

## An example

Below you can find an example of how to use tidystats to combine the output of multiple statistical analyses.

The first step is to load the tidystats package.

`library(tidystats)`

The code below shows how to combine the output of three different types of analyses: a *t*-test, a linear regression, and an ANOVA. The output of each analysis is stored in their own variable, called `sleep_t_test`

, `D9_lm`

and `npk_aov`

, respectively.

```
# t-test
sleep_wide <- reshape(
sleep,
direction = "wide",
idvar = "ID",
timevar = "group",
sep = "_"
)
sleep_t_test <- t.test(
sleep_wide$extra_1,
sleep_wide$extra_2,
paired = TRUE
)
# lm
ctl <- c(4.17, 5.58, 5.18, 6.11, 4.50, 4.61, 5.17, 4.53, 5.33, 5.14)
trt <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69)
group <- gl(2, 10, 20, labels = c("Ctl", "Trt"))
weight <- c(ctl, trt)
D9_lm <- lm(weight ~ group)
# ANOVA
npk_aov <- aov(yield ~ block + N * P * K, npk)
```

After storing the output in variables, create an empty list to collect the statistics of each analysis. Below an empty list is created using `list()`

and stored in a variable called called `statistics`

. Use the `add_stats()`

function to add the output of each analysis to this list. Additional meta-information can be added such as whether the analysis was a primary analysis or preregistered.

```
# Create an empty list to store the statistics in
statistics <- list()
# Add statistics to the list
statistics <- statistics |>
add_stats(sleep_t_test, type = "primary", preregistered = TRUE) |>
add_stats(D9_lm) |>
add_stats(npk_aov, notes = "An ANOVA example")
```

With the output of the the analyses stored in a single list, use the`write_stats()`

function to save the list to a file. Remember to specify the file extension of the file as ".json".

`write_stats(statistics, "statistics.json")`

This produces a file with the output stored in the JSON format. This is a machine-readable format that makes it possible to easily extract statistics using software (like the Microsoft Word add-in). Click here if you want to check out what the file of this example looks like.

## Supported R packages

tidystats has built-in support for a range of functions from various statistical analysis packages. This means that tidystats can automatically extract the statistics that are returned by these functions. See below for an overview of supported functions.

Package | Function |
---|---|

afex | aov_ez(), aov_car(), aov_4(), mixed() |

BayesFactor | generalTestBF(), lmBF(), regressionBF(), ttestBF(), anovaBF(), correlationBF(), contingencyTableBF(), proportionBF(), meta.ttestBF() |

effectsize | cohens_d(), hedges_g(), glass_delta() |

effsize | cohen.d(), VD.A(), cliff.delta() |

emmeans | emmeans(), contrast(), test(), mvcontrast(), eff_size(), emtrends(), joint_tests(), ref_grid() |

Hmisc | rcorr() |

irr | icc() |

lme4 | lmer(), anova() |

lmerTest | lmer(), anova() |

tidystats | count_data(), describe_data() |

stats | anova(), ansari.test(), aov(), bartlett.test(), binom.test(), Box.test(), chisq.test(), confint(), cor.test(), fisher.test(), fligner.test(), friedman.test(), glm(), kruskal.test(), ks.test(), lm(), mantelhaen.test(), mauchly.test(), mcnemar.test(), mood.test(), oneway.test(), pairwise.t.test(), pairwise.prop.test(), pairwise.wilcox.test(), poisson.test(), PP.test(), prop.test(), prop.trend.test(), quade.test(), shapiro.test(), t.test(), var.test(), wilcox.test() |

If a function is missing from the list you can request it to be added by contacting me (see the Support page) or creating an issue on GitHub.

tidystats does not require built-in support for a function for it to be used. You can use tidystats to store and report any statistic. For more on this, see the Tips section.