---
title: "Wald tests of multiple-constraint null hypotheses"
author: "James E. Pustejovsky"
date: "`r Sys.Date()`"
output: 
  rmarkdown::html_vignette:
    number_sections: true
    toc: true
bibliography: bibliography.bib
csl: apa.csl
vignette: >
  %\VignetteIndexEntry{Wald tests of multiple-constraint null hypotheses}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, echo = FALSE, results = "asis", message = FALSE, warning = FALSE}
library(clubSandwich)
AER_available <- requireNamespace("AER", quietly = TRUE)

knitr::opts_chunk$set(eval = AER_available)

if (!AER_available) cat("# Building this vignette requires the AER package. Please install it. {-}")
```

Version 0.5.0 of `clubSandwich` introduced a new syntax for `Wald_test()`, a function for conducting tests of multiple-constraint hypotheses. In previous versions, this function was poorly documented and, consequently, probably little used. This vignette will demonstrate the new syntax. 

For purposes of illustration, I will use the `STAR` data (available in the `AER` package), which is drawn from a randomized trial evaluating the effects of elementary school class size on student achievement. The data consist of individual-level measures for students in each of several dozen schools. For purposes of illustration, I will look at effects on math performance in first grade. Treatment conditions (the variable called `stark`) were assigned at the classroom level, and consisted of either a) a regular-size class, b) a small-size class, or c) a regular-size class but with the addition of a teacher's aide. In all of what follows, I will cluster standard errors by school in order to allow for generalization to a super-population of schools. 

```{r, message = FALSE, warning = FALSE}
library(clubSandwich)

data(STAR, package = "AER")

# clean up a few variables
levels(STAR$stark)[3] <- "aide"
levels(STAR$schoolk)[1] <- "urban"
STAR <- subset(STAR, 
               !is.na(schoolidk),
               select = c(schoolidk, schoolk, stark, gender, ethnicity, math1, lunchk))
head(STAR)
```

# The Wald test function

The `Wald_test()` function can be used to conduct hypothesis tests that involve multiple constraints on the regression coefficients. Consider a linear model for an outcome $Y_{ij}$ regressed on a $1 \times p$ row vector of predictors $\mathbf{x}_{ij}$ (which might include a constant intercept term):
$$
Y_{ij} = \mathbf{x}_{ij} \boldsymbol\beta + \epsilon_{ij}
$$
The regression coefficient vector is $\boldsymbol\beta$. In quite general terms, a set of constraints on the regression coefficient vector can be expressed in terms of a $q \times p$ matrix $\mathbf{C}$, where each row of $\mathbf{C}$ corresponds to one constraint. A joint null hypothesis is then $H_0: \mathbf{C} \boldsymbol\beta = \mathbf{0}$, where $\mathbf{0}$ is a $q \times 1$ vector of zeros.[^more-general] 

[^more-general]: In @pustejovsky2018small we used a more general formulation of multiple-constraint null hypotheses, expressed as $H_0: \mathbf{C} \boldsymbol\beta = \mathbf{d}$ for some fixed $q \times 1$ vector $\mathbf{d}$. In practice, it's often possible to modify the $\mathbf{C}$ matrix so that $\mathbf{d}$ can always be set to $\mathbf{0}$.

Wald-type test are based on the test statistic 
$$
Q = \left(\mathbf{C}\boldsymbol{\hat\beta}\right)' \left(\mathbf{C} \mathbf{V}^{CR} \mathbf{C}'\right)^{-1} \left(\mathbf{C}\boldsymbol{\hat\beta}\right),
$$
where $\boldsymbol{\hat\beta}$ is the estimated regression coefficient vector and $\mathbf{V}^{CR}$ is a cluster-robust variance matrix. If the number of clusters is sufficiently large, then the distribution of $Q$ under the null hypothesis is approximately $\chi^2(q)$. @tipton2015small investigated a wide range of other approximations to the null distribution of $Q$, many of which are included as options in `Wald_test()`. Based on a large simulation, they (...er...we...) recommended a method called the "approximate Hotelling's $T^2$-Z" test, or "AHZ." This test approximates the distribution of $Q / q$ by a $T^2$ distribution, which is a multiple of an $F$ distribution, with numerator degrees of freedom $q$ and denominator degrees of freedom based on a generalization of the Satterthwaite approximation.

The `Wald_test()` function has three main arguments:
```{r}
args(Wald_test)
```

* The `obj` argument is used to specify the estimated regression model on which to perform the test,
* the `constraints` argument is a $\mathbf{C}$ matrix expressing the set of constraints to test, and
* the `vcov` argument is a cluster-robust variance matrix, which is used to construct the test statistic. (Alternately, `vcov` can be the type of cluster-robust variance matrix to construct, in which case it will be computed internally.)

By default, `Wald_test()` will use the HTZ small-sample approximation. Other options are available (via the `test` argument) but not recommended for routine use. The optional `tidy` argument will be demonstrated below.

## Testing treatment effects

Returning to the STAR data, let's suppose we want to examine differences in math performance across class sizes. This can be done with a simple linear regression model, while clustering the standard errors by `schoolidk`. The estimating equation is
$$
\left(\text{Math}\right)_{ij} = \beta_0 + \beta_1 \left(\text{small}\right)_{ij} + \beta_2 \left(\text{aide}\right)_{ij} + e_{ij},
$$
which can be estimated in R as follows:
```{r type-treat}

