Learn R Programming

ivdoctr

Instrumental variables are a useful tool in causal inference. In order to be valid, researchers impose both formal beliefs (instrumental relevance and the exclusion restriction) and informal beliefs (e.g., correlation between endogenous treatment and the error term). The goal of ivdoctr is to formalize those beliefs and quantifies how sensitive a researcher’s instrumental variables are to measurement error and instrument endogeneity. Using data and researcher’s beliefs on measurement error and instrument endogeneity, this package generates the space of consistent beliefs across measurement error, instrument endogeneity, and instrumental relevance for IV regressions.

Installation

You can install the released version of ivdoctr from CRAN with:

install.packages("ivdoctr")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("fditraglia/ivdoctr")

Example

This section illustrates how to use the ivdoctr package in practice. This example comes from “The colonial origins of comparative development: An empirical investigation” by Acemoglu, Johnson, and Robinson (2001). The authors study the effect of institutions on GDP per capita across 64 countries. Since institutional quality is endogenous, they use differences in mortality rates of early western settlers across colonies as an instrumental variable. The regression specification is as follows:

The authors state that there is likely a positive correlation between institutional quality and the error term, which could come from reverse causality (e.g., wealthier societies can afford better institutions) or omitted variables (e.g., rule of law or British culture are positively correlated with present-day institutional quality). This positive correlation is a researcher belief that can be input into ivdoctr using the r_TstarU_restriction argument that accepts a 2-column matrix of bounds. For the exercise, we use 0.9 as the conservative upper bound on the extent of the endogeneity.

The authors also state that up to 40% of the measure of “institutions” is noise. Measurement error is , so is the translation of this belief. The code below implements these beliefs and runs the estimation and saves the TeX table to “colonial.tex”:

library(ivdoctr)
endog <- c(0, 0.9)
meas <- c(0.6, 1)

colonial_example1 <- ivdoctr(y_name = "logpgp95", T_name = "avexpr", 
                             z_name = "logem4", data = colonial,
                             controls = NULL, robust = FALSE,
                             r_TstarU_restriction = endog,
                             k_restriction = meas,
                             example_name = "Colonial Origins")
                             
endog <- c(0, 0.9)
meas <- c(0.001, 0.6)

colonial_example2 <- ivdoctr(y_name = "logpgp95", T_name = "avexpr", 
                             z_name = "logem4", data = colonial,
                             controls = NULL, robust = FALSE,
                             r_TstarU_restriction = endog,
                             k_restriction = meas,
                             example_name = "Colonial Origins")
                                
makeTable(colonial_example1, colonial_example2, output = "colonial.tex")

To explore the surface of estimates consistent with the researcher’s beliefs, ivdoctr also generates an interactive 3D plot of the surface:

library(ivdoctr)
endog <- c(0, 0.9)
meas <- c(0.6, 1)

plot_3d_beta(y_name = "logpgp95", T_name = "avexpr", z_name = "logem4",
             data = colonial, r_TstarU_restriction = endog, k_restriction = meas)

Usage

This package exports three main functions:

  • ivdoctr(): Generates list of estimates, including OLS and IV regression objects
  • makeTable(): Generates the TeX code for a stand-alone regression table and saves it to the specified file.
  • plot_3d_beta(): Generates an interactive 3D plot illustrating the relationship between the causal estimates, instrument endogeneity, instrument invalidity, and measurement error.

Both ivdoctr and plot_3d_beta use the same primary inputs. Users input the name of the dataset (data), the name of the dependent variable (y_name), the name of the treatment variable(s) (T_name), the name(s) of the instrument(s) (z_name), and the names of the control variables (controls). Without any additional arguments, the functions will output the identified set. If users have beliefs over measurement error and/or instrument endogeneity, they can specify those using k_restriction and r_TstarU_restriction, respectively.

Copy Link

Version

Install

install.packages('ivdoctr')

Monthly Downloads

255

Version

1.0.1

License

CC0

Maintainer

Mallick Hossain

Last Published

December 5th, 2021

Functions in ivdoctr (1.0.1)

format_se

Creates LaTeX code for the standard error
get_M

Solves for the magnification factor
get_new_draws

Computes beliefs that support valid instrument
get_k_bounds_unrest

Given observables from the data, generates unrestricted bounds for kappa. Vectorized
get_L

Computes L, lower bound for kappa_tilde in paper
makeTable

Generates table of parameter estimates given user restrictions and data
make_full_row

Takes the OLS and IV estimates and converts it to a row of the LaTeX table
format_HPDI

Creates LaTeX code for the HPDI
get_observables

Given data and function specification, returns the relevant correlations and covariances with any exogenous controls projected out.
format_est

Creates LaTeX code for parameter estimates
candidate2

Evaluates the edge where k is on the boundary. Vectorized wrt multiple draws of obs.
getCoverage

Computes coverage of list of intervals
get_psi_upper

Computes the upper bound of psi for binary data
g_functionA2

G function from Proposition A.2
get_psi_lower

Computes the lower bound of psi for binary data
getInterval

Generates smallest covering interval
get_p_valid

Compute the share of draws that could contain a valid instrument.
rinvwish

Simulate draws from the inverse Wishart distribution
rect_points

Construct vectors of points that outline a rectangle.
get_alpha_bounds

Computes a0 and a1 bounds
get_beta

Solves for beta
get_bounds_unrest

Wrapper function combines all unrestricted bounds together. Vectorized
get_r_uz_bounds

Evaluates r_uz bounds given user restrictions on r_TstarU and kappa
toList

Convert 3-d array to list of matrixes
get_r_uz_bounds_unrest

Given observables from the data, generates the unrestricted bounds for rho_uz. Vectorized
weber

Becker and Woessmann (2009) Dataset
get_r_uz

Solves for r_uz given observables, r_TstarU, and kappa
get_r_TstarU_bounds_unrest

Given observables from the data, generates the unrestricted bounds for rho_TstarU. Data does not impose any restrictions on r_TstarU Vectorized
get_estimates

Computes OLS and IV estimates
get_s_u

Solves for the variance of the error term u
ivdoctr

Generates parameter estimates given user restrictions and data
map2color

Generates a custom color palette given a vector of numbers
myformat

Rounds x to two decimal places
get_beta_bounds_binary

Returns beta bounds in binary case using grid search
make_tex_row

Makes LaTeX code to make a row of a table and shift by some amount of columns if necessary
get_beta_bounds_binary_post

Generates beta bounds off of beta draws
plot_3d_beta

Plot ivdoctr Restrictions
draw_bounds

Computes bounds for simulated data
b_functionA3

B function from Proposition A3
draw_observables

Simulates different data draws
afghan

Burde and Linden (2013, AEJ Applied) Dataset
collapse_3d_array

Collapse 3-d array to matrix
candidate1

Evaluates the corners given user bounds. Vectorized wrt multiple draws of obs.
candidate3

Evaluates the edge where r_TstarU is on the boundary.
colonial

Acemoglu, Johnson, and Robinson (2001) Dataset
draw_sigma_jeffreys

Draws covariance matrix using the Jeffrey's Prior