Tutorial on Systematic Treatment Detection

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  0.708 -0.352 -0.672 -1.565 -0.709 ...
##  $ B   : num  -0.8689 -0.0242 -1.1119 -1.6146 0.5699 ...
##  $ C   : num  -0.897 -0.722 0.868 -0.968 -0.129 ...
##  $ Y.0 : num  -1.423 -0.649 -1.15 -4.902 0.161 ...
##  $ Y.1 : num  -4 -1.32 -2.59 -5.95 0.44 ...
##  $ tau : num  -2.577 -0.672 -1.44 -1.05 0.279 ...
##  $ Z   : num  1 1 0 1 1 0 0 1 0 0 ...
##  $ Yobs: num  -4 -1.32 -1.15 -5.95 0.44 ...

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  
##     -1.0332      -1.1265       0.9643  
## 
## Variance-Covariance Matrix:
##              (Intercept)             A             B
## (Intercept) 0.0051337526  0.0004612468  0.0003875506
## A           0.0004612468  0.0081107855 -0.0026358916
## B           0.0003875506 -0.0026358916  0.0088326835
## 
## Chi-squared test for systematic variation: X^2=201.33; p=0.000
## 
## Details: ATE = -1.015 +/- 0.140     SD(Y(0)) = 3.476   SD(Y(1)) = 3.543

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 τi = β0 + β1Ai + β2Bi.

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.0051337526  0.0004612468  0.0003875506
## A           0.0004612468  0.0081107855 -0.0026358916
## B           0.0003875506 -0.0026358916  0.0088326835
SE( rs )
## (Intercept)           A           B 
##  0.07165021  0.09005990  0.09398236

and confidence intervals (using the normal approximation)

confint( rs )
##                 2.5 %     97.5 %
## (Intercept) -1.173588 -0.8927239
## A           -1.303013 -0.9499852
## B            0.780117  1.1485211

OLS adjustment

OLS uses the empirical covariance matrix 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  
##      -1.061       -1.053        1.000  
## 
## Variance-Covariance Matrix:
##               (Intercept)             A             B
## (Intercept)  1.545682e-03  6.494199e-05 -3.168014e-05
## A            6.494199e-05  2.079654e-03 -1.031775e-03
## B           -3.168014e-05 -1.031775e-03  2.047937e-03
## 
## Chi-squared test for systematic variation: X^2=681.91; p=0.000
## 
## Details: ATE = -1.015 +/- 0.140     SD(Y(0)) = 3.476   SD(Y(1)) = 3.543
M.ols.ours$beta.hat
## (Intercept)           A           B 
##   -1.060926   -1.053120    1.000277

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  
##       1.059        1.668        1.652       -1.061       -1.053        1.000

There are no differences up to machine precision:

M.ols.ours$beta - coef(M0)[4:6]
##   (Intercept)             A             B 
##  0.000000e+00  2.220446e-16 -2.220446e-16

Model adjustment

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.0316      -1.1264       0.9644  
## 
## Variance-Covariance Matrix:
##              (Intercept)             A             B
## (Intercept) 0.0014263454  0.0002728791  0.0001691561
## A           0.0002728791  0.0080878205 -0.0026577415
## B           0.0001691561 -0.0026577415  0.0088111848
## 
## Chi-squared test for systematic variation: X^2=201.37; 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.0508777  -1.1254831   0.9572629

Model adjustment + OLS adjustment

We can also adjust for additional covariates using the OLS implementation:

rsB = estimate_systematic( Yobs ~ Z,  ~ A + B, ~ C, data=df, method = "OLS" )
coef( rsB )
## (Intercept)           A           B 
##   -1.059331   -1.053044    1.000371
rsB2 = estimate_systematic( Yobs ~ Z,  ~ A + B, ~ A + B + C, data=df, method = "OLS" )
coef( rsB2 )
## (Intercept)           A           B 
##  -1.0786902  -1.0519981   0.9932218

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 
##   1.0280878  -1.0477815   0.9802320   1.0063680   2.0157689  -0.9862075 
##         Z:B 
##   0.9942902
cbind( C.only=coef( rsB ), ABC=coef( rsB2 ), lmC=coef( rsB.lm )[c(2,6,7)])
##                C.only        ABC        lmC
## (Intercept) -1.059331 -1.0786902 -1.0477815
## A           -1.053044 -1.0519981 -0.9862075
## B            1.000371  0.9932218  0.9942902

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.0049741289  0.0002984584  0.0004808735
## A           0.0002984584  0.0074682960 -0.0023917155
## B           0.0004808735 -0.0023917155  0.0083282969
## 
## Chi-squared test for systematic variation: X^2=NA; p=NA
SE( Moracle )
## (Intercept)           A           B 
##   0.0705275   0.0864193   0.0912595

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

Looking at R2

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.3196                  0.481         0.9942
## 
## Variance Estimates:
##  Systematic Treatment Effect Variation: 3.1599 
##  Idiosyncratic Treatment Effect Variation: 0.0186 -- 6.7263 (3.4091 Sharp) 
##  Total Treatment Effect Variation: 3.1785 -- 9.8862 (6.569 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.1325                 0.2011         0.4272
## 
## Variance Estimates:
##  Systematic Treatment Effect Variation: 2.9117 
##  Idiosyncratic Treatment Effect Variation: 3.9045 -- 19.0645 (11.5655 Sharp) 
##  Total Treatment Effect Variation: 6.8161 -- 21.9762 (14.4772 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.1293                 0.2268         0.9861
## 
## Variance Estimates:
##  Systematic Treatment Effect Variation: 0.9152 
##  Idiosyncratic Treatment Effect Variation: 0.0129 -- 6.1613 (3.1209 Sharp) 
##  Total Treatment Effect Variation: 0.9281 -- 7.0765 (4.0362 Sharp)
plot( r2 )

See, we have 100% Rτ2, 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 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" )

# adjusted
rs = estimate_systematic( Yobs ~ Z, ~ A + B, ~ C, data=df )
r2 = R2( rs )
plot( r2, ADD=TRUE )

# adjusted + OLS
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)
##  [1] "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