Title: | Randomization Tests |
---|---|
Description: | A collection of randomization tests, data sets and examples. The current version focuses on three testing problems and their implementation in empirical work. First, it facilitates the empirical researcher to test for particular hypotheses, such as comparisons of means, medians, and variances from k populations using robust permutation tests, which asymptotic validity holds under very weak assumptions, while retaining the exact rejection probability in finite samples when the underlying distributions are identical. Second, the description and implementation of a permutation test for testing the continuity assumption of the baseline covariates in the sharp regression discontinuity design (RDD) as in Canay and Kamat (2017) <https://goo.gl/UZFqt7>. More specifically, it allows the user to select a set of covariates and test the aforementioned hypothesis using a permutation test based on the Cramer-von Miss test statistic. Graphical inspection of the empirical CDF and histograms for the variables of interest is also supported in the package. Third, it provides the practitioner with an effortless implementation of a permutation test based on the martingale decomposition of the empirical process for the goodness-of-fit testing problem with an estimated nuisance parameter. An application of this testing problem is the one of testing for heterogeneous treatment effects in a randomized control trial. |
Authors: | Mauricio Olivares-Gonzalez [aut, cre], Ignacio Sarmiento-Barbieri [aut] |
Maintainer: | Mauricio Olivares-Gonzalez <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.1.4 |
Built: | 2024-10-31 18:37:38 UTC |
Source: | https://github.com/ignaciomsarmiento/ratest |
Calculates the Cramer-von Mises test statistic
where and
are the empirical CDFs of the the sample of baseline covariates close to the cutoff from the left and right, respectively. See equation (12) in Canay and Kamat (2017).
CvM.stat(Sn)
CvM.stat(Sn)
Sn |
Numeric. The pooled sample of induced order statistics. The first column of S can be viewed as an independent sample of W conditional on Z being close to zero from the left. Similarly, the second column of S can be viewed as an independent sample of W conditional on Z being close to the cutoff from the right. See section 3 in Canay and Kamat (2017). |
Returns the numeric value of the Cramer - von Mises test statistic.
Maurcio Olivares Gonzalez
Ignacio Sarmiento Barbieri
Canay, I and Kamat V, (2017) Approximate Permutation Tests and Induced Order Statistics in the Regression Discontinuity Design. http://faculty.wcas.northwestern.edu/~iac879/wp/RDDPermutations.pdf
Calculates the empirical CDF of the sample of conditional on
being close to the cutoff from either the left or right. Given the induced order for the baseline covariates
or
, this function will calculate either
or
depending on the argument of the function. See section 3 in Canay & Kamat (2017).
H.cdf(W, t)
H.cdf(W, t)
W |
Numeric. The sample of induced order statistics. The input can be either |
t |
Numeric. The scalar needed for the calculation of the CDF. |
Numeric. For a sample , returns the fraction of observations less or equal to
.
Maurcio Olivares Gonzalez
Ignacio Sarmiento Barbieri
Canay, I and Kamat V, (2017) Approximate Permutation Tests and Induced Order Statistics in the Regression Discontinuity Design. http://faculty.wcas.northwestern.edu/~iac879/wp/RDDPermutations.pdf
Randomized experiments from non-random selection in U.S. House elections
A data frame with 6558 observations and two variables:
Democrat vote share election t+1
Running variable. Diff. democratic share
Democrat vote share t-1
Democrat win t-1
Democrat political experience t
Oppositions political experience t
Democrat electoral experience t
Oposition electoral experience t
Mostly Harmless Econometrics Data Archive: http://economics.mit.edu/faculty/angrist/data1/mhe
Lee, D. (2008) Randomized experiments from non-random selection in U.S. House elections, Journal of Econometrics, 142, 675-697
Plots a histogram and empirical cdf
## S3 method for class 'RDperm' plot(x, w, plot.class = "both", ...)
## S3 method for class 'RDperm' plot(x, w, plot.class = "both", ...)
x |
Object of class "RDperm" |
w |
Character. Name of variable to be plotted |
plot.class |
Character. Can be: "both" for a histogram and cdf plot, "hist" for a histogram or "cdf" for only the cdf plot |
... |
Additional ggplot2 controls |
Maurcio Olivares Gonzalez
Ignacio Sarmiento Barbieri
Canay, I and Kamat V, (2017) Approximate Permutation Tests and Induced Order Statistics in the Regression Discontinuity Design. http://faculty.wcas.northwestern.edu/~iac879/wp/RDDPermutations.pdf
## Not run: permtest<-RDperm(W=c("demshareprev","demwinprev"),z="difdemshare",data=lee2008) plot(permtest,w="demshareprev") ## End(Not run)
## Not run: permtest<-RDperm(W=c("demshareprev","demwinprev"),z="difdemshare",data=lee2008) plot(permtest,w="demshareprev") ## End(Not run)
A permutation test of the two-sample goodness-of-fit hypothesis in the presence of an estimated niusance parameter. The permutation test considered here is based on the Khmaladze transformation of the empirical process (Khmaladze (1981)), and adapted by Chung and Olivares-Gonzalez (2018).
PT.Khmaladze.fit(y1, y0, alpha = 0.05, n.perm = 999)
PT.Khmaladze.fit(y1, y0, alpha = 0.05, n.perm = 999)
y1 |
Numeric. A vector containing the response variable of the treatment group. |
y0 |
Numeric. A vector containing the response variable of the control group. |
alpha |
Numeric. Nominal level for the test. The default is 0.05. |
n.perm |
Numeric. Number of permutations needed for the stochastic approximation of the p-values. The default is n.perm=999. |
An object of class "PT.Khmaladze.fit" is a list containing at least the following components:
n_populations |
Number of grups. |
N |
Sample Size. |
T.obs |
Observed test statistic. |
shift |
The estimated nuisance parameter (average treatment effect). |
cv |
Critical Value. This value is used in the general construction of a randomization test. |
pvalue |
P-value. |
T.perm |
Vector. Test statistic recalculated for all permutations used in the stochastic approximation. |
n_perm |
Number of permutations. |
sample_sizes |
Groups size. |
Maurcio Olivares-Gonzalez
Ignacio Sarmiento Barbieri
Khmaladze, E. (1981). Martingale Approach in the Theory of Goodness-of-fit Tests. Theory of Probability and its Application, 26: 240–257. Chung, Eunyi and Mauricio Olivares (2018). Non-Parametric Hypothesis Testing with a Nuisance Parameter: A Permutation Test Approach. Working Paper.
## Not run: Y0 <- rnorm(100, 1, 1) # Treatment Group with constant shift equals to 1 Y1 <- Y0 + 1 Tx = sample(100) <= 0.5*(100) # Observed Outcome Y = ifelse( Tx, Y1, Y0 ) dta <- data.frame(Y = Y, Z = as.numeric(Tx)) pt.GoF<-PT.Khmaladze.fit(dta$Y[dta$Z==1],data$Y[dta$Z==0],n.perm = 49) summary(pt.GoF) ## End(Not run)
## Not run: Y0 <- rnorm(100, 1, 1) # Treatment Group with constant shift equals to 1 Y1 <- Y0 + 1 Tx = sample(100) <= 0.5*(100) # Observed Outcome Y = ifelse( Tx, Y1, Y0 ) dta <- data.frame(Y = Y, Z = as.numeric(Tx)) pt.GoF<-PT.Khmaladze.fit(dta$Y[dta$Z==1],data$Y[dta$Z==0],n.perm = 49) summary(pt.GoF) ## End(Not run)
A permutation test for continuity of covariates in Sharp Regression Discontinuity Design as described in Canay and Kamat (2017).
RDperm(W, z, data, n.perm = 499, q_type = 10, cutoff = 0, test.statistic = "CvM")
RDperm(W, z, data, n.perm = 499, q_type = 10, cutoff = 0, test.statistic = "CvM")
W |
Character. Vector of covariates names. The procedure will test the null hypothesis of continuity of the distribution of each element in W at the cutoff. |
z |
Character. Running variable name. This is the scalar random variable that defines, along with the cutoff, the treatment assignment rule in the sharp regression discontinuity design. |
data |
Data.frame. |
n.perm |
Numeric. Number of permutations needed for the stochastic approximation of the p-values. See remark 3.2 in Canay and Kamat (2017). The default is B=499. |
q_type |
A fixed and small (relative to the sample size) natural number that will define the |
cutoff |
Numeric. The scalar defining the threshold of the running variable. |
test.statistic |
Character. A rank test statistic satisfying rank invariance. The default is a Cramer-von Mises test statistic. |
The functions summary
and plot
are used to obtain and print a summary and plot of
the estimated regression discontinuity. The object of class RDperm
is a list
containing the following components:
results |
Matrix. Test Statistic, P-values and Q |
test.statistic |
Test Statistic |
q_type |
Type of Q used in the calculations, can be either, "Defined by User", the "Rule of Thumb" or the "Alternative Rule of Thumb". |
n_perm |
number of permutations |
rv |
Character. Running variable name |
Z |
Vector. Running Variable |
cutoff |
cutoff |
data |
data set |
S |
Matrix. Pooled sample of induced order statistics |
S_perm |
List. Permutations of the induced order statistic. |
Maurcio Olivares Gonzalez
Ignacio Sarmiento Barbieri
Canay, I and Kamat V, (2017) Approximate Permutation Tests and Induced Order Statistics in the Regression Discontinuity Design. http://faculty.wcas.northwestern.edu/~iac879/wp/RDDPermutations.pdf
permtest<-RDperm(W=c("demshareprev"),z="difdemshare",data=lee2008) summary(permtest) ## Not run: permtest<-RDperm(W=c("demshareprev","demwinprev"),z="difdemshare",data=lee2008) summary(permtest) ## End(Not run)
permtest<-RDperm(W=c("demshareprev"),z="difdemshare",data=lee2008) summary(permtest) ## Not run: permtest<-RDperm(W=c("demshareprev","demwinprev"),z="difdemshare",data=lee2008) summary(permtest) ## End(Not run)
This function considers the k-sample problem of comparing general parameters, such as means, medians, or parameters that depend on the joint distribution using permutation tests. Under weak assumptions for comparing estimator, the permutation tests implemented here provide a general test procedure whereby the asymptotic validity of the permutation test holds while retaining the exact rejection probability in finite samples when the underlying distributions are identical.
Here we will consider three test for the 2 sample case, but the function works for k-samples.
Difference of means: Here, the null hypothesis is of the form , and the corresponding test statistic is given by
where and
are the sample means from population
and population
, respectively, and
is a consistent estimator of
\sigma^2(P)$ when $X_1,\dots,X_m
are i.i.d. from . Assume consitency also under
.
Difference of medians: Let and
be the CDFs corresponding to
and
, and denote
the median of
i.e.
. Assume that
is continuously differentiable at
with derivative
(and the same with
replaced by
). Here, the null hypothesis is of the form
, and the corresponding test statistic is given by
where is a consistent estimator of
:
Choices of may include the kernel estimator of Devroye and Wagner (1980), the bootstrap estimator of Efron (1992), or the smoothed bootstrap Hall et al. (1989) to list a few. For further details, see Chung and Romano (2013). Current implementation uses the bootstrap estimator of Efron (1992)
Difference of variances: Here, the null hypothesis is of the form , and the corresponding test statistic is given by
where the sample analog of
based on an iid sample
from
. Similarly for
.
We could also have the case when the parameter of interest is a function of the joint distribution. The examples considered here are
Lehmann (1951) two-sample U statistics: Consider testing , or the more general hypothesis that
and
only differ in location against the alternative that the
's are more spread out than the
's. The null hypothesis is of the form
.
Two-sample Wilcoxon statistic, where the null hypothesis is of the form
.
Two-sample Wilcoxon statistic without continuity assumption. In this case, the null hypothesis is of the form
.
Hollander (1967) two-sample U statistics. The null hypothesis is of the form
.
RPT(formula, data, test = "means", n.perm = 499, na.action, wilcoxon.option = "continuity")
RPT(formula, data, test = "means", n.perm = 499, na.action, wilcoxon.option = "continuity")
formula |
a formula object, with the response on the left of a ~ operator, and the groups on the right. |
data |
a data.frame in which to interpret the variables named in the formula. If this is missing, then the variables in the formula should be on the search list. |
test |
test to be perfomed. Multiple options are available, depending on the nature of the testing problem. In general, we have two types of problem. First, when the researcher is interested in comparing parameters. In this case, "means" will perform a Difference of Means, "medians" a Difference of Medians, "variances" a Difference of Variances. This case allows for 2 or more population comparisons. For the test of difference of medians the Efron (1992) bootstrap estimator is used to estimate the variances (for further details, see Chung and Romano (2013)). Second, when the parameter of interest is a function of the joint distribution. In this case, "lehmann.2S.test" will perform Lehmann (1951) two-sample U statistics, "wilcoxon.2s.test" the two-sample Wilcoxon test (with or without continuity assumption), and "hollander.2S.test" Hollander (1967) two sample U statistics. In this case, only 2 sample comparisons are permitted. |
n.perm |
Numeric. Number of permutations needed for the stochastic approximation of the p-values. See remark 3.2 in Canay and Kamat (2017). The default is n.perm=499. |
na.action |
a function to filter missing data. This is applied to the model.frame . The default is na.omit, which deletes observations that contain one or more missing values. |
wilcoxon.option |
Continuity assumption for Wilcoxon test" with continuity ("continuity") or without ("discontinuity"). The default is "continuity" |
An object of class "RPT" is a list containing at least the following components:
description |
Type of test, can be Difference of Means, Medians, or Variances. |
n_populations |
Number of grups. |
N |
Sample Size. |
T.obs |
Observed test statistic. |
pvalue |
P-value. |
T.perm |
Vector. Test statistics from the permutations. |
n_perm |
Number of permutations. |
parameters |
Estimated parameters. |
sample_sizes |
Groups lengths. |
Maurcio Olivares Gonzalez
Ignacio Sarmiento Barbieri
Chung, E. and Romano, J. P. (2013). Exact and asymptotically robust permutation tests. The Annals of Statistics, 41(2):484–507. Chung, E. and Romano, J. P. (2016). Asymptotically valid and exact permutation tests based on two-sample u-statistics. Journal of Statistical Planning and Inference, 168:97–105. Devroye, L. P. and Wagner, T. J. (1980). The strong uniform consistency of kernel density estimates. In Multivariate Analysis V: Proceedings of the fifth International Symposium on Multivariate Analysis, volume 5, pages 59–77. Efron, B. (1992). Bootstrap methods: another look at the jackknife. In Breakthroughs in statistics, pages 569–593. Springer. Hall, P., DiCiccio, T. J., and Romano, J. P. (1989). On smoothing and the bootstrap. The Annals of Statistics, pages 692–704. Hollander, M. (1967). Asymptotic efficiency of two nonparametric competitors of wilcoxon’s two sample test. Journal of the American Statistical Association, 62(319):939–949. Lehmann, E. L. (1951). Consistency and unbiasedness of certain nonparametric tests. The Annals of Mathematical Statistics, pages 165–179.
## Not run: male<-rnorm(50,1,1) female<-rnorm(50,1,2) dta<-data.frame(group=c(rep(1,50),rep(2,50)),outcome=c(male,female)) rpt.var<-RPT(dta$outcome~dta$group,test="variances") summary(rpt.var) ## End(Not run)
## Not run: male<-rnorm(50,1,1) female<-rnorm(50,1,2) dta<-data.frame(group=c(rep(1,50),rep(2,50)),outcome=c(male,female)) rpt.var<-RPT(dta$outcome~dta$group,test="variances") summary(rpt.var) ## End(Not run)
summary
method for class "RDPerm"
## S3 method for class 'RDperm' summary(object, digits = max(3, getOption("digits") - 3), ...)
## S3 method for class 'RDperm' summary(object, digits = max(3, getOption("digits") - 3), ...)
object |
an object of class |
digits |
number of digits to display |
... |
unused |
summary.RDperm
returns an object of class "summary.RDperm
" which has the following components
results |
Matrix with the Test Statistic, P-values and Q used |
Maurcio Olivares Gonzalez
Ignacio Sarmiento Barbieri