-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
413 lines (338 loc) · 27.6 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# EquiTrends
<!-- badges: start -->
[![CRAN status](http://www.r-pkg.org/badges/version/EquiTrends)](https://cran.r-project.org/package=EquiTrends)
[![CRAN_Downloads_Badge](https://cranlogs.r-pkg.org/badges/grand-total/EquiTrends)](https://cran.r-project.org/package=EquiTrends)
[![R-CMD-check](https://github.com/TiesBos/EquiTrends/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/TiesBos/EquiTrends/actions/workflows/R-CMD-check.yaml) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Codecov test coverage](https://codecov.io/gh/TiesBos/EquiTrends/graph/badge.svg)](https://app.codecov.io/gh/TiesBos/EquiTrends)
<!-- badges: end -->
`EquiTrends` is an R package for equivalence testing in the context of Difference-in-Differences
estimation. It allows users to test if pre-treatment trends in the treated group are
“equivalent” to those in the control group. Here, “equivalence” means that rejection of the
null hypothesis implies that a function of the pre-treatment placebo effects (maximum
absolute, average or root mean squared value) does not exceed a pre-specified threshold
below which trend differences are considered negligible. The package is based on the theory
developed in Dette & Schumann ([2024](https://doi.org/10.1080/07350015.2024.2308121)).
The package contains the functions `maxEquivTest` to perform the testing procedure surrounding the maximum placebo coefficient (see equation (3.1) of Dette & Schumann ([2024](https://doi.org/10.1080/07350015.2024.2308121))), `meanEquivTest` to perform the testing procedure surrounding the mean placebo coefficient (see equation (3.2) of Dette & Schumann ([2024](https://doi.org/10.1080/07350015.2024.2308121))) and `rmsEquivTest` to perform the testing procedure surrounding the root mean squared placebo coefficient (see equation (3.3) and (3.4) of Dette & Schumann ([2024](https://doi.org/10.1080/07350015.2024.2308121))). Furthermore, the package contains the function `sim_paneldata` to simulate a paneldataset for such testing purposes.
## Installation
You can install the development version of `EquiTrends` from [GitHub](https://github.com/TiesBos/EquiTrends) with:
```{r echo=TRUE, message=FALSE, results='hide'}
#install.packages("devtools")
devtools::install_github("TiesBos/EquiTrends")
```
The stable version (1.0.0) is available on CRAN:
```
install.packages("EquiTrends")
```
## Data Simulation
The `EquiTrends` package contains a function to simulate panel data, tailored to the Difference-in-Differences framework. The function `sim_paneldata` simulates a panel dataset with a given number of individuals $N$ (`N`), number of periods $T+1$ (in the setting of this package, indicating the number of pre-treatment periods. In `sim_paneldata` $T+1$ is referred to as `tt`), number of covariates $p$ (`p`), and treatment effects. Typically, period $T+1$ is referred to as the "base period". The function also allows for the simulation of heterogeneity in treatment effects (specified through `eta`) and time fixed effects (through `lambda`). Furthermore, the function allows for heteroscedasticty (specified through the binary variable `het`), serial correlation (through the AR(1) coefficient `phi`: $u_{i,t} = \phi u_{i,t-1} + v_{i,t}$ where $v_{i,t}$ follows an i.i.d. $N(0,\sigma^2)$ distribution and $\sigma$ is specified through `sd`), and clustering in the model errors $u_{i,t}$. The function returns a data frame with the following columns: `ID` (the cross-sectional individual identifier), `period` (the time identifier), `Y` (the dependent variable), `G` (a binary vector indicating if an individual receives treatment, indicated by 1, or not, indicated by 0), and `X_1`, `X_2`, ..., `X_p` (additional control variables). The construction of the dependent variable follows the two-way fixed effect model, similar to the model in equation (2.5) of Dette & Schumann ([2024](https://doi.org/10.1080/07350015.2024.2308121)):
$$Y_{i,t} = \eta_i + \lambda_t + \sum_{l=1}^{T}{\beta_l}G_iD_l(t) + X_{1, i, t}\gamma_1+ \dots + X_{p,i,t}\gamma_p +u_{i,t} \quad \text{with} \ \ i=1,...,N, \ \ t=1,...,T+1$$
where $D_l(t)$ is a dummy variable that equals 1 if $t=l$ and 0 otherwise. The error-terms $u_{i,t}$ are generated through a normal distribution with mean 0 and a variance-covariance structure depending on the user-specified parameters. In the following, the $\beta_l$ coefficients are referred to as placebo coefficients, since they represent the difference in pre-trends between the treatment and control group before treatment has been assigned.
An example of the `sim_paneldata` function is provided below:
```{r}
library(EquiTrends)
# Simulate a panel dataset with 500 individuals, 5 periods, 2 additional
# regressors, and a binary treatment variable without heteroscedasticity,
# serial correlation, and clustering. Furthermore, there are no fixed effects or
# pre-trends in the model (since all values in beta are 0).
sim_data <- sim_paneldata(N = 500, tt = 5, p = 2, beta = rep(0, 5),
gamma = rep(1, 2), het = 0, phi = 0, sd = 1,
burnins = 50)
head(sim_data)
```
## Testing for Equivalence of Pre-Trends
The `EquiTrends` package contains functions to test for equivalence of pre-trends in Difference-in-Differences estimation. The functions `rmsEquivTest`, `meanEquivTest`, and `maxEquivTest` are used to test for equivalence of pre-trends in Difference-in-Differences estimation using the placebo coefficients $\beta_{l} \ (l=1,...,T)$ estimates. The functions are based on the work of Dette & Schumann ([2024](https://doi.org/10.1080/07350015.2024.2308121)).
### The `rmsEquivTest` function
`rmsEquivTest` implements the equivalence testing procedure surrounding the root mean squared placebo coefficient as described in section 4.2.3 of Dette & Schumann ([2024](https://doi.org/10.1080/07350015.2024.2308121)). The function tests the null hypothesis that the root mean squared placebo coefficient is larger than or equal to a user-specified equivalence threshold $\delta$. That is, if
$$\beta_{RMS} = \sqrt{\frac{1}{T}\sum_{l=1}^{T} \beta_l^2},$$
the tested hypotheses can be represented as
$$H_0: \beta_{RMS} \geq \delta \quad \text{vs.} \quad H_1: \beta_{RMS} < \delta.$$
The null and alternative hypothesis can therefore be seen as non-negligible and negligible differences in pre-trends, respectively. The function returns an object of class `rmsEquivTest` containing
- `placebo_coefficients`: A numeric vector of the estimated placebo coefficients,
- `rms_placebo_coefs`: The root mean squared value of the placebo coefficients,
- `significance_level`: The significance level of the test,
- `base_period`: The base period used in the testing procedure,
- `num_individuals`: The number of cross-sectional individuals in the panel used for testing,
- `num_periods`: The number of pre-treatment periods in the panel used for testing (if the panel is unbalanced, `num_periods` represents the range in the number of time periods covered by different individuals),
- `num_observations`: The total number of observations in the panel used for testing,
- `is_panel_balanced`: A logical value indicating whether the used panel is balanced,
- `equiv_threshold_specified`: A logical value indicating whether an equivalence threshold was specified.
- If `equiv_threshold_specified = TRUE`, then additionally:
- `rms_critical_value`: The critical value at the chosen significance level,
- `reject_null_hypothesis`: A logical value indicating whether to reject the null hypothesis,
- `equiv_threshold`: The equivalence threshold specified.
- If `equiv_threshold_specified = FALSE`, then additionally:
- `minimum_equiv_threshold`: The minimum equivalence threshold for which the null hypothesis of non-negligible trend-differences can be rejected.
One should note that rows containing `NA` values are removed from the panel before the testing procedure is performed.
Please be aware that the equivalence test based on the root mean squared placebo coefficient applies a randomization technique (as described by Dette & Schumann (2024)), leading to a stochastic critical value and minimum equivalence threshold. Therefore, the results may vary between different runs of the function.
```{r}
# Perform the equivalence test using an equivalence threshold of 1 with periods
# 1-4 as pre-treatment periods based on the RMS testing procedure:
# - option 1: using column names in the panel
# One can use the names of the columns in the panel to specify the variables:
rmsEquivTest(Y = "Y", ID = "ID", G = "G", period = "period", X = c("X_1", "X_2"),
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4)
```
```{r echo = TRUE, results='hide'}
# - option 2: using column numbers in the panel
# Alternatively, one can use the column numbers in the panel to specify the variables:
rmsEquivTest(Y = 3, ID = 1, G = 4, period = 2, X = c(5, 6),
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4)
# - option 3: using separate variables
# One can also use the variables directly without specifying the data variable:
data_Y <- sim_data$Y
data_ID <- sim_data$ID
data_G <- sim_data$G
data_period <- sim_data$period
data_X <- cbind(sim_data$X_1, sim_data$X_2)
rmsEquivTest(Y = data_Y, ID = data_ID, G = data_G, period = data_period, X = data_X,
equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4)
```
The testing procedures can also be performed without specifying the equivalence threshold. Then, the minimum equivalence threshold is returned for which the null hypothesis of non-negligible trend-differences can be rejected. Again, the three possible ways of entering the data as above can be used.
```{r}
rmsEquivTest(Y = "Y", ID = "ID", G = "G", period = "period", X = c("X_1", "X_2"),
data = sim_data, equiv_threshold = NULL, pretreatment_period = 1:4,
base_period = 4)
```
Finally, one should note that the test procedure also works for unbalanced panels.
```{r echo = TRUE, results='hide'}
# To illustrate this, we generate an unbalanced panel dataset by randomly selecting
# 70% of the observations from the balanced panel dataset:
random_indices <- sample(nrow(sim_data), 0.7*nrow(sim_data))
unbalanced_sim_data <- sim_data[random_indices, ]
# With Equivalence Threshold:
rmsEquivTest(Y = 3, ID = 1, G = 4, period = 2, X = c(5, 6),
data = unbalanced_sim_data, equiv_threshold = 1,
pretreatment_period = 1:4, base_period = 4)
# Without Equivalence Threshold:
rmsEquivTest(Y = 3, ID = 1, G = 4, period = 2, X = c(5, 6),
data = unbalanced_sim_data, equiv_threshold = NULL,
pretreatment_period = 1:4, base_period = 4)
```
### The `maxEquivTest` function
The `maxEquivTest` function tests the null hypothesis that the maximum placebo coefficient is
larger than or equal to a user-specified equivalence threshold $\delta$. That is, if
$$\lVert\beta\rVert_\infty = \max_{l=1,...T} |\beta_l|,$$
the tested hypotheses can be represented as
$$H_0: \lVert\beta\rVert_\infty \geq \delta \quad \text{vs.} \quad H_1: \lVert\beta\rVert_\infty < \delta.$$
The null and alternative hypothesis can therefore be seen as non-negligible and negligible differences in pre-trends, respectively.
The function `maxEquivTest` contains three testing procedures for this test, as described in Section 4.2.1. of Dette & Schumann ([2024](https://doi.org/10.1080/07350015.2024.2308121)). The function allows for the testing of the equivalence of pre-trends using a bootstrap for spherical errors (`type = "Boot"`), a wild bootstrap for clustered standard errors (`type = "Wild"`), and an Intersection Union approach (`type = "IU"`) that rejects the null if all estimates for $\beta_1,...,\beta_{T}$ are smaller than their individual critical values. The function returns an object of class `maxEquivTestBoot` if `type = "Boot"` or `type = "Wild"` or `maxEquivTestIU` if `type = "IU"`. If no type is specified, `maxEquivTest` applies the Intersection Union procedure for efficiency reasons.
#### Implemention of the `maxEquivTest` function with `type = "IU"`
Examples of implementing the Intersection unit test with different possible variance-covariance matrices (required to perform the test) are provided below (for more information on the possible variance-covariance matrices, see the documentation of the `maxEquivTest` function). If an equivalence threshold is supplied, the function will test the previous hypothesis. If no equivalence threshold is supplied, the function finds the minimum equivalence threshold for which the null of non-negligible trend-differences can be reject using the Intersection Union test. The function returns an object of class `maxEquivTestIU` containing the following information:
- `placebo_coefficients`: A numeric vector of the estimated placebo coefficients,
- `abs_placebo_coefficients`: A numeric vector with the absolute values of estimated placebo coefficients,
- `placebo_coefficients_se`: A numeric vector with the standard errors of the placebo coefficients,
- `significance_level`: The chosen significance level of the test,
- `base_period`: The base period used in the testing procedure,
- `placebo_names`: The names corresponding to the placebo coefficients,
- `num_individuals`: The number of cross-sectional individuals in the panel used for testing,
- `num_periods`: The number of pre-treatment periods in the panel used for testing (if the panel is unbalanced, `num_periods` represents the range in the number of time periods covered by different individuals),
- `num_observations`: The number of observations in the panel used for testing,
- `is_panel_balanced`: A logical value indicating whether the panel data is balanced,
- `equiv_threshold_specified`: A logical value indicating whether an equivalence threshold was specified.
- If `equiv_threshold_specified = TRUE`, then additionally:
- `IU_critical_values`: A numeric vector with the individual critical values for each of the placebo coefficients,
- `reject_null_hypothesis`: A logical value indicating whether the null hypothesis of negligible pre-trend differences can be rejected at the specified significance level,
- `equiv_threshold`: The equivalence threshold employed.
- If `equiv_threshold_specified = FALSE`, then additionally:
- `minimum_equiv_thresholds`: A numeric vector including for each placebo coefficient the minimum equivalence threshold for which the null hypothesis of negligible pre-trend differences can be rejected for the corresponding placebo coefficient individually,
- `minimum_equiv_threshold`: A numeric scalar minimum equivalence threshold for which the null hypothesis of negligible pre-trend differences can be rejected for all placebo coefficients.
One should note that rows containing `NA` values are removed from the panel before the testing procedure is performed.
```{r}
# Perform the test with equivalent threshold specified as 1 based on
# pre-treatment periods 1-4 and homoscedastic error-terms:
# To select variables, one can use the column names / numbers in the panel data
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = 2, X= c(5,6),
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "IU")
# Alternatively, one can enter the variables separately:
data_Y <- sim_data$Y
data_ID <- sim_data$ID
data_G <- sim_data$G
data_period <- sim_data$period
data_X <- sim_data[, c(5, 6)]
maxEquivTest(Y = data_Y, ID = data_ID, G = data_G, period = data_period, X = data_X,
equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "IU")
```
```{r}
# Perform the test without specifying the equivalence threshold with heteroscedastic
# and autocorrelation robust variance-covariance matrix estimator:
maxEquivTest(Y = 3, ID = 1, G = 4, period = 2,
data = sim_data, equiv_threshold = NULL, pretreatment_period = 1:4,
base_period = 4, type = "IU", vcov = "HAC")
```
```{r echo=TRUE, results='hide'}
# Perform the test without specifying the equivalence threshold with a custom
# variance-covariance matrix estimator:
vcov_func <- function(x) {plm::vcovHC(x, method = "white1", type = "HC2")}
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "IU", vcov = vcov_func)
# Perform the test using clustered standard errors based on a vector indicating
# the cluster. For instance, two clusters with the following rule: all
# individuals with an ID below 250 are in the same cluster.
cluster_ind <- ifelse(sim_data$ID < 250, 1, 2)
maxEquivTest(Y = data_Y, ID = data_ID, G = data_G, period = data_period, X = data_X,
equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "IU", vcov = "CL", cluster = cluster_ind)
```
Note that the testing procedure can also handle unbalanced panels.
```{r echo = TRUE, results = 'hide'}
# To illustrate this, we generate an unbalanced panel dataset by randomly selecting
# 70% of the observations from the balanced panel dataset:
random_indices <- sample(nrow(sim_data), 0.7*nrow(sim_data))
unbalanced_sim_data <- sim_data[random_indices, ]
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period", X = c(5, 6),
data = unbalanced_sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "IU", vcov = "HAC")
```
##### Implementation of the bootstrap approaches
Examples of implementing the bootstrap based test are provided below. For both `type = "Boot"` and `type = "Wild"`, an equivalence threshold is required to perform the test. Furthermore, both testing procedures return an object of class "maxEquivTestBoot" containing
- `placebo_coefficients`: A numeric vector of the estimated placebo coefficients,
- `abs_placebo_coefficients`: A numeric vector with the absolute values of estimated placebo coefficients,
- `max_abs_coefficient`: The maximum absolute estimated placebo coefficient,
- `B`: The number of bootstrap samples used to find the critical value,
- `significance_level`: The chosen significance level of the test,
- `base_period`: The base period used in the testing procedure,
- `placebo_names`: The names corresponding to the placebo coefficients,
- `num_individuals`: The number of cross-sectional individuals in the panel used for testing,
- `num_periods`: The number of pre-treatment periods in the panel used for testing (if the panel is unbalanced, `num_periods` represents the range in the number of time periods covered by different individuals),
- `num_observations`: The total number of observations in the panel used for testing,
- `is_panel_balanced`: A logical value indicating whether the panel data is balanced,
- `equiv_threshold_specified`: A logical value indicating whether an equivalence threshold was specified.
- If `equiv_threshold_specified = TRUE`, then additionally:
- `bootstrap_critical_value`: The by bootstrap found critical value for the equivalence test based on the maximum absolute placebo coefficient,
- `reject_null_hypothesis`: A logical value indicating whether the null hypothesis of negligible pre-trend differences can be rejected at the specified significance level,
- If `equiv_threshold_specified = FALSE`, then additionally:
- `minimum_equiv_threshold`: The minimum equivalence threshold for which the null hypothesis of negligible pre-trend differences can be rejected for the bootstrap procedure.
One should note that rows containing `NA` values are removed from the panel before the testing procedure is performed.
On top of that, please be aware that the bootstrap procedures for the equivalence test based on the maximum absolute placebo coefficient apply a bootstrap procedure (as described by Dette & Schumann (2024)), leading to a stochastic critical value and minimum equivalence threshold. Therefore, the results may vary slightly between different runs of the function.
The bootstrap for spherical errors with 1000 bootstrap iterations:
```{r}
# Perform the test with equivalence threshold specified as 1 based on
# pre-treatment periods 1:4 (with base period 4) with the general bootstrap procedure:
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "Boot", B = 1000)
```
The Wild boostrap with 1000 bootstrap iterations:
```{r}
# Perform the test with the equivalence threshold specified as 1 based on
# pre-treatment periods 1:4 (with base period 4) with the wild bootstrap procedure:
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "Wild")
```
The bootstrap procedures can handle unspecified equivalence thresholds:
```{r echo = TRUE, results='hide'}
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = NULL, pretreatment_period = 1:4,
base_period = 4, type = "Boot", B = 1000)
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = NULL, pretreatment_period = 1:4,
base_period = 4, type = "Wild", B = 1000)
```
The bootstrap procedures can handle unbalanced panels:
```{r echo = TRUE, results='hide'}
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = unbalanced_sim_data, equiv_threshold = 1,
pretreatment_period = 1:4,
base_period = 4, type = "Boot", B = 1000)
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = unbalanced_sim_data, equiv_threshold = 1,
pretreatment_period = 1:4,
base_period = 4, type = "Wild", B = 1000)
```
### The `meanEquivTest` function
The `meanEquivTest` implements the equivalence testing procedure surrounding the mean placebo coefficient, as described in Section 4.2.2. of Dette & Schumann ([2024](https://doi.org/10.1080/07350015.2024.2308121)). The function tests the null hypothesis that the absolute mean placebo coefficient is larger than or equal to a user-specified equivalence threshold, $\delta$. That is, if
$$\bar{\beta} = \frac{1}{T}\sum_{l=1}^{T} \beta_l,$$
the tested hypotheses can be represented as
$$H_0: |\bar{\beta}| \geq \delta \quad \text{vs.} \quad H_1: |\bar{\beta}| < \delta.$$
The null and alternative hypothesis can therefore be seen as non-negligible and negligible differences in pre-trends, respectively. Implementation of the test is similar to the `maxEquivTest` function in terms of the possible variance-covariance matrices (for more information on the possible variance-covariance matrices, see the documentation of the `meanEquivTest` function). The function returns an object of class `meanEquivTest` containing
- `placebo_coefficients`: A numeric vector of the estimated placebo coefficients,
- `abs_mean_placebo_coefs`: The absolute value of the mean of the placebo coefficients,
- `var_mean_placebo_coef`: The estimated variance of the mean placebo coefficient,
- `significance_level`: The significance level of the test,
- `base_period`: The base period used in the testing procedure,
- `num_individuals`: The number of cross-sectional individuals in the panel used for testing,
- `num_periods`: The number of pre-treatment periods in the panel used for testing (if the panel is unbalanced, `num_periods` represents the range in the number of time periods covered by different individuals)
- `num_observations`: The total number of observations in the panel used for testing,
- `is_panel_balanced`: A logical value indicating whether the panel is balanced,
- `equiv_threshold_specified`: A logical value indicating whether an equivalence threshold was specified.
- If `equiv_threshold_specified = TRUE`, then additionally:
- `mean_critical_value`: The critical value at the chosen significance level,
- `p_value`: The p-value of the test,
- `reject_null_hypothesis`: A logical value indicating whether to reject the null hypothesis,
- `equiv_threshold`: The equivalence threshold specified.
- If `equiv_threshold_specified = FALSE`, then additionally:
- `minimum_equiv_threshold`: The minimum equivalence threshold for which the null hypothesis of non-negligible (based on the equivalence threshold) trend-differences can be rejected.
One should note that rows containing `NA` values are removed from the panel before the testing procedure is performed.
```{r}
# Perform the test with equivalent threshold specified as 1 based on
# pre-treatment periods 1-4 and assuming homoscedastic error-terms:
# To select variables, one can use the column names / column numbers in the panel data:
meanEquivTest(Y = "Y", ID = "ID", G = "G", period = 2, X = c(5, 6),
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4)
```
```{r echo = TRUE, results='hide'}
# Alternatively, one can use separate variables:
data_Y <- sim_data$Y
data_ID <- sim_data$ID
data_G <- sim_data$G
data_period <- sim_data$period
data_X <- sim_data[, c(5, 6)]
meanEquivTest(Y = data_Y, ID = data_ID, G = data_G, period = data_period, X = data_X,
equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4)
```
```{r}
# Perform the test with a heteroscedastic and autocorrelation robust
# variance-covariance matrix estimator, and without specifying the equivalence threshold:
meanEquivTest(Y = "Y", ID = "ID", G = "G", period = "period", X = c(5, 6),
data = sim_data, equiv_threshold = NULL, pretreatment_period = 1:4,
base_period = 4, vcov = "HAC")
```
```{r echo = TRUE, results='hide'}
# Perform the test with an equivalence threshold of 1 and a custom
# variance-covariance matrix estimator:
vcov_func <- function(x) {plm::vcovHC(x, method = "white1", type = "HC2")}
meanEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, vcov = vcov_func)
# Perform the test using clustered standard errors based on a vector indicating
# the cluster. For instance, two clusters with the following rule: all
# individuals with an ID below 250 are in the same cluster:
cluster_ind <- ifelse(sim_data$ID < 250, 1, 2)
meanEquivTest(Y = data_Y, ID = data_ID, G = data_G, period = data_period, X = data_X,
equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, vcov = "CL", cluster = cluster_ind)
```
Note that the testing procedure can also handle unbalanced panels:
```{r echo = TRUE, results='hide'}
# Finally, one should note that the test procedure also works for unbalanced panels.
# To illustrate this, we generate an unbalanced panel dataset by randomly selecting
# 70% of the observations from the balanced panel dataset:
random_indices <- sample(nrow(sim_data), 0.7*nrow(sim_data))
unbalanced_sim_data <- sim_data[random_indices, ]
meanEquivTest(Y = "Y", ID = "ID", G = "G", period = "period", X = c(5, 6),
data = unbalanced_sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, vcov = "HAC")
```
## References
Dette H., & Schumann M. (2024). "Testing for Equivalence of Pre-Trends in Difference-in-Differences Estimation." *Journal of Business & Economic Statistics*, 1–13. DOI: [10.1080/07350015.2024.2308121](https://doi.org/10.1080/07350015.2024.2308121)