| Title: | Hierarchically Regularized Entropy Balancing |
|---|---|
| Description: | Implements hierarchically regularized entropy balancing proposed by Xu and Yang (2022) <doi:10.1017/pan.2022.12>. The method adjusts the covariate distributions of the control group to match those of the treatment group. 'hbal' automatically expands the covariate space to include higher order terms and uses cross-validation to select variable penalties for the balancing conditions. |
| Authors: | Yiqing Xu [aut, cre] (ORCID: <https://orcid.org/0000-0003-2041-6671>), Eddie Yang [aut] |
| Maintainer: | Yiqing Xu <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.2.15 |
| Built: | 2026-05-20 08:26:47 UTC |
| Source: | https://github.com/xuyiqing/hbal |
Function to load package description.
.onAttach(lib, pkg).onAttach(lib, pkg)
lib |
libname |
pkg |
package name |
Xu, Y., & Yang, E. (2022). Hierarchically Regularized Entropy Balancing. Political Analysis, 1-8. doi:10.1017/pan.2022.12
att estimates the average treatment effect on the treated (ATT) from an
hbal object returned by hbal.
att(hbalobject, method="lm_robust", dr=TRUE, displayAll=FALSE, alpha=0.9, ...)att(hbalobject, method="lm_robust", dr=TRUE, displayAll=FALSE, alpha=0.9, ...)
hbalobject |
an object of class |
method |
estimation method for the ATT. Default is the Lin (2016) estimator. |
dr |
doubly robust, whether an outcome model is included in estimating the ATT. |
displayAll |
only displays treatment effect by default. |
alpha |
tuning paramter for glmnet |
... |
arguments passed to lm_lin or lm_robust |
This is a wrapper for lm_robust and lm_lin from the estimatr package.
A matrix of estimates with their robust standard errors
Yiqing Xu, Eddie Yang
#EXAMPLE 1 set.seed(1984) N <- 500 X1 <- rnorm(N) X2 <- rbinom(N,size=1,prob=.5) X <- cbind(X1, X2) treat <- rbinom(N, 1, prob=0.5) # Treatment indicator y <- 0.5 * treat + X[,1] + X[,2] + rnorm(N) # Outcome dat <- data.frame(treat=treat, X, Y=y) out <- hbal(Treat = 'treat', X = c('X1', 'X2'), Y = 'Y', data=dat) sout <- summary(att(out))#EXAMPLE 1 set.seed(1984) N <- 500 X1 <- rnorm(N) X2 <- rbinom(N,size=1,prob=.5) X <- cbind(X1, X2) treat <- rbinom(N, 1, prob=0.5) # Treatment indicator y <- 0.5 * treat + X[,1] + X[,2] + rnorm(N) # Outcome dat <- data.frame(treat=treat, X, Y=y) out <- hbal(Treat = 'treat', X = c('X1', 'X2'), Y = 'Y', data=dat) sout <- summary(att(out))
Data on the contender judges from Black and Owens (2016): Courting the president: how circuit court judges alter their behavior for promotion to the Supreme Court
This dataset includes 10,171 period-judge observations for a total of 68 judges.
The treatment variable of interest is treatFinal0, which indicates whether there was a vacancy in the Supreme Court
The outcome of interest is ideological alignment of judges' votes with the sitting President (presIdeoVote).
The remaining variables are characteristics of the judges and courts, to be used as controls.
A data frame with 10171 rows and 10 columns.
ideological alignment of judges' votes with the sitting President (outcome)
treatment indicator for vacancy period
judge’s Judicial Common Space (JCS)score
Ideological distribution of the sitting President
ideological composition of the panel with whom the judge sat
median JCS score of the circuit judges
JCS score of the median justice on the Supreme Court
indicator for whether the case decision was reversed by the circuit court
indicator for the publication status of thecourt’s opinion
name of the judge
Black, R. C., and Owens, R. J. (2016). Courting the president: how circuit court judges alter their behavior for promotion to the Supreme Court. American Journal of Political Science, 60(1), 30-43.
Internal function called by hbal to serially expand covariates.
covarExclude(colname, exclude)covarExclude(colname, exclude)
colname |
column name. |
exclude |
list of covariate name pairs or triplets to be excluded. |
Logical
Yiqing Xu, Eddie Yang
Internal function called by hbal to serially expand covariates.
covarExpand(X, exp.degree = 3, treatment = NULL, exclude = NULL)covarExpand(X, exp.degree = 3, treatment = NULL, exclude = NULL)
X |
matrix of covariates. |
exp.degree |
the degree of the polynomial. |
treatment |
treatment indicator |
exclude |
list of covariate name pairs or triplets to be excluded. |
A matrix of serially expanded covariates
Yiqing Xu, Eddie Yang
Internal function called by hbal to select ridge penalties through cross-validation.
crossValidate( group.alpha = NULL, penalty.pos = NULL, penalty.val = NULL, group.exact = NULL, grouping = NULL, folds = NULL, treatment = NULL, fold.co = NULL, fold.tr = NULL, coefs = NULL, control = NULL, constraint.tolerance = NULL, print.level = NULL, base.weight = NULL, full.t = NULL, full.c = NULL, shuffle.treat = NULL )crossValidate( group.alpha = NULL, penalty.pos = NULL, penalty.val = NULL, group.exact = NULL, grouping = NULL, folds = NULL, treatment = NULL, fold.co = NULL, fold.tr = NULL, coefs = NULL, control = NULL, constraint.tolerance = NULL, print.level = NULL, base.weight = NULL, full.t = NULL, full.c = NULL, shuffle.treat = NULL )
group.alpha |
group.alpha. Controls degree of regularization. |
penalty.pos |
positions of user-supplied penalties. |
penalty.val |
values of user-supplied penalties. |
group.exact |
binary indicator of whether each covariate group should be penalized. |
grouping |
different groupings of the covariates. |
folds |
number of folds to perform cross validation. |
treatment |
covariate matrix for treatment group. |
fold.co |
fold assignments for control units. |
fold.tr |
fold assignments for treated units. |
coefs |
starting coefficients (lambda). |
control |
covariate matrix for control group. |
constraint.tolerance |
tolerance level for imbalance. |
print.level |
details of printed output. |
base.weight |
target weight distribution for the control units. |
full.t |
(unresidualized) ovariate matrix for treatment group. |
full.c |
(unresidualized) ovariate matrix for control group. |
shuffle.treat |
whether to create folds for the treated units |
group.alpha, lambda
Yiqing Xu, Eddie Yang
Internal function called by hbal to perform double selection.
doubleSelection(X, W, Y, grouping)doubleSelection(X, W, Y, grouping)
X |
covaraite matrix |
W |
treatment indicator |
Y |
outcome variable |
grouping |
groupings of covariates |
resX, penalty.list, covar.keep
Yiqing Xu, Eddie Yang
hbal performs hierarchically regularized entropy balancing
such that the covariate distributions of the control group match those of the
treatment group. hbal automatically expands the covariate space to include
higher order terms and uses cross-validation to select variable penalties for the
balancing conditions.
hbal performs hierarchically regularized entropy balancing such that the covariate distributions of the control group match those of the treatment group. hbal automatically expands the covariate space to include higher order terms and uses cross-validation to select variable penalties for the balancing conditions.
hbal(data, Treat, X, Y = NULL, w = NULL, X.expand = NULL, X.keep = NULL, expand.degree = 1, coefs = NULL, max.iterations = 200, cv = NULL, folds = 4, ds = FALSE, group.exact = NULL, group.alpha = NULL, term.alpha = NULL, constraint.tolerance = 1e-3, print.level = 0, grouping = NULL, group.labs = NULL, linear.exact = TRUE, shuffle.treat = TRUE, exclude = NULL,force = FALSE, seed = 94035)hbal(data, Treat, X, Y = NULL, w = NULL, X.expand = NULL, X.keep = NULL, expand.degree = 1, coefs = NULL, max.iterations = 200, cv = NULL, folds = 4, ds = FALSE, group.exact = NULL, group.alpha = NULL, term.alpha = NULL, constraint.tolerance = 1e-3, print.level = 0, grouping = NULL, group.labs = NULL, linear.exact = TRUE, shuffle.treat = TRUE, exclude = NULL,force = FALSE, seed = 94035)
data |
a dataframe that contains the treatment, outcome, and covariates. |
Treat |
a character string of the treatment variable. |
X |
a character vector of covariate names to balance on. |
Y |
a character string of the outcome variable. |
w |
a character string of the weighting variable for base weights |
X.expand |
a character vector of covariate names for serial expansion. |
X.keep |
a character vector of covariate names to keep regardless of whether they are selected in double selection. |
expand.degree |
degree of series expansion. 1 means no expansion. Default is 1. |
coefs |
initial coefficients for the reweighting algorithm (lambdas). |
max.iterations |
maximum number of iterations. Default is 200. |
cv |
whether to use cross validation. Default is |
folds |
number of folds for cross validation. Only used when cv is |
ds |
whether to perform double selection prior to balancing. Default is |
group.exact |
binary indicator of whether each covariate group should be exact balanced. |
group.alpha |
penalty for each covariate group |
term.alpha |
a named vector of user-specified ridge penalties. The names need to be variable names. Value should be non-negative (0 means exact balancing). Only work with 'expand.degree = 1' |
constraint.tolerance |
tolerance level for overall imbalance. Default is 1e-3. |
print.level |
details of printed output. |
grouping |
different groupings of the covariates. Must be specified if expand is |
group.labs |
labels for user-supplied groups |
linear.exact |
seek exact balance on the level terms |
shuffle.treat |
whether to use cross-validation on the treated units. Default is |
exclude |
list of covariate name pairs or triplets to be excluded. |
force |
binary indicator of whether to expand covariates when there are too many |
seed |
random seed to be set. Set random seed when cv= |
In the simplest set-up, user can just pass in {Treatment, X, Y}. The default settings will serially expand X to include higher order terms, hierarchically residualize these terms, perform double selection to only keep the relevant variables and use cross-validation to select penalities for different groupings of the covariates.
An list object of class hbal with the following elements:
coefs |
vector that contains coefficients from the reweighting algorithm. |
mat |
matrix of serially expanded covariates if expand= |
penalty |
vector of ridge penalties used for each covariate |
weights |
vector that contains the control group weights assigned by hbal. |
W |
vector of treatment status |
Y |
vector of outcome |
Yiqing Xu, Eddie Yang
Yiqing Xu <[email protected]>, Eddie Yang <[email protected]>
Xu, Y., & Yang, E. (2022). Hierarchically Regularized Entropy Balancing. Political Analysis, 1-8. doi:10.1017/pan.2022.12
# Example 1 set.seed(1984) N <- 500 X1 <- rnorm(N) X2 <- rbinom(N,size=1,prob=.5) X <- cbind(X1, X2) treat <- rbinom(N, 1, prob=0.5) # Treatment indicator y <- 0.5 * treat + X[,1] + X[,2] + rnorm(N) # Outcome dat <- data.frame(treat=treat, X, Y=y) out <- hbal(Treat = 'treat', X = c('X1', 'X2'), Y = 'Y', data=dat) summary(hbal::att(out)) # Example 2 ## Simulation from Kang and Shafer (2007). library(MASS) set.seed(1984) n <- 500 X <- mvrnorm(n, mu = rep(0, 4), Sigma = diag(4)) prop <- 1 / (1 + exp(X[,1] - 0.5 * X[,2] + 0.25*X[,3] + 0.1 * X[,4])) # Treatment indicator treat <- rbinom(n, 1, prop) # Outcome y <- 210 + 27.4*X[,1] + 13.7*X[,2] + 13.7*X[,3] + 13.7*X[,4] + rnorm(n) # Observed covariates X.mis <- cbind(exp(X[,1]/2), X[,2]*(1+exp(X[,1]))^(-1)+10, (X[,1]*X[,3]/25+.6)^3, (X[,2]+X[,4]+20)^2) dat <- data.frame(treat=treat, X.mis, Y=y) out <- hbal(Treat = 'treat', X = c('X1', 'X2', 'X3', 'X4'), Y='Y', data=dat) summary(att(out))# Example 1 set.seed(1984) N <- 500 X1 <- rnorm(N) X2 <- rbinom(N,size=1,prob=.5) X <- cbind(X1, X2) treat <- rbinom(N, 1, prob=0.5) # Treatment indicator y <- 0.5 * treat + X[,1] + X[,2] + rnorm(N) # Outcome dat <- data.frame(treat=treat, X, Y=y) out <- hbal(Treat = 'treat', X = c('X1', 'X2'), Y = 'Y', data=dat) summary(hbal::att(out)) # Example 2 ## Simulation from Kang and Shafer (2007). library(MASS) set.seed(1984) n <- 500 X <- mvrnorm(n, mu = rep(0, 4), Sigma = diag(4)) prop <- 1 / (1 + exp(X[,1] - 0.5 * X[,2] + 0.25*X[,3] + 0.1 * X[,4])) # Treatment indicator treat <- rbinom(n, 1, prop) # Outcome y <- 210 + 27.4*X[,1] + 13.7*X[,2] + 13.7*X[,3] + 13.7*X[,4] + rnorm(n) # Observed covariates X.mis <- cbind(exp(X[,1]/2), X[,2]*(1+exp(X[,1]))^(-1)+10, (X[,1]*X[,3]/25+.6)^3, (X[,2]+X[,4]+20)^2) dat <- data.frame(treat=treat, X.mis, Y=y) out <- hbal(Treat = 'treat', X = c('X1', 'X2', 'X3', 'X4'), Y='Y', data=dat) summary(att(out))
The contenderJudges dataset is from Black and Owens (2016): Courting the president: how circuit court judges alter their behavior for promotion to the Supreme Court
This dataset includes 10,171 period-judge observations for a total of 68 judges.
The treatment variable of interest is treatFinal0, which indicates whether there was a vacancy in the Supreme Court
The outcome of interest is ideological alignment of judges' votes with the sitting President (presIdeoVote).
The remaining variables are characteristics of the judges and courts, to be used as controls.
The LaLonde dataset has treated units from Dehejia and Wahba (1999), containing 185 individuals; data on the control units is from Panel Study of Income Dynamics (PSID-1), containing 2,490 individuals.
data(hbal)data(hbal)
Black, R. C., and Owens, R. J. (2016). Courting the president: how circuit court judges alter their behavior for promotion to the Supreme Court. American Journal of Political Science, 60(1), 30-43.
Dehejia, R. H., and Wahba, S. (1999). Causal effects in nonexperimental studies: Reevaluating the evaluation of training programs. Journal of the American statistical Association, 94(448), 1053-1062.
Hazlett, C. (2020). KERNEL BALANCING. Statistica Sinica, 30(3), 1155-1189.
Data on the treated units is from Dehejia and Wahba (1999), containing 185 individuals; data on the control units is from Panel Study of Income Dynamics (PSID-1), containing 2,490 individuals.
A data frame with 2675 rows and 13 columns.
treatment indicator of whether an individual participated in the National Supported Work (NSW) program
years of education
demographic indicator variables for Black
idemographic indicator variables for Hispanic
demographic indicator variables for married
real earnings in 1974
real earnings in 1975
real earnings in 1978, outcome
unemployment indicator for 1974
unemployment indicator for 1975
unemployment indicator for 1978
indicator for no high school degree
Dehejia, R. H., and Wahba, S. (1999). Causal effects in nonexperimental studies: Reevaluating the evaluation of training programs. Journal of the American statistical Association, 94(448), 1053-1062.
Hazlett, C. (2020). KERNEL BALANCING. Statistica Sinica, 30(3), 1155-1189.
hbal ObjectThis function plots the covariate difference between the control and treatment groups in standardized means before and after weighting.
## S3 method for class 'hbal' plot(x, type = 'balance', log = TRUE, base_size = 10, ...)## S3 method for class 'hbal' plot(x, type = 'balance', log = TRUE, base_size = 10, ...)
x |
an object of class |
type |
type of graph to plot. |
log |
log scale for the weight plot |
base_size |
base font size |
... |
Further arguments to be passed to |
A matrix of ggplots of covariate balance by group
Yiqing Xu, Eddie Yang
hbal ObjectThis function prints a summary from an hbal Object.
## S3 method for class 'hbal' summary(object, print.level = 0, ...)## S3 method for class 'hbal' summary(object, print.level = 0, ...)
object |
an object of class |
print.level |
level of details to be printed |
... |
Further arguments to be passed to |
a summary table
Yiqing Xu, Eddie Yang
Internal function called by hbal to residualize covariates.
updateCoef(old.coef, new.coef, counter)updateCoef(old.coef, new.coef, counter)
old.coef |
previous coefficients |
new.coef |
new coefficients |
counter |
which fold in CV |
updated coefficients
Yiqing Xu, Eddie Yang