Giter Club home page Giter Club logo

cragg's Introduction

Cragg

Build Status Codecov License: GPL v3 CRAN Downloads

An R package to implement the Cragg-Donald test for weak instruments.

Overview

The Cragg-Donald (1993) test is a common way to test for weak instruments in an IV regression but has never been implemented in R. The cragg package provides an implementation of the Cragg-Donald test in R and provides access to the critical values for the Cragg-Donald statistic developed in Stock and Yogo (Stock 2005). These tests quantify the degree to which weak instruments can undermine regression estimates for models with multiple endogenous variables / treatments.

Main Features

  • Calculates Cragg-Donald statistics for weak instruments.

  • Recommends critical values for the Cragg-Donald Statistic based on the largest allowable bias relative to regular OLS or the maximum allowable size distortion of the Wald test statistic.

Installation

install.packages("cragg")
library(cragg)

Usage

The cragg package has two main functions cragg_donald(), and stock_yogo_test().

cragg_donald() implements the Cragg-Donald test for weak instruments in R. It can be thought of as the matrix-equivalent of the first-stage F-test for weak instruments, and is used to evaluate models with multiple endogenous variables. This function has been tested against the results from STATA’s ivreg2 package (Baum, Mark, Stillman 2002) to ensure accuracy.

The syntax is show below:

library(cragg)
#Cragg-Donald Test
cragg_donald(
    ~X1+X2+X3,  #Controls
    ~D1 + D2,           #Treatments
    ~Z1 + Z2 + Z3,  #Instruments
    data =example_data
)
#> Cragg-Donald test for weak instruments:
#> 
#>      Data:                        example_data 
#>      Controls:                    ~X1 + X2 + X3 
#>      Treatments:                  ~D1 + D2 
#>      Instruments:                 ~Z1 + Z2 + Z3 
#> 
#>      Cragg-Donald Statistic:        186.1346 
#>      Df:                                 993

stock_yogo_test() implements the Stock and Yogo test for weak instruments. The test developed by Stock and Yogo (2005) is a decision rule meant to ensure that weak instruments do not pose a problem. Stock and Yogo suggest two methods to select the critical values: one based on maximum allowable bias relative to normal OLS and another based on the maximum size of a Wald test on all of the instruments. Both of these decision rules are implemented.

stock_yogo_test(
    ~X1+X2+X3,  #Controls
    ~D1,            #Treatments
    ~Z1 + Z2,   #Instruments
    B=.1,       #Maximum Allowable Size Distortion
    size_bias="size", #Calculate critical value for size distortions
    data =example_data
)
#> Results of Stock and Yogo test for weak instruments:
#> 
#>      Null Hypothesis:             Instruments are weak 
#>      Alternative Hypothesis:      Instruments are not weak 
#> 
#>      Data:                        example_data 
#>      Controls:                    ~X1 + X2 + X3 
#>      Treatments:                  ~D1 
#>      Instruments:                 ~Z1 + Z2 
#> 
#>      Alpha:                             0.05 
#>      Acceptable level of bias:    10% Wald test distortion.
#>      Critical Value:                   19.93 
#> 
#>      Cragg-Donald Statistic:        360.5978 
#>      Df:                                 994

References

Baum, Christopher F, Mark E Schaffer, and Steven Stillman. 2002. “IVREG2: Stata module for extended instrumental variables/2SLS and GMM estimation.” Statistical Software Components, Boston College Department of Economics.

Cragg, John G., and Stephen G. Donald. 1993. “Testing Identifiability and Specification in Instrumental Variable Models.” Econometric Theory 9 (2): 222–40. https://doi.org/10.1017/s0266466600007519.

Stock, James H., and Motohiro Yogo. 2005. “Testing for Weak Instruments in Linear IV Regression.” In Identification and Inference for Econometric Models, 80–108. Cambridge University Press. https://doi.org/10.1017/cbo9780511614491.006.

cragg's People

Contributors

beniaminogreen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

allisterh

cragg's Issues

Error in stock_yogo_reccomender

Hi,
I am having trouble running a toy example. How can I fix this problem?

> library(cragg)
> library(AER) # for dataaset
> data("WeakInstrument")
> stock_yogo_test(
+     # control variables
+     X = ~ 1, 
+     # endogeneous variables
+     D = ~ x, 
+     # instrument variables 
+     Z = ~ z, 
+     data = WeakInstrument
+ )
Error in stock_yogo_reccomender(K = cd$K2, N = cd$N, B, size_bias) : 
  Error: K must be greater than N+1 for this operation

Error: cannot allocate vector of size 741.2 Gb

Hello,
I ran into the error above when trying to implement the Stock-Yogo test using your cragg package. I'm working with a data set of about 1.4 million observations, 28 IVs, and one endogenous variable (I'm assuming that for size distortion results it's ok to have one endogenous variable but let me know if this is a problem for the package). The offending code is below:

stock_yogo_test(
  ~factor(region) + factor(yob) + Race + SMSA + Married + Age + Age2,  #Controls
  ~educ,            #Treatments
  ~factor(birthqtr):factor(yob),   #Instruments
  B=.1,       #Maximum Allowable Size Distortion
  size_bias="size", #Calculate critical value for size distortions
  data = df
)

I'm happy to provide the data. They are the Angrist and Krueger "quarter of birth-returns to education" data, which I'm using to create a problem set. Thanks in advance.

intercept handling

I have issues with the functions in the package when an intercept is included explicitly and when the model should not contain an intercept.

The following code models the intercept explicitly but throws an error. The reason is that it leads to two intercept columns in X_m that are of course perfectly collinear.

df <- iris
df$CONS <- 1
cragg_donald(X=~CONS+Sepal.Length, D=~Sepal.Width, Z=~Petal.Length + Petal.Width + Species, data = df)

Similarly, the code internally always adds an intercept column to X_m but some models might not have an intercept.

It would be good to simply have a consistent handling of an intercept. It is possibly easiest if users need to specify an intercept explicitly by creating a variable and add it to the ~X formula. Alternatively, a function argument could ask the user whether an intercept should be added or not.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.