Fits a two-stage least squares instrumental variables regression and returns heteroskedasticity-robust or cluster-robust standard errors, with optional weak-instrument, Wu-Hausman, and overidentification diagnostics.
iv_robust(
formula,
data,
weights,
subset,
clusters,
fixed_effects,
se_type = NULL,
ci = TRUE,
alpha = 0.05,
diagnostics = FALSE,
return_vcov = TRUE,
try_cholesky = FALSE
)An object of class "iv_robust", a list holding the estimate table in coefficients, std.error, df, statistic,
p.value, conf.low, conf.high, term, and outcome; the fit in
fitted.values, residuals, vcov, nobs, k, rank, df.residual,
and res_var; the summary statistics r.squared, adj.r.squared,
tss, and fstatistic; and se_type, weighted, clustered, fes,
alpha, terms, xlevels, and call.
residuals are the structural residuals, y - X beta, rather than the
second-stage ones. ei.iv, terms_regressors, and formula record the
two-stage structure. With diagnostics = TRUE the object also holds
diagnostic_first_stage_fstatistic, diagnostic_endogeneity_test, and
diagnostic_overid_test.
(required) An object of class formula with regressors and instruments,
e.g. y ~ x1 + x2 | z1 + z2.
(optional) A data.frame
(optional) The bare (unquoted) name of the weights variable
(optional) A bare (unquoted) expression specifying a subset
(optional) A bare (unquoted) name of the cluster variable
(optional) A one-sided formula of fixed effects to absorb,
such as ~ blockID. Uses FWL demeaning (see lm_robust() for details and
SE type restrictions). Diagnostics are not available with fixed_effects.
(optional) The standard error type. "HC2" and "HC3" work with
fixed_effects at any number of factors: the second stage runs on fitted
regressors, but those are demeaned by the same fixed effects, so the
leverage decomposition lm_robust() describes applies unchanged. "CR2"
with fixed_effects expands the dummies, as in estimatr 1.0.6.
Defaults: "HC2" (no clusters, with or without FE), "CR2" (clusters, no
FE), "CR0" (clusters, with FE).
(optional) Logical. Whether to compute p-values and confidence intervals.
(optional) The significance level, 0.05 by default.
(optional) Logical. Whether to compute IV diagnostic
statistics: the first-stage F test of the excluded instruments for each
endogenous regressor, a regression-based Wu-Hausman test of endogeneity,
and, when the model is overidentified, a test of the overidentifying
restrictions. That test is Sargan's with se_type = "classical" and
Wooldridge's (1995) robust score test otherwise, with the score's variance
summed within clusters when clusters is given. With weights, each
test is the one on the model with every row multiplied by the square root
of its weight. The first-stage F and Wu-Hausman tests are Wald tests
under the fit's own se_type, so a classical one is valid exactly when
the classical weighted standard errors are and a robust one exactly when
the robust ones are. The robust score test uses the score's HC0 or CR0
sandwich under every robust se_type, as Wooldridge (1995) and Stata
define it; the HC1, HC2, HC3, CR2, and "stata" refinements correct a
coefficient covariance and have no counterpart in a score test. The
overidentification test is NA, with a warning, for a clustered fit with
no more clusters than restrictions. All three reproduce Stata's
estat firststage, estat endogenous, and estat overid on every row of
the test suite's Stata fixture that Stata answers, except the robust score
test after aweights under forceweights, where Stata computes the
frequency-weight statistic instead.
(optional) Logical. Whether to return the vcov matrix.
(optional) Logical. Whether to solve by Cholesky
decomposition of X'X rather than by the default pivoted QR. FALSE by
default, and worth turning on in most applied settings: about 1.4 times
faster at n = 100,000 with two regressors, and 1.7 times faster at
n = 200,000 with 60 regressors, where it is 0.15s against 0.25s. The
saving is per fit, so it is worth most in a simulation that fits the same
design thousands of times.
Rank deficiency is caught on either path. Redundant columns come back as
NA exactly as they do from lm() whichever path ran, and a design that
is rank deficient falls back to the QR.
Whether it is safe turns on one question, whether two regressors are
nearly the same variable. Forming X'X squares the condition number, so
the Cholesky path has about twice the rounding error of the QR, and
only near-collinearity makes that visible. Differences of scale do not,
because the columns are normalized before either decomposition, so a
covariate in dollars beside one in years costs nothing. For a treatment
indicator, a few covariates, block or cluster dummies, the centered
interactions lm_lin() builds, or a factorial, the two paths agree to at
least 10 significant digits, which is why difference_in_means() sets it
to TRUE internally. Agreement falls to about 3 digits as the scaled
condition index reaches 1e6, and the QR fallback takes over above
roughly 1e8. Nothing interpretable lives in that range: a design at
1e6 returns a coefficient of 4.8e4 with a standard error of 4.6e4 on a
regressor whose true effect is zero. To check a design directly, scale the
columns first, since the unscaled condition number of a design in mixed
units is large for a reason that does not affect the fit:
kappa(sweep(X, 2, sqrt(colSums(X^2)), "/"), exact = TRUE).
set.seed(25)
n <- 200
dat <- data.frame(z = rbinom(n, 1, 0.5), cl = rep(1:20, each = 10))
dat$x <- dat$z * rbinom(n, 1, 0.7)
dat$y <- dat$x + rnorm(n)
# Endogenous regressor on the left of the bar, instrument on the right
fit <- iv_robust(y ~ x | z, data = dat)
tidy(fit)
# The same variance menu as lm_robust()
iv_robust(y ~ x | z, data = dat, se_type = "classical")
iv_robust(y ~ x | z, data = dat, clusters = cl)
# Weak-instrument, endogeneity, and overidentification tests
summary(iv_robust(y ~ x | z, data = dat, diagnostics = TRUE))
Run the code above in your browser using DataLab