# Introduction

This document demonstrates how to use the systematic variation estimation methods of the hettx package. First load the package:

library( hettx )

This package includes code to make synthetic data, which can be useful for simulation studies and illustrations. Here we make a dataset with 3 covariates that we will use to illustrate the function calls. For this dataset, the first two variables have a systematic relationship with treatment impact, and the third is good for adjustment for increasing power:

df = make.randomized.dat( 10000, gamma.vec=c(1,1,1,2), beta.vec=c(-1,-1,1,0) )
str( df )
## 'data.frame':    10000 obs. of  8 variables:
##  $A : num 1.5 -0.187 -0.985 1.212 0.347 ... ##$ B   : num  0.633 -2.295 -2.526 1.648 -1.112 ...
##  $C : num 0.0301 -1.3996 -2.2623 1.6664 -0.3916 ... ##$ Y.0 : num  3.218 -4.846 -6.432 7.383 -0.504 ...
##  $Y.1 : num 1.35 -7.95 -8.97 6.82 -2.96 ... ##$ tau : num  -1.867 -3.108 -2.541 -0.563 -2.459 ...
##  $Z : num 1 0 1 1 0 0 0 0 0 0 ... ##$ Yobs: num  1.35 -4.846 -8.972 6.82 -0.504 ...

## Basic estimation

The function estimate_systematic is our core estimator that implements our various methods:

rs = estimate_systematic( Yobs ~ Z,  interaction.formula = ~ A + B, data=df )
summary(rs)
##
## Systematic Estimation Regression of Heterogeneous Treatment Effects: RI-Unadjusted
##
## Call:
## estimate_systematic(formula = Yobs ~ Z, data = df, interaction.formula = ~A +
##     B)
##
## Coefficients:
## (Intercept)            A            B
##     -0.9909      -0.9960       0.7906
##
## Variance-Covariance Matrix:
##              (Intercept)             A            B
## (Intercept) 0.0051938261  0.0005228734  0.000241036
## A           0.0005228734  0.0080387823 -0.002849295
## B           0.0002410360 -0.0028492949  0.009642292
##
## Chi-squared test for systematic variation: X^2=145.58; p=0.000
##
## Details: ATE = -0.982 +/- 0.141     SD(Y(0)) = 3.482   SD(Y(1)) = 3.607

The arguments are observed outcome, observed treatment assignment, and what variables to find a systematic relationship to, expressed as a formula using the tilde notation (categorical covariates will be automatically converted). The output give coefficients for the model for individual systematic treatment effects. In the above, our model of effects is $$\tau_i = \beta_0 + \beta_1 A_i + \beta_2 B_i$$.

We can obtain our standard errors and variance-covariance matrix for our estimators that comes from the design-based theory:

vcov( rs )
##              (Intercept)             A            B
## (Intercept) 0.0051938261  0.0005228734  0.000241036
## A           0.0005228734  0.0080387823 -0.002849295
## B           0.0002410360 -0.0028492949  0.009642292
SE( rs )
## (Intercept)           A           B
##  0.07206820  0.08965926  0.09819517

and confidence intervals (using the normal approximation)

confint( rs )
##                  2.5 %     97.5 %
## (Intercept) -1.1321202 -0.8496181
## A           -1.1716828 -0.8202249
## B            0.5981765  0.9830945

OLS uses the empirical covariance matrix $$\widehat{S}_{xx}$$ (Sxx.hat) for each treatment arm rather than the known Sxx:

M.ols.ours = estimate_systematic( Yobs ~ Z, ~ A + B, data=df, method="OLS" )
summary(M.ols.ours)
##
## Systematic Estimation Regression of Heterogeneous Treatment Effects: OLS-Unadjusted
##
## Call:
## estimate_systematic(formula = Yobs ~ Z, data = df, interaction.formula = ~A +
##     B, method = "OLS")
##
## Coefficients:
## (Intercept)            A            B
##     -0.9880      -0.9682       1.0034
##
## Variance-Covariance Matrix:
##               (Intercept)             A             B
## (Intercept)  1.484292e-03 -0.0000164369 -3.755142e-05
## A           -1.643690e-05  0.0020479848 -1.057453e-03
## B           -3.755142e-05 -0.0010574530  2.033359e-03
##
## Chi-squared test for systematic variation: X^2=628.20; p=0.000
##
## Details: ATE = -0.982 +/- 0.141     SD(Y(0)) = 3.482   SD(Y(1)) = 3.607
M.ols.ours$beta.hat ## (Intercept) A B ## -0.9879838 -0.9682314 1.0034419 Simple interaction-based OLS approach, as a comparison: M0 = lm( Yobs ~ (A+B) * Z, data=df ) M0 ## ## Call: ## lm(formula = Yobs ~ (A + B) * Z, data = df) ## ## Coefficients: ## (Intercept) A B Z A:Z B:Z ## 0.9771 1.6727 1.6482 -0.9880 -0.9682 1.0034 There are no differences up to machine precision: M.ols.ours$beta - coef(M0)[4:6]
##   (Intercept)             A             B
## -1.221245e-15 -8.881784e-16 -4.218847e-15

