Author: Josie Athens
Date: 2025-10-10

# Introduction to the pubh package

#### Josie Athens

#### 2025-10-10

# 1 Introduction

## 1.1 Rationale for the package

There has been a long relationship between Epidemiology / Public Health and Biostatistics. Students frequently find introductory textbooks explaining statistical methods and the math behind them, but how to implement those techniques on a computer is rarely explained.

One of the most popular statistical software’s in public health settings is `Stata`. `Stata` has the advantage of offering a solid interface with functions that can be accessed via the use of commands or by selecting the proper input in the graphical unit interface (GUI). Furthermore, `Stata` offers “Grad Plans” to postgraduate students, making it relatively affordable from an economic point of view.

`R` is a free alternative to `Stata`. Its use keeps growing, and its popularity is also increasing due to the relatively high number of packages and textbooks available.

In epidemiology, some good packages are already available for `R`, including: `Epi`, `epibasix`, `epiDisplay`, `epiR` and `epitools`. The `pubh` package does not intend to replace any of them, but to only provide a standard syntax for the most frequent statistical analysis in epidemiology.

## 1.2 Syntax: the use of formulas

Most students and professionals from the disciplines of Epidemiology and Public Health analyse variables in terms of outcome, exposure and confounders. The following table shows the most common names used in the literature to characterise variables in a cause-effect relationships:

Response variable | Explanatory variable(s)
--- | ---
Outcome | Exposure and confounders
Outcome | Predictors
Dependent variable | Independent variable(s)
y | x

In `R`, `formulas` declare relationships between variables. Formulas are also common in classic statistical tests and regression methods.

Formulas have the following standard syntax:

    y ~ x, data = my_data

Where `y` is the outcome or response variable, `x` is the exposure or predictor of interest, and `my_data` specifies the data frame’s name where `x` and `y` can be found.

The symbol `~` is used in `R` for formulas. It can be interpreted as depends on. In the most typical scenario, `y ~ x` means `y` depends on `x` or `y` is a function of `x`:

`y = f(x)`

Using epidemiology friendly terms:

`Outcome = f(Exposure)`

It is worth noting that `Stata` requires variables to be given in the same order as in formulas: outcome first, predictors next.

The `pubh` package integrates well with other packages of the `tidyverse` which use formulas and the pipe operator `|>` to add layers over functions. In particular, `pubh` uses `ggformula` as a graphical interface for plotting and takes advantage of variable labels from `sjlabelled`. This versatility allows it to interact also with tables from `crosstable`.

### 1.2.1 Stratification

One way to control for confounders is the use of stratification. In `Stata`, stratification is done by including the `by` option as part of the command. In the `ggformula` package, one way of doing stratification is with a formula like:

    y ~ x|z, data = my_data

Where `y` is the outcome or response variable, `x` is the exposure or predictor of interest, `z` is the stratification variable (a factor) and `my_data` specifies the name of the data frame where `x`, `y` and `z` can be found.

## 1.3 Pipes and the `tidyverse`

The `tidyverse` has become now the standard in data manipulation in `R`. The use of the pipe function `|>` allows for cleaner code. The principle of pipes is to change the paradigm in coding. In standard codding, when many functions are used, one goes from inner parenthesis to outer ones.

For example if we have three functions, a common syntax would look like:

`f3(f2(f1(..., data), ...), ...)`

With pipes, however, the code reads top to bottom and left to right:

    data |>
      f1(...) |>
      f2(...) |>
      f3(...)

Most of the functions from `pubh` are compatible with pipes and the `tidyverse`.

## 1.4 Contributions of the `pubh` package

The main contributions of the `pubh` package to students and professionals from the disciplines of Epidemiology and Public Health are:

1. Use of a common syntax for the most used analysis.
2. A function, `glm_coef` that displays coefficients from most common regression analysis in a way that can be easy interpreted and used for publications.

# 2 Descriptive statistics

There are many options currently available for displaying descriptive statistics. I recommend the function `crosstable` from the `crosstable` package for constructing tables of descriptive statistics where results need to be stratified.

The `estat` function from the `pubh` package displays descriptive statistics of continuous outcomes; it can use labels to display nice tables. To aid in understanding the variability, `estat` also shows the relative dispersion (coefficient of variation), which is particularly interesting for comparing variances between groups (factors).

Some examples. We start by loading packages.

    rm(list = ls())
    library(dplyr)
    library(rstatix)
    library(pubh)
    library(sjlabelled)

We will use a data set about a study of onchocerciasis in Sierra Leone.

    data(Oncho)
    Oncho |> head()

id | mf | area | agegrp | sex | mfload | lesions
--- | --- | --- | --- | --- | --- | ---
1 | Infected | Savannah | 20-39 | Female | 1 | No
2 | Infected | Rainforest | 40+ | Male | 3 | No
3 | Infected | Savannah | 40+ | Female | 1 | No
4 | Not-infected | Rainforest | 20-39 | Female | 0 | No
5 | Not-infected | Savannah | 40+ | Female | 0 | No
6 | Not-infected | Rainforest | 20-39 | Female | 0 | No

