*! version 1.0.01 29july2015
help avar

Title

avar -- Asymptotic covariance estimation for iid and non-iid data
robust to heteroskedasticity, autocorrelation, 1- and 2-way clustering,
common cross-panel autocorrelated disturbances, etc.

Syntax

Single-equation context:

avar
evarname (zvarlist)
[weight] [if exp] [in range]
[, vce_options misc_options]

Multiple-equation context:

avar
(evarlist) (zvarlist)
[weight] [if exp] [in range]
[, vce_options misc_options]

vce_options

robust
heteroskedastic-robust VCE

cluster(cvarlist)
1- or 2-way cluster- and heteroskedasticity-robust VCE

bw(#)
bandwidth for kernel-robust (AC or HAC) VCE

kernel(string)
kernel for kernel-robust (AC or HAC) VCE

dkraay(#)
VCE robust to autocorrelated cross-panel disturbances with bandwidth=#

kiefer
VCE robust to within-panel autocorrelation

center
use centered moments

misc_options

partial(pvarlist)
partial-out (project-out) variables in pvarlist from variables in evarlist and zvarlist;
constant also partialled-out

noconstant
suppress constant from (zvarlist)

demean
center (demean) variables in evarlist and zvarlist (implies noconstant)

smata(mname)
name for avar matrix left in Mata environment if requested

avar may be used with time-series or panel data,
in which case the data must be tsset first; see help tsset.

All varlists may contain time-series operators,
but factor variables are not currently supported; see help varlist.

Contents

Description

Summary of VCE options

Examples of usage

Replication examples

Saved results

References

Citation of avar

Description

avar is a routine for estimating S, the asymptotic variance of (1/N)*Z'e,
where Z is an NxL matrix of L variables,
e is an Nxp matrix of p variables,
and N is the sample size.
Typically, S would be used to form a sandwich-type estimate of the variance of an estimator,
where S is the "filling" of the sandwich.

avar can estimate VCEs for single and multiple equations
that are robust to various violations of the assumption of iid data,
including heteroskedasticity, autocorrelation, 1- and 2-way clustering, common cross-panel disturbances, etc.
It supports time-series and panel data.

For example, in the most basic application - linear regression -
e is a Nx1 vector of residuals from an OLS estimation
and Z is the NxL matrix of L regressors.
The variance of the OLS estimator var(beta) can be estimated by the "sandwich" N*DSD,
where the "filling" S is the estimate of the asymptotic variance of (1/N)*Z'e
and the "bread" is D=(1/N)inv(X'X) .
This estimate of the variance of the OLS estimator is as robust as S:
if S is robust to heteroskedasticity, so will be var(beta);
if S is robust to 2-way clustering, so will be var(beta); etc.

In the multi-equation context,
e is a Nxp matrix of residuals from p different estimations.
avar will return an estimate of the asymptotic variance
including the covariances across equations.
For example, avar can estimate an S
that is consistent with the var(beta) reported by
the Stata routines sureg, reg3 and suest;
see the examples below for illustrations.
Important: in its current implementation,
avar uses listwise deletion.
This means it will only use observations for which
there are no missing values in any of the variables in elist.
(This is the behavior of sureg and reg3
but not of suest.)

The partial(pvarlist) option may be used to partial out (project out)
the additional variables pvarlist
from the variables in evarlist and zvarlist.
Note that these variables should not be included in zvarlist.
A constant is automatically partialled out as well.
The demean option can be used if only the constant is to be partialled out.

avar uses the Mata library livreg2
used by ivreg2, ranktest and related routines.
For a more detailed discussion of the various VCE options
see the ivreg2 help file.
For a discussion of the theory behind
the estimation of asymptotic variances in the econometric context,
see Hayashi (2000), especially chapters 2, 4 and 6.

Summary of VCE options

robust
heteroskedastic-consistent (HC) or "robust" VCE

bw(#)
autocorrelation-consistent (AC) VCE with bandwidth=#

robust bw(#)
heteroskedastic- and autocorrelation-consistent (HAC) VCE with bandwidth=#

kernel(string)
kernel used in AC and HAC estimation;
default kernel is Bartlett;
supported kernels (abbreviations) are Bartlett (bar);
Truncated (tru); Parzen (par); Tukey-Hanning (thann); Tukey-Hamming (thamm); Danielle (dan);
Tent (ten); and Quadratic-Spectral (qua or qs)

cluster(cvar)
VCE robust to heteroskedasticity and within-group correlation, where varname defines the group;
may be combined with bw(#) if data are tsset with cvar as the time variable

cluster(cvar1 cvar2)
2-way cluster-robust VCE, robust to heteroskedasticity and correlation within groups defined by var1 and var2;
may be combined with bw(#) if data are tsset with either cvar1 or cvar2 as the time variable

dkraay(#)
VCE robust to autocorrelated cross-panel disturbances with bandwidth=# as per Driscoll & Kraay (1998);
equivalent to cluster(tvar) + bw(#) where tvar is the time variable as per tsset

kiefer
VCE robust to within-group autocorrelation (but not heteroskedasticity) as per Kiefer (1980);
equivalent to kernel(tru) + bw(#) where # is the full length of the panel

center
use centered moments (sample moments with mean zero), i.e., instead of Z'e use (Z'e-mean(Z'e)),
as per e.g. Hall (2005) pp. 131-8 and 145-8

Examples (nb: examples can also be found in the supplementary certification do file)

Setup

Set up artificial dataset

. clear

. set obs 100

. set seed 12345

. gen double y1 = runiform()

. gen double y2 = runiform()

. gen double x1 = runiform()

. gen double x2 = runiform()

. gen double z1 = runiform()

. gen double z2 = runiform()

OLS and IV residuals

. qui reg y1 x1 x2

. predict double e1, resid

. qui reg y2 x1 x2

. predict double e2, resid

. qui reg y1 x1

. predict double es1, resid

. qui reg y2 x2

. predict double es2, resid

. qui ivreg y1 (x1 x2 = z1 z2)

. predict double eiv1, resid

. qui ivreg y2 (x1 x2 = z1 z2)

. predict double eiv2, resid

Cross-section, time and panel variables

. gen int id1 = _n

. gen int t1 = _n

. gen int id2 = ceil(_n/5)

. gen int t2 = 5-(id2*5-t1)

Basic usage

. avar e1 (x1 x2)

. avar e1 (x1 x2), robust

. avar e1 (x1 x2), cluster(id2) partial(z1)

. avar (e1 e2) (x1 x2), robust

. avar e1 (x1 x2), smata(my_avar_matrix)

. mata: my_avar_matrix

Replication examples

Replicate VCV for OLS+robust (note small-sample correction used by Stata's regress)

. reg y1 x1 x2, rob

. mat V1 = e(V)

. mat accum XX=x1 x2

. mat Sxx=XX*1/r(N)

. mat Sxxi=syminv(Sxx)

. avar e1 (x1 x2), rob

. mat S=r(S)

. mat V2 = Sxxi*S*Sxxi*1/r(N)

. mat V2 = V2 * e(N)/e(df_r)

. mat list V1

. mat list V2

Replicate VCV for OLS+Newey-West (note small-sample correction)

. tsset t1

. newey y1 x1 x2, lag(3)

. mat V1 = e(V)

. mat accum XX=x1 x2

. mat Sxx=XX*1/r(N)

. mat Sxxi=syminv(Sxx)

. avar e1 (x1 x2), rob bw(4) kernel(bartlett)

. mat S=r(S)

. mat V2 = Sxxi*S*Sxxi*1/r(N)

. mat V2 = V2 * e(N)/e(df_r)

. mat list V1

. mat list V2

Replicate VCV for SUR estimator: 2 eqns, exactly-identified, iid errors

. sureg (y1 x1 x2) (y2 x1 x2)

. mat V1 = e(V)

. mat accum XX=x1 x2

. mat Sxx=XX*1/r(N)

. mat Sxxi=syminv(Sxx)

. avar (e1 e2) (x1 x2)

. mat S=r(S)

. global cn : colfullnames S

. mat KSxxi= I(2)#Sxxi

. mat V2 = KSxxi*S*KSxxi*1/r(N)

. mat colnames V2=$cn

. mat rownames V2=$cn

. mat list V1

. mat list V2

Replicate VCV for suest: 2 eqns, different regressors, robust (note small-sample correction)

. reg y1 x1

. est store eq_1

. reg y2 x2

. est store eq_2

. qui suest eq_1 eq_2

. mat V1 = e(V)

. mat V1a = V1[1..2,1..2]

. mat V1b = V1[4..5,1..2]

. mat V1c = V1[4..5,4..5]

. mat V1 = (V1a, V1b') \ (V1b, V1c)

. avar (es1 es2) (x1 x2), rob

. mat S=r(S)

. mat Sa=S[1..1,1..6]

. mat Sb=S[3..3,1..6]

. mat Sc=S[5..5,1..6]

. mat Sd=S[6..6,1..6]

. mat S = Sa \ Sb \ Sc \ Sd

. mat Sa=S[1..4,1..1]

. mat Sb=S[1..4,3..3]

. mat Sc=S[1..4,5..5]

. mat Sd=S[1..4,6..6]

. mat S = Sa , Sb , Sc , Sd

. mat accum XX1=x1

. mat Sxx1=XX1*1/r(N)

. mat Sxx1i=syminv(Sxx1)

. mat accum XX2=x2

. mat Sxx2=XX2*1/r(N)

. mat Sxx2i=syminv(Sxx2)

. mat KSxxi= (Sxx1i, J(2,2,0)) \ (J(2,2,0), Sxx2i)

. mat V2 = KSxxi*S*KSxxi*1/r(N)

. mat V2 = V2 * e(N)/(e(N)-1)

. global cn : colfullnames S

. mat colnames V2=$cn

. mat rownames V2=$cn

. mat list V1

. mat list V2

Replicate VCV for suest: 2 eqns, exactly-identified, cluster-robust (note small-sample correction)

. reg y1 x1 x2

. est store eq_1

. reg y2 x1 x2

. est store eq_2

. suest eq_1 eq_2, cluster(id2)

. mat V1 = e(V)

. mat V1a = V1[1..3,1..3]

. mat V1b = V1[5..7,1..3]

. mat V1c = V1[5..7,5..7]

. mat V1 = (V1a, V1b') \ (V1b, V1c)

. mat accum XX=x1 x2

. mat Sxx=XX*1/r(N)

. mat Sxxi=syminv(Sxx)

. avar (e1 e2) (x1 x2), rob cluster(id2)

. mat S=r(S)

. global cn : colfullnames S

. mat KSxxi= I(2)#Sxxi

. mat V2 = KSxxi*S*KSxxi*1/r(N)

. mat V2 = V2 * e(N_clust)/(e(N_clust)-1)

. mat colnames V2=$cn

. mat rownames V2=$cn

. mat list V1

. mat list V2

Replicate VCV for efficient IV with iid error (note small-sample correction)

. ivreg y1 (x1 x2 = z1 z2)

. mat V1 = e(V)

. mat accum ZZ=z1 z2

. mat Szz=ZZ*1/r(N)

. mat Szzi=syminv(Szz)

. mat accum A=x1 x2 z1 z2

. mat A1 = A[3..5,1..2]

. mat A2 = A[3..5,5..5]

. mat Szx = (A1 , A2)*1/r(N)

. avar eiv1 (z1 z2)

. mat S=r(S)

. mat Si=syminv(S)

. mat V2 = syminv(Szx'*Si*Szx)*1/r(N)

. mat V2 = V2 * e(N)/e(df_r)

. mat list V1

. mat list V2

Replicate VCV for inefficient IV with robust SEs (note small-sample correction)

. ivreg y1 (x1 x2 = z1 z2), rob

. mat V1 = e(V)

. mat accum ZZ=z1 z2

. mat Szz=ZZ*1/r(N)

. mat Szzi=syminv(Szz)

. mat accum A=x1 x2 z1 z2

. mat A1 = A[3..5,1..2]

. mat A2 = A[3..5,5..5]

. mat Szx = (A1 , A2)*1/r(N)

. avar eiv1 (z1 z2), rob

. mat S=r(S)

. mat Si=syminv(S)

. mat V2 = syminv(Szx'*Szzi*Szx)*(Szx'*Szzi*S*Szzi*Szx)*syminv(Szx'*Szzi*Szx)*1/r(N)

. mat V2 = V2 * e(N)/e(df_r)

. mat list V1

. mat list V2

Saved results

avar saves the following in r():

Scalars

r(N) sample size

Matrices

r(S) estimate of the asymptotic variance

Mata

mname (optional) estimate of the asymptotic variance

References

Driscoll, J.C. and Kraay, A. 1998. Consistent Covariance Matrix Estimation With Spatially Dependent Panel Data.
Review of Economics and Statistics. Vol. 80, No. 4, pp. 549-560.

Hayashi, F. Econometrics. 2000. Princeton: Princeton University Press.

Kiefer, N.M. 1980. Estimation of Fixed Effect Models for Time Series of Cross-Sections with
Arbitrary Intertemporal Covariance. Journal of Econometrics, Vol. 14, No. 2, pp. 195-202.

Hall, A.R. Generalized Method of Moments. 2005. Oxford: Oxford University Press.

Citation of avar

avar is not an official Stata command. It is a free contribution
to the research community, like a paper. Please cite it as such:

Baum, C.F., Schaffer, M.E. 2013.
avar: Asymptotic covariance estimation for iid and non-iid data
robust to heteroskedasticity, autocorrelation, 1- and 2-way clustering,
common cross-panel autocorrelated disturbances, etc.
http://ideas.repec.org/c/boc/bocode/XXX.html

Authors

Christopher F Baum, Boston College, USA
baum@bc.edu

Mark E Schaffer, Heriot-Watt University, UK
m.e.schaffer@hw.ac.uk

Also see

Help: ivreg2