The model-adjusted estimator is used automatically if you give two formula, one for the treatment model and one for the control adjustment model.

estimate_systematic( Yobs ~ Z, interaction.formula = ~ A + B,
control.formula = ~ C, data=df )
##
## Coefficients:
## (Intercept)            A            B
##     -1.0082      -0.9975       0.7896
##
## Variance-Covariance Matrix:
##              (Intercept)             A             B
## (Intercept) 1.395837e-03  0.0001944384  8.990417e-06
## A           1.944384e-04  0.0080025252 -2.875889e-03
## B           8.990417e-06 -0.0028758892  9.622690e-03
##
## Chi-squared test for systematic variation: X^2=145.97; p=0.000

These formula can use the same covariates. Here we also adjust for the covariates used in our treatment model:

rsA2 = estimate_systematic( Yobs ~ Z,  ~ A + B, ~ A + B + C, data=df )
coef( rsA2 )
## (Intercept)           A           B
##  -1.0008045  -0.9867032   0.7845162

rsB = estimate_systematic( Yobs ~ Z,  ~ A + B, ~ C, data=df, method = "OLS" )
coef( rsB )
## (Intercept)           A           B
##  -1.0052866  -0.9696844   1.0024128
rsB2 = estimate_systematic( Yobs ~ Z,  ~ A + B, ~ A + B + C, data=df, method = "OLS" )
coef( rsB2 )
## (Intercept)           A           B
##  -0.9979821  -0.9588914   0.9972965

As a comparison, using lm() we have

rsB.lm = lm( Yobs ~ Z * (A+B) + C, data=df )
coef( rsB.lm )
## (Intercept)           Z           A           B           C         Z:A
##   0.9940274  -1.0032807   0.9999781   0.9876401   1.9814977  -0.9676683
##         Z:B
##   0.9908800
cbind( C.only=coef( rsB ), ABC=coef( rsB2 ), lmC=coef( rsB.lm )[c(2,6,7)])
##                 C.only        ABC        lmC
## (Intercept) -1.0052866 -0.9979821 -1.0032807
## A           -0.9696844 -0.9588914 -0.9676683
## B            1.0024128  0.9972965  0.9908800

Note that the model adjustment approach is not the same as including a term as a control variable in a linear regression (and you can do both).

# Oracle estimator (for simulations and verification of formulae)

If we know all potential outcomes, we can calculate the exact beta for the sample. (This is useful for simulation studies.) We can also get the true SE, which is why we pass a sample treatment vector (so it can calculate proportion treated, under the assumption of simple random assignment):

Moracle = estimate_systematic( Y.1 + Y.0 ~ Z, ~ A + B, data=df )
summary(Moracle)
##
## Systematic Estimation Regression of Heterogeneous Treatment Effects: Oracle RI
##
## Call:
## estimate_systematic(formula = Y.1 + Y.0 ~ Z, data = df, interaction.formula = ~A +
##     B)
##
## Coefficients:
## (Intercept)            A            B
##          -1           -1            1
##
## Variance-Covariance Matrix:
##              (Intercept)             A             B
## (Intercept) 0.0051106507  0.0004245175  0.0003725357
## A           0.0004245175  0.0076442568 -0.0026497002
## B           0.0003725357 -0.0026497002  0.0091689200
##
## Chi-squared test for systematic variation: X^2=NA; p=NA
SE( Moracle )
## (Intercept)           A           B
##  0.07148881  0.08743144  0.09575448

It will give the same results regardless of $$Z$$ assuming the total number of units remains the same.

# Looking at $$R^2$$

We can look at treatment effect explained. We will look at two scenarios, one with no ideosyncratic variation on top of the systematic variation, and one with a substantial amount. We will plot the R2 sensitivity curves for each on top of each other.