lm_trt <- lm(math1 ~ stark, data = STAR)
V_trt <- vcovCR(lm_trt, cluster = STAR$schoolidk, type = "CR2")
coef_test(lm_trt, vcov = V_trt)

```

In this estimating equation, the coefficients $\beta_1$ and $\beta_2$ represent treatment effects, or differences in average math scores relative to the reference level of `stark`, which in this case is a regular-size class. The t-statistics and p-values reported by `coef_test` are separate tests of the null hypotheses that each of these coefficients are equal to zero, meaning that there is no difference between the specified treatment condition and the reference level. We might want to instead test the _joint_ null hypothesis that there are no differences among _any_ of the conditions. This null can be expressed by a set of multiple constraints on the parameters: $\beta_1 = 0$ and $\beta_2 = 0$.  

To test the null hypothesis that $\beta_1 = \beta_2 = 0$ based on the treatment effects model specification, we can use:
```{r}
C_trt <- matrix(c(0,0,1,0,0,1), 2, 3)
C_trt
Wald_test(lm_trt, constraints = C_trt, vcov = V_trt)
```
The result includes details about the form of `test` computed, the $F$-statistic, the numerator and denominator degrees of freedom used to compute the reference distribution, and the $p$-value corresponding to the specified null hypothesis. In this example, $p = `r if (AER_available) format.pval(Wald_test(lm_trt, constraints = C_trt, vcov = V_trt)$p_val, digits = 3)`$, so we can rule out the null hypothesis that there are no differences in math performance across conditions. 

The representation of null hypotheses as arbitrary constraint matrices is useful for developing theory about how to test such hypotheses, but it is not all that helpful for actually running tests---constructing constraint matrices "by hand" is just too cumbersome of an exercise. Moreover, $\mathbf{C}$ matrices typically follow one of a small number of patterns. Two common use cases are a) constraining a set of $q > 1$ parameters to all be equal to zero and b) constraining a set of $q + 1$ parameters to be equal to a common value. The `clubSandwich` package now includes a set of helper functions to create constraint matrices for these common use cases.

## `constrain_zero()` 

To constrain a set of $q$ regression coefficients to all be equal to zero, the simplest form of the $\mathbf{C}$ matrix would consist of a set of $q$ rows, where a single entry in each row would be equal to 1 and the remaining entries would all be zero. For the `lm_trt` model, the C matrix would look like this:
$$
\mathbf{C} = \left[\begin{array}{ccc} 0 & 1 & 0 \\ 0 & 0 & 1 \end{array} \right],
$$
so that 
$$
\mathbf{C}\boldsymbol\beta = \left[\begin{array}{ccc} 0 & 1 & 0 \\ 0 & 0 & 1 \end{array} \right] \left[\begin{array}{c} \beta_0 \\ \beta_1 \\ \beta_2 \end{array} \right] = \left[\begin{array}{c} \beta_1 \\ \beta_2 \end{array} \right],
$$
which is set equal to $\left[\begin{array}{c} 0 \\ 0 \end{array} \right]$.

The `constrain_zero()` function will create matrices like this automatically. The function takes two main arguments:
```{r}
args(constrain_zero)
```
* The `constraints` argument is used to specify _which_ coefficients in a regression model to set equal to zero. 
* The `coefs` argument is the set of estimated regression coefficients, for which to calculate the constraints. 

Constraints can be specified by position index, by name, or via a regular expression. To test the joint null hypothesis that average math performance is equal across the three treatment conditions, we need to constrain the second and third coefficients to zero:
```{r}
constrain_zero(2:3, coefs = coef(lm_trt))
```
Or equivalently:
```{r}
constrain_zero(c("starksmall","starkaide"), coefs = coef(lm_trt))
```
or 
```{r}
constrain_zero("^stark", coefs = coef(lm_trt), reg_ex = TRUE)
```
Note that if `constraints` is a regular expression, then the `reg_ex` argument needs to be set to `TRUE`. 

The result of `constrain_zero()` can then be fed into the `Wald_test()` function:
```{r}
C_trt <- constrain_zero(2:3, coefs = coef(lm_trt))
Wald_test(lm_trt, constraints = C_trt, vcov = V_trt)
```
To reduce redundancy in the syntax, we can also omit the `coefs` argument to `constrain_zero`, so long as we call it inside of `Wald_test`[^under-the-hood]:
```{r}
Wald_test(lm_trt, constraints = constrain_zero(2:3), vcov = V_trt)
```

[^under-the-hood]: How does this work? If we omit the `coefs` argument, `constrain_zero()` acts as a functional, by returning a function equivalent to `function(coefs) constrain_zero(constraints, coefs = coefs)`. If this function is fed into the `constraints` argument of `Wald_test()`, `Wald_test()` recognizes that it is a function and evaluates the function with `coef(obj)`. It's a kinda-sorta hacky substitute for lazy evaluation. If you have suggestions for how to do this more elegantly, please send them my way.

## `constrain_equal()`

Another common type of constraints involve setting a set of $q + 1$ regression coefficients to be all equal to a common (but unknown) value ($q + 1$ because it takes $q$ constraints to do this). There are many equivalent ways to express such a set of constraints in terms of a $\mathbf{C}$ matrix. One fairly simple form consists of a set of $q$ rows, where the entry corresponding to one of the coefficients of interest is equal to -1 and the entry corresponding to another coefficient of interest is equal to 1. 

To see how this works, let's look at a different way of parameterizing our simple model for the STAR data, by using separate intercepts for each treatment condition. The estimating equation would then be
$$
\left(\text{Math}\right)_{ij} = \beta_0 \left(\text{regular}\right)_{ij} + \beta_1 \left(\text{small}\right)_{ij} + \beta_2 \left(\text{aide}\right)_{ij} + e_{ij}.
$$
This model can be estimated in R by dropping the intercept term:
```{r type-sep}

