*! version 1.0 9 Oct 2025

aivreg — Anti-IV Regression

Syntax

aivreg [estimator] depvar varlist [if] [in],
aiv(varlist)
[options]

estimators

ratio default estimator; ratio-of-coefficients. Allows a single Anti-IV.

2sls two-stage least squares Anti-IV estimator. Allows multiple Anti-IVs.

gmm generalized method of moments Anti-IV estimator. Allows multiple Anti-IVs.

options

Model specification
aiv(varlist) anti-IV variables.

control(varlist) control variables.

fe(varlist) fixed effects to absorb.

weight(...) observation weights for estimation.

twostep default for GMM; run twostep GMM.

initialweightmatrix(matrix) initial estimation weight matrix for two-step GMM.

onestep run onestep GMM.

weightingmatrix(matrix) estimation weight matrix for one-step GMM.

ignoresingularity if a (nearly) singular matrix is detected, continue without an error.

Estimation & storage
eststo(name) store estimates under name.

savefirst save first-stage regression results.

firststo(name) store first-stage estimates under name.

Variance & inference
vce(type) variance estimator: ar (default), boot, asymp.

cluster(varlist) cluster-robust SEs.

reps(#) number of bootstrap replications.

seed(#) random seed for bootstrap.

Description

aivreg implements the anti-IV estimator outlined in Bell, Billings, Calder-Wang, & Zhong (2024). The method allows the user to estimate implicit amenity prices in the presence of an unobservable confounder.

Details

Estimator

- ratio default estimator; ratio-of-coefficients. Allows a single Anti-IV.

- 2sls two-stage least squares Anti-IV estimator. Allows multiple Anti-IVs.

- gmm generalized method of moments Anti-IV estimator. Allows multiple Anti-IVs.

Model specification

- aiv(varlist) Anti-IV variables (one for ratio, one or more for 2sls and gmm).

- control(varlist) Exogenous controls included in both stages; assumed conditionally orthogonal with the Anti-IV and outcome given the latent confounder.

- fe(varlist) Absorb fixed effects.

- weight(...) For ratio: probability/frequency/analytic weights using brackets, e.g., weight([aw=wt]). For gmm and 2sls: probability weights only, e.g., weight(wvar).

ratio

- vce(type) Variance estimator: AR (default, Anderson–Rubin), bootstrap, or asymptotic.

- cluster(varlist) Cluster-robust SEs.

- reps(#) Bootstrap repetitions (default 50).

- seed(#) Random seed for bootstrap reproducibility.

2sls

- cluster(varlist) Cluster-robust SEs.

- onestep Default, runs one-step GMM with 2SLS-equivalent weight matrix.

- twostep Runs two-step GMM with 2SLS in step one.

- ignoresingularity If a (nearly) singular matrix is detected, continue without an error.

gmm

- cluster(varlist) Cluster-robust SEs.

- twostep Default; runs two-step GMM.

- initweightmatrix(matrix) Initial weighting matrix for two-step GMM. Defaults to identity. Accepts identity, unadjusted, or a matrix name. initialweightmatrix(unadjusted) requests a weight matrix that is suitable when the errors are homoskedastic. The GMM estimator with this weight matrix is equivalent to the 2SLS estimator.

- onestep Runs one-step GMM.

- weightingmatrix(matrix) Weight matrix for one-step GMM. Defaults to identity. Accepts identity, unadjusted, or a matrix name. weightingmatrix(unadjusted) requests a weight matrix that is suitable when the errors are homoskedastic. The GMM estimator with this weight matrix is equivalent to the 2SLS estimator.

- ignoresingularity If a (nearly) singular matrix is detected, continue without an error.

Estimation & storage

- eststo(name) Store fitted model under name. Also compatible with eststo: aivreg ....

- savefirst Report and store first-stage regression. For ratio, if firststo() is unspecified, first stage is named _ivreg2_varname where varname is the Anti-IV. For gmm and 2sls, defaults to saving as aivgmm_h for each Anti-IV h.

- firststo(name) Store first-stage estimates under name for ratio; for gmm and 2sls, first stages are stored as nameh for each Anti-IV h.

Saved results

aivreg saves results in e().

Scalars

e(Partial_F) partial F-statistic from first stage (Not in 2SLS or GMM)

e(df_r) residual degrees of freedom

e(N) number of observations

e(Jval) J-test statistic (2SLS and GMM only, and only when there are multiple Anti-IVs)

e(pval_J) p-value of J-test (2SLS and GMM only, and only when there are multiple Anti-IVs)

e(betavarname) coefficient on variable varname

e(SE_vcevarname) standard error of the coefficient on variable varname, using vce (either AR, asymp, boot, 2sls, or gmm); if AR, SE approximated using CI closest to zero

e(t_valvarname) t-value for the coefficient on variable varname

e(p_more_tvarname) t-test statistic for the coefficient on variable varname

e(lb_vcevarname) lower bound for the coefficient on variable varname (95% confidence), using vce (either AR, asymp, boot, 2sls, or gmm)

e(lb_vcevarname) upper bound for the coefficient on variable varname (95% confidence), using vce (either AR, asymp, boot, 2sls, or gmm)

e(kappa) condition number for inverted matrix in 2SLS and GMM formulas. Warning message appears when it is > 10^12. (2SLS and GMM only)[p_end}

Macros

e(cmd) aivreg

Matrices

e(b) coefficient vector

e(V) estimated covariance matrix of coefficients; in AR, diagonal matrix with values approximated from AR CI closest to zero

e(S) estimated covariance matrix of moments (2SLS and GMM only)

e(weightingmatrix) weight matrix (2SLS and GMM only; last weight matrix used for twostep)

Examples

The following examples use simulated or sampled data which are included in the aivreg package SSC release. All commands are clickable.

Flood Risk Example

The underlying data in Bell, Billings, Calder-Wang and Zhong (2024) are from commercial providers; for illustrative purposes, we thus provided a small, simulated version of the data. For the underlying DGP, please see simulate_flood_risk_data.do.

Load the simulated flood risk dataset.

use simulated_flood_risk.dta, clear

estimates clear

An OLS regression with block FE is not sufficient to retrieve the implicit price of flood risk.

eststo: reghdfe log_price i.flood_factor, absorb(block_id)

If we control for the log income of the home buyers, under the intuition that it is informative for the unobserved quality, the estimates are still biased.

eststo: reghdfe log_price i.flood_factor log_income, absorb(block_id)

But when aivreg uses income as the anti-IV, it will correctly estimate the implicit price of flood risk.

eststo: aivreg log_price i.flood_factor, aiv(log_income) fe(block_id) vce(asymp)

aivreg can also use Anderson-Rubin confidence intervals. This is particularly helpful when there is a weak anti-IV. Anderson-Rubin confidence intervals are the default of aivreg; however, one can also call them using vce(AR). In this setting, log income is a strong anti-IV, so the confidence interval is similar to those calculated above.

eststo: aivreg log_price i.flood_factor, aiv(log_income) fe(block_id) vce(AR)

The option savefirst shows the first stage regression to help judge the strength on the anti-IV.

aivreg log_price i.flood_factor, aiv(log_income) fe(block_id) vce(AR) savefirst

Display or export results with esttab.

esttab est1 est2 est3 est4, mgroup("reghdfe" "reghdfe + anti-IV control" "aivreg" "aivreg + AR CI", pattern(1 1 1 1)) modelwidth(20) varwidth(18) label

estimates clear

There is also a 2SLS version which allows for multiple anti-IV variables.

eststo: aivreg 2sls log_price i.flood_factor i.block_id, aiv(log_income)

And this is the more general GMM version of aivreg.

eststo: aivreg gmm log_price i.flood_factor i.block_id, aiv(log_income)

GMM and 2SLS estimators also offer a savefirst option.

aivreg gmm log_price i.flood_factor i.block_id, aiv(log_income) savefirst

Show results in esttab.

esttab est1 est2, keep(flood_factor*) mgroup("2sls" "GMM", pattern(1 1)) label

Safety and Wages Example

Load the dataset of wages and job safety, which is sampled from the data used in Bell (2020).

use safety_aivreg_example.dta, clear

estimates clear

A naive hedonic regression can be misleading.

eststo: reg wage safety

Even controlling for the anti-IV in OLS may not fix it.

eststo: reg wage safety afqt_1_1981

aivreg improves identification using AFQT scores as an anti-IV.

eststo: aivreg wage safety, aiv(afqt_1_1981)

Show results in esttab.

esttab est1 est2 est3

aivreg GMM and 2SLS versions can use multiple anti-IVs. And savefirst shows the first stages. Here are the results using test scores again.

eststo: aivreg 2sls wage safety, aiv(afqt_1_1981)

Next, here is using height after controlling for sex.

eststo: aivreg 2sls wage safety, aiv(height_res_sex)

And now we use both anti-IVs. In the top right, aivreg shows the J-Test statistic and the corresponding p-value.

eststo: aivreg 2sls wage safety, aiv(afqt_1_1981 height_res_sex) savefirst

The results can be compared using esttab.

esttab est4 est5 est6

Dependencies

aivreg requires stata 17 or higher. It also requires ivreg2, ranktest, reghdfe, and ivreghdfe.

Contact

Questions or concerns: aivregstata@gmail.com

If you encounter "option requirements not allowed r(198)"

This is a rather common issue that stems from ivreghdfe not having been properly installed. Try reinstalling required packages through the following commands:

Install ftools

cap ado uninstall ftools

//raw.githubusercontent.com/sergiocorreia/ftools/master/src/") replace"'

Install reghdfe

cap ado uninstall reghdfe

//raw.githubusercontent.com/sergiocorreia/reghdfe/master/src/") replace"'

Install ivreg2

cap ado uninstall ivreg2

ssc install ivreg2, replace

Install ivreghdfe

cap ado uninstall ivreghdfe

//raw.githubusercontent.com/sergiocorreia/ivreghdfe/master/src/") replace"'

References

- Bell, A. (2020) Job Amenities and Earnings Inequality.

- Bell, A, Billings, S. B., Calder-Wang, S., & Zhong, S. (2024) An Anti-IV Approach for Pricing Residential Amenities: Applications to Flood Risk

- Correia, S. (2018) IVREGHDFE: Stata module for extended instrumental variable regressions.