Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Request follow-up for #30: Various minor fixes #42

Merged
merged 10 commits into from
Oct 22, 2023
13 changes: 10 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ Imports:
Suggests:
knitr,
rmarkdown
Author: Ashesh Rambachan <ashesh.a.rambachan@gmail.com>
Maintainer: Ashesh Rambachan <ashesh.a.rambachan@gmail.com>
Authors@R:
c(person(given = "Ashesh",
family = "Rambachan",
role = c("aut", "cph", "cre"),
email = "ashesh.a.rambachan@gmail.com"),
person(given = "Jonathan",
family = "Roth",
role = c("aut", "cph"),
email = "jonathan_roth@brown.edu"))
Description:
Provides functions to conduct robust inference in difference-in-differences and event study designs by implementing the methods developed in Rambachan & Roth (2023, RESTUD), "A More Credible Approach to Parallel Trends" [Previously titled "An Honest Approach...". Inference is conducted under a weaker version of the parallel trends assumption. Uniformly valid confidence sets are constructed based upon conditional confidence sets, fixed-length confidence sets and hybridized confidence sets.
Provides functions to conduct robust inference in difference-in-differences and event study designs by implementing the methods developed in Rambachan & Roth (2023, RESTUD), "A More Credible Approach to Parallel Trends" [Previously titled "An Honest Approach..."]. Inference is conducted under a weaker version of the parallel trends assumption. Uniformly valid confidence sets are constructed based upon conditional confidence sets, fixed-length confidence sets and hybridized confidence sets.
Encoding: UTF-8
LazyData: true
VignetteBuilder:
Expand Down
2 changes: 1 addition & 1 deletion R/HonestDiD-Temp.R
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ createEventStudyPlot <- function(betahat, stdErrors = NULL, sigma = NULL,
stdErrors = sqrt(diag(sigma))
}

if (useRelativeEventTime == T) {
if (useRelativeEventTime == TRUE) {
timeVec = timeVec - referencePeriod
referencePeriod = 0
}
Expand Down
4 changes: 2 additions & 2 deletions R/arp-nonuisance.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

# Run the first-stage test
if (base::max(A_firststage %*% y - d_firststage) > 0) {
reject <- T
reject <- TRUE
} else {
# Per ARP (2021), CV = max(0, c_{1-alpha-tilde}), where alpha-tilde = (alpha - kappa)/(1-kappa)
# quantile of truncated normal that accounts for failing to reject in the first stage.
Expand Down Expand Up @@ -181,7 +181,7 @@
}

# Compute length, else return grid
if (returnLength == T) {
if (returnLength == TRUE) {
gridLength <- 0.5 * ( base::c(0, base::diff(thetaGrid)) + base::c(base::diff(thetaGrid), 0 ) )
base::return(base::sum(resultsGrid[, 2]*gridLength))
} else {
Expand Down
8 changes: 4 additions & 4 deletions R/arp-nuisance.R
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
eta_vec = base::apply(-xi.draws, 1, .compute_eta, f, C)

# We compute the 1-kappa quantile of eta_vec and return this value
base::return(stats::quantile(eta_vec, probs=1-hybrid_kappa, names=FALSE, na.rm=T))
base::return(stats::quantile(eta_vec, probs=1-hybrid_kappa, names=FALSE, na.rm=TRUE))
}
}

Expand Down Expand Up @@ -513,11 +513,11 @@

# Store which moments are binding: Do so using lambda rather than the moments.
B_index = (linSoln$lambda > tol_lambda)
Bc_index = (B_index == F)
Bc_index = (B_index == FALSE)
X_TB = base::matrix( X_T_ARP[B_index,], ncol = base::ncol(X_T_ARP) ) # select binding moments
# Check whether binding moments have full rank.
if (base::is.vector(X_TB)) {
fullRank_flag = F
fullRank_flag = FALSE
} else {
fullRank_flag = (Matrix::rankMatrix(X_TB) == base::min(base::dim( X_TB ) ))
}
Expand Down Expand Up @@ -761,7 +761,7 @@
}

# Compute length, else return grid
if (returnLength == T) {
if (returnLength == TRUE) {
gridLength <- 0.5 * ( base::c(0, base::diff(thetaGrid)) + base::c(base::diff(thetaGrid), 0 ) )
base::return(base::sum(resultsGrid[, 2]*gridLength))
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/delta_utility_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}
}

# If postPeriodMomentsOnly == T, exclude moments that only involve pre-periods
# If postPeriodMomentsOnly == TRUE, exclude moments that only involve pre-periods
if(postPeriodMomentsOnly){
postPeriodIndices <- (numPrePeriods +1):base::NCOL(A_M)
prePeriodOnlyRows <- base::which( base::rowSums( A_M[ , postPeriodIndices] != 0 ) == 0 )
Expand Down
18 changes: 9 additions & 9 deletions R/deltarm.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

# Delta^{RM} functions -----------------------------------------------
.create_A_RM <- function(numPrePeriods, numPostPeriods,
Mbar = 1, s, max_positive = T,
dropZero = T) {
Mbar = 1, s, max_positive = TRUE,
dropZero = TRUE) {
# This function creates a matrix for the linear constraints that
# \delta \in Delta^RM_{s,(.)}(Mbar), where (.) is + if max_positve = T and (-) if max_positive = F.
# \delta \in Delta^RM_{s,(.)}(Mbar), where (.) is + if max_positve = TRUE and (-) if max_positive = FALSE.
#
# Inputs:
# numPrePeriods = number of pre-periods. This is an element of resultsObjects.
Expand All @@ -27,11 +27,11 @@
Atilde[r, r:(r+1)] = c(-1, 1)
}

# Create a vector to extract the max first dif, which corresponds with the first dif for period s, or minus this if max_positive == F
# Create a vector to extract the max first dif, which corresponds with the first dif for period s, or minus this if max_positive == FALSE
v_max_dif <- base::matrix(0, nrow = 1, ncol = numPrePeriods + numPostPeriods + 1)
v_max_dif[(numPrePeriods+s):(numPrePeriods+1+s)] <- c(-1,1)

if (max_positive == F){
if (max_positive == FALSE){
v_max_dif <- -v_max_dif
}

Expand All @@ -55,9 +55,9 @@
}
}

.create_d_RM <- function(numPrePeriods, numPostPeriods, dropZero = T){
.create_d_RM <- function(numPrePeriods, numPostPeriods, dropZero = TRUE){
# This function creates a vector for the linear constraints that
# delta is in Delta^RM_{s,(.)}(Mbar), where (.) is + if max_positve = T and - if max_positive = F.
# delta is in Delta^RM_{s,(.)}(Mbar), where (.) is + if max_positve = TRUE and - if max_positive = FALSE.
# It implements this using the general characterization of d, NOT the sharp
# characterization of the identified set.
#
Expand Down Expand Up @@ -241,7 +241,7 @@
computeConditionalCS_DeltaRM <- function(betahat, sigma, numPrePeriods, numPostPeriods,
l_vec = .basisVector(index = 1, size = numPostPeriods), Mbar = 0,
alpha = 0.05, hybrid_flag = "LF", hybrid_kappa = alpha/10,
returnLength = F, postPeriodMomentsOnly = T,
returnLength = FALSE, postPeriodMomentsOnly = TRUE,
gridPoints = 10^3, grid.ub = NA, grid.lb = NA) {
# This function computes the ARP CI that includes nuisance parameters
# for Delta^{RM}(Mbar). This functions uses ARP_computeCI for all
Expand Down Expand Up @@ -304,7 +304,7 @@ computeConditionalCS_DeltaRM <- function(betahat, sigma, numPrePeriods, numPostP
CI_RM = tibble::tibble(grid = base::seq(grid.lb, grid.ub, length.out = gridPoints),
accept = base::pmax(CIs_RM_plus_maxS, CIs_RM_minus_maxS))

# Compute length if returnLength == T, else return grid
# Compute length if returnLength == TRUE, else return grid
if (returnLength) {
gridLength <- 0.5 * ( base::c(0, base::diff(CI_RM$grid)) + base::c(base::diff(CI_RM$grid), 0 ) )
base::return(base::sum(CI_RM$accept*gridLength))
Expand Down
20 changes: 10 additions & 10 deletions R/deltarmb.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

# Delta^{RMB} functions -----------------------------------------------
.create_A_RMB <- function(numPrePeriods, numPostPeriods,
Mbar = 1, s, max_positive = T,
dropZero = T, biasDirection) {
Mbar = 1, s, max_positive = TRUE,
dropZero = TRUE, biasDirection) {
# This function creates a matrix for the linear constraints that
# \delta \in Delta^RMB_{s,(.)}(Mbar), where (.) is + if max_positve = T and (-) if max_positive = F.
# \delta \in Delta^RMB_{s,(.)}(Mbar), where (.) is + if max_positve = TRUE and (-) if max_positive = FALSE.
#
# Inputs:
# numPrePeriods = number of pre-periods. This is an element of resultsObjects.
Expand All @@ -30,11 +30,11 @@
Atilde[r, r:(r+1)] = base::c(-1, 1)
}

# Create a vector to extract the max first dif, which corresponds with the first dif for period s, or minus this if max_positive == F
# Create a vector to extract the max first dif, which corresponds with the first dif for period s, or minus this if max_positive == FALSE
v_max_dif <- base::matrix(0, nrow = 1, ncol = numPrePeriods + numPostPeriods + 1)
v_max_dif[(numPrePeriods+s):(numPrePeriods+1+s)] <- c(-1,1)

if (max_positive == F){
if (max_positive == FALSE){
v_max_dif <- -v_max_dif
}

Expand Down Expand Up @@ -65,9 +65,9 @@
}
}

.create_d_RMB <- function(numPrePeriods, numPostPeriods, dropZero = T){
.create_d_RMB <- function(numPrePeriods, numPostPeriods, dropZero = TRUE){
# This function creates a vector for the linear constraints that
# delta is in Delta^RMB_{s,(.)}(Mbar), where (.) is + if max_positve = T and - if max_positive = F.
# delta is in Delta^RMB_{s,(.)}(Mbar), where (.) is + if max_positve = TRUE and - if max_positive = FALSE.
# It implements this using the general characterization of d, NOT the sharp
# characterization of the identified set.
#
Expand Down Expand Up @@ -258,8 +258,8 @@
computeConditionalCS_DeltaRMB <- function(betahat, sigma, numPrePeriods, numPostPeriods,
l_vec = .basisVector(index = 1, size = numPostPeriods), Mbar = 0,
alpha = 0.05, hybrid_flag = "LF", hybrid_kappa = alpha/10,
returnLength = F, biasDirection = "positive",
postPeriodMomentsOnly = T,
returnLength = FALSE, biasDirection = "positive",
postPeriodMomentsOnly = TRUE,
gridPoints = 10^3, grid.ub = NA, grid.lb = NA) {
# This function computes the ARP CI that includes nuisance parameters
# for Delta^{MB}(Mbar). This functions uses ARP_computeCI for all
Expand Down Expand Up @@ -323,7 +323,7 @@ computeConditionalCS_DeltaRMB <- function(betahat, sigma, numPrePeriods, numPost
CI_RMB = tibble::tibble(grid = base::seq(grid.lb, grid.ub, length.out = gridPoints),
accept = base::pmax(CIs_RMB_plus_maxS, CIs_RMB_minus_maxS))

# Compute length if returnLength == T, else return grid
# Compute length if returnLength == TRUE, else return grid
if (returnLength) {
gridLength <- 0.5 * ( base::c(0, base::diff(CI_RMB$grid)) + base::c(base::diff(CI_RMB$grid), 0 ) )
base::return(base::sum(CI_RMB$accept*gridLength))
Expand Down
18 changes: 9 additions & 9 deletions R/deltarmm.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

# Delta^{RMM} functions -----------------------------------------------
.create_A_RMM <- function(numPrePeriods, numPostPeriods,
Mbar = 1, s, max_positive = T,
dropZero = T, monotonicityDirection) {
Mbar = 1, s, max_positive = TRUE,
dropZero = TRUE, monotonicityDirection) {
# This function creates a matrix for the linear constraints that
# \delta \in Delta^RM_{s,(.)}(Mbar), where (.) is + if max_positve = T and (-) if max_positive = F.
# \delta \in Delta^RM_{s,(.)}(Mbar), where (.) is + if max_positve = TRUE and (-) if max_positive = FALSE.
#
# Inputs:
# numPrePeriods = number of pre-periods. This is an element of resultsObjects.
Expand All @@ -29,11 +29,11 @@
Atilde[r, r:(r+1)] = c(-1, 1)
}

# Create a vector to extract the max first dif, which corresponds with the first dif for period s, or minus this if max_positive == F
# Create a vector to extract the max first dif, which corresponds with the first dif for period s, or minus this if max_positive == FALSE
v_max_dif <- base::matrix(0, nrow = 1, ncol = numPrePeriods + numPostPeriods + 1)
v_max_dif[(numPrePeriods+s):(numPrePeriods+1+s)] <- c(-1,1)

if (max_positive == F){
if (max_positive == FALSE){
v_max_dif <- -v_max_dif
}

Expand Down Expand Up @@ -63,9 +63,9 @@
}
}

.create_d_RMM <- function(numPrePeriods, numPostPeriods, dropZero = T){
.create_d_RMM <- function(numPrePeriods, numPostPeriods, dropZero = TRUE){
# This function creates a vector for the linear constraints that
# delta is in Delta^RM_{s,(.)}(Mbar), where (.) is + if max_positve = T and - if max_positive = F.
# delta is in Delta^RM_{s,(.)}(Mbar), where (.) is + if max_positve = TRUE and - if max_positive = FALSE.
# It implements this using the general characterization of d, NOT the sharp
# characterization of the identified set.
#
Expand Down Expand Up @@ -257,7 +257,7 @@
computeConditionalCS_DeltaRMM <- function(betahat, sigma, numPrePeriods, numPostPeriods,
l_vec = .basisVector(index = 1, size = numPostPeriods), Mbar = 0,
alpha = 0.05, hybrid_flag = "LF", hybrid_kappa = alpha/10,
returnLength = F, postPeriodMomentsOnly = T, monotonicityDirection = "increasing",
returnLength = FALSE, postPeriodMomentsOnly = TRUE, monotonicityDirection = "increasing",
gridPoints = 10^3, grid.ub = NA, grid.lb = NA) {
# This function computes the ARP CI that includes nuisance parameters
# for Delta^{RMM}(Mbar). This functions uses ARP_computeCI for all
Expand Down Expand Up @@ -320,7 +320,7 @@ computeConditionalCS_DeltaRMM <- function(betahat, sigma, numPrePeriods, numPost
CI_RMM = tibble::tibble(grid = base::seq(grid.lb, grid.ub, length.out = gridPoints),
accept = base::pmax(CIs_RMM_plus_maxS, CIs_RMM_minus_maxS))

# Compute length if returnLength == T, else return grid
# Compute length if returnLength == TRUE, else return grid
if (returnLength) {
gridLength <- 0.5 * ( base::c(0, base::diff(CI_RMM$grid)) + base::c(base::diff(CI_RMM$grid), 0 ) )
base::return(base::sum(CI_RMM$accept*gridLength))
Expand Down
14 changes: 7 additions & 7 deletions R/deltasd.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# In this section, we implement helper functions to place testing with
# Delta^{SD}(M) into the form needed to use the ARP functions.

.create_A_SD <- function(numPrePeriods, numPostPeriods, postPeriodMomentsOnly = F) {
.create_A_SD <- function(numPrePeriods, numPostPeriods, postPeriodMomentsOnly = FALSE) {
# This function creates a matrix for the linear constraints that \delta \in Delta^SD(M).
# It implements this using the general characterization of A, NOT the sharp
# characterization of the identified set.
Expand All @@ -31,7 +31,7 @@
}
Atilde = Atilde[, -(numPrePeriods+1)]

# If postPeriodMomentsOnly == T, exclude moments that only involve pre-periods
# If postPeriodMomentsOnly == TRUE, exclude moments that only involve pre-periods
if(postPeriodMomentsOnly){
postPeriodIndices <- (numPrePeriods +1):base::NCOL(Atilde)
prePeriodOnlyRows <- base::which( base::rowSums( Atilde[ , postPeriodIndices] != 0 ) == 0 )
Expand All @@ -43,7 +43,7 @@
base::return(A)
}

.create_d_SD <- function(numPrePeriods, numPostPeriods, M, postPeriodMomentsOnly = F) {
.create_d_SD <- function(numPrePeriods, numPostPeriods, M, postPeriodMomentsOnly = FALSE) {
# This function creates a vector for the linear constraints that \delta \in Delta^SD(M).
# It implements this using the general characterization of d, NOT the sharp
# characterization of the identified set.
Expand Down Expand Up @@ -129,8 +129,8 @@
computeConditionalCS_DeltaSD <- function(betahat, sigma, numPrePeriods, numPostPeriods,
l_vec = .basisVector(index = 1, size = numPostPeriods),
M = 0, alpha = 0.05, hybrid_flag = "FLCI",
hybrid_kappa = alpha/10, returnLength = F,
postPeriodMomentsOnly = T,
hybrid_kappa = alpha/10, returnLength = FALSE,
postPeriodMomentsOnly = TRUE,
gridPoints =10^3, grid.ub = NA, grid.lb = NA) {
# This function computes the ARP CI that includes nuisance parameters
# for Delta^{SD}(M). This functions uses ARP_computeCI for all
Expand All @@ -154,8 +154,8 @@ computeConditionalCS_DeltaSD <- function(betahat, sigma, numPrePeriods, numPostP
# data_frame containing upper and lower bounds of CI.

# Construct A_SD, d_SD
A_SD = .create_A_SD(numPrePeriods = numPrePeriods, numPostPeriods = numPostPeriods, postPeriodMomentsOnly = F)
d_SD = .create_d_SD(numPrePeriods = numPrePeriods, numPostPeriods = numPostPeriods, M = M, postPeriodMomentsOnly = F)
A_SD = .create_A_SD(numPrePeriods = numPrePeriods, numPostPeriods = numPostPeriods, postPeriodMomentsOnly = FALSE)
d_SD = .create_d_SD(numPrePeriods = numPrePeriods, numPostPeriods = numPostPeriods, M = M, postPeriodMomentsOnly = FALSE)

if (postPeriodMomentsOnly & numPostPeriods > 1){
postPeriodIndices <- (numPrePeriods +1):base::NCOL(A_SD)
Expand Down
12 changes: 6 additions & 6 deletions R/deltasdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# In this section, we implement helper functions to place testing with
# Delta^{SDB}(M) into the form needed to use the ARP functions.
.create_A_SDB <- function(numPrePeriods, numPostPeriods,
biasDirection = "positive", postPeriodMomentsOnly = F) {
biasDirection = "positive", postPeriodMomentsOnly = FALSE) {
# This function creates a matrix for the linear constraints that \delta \in Delta^SDB(M).
# It implements this using the general characterization of A.
#
Expand All @@ -29,7 +29,7 @@
base::return(A)
}

.create_d_SDB <- function(numPrePeriods, numPostPeriods, M, postPeriodMomentsOnly = F) {
.create_d_SDB <- function(numPrePeriods, numPostPeriods, M, postPeriodMomentsOnly = FALSE) {
# This function creates a vector for the linear constraints that \delta \in Delta^SDB(M).
# It implements this using the general characterization of d.
#
Expand Down Expand Up @@ -112,8 +112,8 @@
computeConditionalCS_DeltaSDB <- function(betahat, sigma, numPrePeriods, numPostPeriods,
M = 0, l_vec = .basisVector(index = 1, size=numPostPeriods),
alpha = 0.05, hybrid_flag = "FLCI", hybrid_kappa = alpha/10,
returnLength = F, biasDirection = "positive",
postPeriodMomentsOnly = T,
returnLength = FALSE, biasDirection = "positive",
postPeriodMomentsOnly = TRUE,
gridPoints = 10^3, grid.lb = NA, grid.ub = NA) {
# This function computes the ARP CI that includes nuisance parameters
# for Delta^{SDPB}(M). This functions uses ARP_computeCI for all
Expand All @@ -140,8 +140,8 @@ computeConditionalCS_DeltaSDB <- function(betahat, sigma, numPrePeriods, numPost
# Construct A_SDB, d_SDB
A_SDB = .create_A_SDB(numPrePeriods = numPrePeriods, numPostPeriods = numPostPeriods,
biasDirection = biasDirection,
postPeriodMomentsOnly = F)
d_SDB = .create_d_SDB(numPrePeriods = numPrePeriods, numPostPeriods = numPostPeriods, M = M, postPeriodMomentsOnly = F)
postPeriodMomentsOnly = FALSE)
d_SDB = .create_d_SDB(numPrePeriods = numPrePeriods, numPostPeriods = numPostPeriods, M = M, postPeriodMomentsOnly = FALSE)

if (postPeriodMomentsOnly & numPostPeriods > 1) {
postPeriodIndices <- (numPrePeriods +1):base::NCOL(A_SDB)
Expand Down
Loading