A two-by-two contingency table:

    Oncho |>
      mutate(
        mf = relevel(mf, ref = "Infected")
      ) |>
      copy_labels(Oncho) |>
      select(mf, area) |>
      cross_tbl(by = "area") |>
      theme_pubh(2)

| Residence |
--- | --- | ---
| Savannah N = 548 | Rainforest N = 754 | Overall N = 1,302
Infection |  |  |
Infected | 281 (51%) | 541 (72%) | 822 (63%)
Not-infected | 267 (49%) | 213 (28%) | 480 (37%)

Table with all descriptive statistics except `id` and `mfload`:

    Oncho |>
      select(- c(id, mfload)) |>
      mutate(
        mf = relevel(mf, ref = "Infected")
      ) |>
      copy_labels(Oncho) |>
      cross_tbl(by = "area") |>
      theme_pubh(2)

| Residence |
--- | --- | ---
| Savannah N = 548 | Rainforest N = 754 | Overall N = 1,302
Infection |  |  |
Infected | 281 (51%) | 541 (72%) | 822 (63%)
Not-infected | 267 (49%) | 213 (28%) | 480 (37%)
Age group (years) |  |  |
5-9 | 93 (17%) | 109 (14%) | 202 (16%)
10-19 | 72 (13%) | 146 (19%) | 218 (17%)
20-39 | 208 (38%) | 216 (29%) | 424 (33%)
40+ | 175 (32%) | 283 (38%) | 458 (35%)
Sex |  |  |
Male | 247 (45%) | 369 (49%) | 616 (47%)
Female | 301 (55%) | 385 (51%) | 686 (53%)
Severe eye lesions? | 67 (12%) | 134 (18%) | 201 (15%)

Next, we use a data set about blood counts of T cells from patients in remission from Hodgkin’s disease or in remission from disseminated malignancies. We generate the new variable `Ratio` and add labels to the variables.

    data(Hodgkin)
    Hodgkin <- Hodgkin |>
      mutate(Ratio = CD4/CD8) |>
      var_labels(
        Ratio = "CD4+ / CD8+ T-cells ratio"
        )

    Hodgkin |> head()

CD4 | CD8 | Group | Ratio
--- | --- | --- | ---
396 | 836 | Hodgkin | 0.474
568 | 978 | Hodgkin | 0.581
1212 | 1678 | Hodgkin | 0.722
171 | 212 | Hodgkin | 0.807
554 | 670 | Hodgkin | 0.827
1104 | 1335 | Hodgkin | 0.827

Descriptive statistics for CD4+ T cells:

    Hodgkin |>
      estat(~ CD4)

| N | Min | Max | Mean | Median | SD | CV
--- | --- | --- | --- | --- | --- | --- | ---
CD4+ T-cells | 40 | 116 | 2.42e+03 | 673 | 528 | 470 | 0.7

In the previous code, the left-hand side of the formula is empty as it’s the case when working with only one variable.

For stratification, `estat` recognises the following two syntaxes:

- `outcome ~ exposure`
- `~ outcome | exposure`

where, `outcome` is continuous and `exposure` is a categorical (`factor`) variable.

For example:

    Hodgkin |>
      estat(~ Ratio|Group)

| Disease | N | Min | Max | Mean | Median | SD | CV
--- | --- | --- | --- | --- | --- | --- | --- | ---
CD4+ / CD8+ T-cells ratio | Non-Hodgkin | 20 | 1.1 | 3.49 | 2.12 | 2.15 | 0.73 | 0.34
| Hodgkin | 20 | 0.47 | 3.82 | 1.5 | 1.19 | 0.91 | 0.61

As before, we can report a table of descriptive statistics for all variables in the data set:

    Hodgkin |>
      mutate(
        Group = relevel(Group, ref = "Hodgkin")
      ) |>
      copy_labels(Hodgkin) |>
      cross_tbl(by = "Group", bold = FALSE) |>
      theme_pubh(2) |>
      add_footnote("Median (IQR)", font_size = 9)

| Disease |
--- | --- | ---
| Hodgkin N = 20 | Non-Hodgkin N = 20 | Overall N = 40
CD4+ T-cells | 682 (397, 1,158) | 433 (345, 718) | 529 (375, 930)
CD8+ T-cells | 448 (299, 824) | 232 (147, 325) | 319 (206, 601)
CD4+ / CD8+ T-cells ratio | 1.19 (0.83, 2.00) | 2.15 (1.55, 2.69) | 1.70 (1.13, 2.39)
Median (IQR)

## 2.1 Inferential statistics

From the descriptive statistics of Ratio, we know that the relative dispersion in the Hodgkin group is almost as high as the relative dispersion in the non-Hodgkin group.

