Package 'civ'

Title: Categorical Instrumental Variables
Description: Implementation of the categorical instrumental variable (CIV) estimator proposed by Wiemann (2023) <arXiv:2311.17021>. CIV allows for optimal instrumental variable estimation in settings with relatively few observations per category. To obtain valid inference in these challenging settings, CIV leverages a regularization assumption that implies existence of a latent categorical variable with fixed finite support achieving the same first stage fit as the observed instrument.
Authors: Thomas Wiemann [aut, cre]
Maintainer: Thomas Wiemann <[email protected]>
License: GPL (>= 3)
Version: 0.1.0.9000
Built: 2026-10-13 01:15:49 UTC
Source: https://github.com/thomaswiemann/civ

Help Index


Select K for civ() by BIC.

Description

Calls kcmeans::bic() on the first-stage design civ() uses and returns that selection. Pass $K to civ().

Usage

bic(D, Z, X = NULL, K_bar, regularize_EDZ = FALSE)

Arguments

D

A matrix of endogenous variables.

Z

A matrix of instruments, where the first column corresponds to the categorical instrument.

X

An optional matrix of control variables.

K_bar

Upper bound passed to kcmeans::bic().

regularize_EDZ

A boolean indicating whether E[D∣Z]E[D|Z] should be regularized directly rather than E[D−X⊤π∣Z]E[D - X^\top\pi\vert Z].

Details

With regularize_EDZ = FALSE the call is kcmeans::bic(D, cbind(Z, X), K_bar = K_bar). With regularize_EDZ = TRUE it is kcmeans::bic(D, Z, K_bar = K_bar).

Value

The list returned by kcmeans::bic().

Examples

nobs <- 800
Z <- sample(1:20, nobs, replace = TRUE)
Z0 <- Z %% 2
U_V <- matrix(rnorm(2 * nobs, 0, 1), nobs, 2) %*%
  chol(matrix(c(1, 0.6, 0.6, 1), 2, 2))
D <- Z0 + U_V[, 2]
y <- D + U_V[, 1]
sel <- bic(D, Z, K_bar = 4)
civ(y, D, Z, K = sel$K)

Categorical Instrumental Variable Estimator.

Description

Implementation of the categorical instrumental variable estimator.

Usage

civ(y, D, Z, X = NULL, K = 2, regularize_EDZ = FALSE)

Arguments

y

The outcome variable, a numerical vector.

D

A matrix of endogenous variables.

Z

A matrix of instruments, where the first column corresponds to the categorical instrument.

X

An optional matrix of control variables.

K

The number of support points of the estimated instrument m^K\hat{m}_K, an integer greater than 2.

regularize_EDZ

A boolean indicating whether E[D∣Z]E[D|Z] should be regularized directly rather than E[D−X⊤π∣Z]E[D - X^\top\pi\vert Z].

Value

civ returns an object of S3 class civ. An object of class civ is a list containing the following components:

coef

A vector of second-stage coefficient estimates.

iv_fit

Object of class ivreg from the IV regression of y on D and X using the the estimated F^K\hat{F}_K as an instrument for D. See also AER::ivreg() for details.

kcmeans_fit

Object of class kcmeans from the K-Conditional-Means regression of D on Z and X. See also kcmeans::kcmeans() for details.

K

Pass-through of selected user-provided arguments. See above.

References

Fox J, Kleiber C, Zeileis A (2023). "ivreg: Instrumental-Variables Regression by '2SLS', '2SM', or '2SMM', with Diagnostics". R package.

Wiemann T (2023). "Optimal Categorical Instruments." https://arxiv.org/abs/2311.17021

Examples

# Simulate data from a simple IV model with 800 observations
nobs = 800 # sample size
Z <- sample(1:20, nobs, replace = TRUE) # observed instrument
Z0 <- Z %% 2 # underlying latent instrument
U_V <- matrix(rnorm(2 * nobs, 0, 1), nobs, 2) %*%
  chol(matrix(c(1, 0.6, 0.6, 1), 2, 2)) # first and second stage errors
D <- Z0 + U_V[, 2] # endogenous variable
y <- D + U_V[, 1] # outcome variable
# Estimate categorical instrument variable estimator with K = 2
civ_fit <- civ(y, D, Z, K = 3)
summary(civ_fit)

Inference Methods for the Categorical Instrumental Variable Estimator.

Description

Inference methods for the categorical instrumental variable estimators. Simple wrapper for AER::summary.ivreg().

Usage

## S3 method for class 'civ'
summary(object, ...)

Arguments

object

An object of class civ as fitted by civ().

...

Additional arguments passed to summary.ivreg. See AER::summary.ivreg() for a complete list of arguments.

Value

An object of class summary.ivreg with inference results.

References

Fox J, Kleiber C, Zeileis A (2023). "ivreg: Instrumental-Variables Regression by '2SLS', '2SM', or '2SMM', with Diagnostics". R package.

Wiemann T (2023). "Optimal Categorical Instruments." https://arxiv.org/abs/2311.17021

See Also

AER::summary.ivreg()

Examples

# Simulate data from a simple IV model with 800 observations
nobs = 800 # sample size
Z <- sample(1:20, nobs, replace = TRUE) # observed instrument
Z0 <- Z %% 2 # underlying latent instrument
U_V <- matrix(rnorm(2 * nobs, 0, 1), nobs, 2) %*%
  chol(matrix(c(1, 0.6, 0.6, 1), 2, 2)) # first and second stage errors
D <- Z0 + U_V[, 2] # endogenous variable
y <- D + U_V[, 1] # outcome variable
# Estimate categorical instrument variable estimator with K = 2
civ_fit <- civ(y, D, Z, K = 3)
summary(civ_fit)