lm_sep <- lm(math1 ~ 0 + stark, data = STAR)
V_sep <- vcovCR(lm_sep, cluster = STAR$schoolidk, type = "CR2")
coef_test(lm_sep, vcov = V_sep)

```
In this parameterization, the coefficients $\beta_0$, $\beta_1$, and $\beta_2$ represent the average math performance levels of students in each of the treatment conditions. The t-tests and p-values now have a very different interpretation because they pertain to the null hypothesis that the average performance level for a given condition is equal to zero. With this separate-intercepts model, the joint null hypothesis that performance levels are equal across conditions amounts to constraining the intercepts to be equal to each other: $\beta_0 = \beta_1$ and $\beta_0 = \beta_2$ (note that we don't need the constraint $\beta_1 = \beta_2$ because it is implied by the first two).

For the `lm_sep` model, which has separate intercepts $\beta_0$, $\beta_1$, and $\beta_2$, the C matrix would look like this:
$$
\mathbf{C} = \left[\begin{array}{ccc} -1 & 1 & 0 \\ -1 & 0 & 1 \end{array} \right],
$$
so that 
$$
\mathbf{C}\boldsymbol\beta = \left[\begin{array}{ccc} -1 & 1 & 0 \\ -1 & 0 & 1 \end{array} \right] \left[\begin{array}{c} \beta_0 \\ \beta_1 \\ \beta_2 \end{array} \right] = \left[\begin{array}{c} \beta_1 - \beta_0 \\ \beta_2 - \beta_0 \end{array} \right],
$$
which is set equal to $\left[\begin{array}{c} 0 \\ 0 \end{array} \right]$. 

The `constrain_equal()` function will create matrices like this automatically, given a set of coefficients to constrain. The syntax is identical to `constrain_zero()`:
```{r}
args(constrain_equal)
```
To test the joint null hypothesis that average math performance is equal across the three treatment conditions, we can constrain all three coefficients of `lm_sep` to be equal:
```{r}
constrain_equal(1:3, coefs = coef(lm_sep))
```
Or equivalently:
```{r}
constrain_equal(c("starkregular","starksmall","starkaide"), coefs = coef(lm_sep))
```
or
```{r}
constrain_equal("^stark", coefs = coef(lm_sep), reg_ex = TRUE)
```
Just as with `constrain_zero`, if `constraints` is a regular expression, then the `reg_ex` argument needs to be set to `TRUE`. 

This constraint matrix can then be fed into `Wald_test()`:
```{r}
C_sep <- constrain_equal("^stark", coefs = coef(lm_sep), reg_ex = TRUE)
Wald_test(lm_sep, constraints = C_sep, vcov = V_sep)
```
or equivalently:
```{r}
Wald_test(lm_sep, constraints = constrain_equal(1:3), vcov = V_sep)
```
Note that these test results are exactly equal to the tests based on `lm_trt` with `constrain_zero()`. They're algebraically equivalent---just different ways of parameterizing the same model and constraints. 

# Testing an interaction

Let's now consider how these functions can be applied in a more complex model. Suppose that we are interested in understanding whether the effect of being in a small class is consistent across schools in different areas, where areas are categorized as urban, suburban, or rural. To answer this question, we need to test for an interaction between urbanicity and treatment condition. One estimating equation that would let us examine this question is:
$$
\begin{aligned}
\left(\text{Math}\right)_{ij} &= \beta_0 + \beta_1 \left(\text{suburban}\right)_{ij} + \beta_2 \left(\text{rural}\right)_{ij} \\
& \quad + \beta_3 \left(\text{small}\right)_{ij} + \beta_4 \left(\text{aide}\right)_{ij} \\
& \quad\quad + \beta_5 \left(\text{small}\right)(\text{suburban})_{ij} + \beta_6 \left(\text{aide}\right)(\text{suburban})_{ij} \\
& \quad\quad\quad + \beta_{7} \left(\text{small}\right)(\text{rural})_{ij} + \beta_{8} \left(\text{aide}\right)(\text{rural})_{ij} \\
& \quad\quad\quad\quad + \mathbf{x}_{ij} \boldsymbol\gamma  + e_{ij},
\end{aligned}
$$
where $\mathbf{x}_{ij}$ is a row vector of student characteristics, included just to make the regression look fancier. In this specification, $\beta_3$ and $\beta_4$ represent the effects of being in a small class or aide class, compared to being in a regular class, but _only for the reference level of urbanicity_---in this case, urban schools. The coefficients $\beta_5, \beta_6, \beta_7, \beta_8$ all represent _interactions_ between treatment condition and urbanicity. Here's the model, estimated in R:
```{r}
lm_urbanicity <- lm(math1 ~ schoolk * stark + gender + ethnicity + lunchk, data = STAR)
V_urbanicity <- vcovCR(lm_urbanicity, cluster = STAR$schoolidk, type = "CR2")
coef_test(lm_urbanicity, vcov = V_urbanicity)
```
With this specification, there are several different null hypotheses that we might want to test. For one, perhaps we want to see if there is _any_ variation in treatment effects across different levels of urbanicity. This can be expressed in the null hypothesis that all four interaction terms are zero, or $H_{0A}: \beta_5 = \beta_6 = \beta_7 = \beta_8 = 0$. With Wald test:
```{r}
Wald_test(lm_urbanicity, 
          constraints = constrain_zero("schoolk.+:stark", reg_ex = TRUE),
          vcov = V_urbanicity)