df = make.randomized.dat( 1000, beta.vec=c(-1,1,1) )
rs = estimate_systematic( Yobs ~ Z, ~ A + B, data=df, method="OLS" )
r2 = R2( rs )
r2
##
##  R2 for Systematic Estimation Regression of Heterogeneous Treatment Effects (ITT)
##
## R2 Estimates:
##   R2 Lower Bound R2 Lower Bound (Sharp) R2 Upper Bound
## 1         0.3245                 0.4864         0.9951
##
## Variance Estimates:
##  Systematic Treatment Effect Variation: 3.2261
##  Idiosyncratic Treatment Effect Variation: 0.0158 -- 6.7161 (3.4068 Sharp)
##  Total Treatment Effect Variation: 3.2419 -- 9.9422 (6.6329 Sharp)

And now our DGP with lots of idiosyncratic variation:

df = make.randomized.dat( 1000, beta.vec=c(-1,1,1), ideo.sd=3 )
rs = estimate_systematic( Yobs ~ Z, ~ A + B, data=df, method="OLS" )
r2b = R2( rs )
r2b    
##
##  R2 for Systematic Estimation Regression of Heterogeneous Treatment Effects (ITT)
##
## R2 Estimates:
##   R2 Lower Bound R2 Lower Bound (Sharp) R2 Upper Bound
## 1         0.1392                 0.2154         0.4894
##
## Variance Estimates:
##  Systematic Treatment Effect Variation: 3.1615
##  Idiosyncratic Treatment Effect Variation: 3.299 -- 19.5485 (11.5176 Sharp)
##  Total Treatment Effect Variation: 6.4605 -- 22.71 (14.6791 Sharp)

Plot our results:

plot( r2 )
plot( r2b, ADD=TRUE, col="green" ) And here is a case where we have 100% systematic variation along a single variable.

df = make.randomized.dat( 1000, beta.vec=c(-1,1,0) )
rs = estimate_systematic( Yobs ~ Z, ~ A + B, data=df, method="OLS" )
r2 = R2( rs )
r2    
##
##  R2 for Systematic Estimation Regression of Heterogeneous Treatment Effects (ITT)
##
## R2 Estimates:
##   R2 Lower Bound R2 Lower Bound (Sharp) R2 Upper Bound
## 1         0.1606                 0.2741         0.9877
##
## Variance Estimates:
##  Systematic Treatment Effect Variation: 1.2333
##  Idiosyncratic Treatment Effect Variation: 0.0154 -- 6.4467 (3.2658 Sharp)
##  Total Treatment Effect Variation: 1.2487 -- 7.6801 (4.4991 Sharp)
plot( r2 ) See, we have 100% $$R^2_\tau$$, if we knew the true individual treatment effects:

plot( df$tau ~ df$A ) ## Comparing estimators

Here we look at how our ability to capture $$R^2_\tau$$ differs across different estimators. We have systematic effects for both $$A$$ and $$B$$, and $$C$$ is related to baseline outcomes but not impacts.

set.seed( 1020 )
df = make.randomized.dat( 1000, beta.vec=c(-1,1,1),
gamma.vec = c( 1, 2, 2, 1 ),
ideo.sd=1 )

rs = estimate_systematic( Yobs ~ Z, ~ A + B, data=df )
r2 = R2( rs )
plot( r2, col="green" )

rs = estimate_systematic( Yobs ~ Z, ~ A + B, ~ C, data=df )
r2 = R2( rs )

rs = estimate_systematic( Yobs ~ Z, ~ A + B, ~ C, data=df, method = "OLS" )
r2 = R2( rs )
plot( r2, ADD=TRUE, col="blue" ) # Treatment variation and non-compliance

Our estimators also work in non-compliance contexts (see paper for details). The story is analogous to the code above.

For this illustration we again generate some fake data using a provided function included with the package. This method takes a complier treatment heterogeniety model defined by beta:

beta = c(-1,6,0)
n = 10000

data = make.randomized.compliance.dat( n, beta.vec=beta )
names(data)
##   "A"    "B"    "C"    "Y.0"  "Y.1"  "tau"  "Z"    "Yobs" "S"    "D"

Our four observable groups defined by treatment assignment and take-up are as follows:

zd = with( data, interaction( Z, D, sep="-" ) )
boxplot( Yobs ~ zd, data=data, ylab="Yobs") The true relationships for the three latent groups are as follows:

