---
title: "Statistical test workflows"
output: rmarkdown::pdf_document
vignette: >
  %\VignetteIndexEntry{Statistical test workflows}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, echo = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = TRUE)
```

## Introduction

`testflow` provides statistical testing workflows organized by study design.

## One numerical variable

```{r}
library(testflow)
cardio <- make_cardio_data()
test_one_sample(cardio, sbp_3m, mu = 140)
```

## Two independent groups

```{r}
test_two_groups(sbp_3m ~ sex, data = cardio)
```

## Paired measurements

```{r}
test_paired(sbp_3m ~ sbp_baseline, data = cardio)
```

## More than two groups

```{r}
test_groups(sbp_3m ~ treatment, data = cardio)
```

## Factorial designs

```{r}
test_factorial(sbp_3m ~ sex * treatment, data = cardio)
```

## Repeated measurements

```{r}
test_repeated(cardio, c(sbp_baseline, sbp_3m, sbp_6m), id = id)
```

The repeated numeric workflow chooses repeated-measures ANOVA when the
within-time normality checks are acceptable and Friedman otherwise. Post-hoc
comparisons are paired t-tests for the parametric branch and paired Wilcoxon
tests for the non-parametric branch.

## Categorical outcomes

```{r}
test_categorical(treatment ~ controlled_3m, data = cardio)
```

## Repeated categorical outcomes

```{r}
test_repeated_categorical(cardio, c(controlled_baseline, controlled_3m, controlled_6m))
```

The repeated categorical workflow uses Cochran Q for binary repeated outcomes
and pairwise McNemar tests for follow-up comparisons.

## References

- Fisher, R. A. (1925). \emph{Statistical Methods for Research Workers}.
- Gosset, W. S. (1908). The probable error of a mean.
- Welch, B. L. (1947). Generalization of Student's problem with unequal variances.
- Wilcoxon, F. (1945). Individual comparisons by ranking methods.
- Mann, H. B., & Whitney, D. R. (1947). On a test of whether one of two random variables is stochastically larger than the other.
- Levene, H. (1960). Robust tests for equality of variances.
- Kruskal, W. H., & Wallis, W. A. (1952). Use of ranks in one-criterion variance analysis.
- Tukey, J. W. (1949). Comparing individual means in the analysis of variance.
- Dunn, O. J. (1964). Multiple comparisons using rank sums.
- Friedman, M. (1937). The use of ranks to avoid the assumption of normality implicit in the analysis of variance.
- Cochran, W. G. (1950). The comparison of percentages in matched samples.
- McNemar, Q. (1947). Note on the sampling error of the difference between correlated proportions or percentages.
- Pearson, K. (1895, 1900).
- Spearman, C. (1904). The proof and measurement of association between two things.
- Kendall, M. G. (1938). A new measure of rank correlation.
- Cramer, H. (1946). \emph{Mathematical Methods of Statistics}.
- Clopper, C. J., & Pearson, E. S. (1934). The use of confidence or fiducial limits illustrated in the case of the binomial.
- Cohen, J. (1988). \emph{Statistical Power Analysis for the Behavioral Sciences}.

## Correlation

```{r}
test_correlation(sbp_3m ~ age, data = cardio)
```

## Outliers

```{r}
test_outliers(c(sbp_3m, ldl, crp), data = cardio)
```

## Reporting and plotting

Every workflow returns a `testflow` object. Use `report(x)`, `plot(x)`, and `as_tibble(x)`.
See `effect-size-formulas.Rmd` for the exact formulas used by the reported
effect-size estimates.