```

Another possibility is that we might want to focus on variation in the effect of being in a small class or regular class, while ignoring whatever is going on in the aide class condition. Here, the null hypothesis would be simply $H_{0B}: \beta_5 = \beta_6 = 0$, tested as:
```{r}
Wald_test(lm_urbanicity, 
          constraints = constrain_zero("schoolk.+:starksmall", reg_ex = TRUE),
          vcov = V_urbanicity)
```

## Lists of constraints

In models like the urbanicity-by-treatment interaction specification, we may need to run multiple tests on the same estimating equation. This can be accomplished with `Wald_test` by providing a _list_ of constraints to the `constraints` argument. For example, we could test the hypotheses described above by creating a list of several constraint matrices and then passing it to `Wald_test`:
```{r}
C_list <- list(
  `Any interaction` = constrain_zero("schoolk.+:stark", 
                                     coef(lm_urbanicity), reg_ex = TRUE),
  `Small vs regular` = constrain_zero("schoolk.+:starksmall", 
                                      coef(lm_urbanicity), reg_ex = TRUE)
)

Wald_test(lm_urbanicity, 
          constraints = C_list,
          vcov = V_urbanicity)

```
Setting the option `tidy = TRUE` will arrange the output of all the tests into a single data frame: 
```{r}

Wald_test(lm_urbanicity, 
          constraints = C_list,
          vcov = V_urbanicity, 
          tidy = TRUE)