par( mfrow=c(1,2), mgp=c(1.8,0.8,0), mar=c(3,3,0.5,0.5) )
plot( Y.1 - Y.0 ~ A, data=data, col=as.factor(data$S), pch=19, cex=0.5 ) plot( Y.1 - Y.0 ~ B, data=data, col=as.factor(data$S), pch=19, cex=0.5 )
legend( "topleft", legend=levels( as.factor( data$S ) ), pch=19, col=1:3 ) (We see no impacts for the AT and the NT as required under the assumptions of noncompliance here.) In this scenario we have a moderate compiance rate, meaning not a particulary weak instrument: prop.table( table( data$S ) )
##
##    AT     C    NT
## 0.157 0.693 0.150

## Estimating the effects

We use our same method, but by using the bar’’ notation, we can specify our treatment assigment $$Z$$ and our compliance status $$D$$ in our primary formula. Now our treatment variation formula is for the compliers (the always- and never-takers have no impact or variation).

rs = estimate_systematic( Yobs ~ D | Z, ~ A + B, data=data )
summary(rs)
##
## Systematic Estimation Regression of Heterogeneous Treatment Effects: LATE RI-RI
##
## Call:
## estimate_systematic(formula = Yobs ~ D | Z, data = data, interaction.formula = ~A +
##     B)
##
## Coefficients:
## (Intercept)            A            B
##     -0.8081       5.9232       0.3376
##
## Variance-Covariance Matrix:
##             (Intercept)          A          B
## (Intercept)  0.01112982 0.02075892 0.02356403
## A            0.02075892 0.07380709 0.05391864
## B            0.02356403 0.05391864 0.06271154
##
## Chi-squared test for systematic variation: X^2=1157.83; p=0.000
rs$beta.hat ## (Intercept) A B ## -0.8081404 5.9232272 0.3375529 SE( rs ) ## (Intercept) A B ## 0.1054980 0.2716746 0.2504227 We can get our R2 measure r2 = R2( rs ) r2 ## ## R2 for Systematic Estimation Regression of Heterogeneous Treatment Effects (LATE) ## ## R2 Estimates: ## R2 Lower Bound R2 Lower Bound (Sharp) R2 Upper Bound ## Compliers 0.7011 0.8138 0.9783 ## Noncompliers 0.0031 0.0036 0.0043 ## Covariates and compliers 0.7021 0.8145 0.9784 ## ## Variance Estimates: ## Systematic Treatment Effect Variation for Compliers: 18.3702 ## Systematic Treatment Effect Variation for Strata: 0.0558 ## Total Systematic Treatment Effect Variation: 12.7664 ## Idiosyncratic Treatment Effect Variation for Compliers: 0.4068 -- 7.8304 (4.2019 Sharp) ## Total Treatment Effect Variation: 13.0479 -- 18.1844 (15.6738 Sharp) ## ## Details: LATE = -0.512; ITT = -0.354; Proportion compliers = 0.692 plot( r2 ) ## The 2SLS Approach Analogous to the OLS approach, above, we can use a 2SLS approach here. rs2SLS = estimate_systematic( Yobs ~ Z | D, ~ A + B, data=data, method="2SLS" ) summary(rs2SLS) ## ## Systematic Estimation Regression of Heterogeneous Treatment Effects: LATE RI-2SLS ## ## Call: ## estimate_systematic(formula = Yobs ~ Z | D, data = data, interaction.formula = ~A + ## B, method = "2SLS") ## ## Coefficients: ## (Intercept) A B ## -2.624 9.050 0.175 ## ## Variance-Covariance Matrix: ## (Intercept) A B ## (Intercept) 0.5449229 0.3184124 0.7926288 ## A 0.3184124 0.2845082 0.5020666 ## B 0.7926288 0.5020666 1.2066902 ## ## Chi-squared test for systematic variation: X^2=1065.75; p=0.000 SE( rs2SLS ) ## (Intercept) A B ## 0.7381889 0.5333931 1.0984945 Comparing our errors in estimation from the two approaches we have: err = rs$beta.hat - beta
err2SLS = rs2SLS\$beta.hat - beta
data.frame( SE.RI = SE( rs ), err.RI=err, SE.2SLS = SE( rs2SLS ), err.2SLS = err2SLS )
##                 SE.RI      err.RI   SE.2SLS   err.2SLS
## (Intercept) 0.1054980  0.19185956 0.7381889 -1.6236665
## A           0.2716746 -0.07677283 0.5333931  3.0496196
## B           0.2504227  0.33755293 1.0984945  0.1749693