For new users of `R`, it helps to have a standard syntax in most of the commands, as `R` could sometimes be challenging and even intimidating. We can test if the difference in variance is statistically significant with the `var.test` command, which uses can use a formula syntax:

    var.test(Ratio ~ Group, data = Hodgkin)

    ##
    ##  F test to compare two variances
    ##
    ## data:  Ratio by Group
    ## F = 0.6294, num df = 19, denom df = 19, p-value = 0.3214
    ## alternative hypothesis: true ratio of variances is not equal to 1
    ## 95 percent confidence interval:
    ##  0.2491238 1.5901459
    ## sample estimates:
    ## ratio of variances
    ##          0.6293991

What about normality? We can look at the QQ-plots against the standard Normal distribution.

    Hodgkin |>
      qq_plot(y = Ratio) +
      facet_grid(cols = vars(Group))

Let’s say we choose a non-parametric test to compare the groups:

    wilcox.test(Ratio ~ Group, data = Hodgkin)

    ##
    ##  Wilcoxon rank sum exact test
    ##
    ## data:  Ratio by Group
    ## W = 298, p-value = 0.007331
    ## alternative hypothesis: true location shift is not equal to 0

## 2.2 Graphical output

For relatively small samples (for example, less than 30 observations per group), it is a standard practice to show the actual data in dot plots with error bars. The `pubh` package offers two options to show graphically differences in continuous outcomes among groups:

- For small samples: `strip_error`
- For medium to large samples: `bar_error`

For our current example:

    Hodgkin |>
      strip_error(x = Group, y = Ratio)

The error bars represent 95% confidence intervals around mean values.

Adding a line on top is relatively easy to show that the two groups are significantly different. The function `gg_star` needs the reference point on how to draw an horizontal line to display statistical differences or to annotate a plot; in summary, `gg_star`:

- Draws an horizontal line from \((x1, y2)\) to \((x2, y2)\).
- Draws a vertical line from \((x1, y1)\) to \((x1, y1)\).
- Draws a vertical line from \((x2, y1)\) to \((x2, y1)\).
- Writes a character (the legend or “star”) at the mid point between \(x1\) and \(x2\) at high \(y3\).

Thus:

\[ y1 < y2 < y3 \]

In our current example:

    p1 <- Hodgkin |>
      strip_error(x = Group, y = Ratio)

    gg_star(p1, x1 = 1, y1 = 4, x2 = 2, y2 = 4.05, y3 = 4.1, "**") +
      ylim(0, 4.2)

For larger samples we could use bar charts with error bars. For example:

    data(birthwt, package = "MASS")
    birthwt <- birthwt |>
      mutate(
        smoke = factor(smoke, labels = c("Non-smoker", "Smoker")),
        Race = factor(race > 1, labels = c("White", "Non-white")),
        race = factor(race, labels = c("White", "Afican American", "Other"))
        ) |>
      var_labels(
        bwt = 'Birth weight (g)',
        smoke = 'Smoking status',
        race = 'Race',
      )

    birthwt |>
      bar_error(x = smoke, y = bwt)

Both `strip_error` and `bar_error` can generate plots stratified by a third variable, for example:

    birthwt |>
      bar_error(x = smoke, y = bwt) +
      facet_grid(cols = vars(Race))

    birthwt |>
      strip_error(x = smoke, y = bwt) +
      facet_grid(cols = vars(Race))

# 3 Regression models

The `pubh` package includes the function `cosm_reg`, which adds some cosmetics to objects generated by `tbl_regression` and `huxtable`. The function `multiple` helps analyse and visualise multiple comparisons.

For simplicity, here we show the analysis of the linear model of smoking on birth weight, adjusting by race (and not by other potential confounders).

    model_bwt <- lm(bwt ~ smoke + race, data = birthwt)

    model_bwt |>
      glm_coef(labels = model_labels(model_bwt))

Parameter | Coefficient | Pr(>|t|)
--- | --- | ---
Constant | 3334.95 (3153.89, 3516.01) | < 0.001
Smoking status: Smoker | -428.73 (-643.86, -213.6) | < 0.001
Race: Afican American | -450.36 (-752.45, -148.27) | 0.004
Race: Other | -452.88 (-682.67, -223.08) | < 0.001

In the last table of coefficients, confidence intervals and p-values for race levels are not adjusted for multiple comparisons. We can use functions from the `emmeans` package to make the corrections.

    multiple(model_bwt, ~ race)

contrast | estimate | SE | t.ratio | p.value | CI_low | CI_high
--- | --- | --- | --- | --- | --- | ---
Afican American - White | -450 | 153 | -2.94 | 0.007 | -820 | -80.4
Other - White | -453 | 116 | -3.89 | < 0.001 | -734 | -171
Other - Afican American | -2.52 | 161 | -0.02 | 0.988 | -391 | 385

    multiple(model_bwt, ~ race) |>
      effect_plot(x = estimate, y = contrast, orientation = "y") +
      geom_vline(xintercept = 0, lty = 2, col = bmj[2]) +
      xlab("Difference in birth weight (g)") +
      ylab("")

    ## Warning: Use of `data[["CI_low"]]` is discouraged.
    ## ℹ Use `.data[["CI_low"]]` instead.

    ## Warning: Use of `data[["CI_high"]]` is discouraged.
    ## ℹ Use `.data[["CI_high"]]` instead.