```
The list of constraints can also be created inside `Wald_test`, so that the `coefs` argument can be omitted from `constrain_zero()`:
```{r}

Wald_test(
  lm_urbanicity, 
  constraints = list(
    `Any interaction` = constrain_zero("schoolk.+:stark", reg_ex = TRUE),
    `Small vs regular` = constrain_zero("schoolk.+:starksmall", reg_ex = TRUE)
  ),
  vcov = V_urbanicity, 
  tidy = TRUE
)

```

# Pairwise t-tests

The `clubSandwich` package also provides a further convenience function, `constrain_pairwise()` that can be used in combination with `Wald_test()` to conduct pairwise comparisons among a set of regression coefficients. This function differs from the other two `constrain_*()` functions because it returns a _list_ of constraint matrices, each of which corresponds to a single linear combination of covariates. Specifically, the `constrain_pairwise()` function provides a list of constraints that represent the differences between every possible pair among a specified set of coefficients. The syntax is very similar to the other `constrain_*()` functions. 

To demonstrate, consider the separate-intercepts specification of the simpler regression model:
```{r}
coef_test(lm_sep, vcov = V_sep)
```
This specification is nice because it lets us simply read off the average outcomes for each group. However, we will naturally also want to know about whether there are differences between the groups, so we'll want to compare the small-class condition to the regular-size class condition, the aide condition to the regular-size class condition, and the small-class condition to the aide condition. Thus, we'll want comparisons among all three coefficients:
```{r}
C_pairs <- constrain_pairwise(1:3, coefs = coef(lm_sep))
C_pairs
```
Feeding these constraints into `Wald_test()` gives us significance tests for each pair:
```{r}
Wald_test(lm_sep, constraints = C_pairs, vcov = V_sep, tidy = TRUE)
```

The first two of these tests are equivalent to the tests of the treatment effect coefficients in the other parameterization of the model. Indeed, the denominator degrees of freedom are identical to the results of `coef_test(lm_trt, vcov = V_trt)`; the `Fstat`s here are equal to the squared t-statistics from the first model:
```{r}
t_stats <- coef_test(lm_trt, vcov = V_trt)$tstat[2:3]
F_stats <- Wald_test(lm_sep, constraints = C_pairs, vcov = V_sep, tidy = TRUE)$Fstat[1:2]
all.equal(t_stats^2, F_stats)
```
It is important to note that the p-values from the pairwise comparisons are _not_ corrected for multiplicity.[^multiplicity] For now, please correct-your-own using `p.adjust()` or your preferred method.

[^multiplicity]: Options to include multiplicity corrections (Bonferroni, Holm, Benjamini-Hochberg, etc.) might be included in a [future release](https://github.com/jepusto/clubSandwich/issues/33). Reach out if this is of interest to you.

Pairwise comparisons might also be of use in the model with treatment-by-urbanicity interactions. Here's the model results again:
```{r}
coef_test(lm_urbanicity, vcov = V_urbanicity)
```
Suppose that we are interested in the effect of small versus regular size classes, and in particular whether this effect varies across schools in different areas. The coefficients on `schoolksuburban:starksmall` and `schoolkrural:starksmall` already give us the differences in treatment effects between suburban schools versus urban schools and between rural schools versus urban schools. The difference between these coefficients gives us the difference in treatment effects between suburban schools and rural schools. We can look at all three of these contrasts using `constrain_pairwise()` by setting the option `with_zero = TRUE`:
```{r}
Wald_test(lm_urbanicity, 
          constraints = constrain_pairwise(":starksmall", reg_ex = TRUE, with_zero = TRUE),
          vcov = V_urbanicity,
          tidy = TRUE)
```
Again, the results of the first two tests are identical to the t-tests reported in `coef_test()`. 

# Remark

All of the preceding examples were based on ordinary linear regression models with clustered standard errors. However, `Wald_test()` and its helper functions all work identically for all of the other models with supporting `clubSandwich` methods, including `nlme::lme()`, `nlme::gls()`, `lme4::lmer()`, `rma.uni()`, `rma.mv()`, and `robu()`, among others. 

# References
