[Gmm-commits] r220 - in pkg/momentfit: . R man vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Feb 2 23:39:35 CET 2024


Author: chaussep
Date: 2024-02-02 23:39:34 +0100 (Fri, 02 Feb 2024)
New Revision: 220

Modified:
   pkg/momentfit/NAMESPACE
   pkg/momentfit/R/weak.R
   pkg/momentfit/man/weakTest.Rd
   pkg/momentfit/vignettes/empir.bib
   pkg/momentfit/vignettes/weak.Rmd
   pkg/momentfit/vignettes/weak.pdf
Log:
started to add Montiel Olea and Pflueger 2013 test for weak instruments

Modified: pkg/momentfit/NAMESPACE
===================================================================
--- pkg/momentfit/NAMESPACE	2024-02-02 16:25:36 UTC (rev 219)
+++ pkg/momentfit/NAMESPACE	2024-02-02 22:39:34 UTC (rev 220)
@@ -44,7 +44,7 @@
        rhoET, rhoEL, rhoEEL, rhoHD, Wu_lam, EEL_lam, REEL_lam, getLambda, 
        solveGel, rhoETEL, rhoETHD, ETXX_lam, gelFit, evalGel, getImpProb,
        evalGelObj, momFct, gel4, setCoef, lse, getK, kclassfit, CDtest,
-       SYTables, SWtest)
+       SYTables, SWtest, MOPtest)
  
 ###  S3 methods ###
 

Modified: pkg/momentfit/R/weak.R
===================================================================
--- pkg/momentfit/R/weak.R	2024-02-02 16:25:36 UTC (rev 219)
+++ pkg/momentfit/R/weak.R	2024-02-02 22:39:34 UTC (rev 220)
@@ -236,6 +236,7 @@
     }
     cat("Number of included Endogenous: ", ncol(X2), add1, "\n", sep="")
     cat("Number of excluded Exogenous: ", ncol(Z2), add2, "\n", sep="")
+    cat("The test is not robust to heteroskedasticity\n")
     cat("Statistics: ", formatC(test, ...), "\n\n", sep="")
     SYTables(object, TRUE, SWcrit)
 }
@@ -317,7 +318,7 @@
     spec <- modelDims(object)
     if (sum(spec$isEndo)<1)
     {
-        warning("The model does not contain an endogenous variable")
+        warning("The model does not contain endogenous variables")
         return(NA)
     }
     if (sum(spec$isEndo)==1)
@@ -354,6 +355,66 @@
     add2 <- paste("(-", ncol(X2)-1, " for the critical values)", sep="")
     cat("Number of included Endogenous: ", ncol(X2), add1, "\n", sep="")
     cat("Number of excluded Exogenous: ", sum(z2n), add2, "\n", sep="")
+    cat("The test is not robust to heteroskedasticity\n")    
     cat("Statistics: ", formatC(test, ...), "\n\n", sep="")
     SYTables(object, TRUE, TRUE)
 }
+
+
+## Montiel Olea and Pflueger (2013)
+
+MOPtest <- function(object, tau=0.10, print=TRUE, ...)
+{
+    if (!inherits(object, "linearModel"))
+        stop("object must be of class linearModel")
+    spec <- modelDims(object)
+    if (sum(spec$isEndo)<1)
+    {
+        warning("The model does not contain endogenous variables")
+        return(NA)
+    }    
+    if (sum(spec$isEndo)>1)
+    {
+        warning("The MOP test is defined for models with only one endogenous variable")
+        return(NA)
+    }
+    Z <- model.matrix(object, "instrument")
+    X <- model.matrix(object)
+    X2 <- X[, spec$isEndo, drop=FALSE]
+    X1 <- X[, !spec$isEndo, drop=FALSE]
+    if (ncol(X1))
+    {
+        z2n <- !(colnames(Z) %in% colnames(X1))
+        fitX1 <- lm.fit(X1, Z[,z2n])
+        Z <- as.matrix(fitX1$residuals)
+        X2 <- qr.resid(fitX1$qr, X2)
+    }
+    Z <- qr.Q(qr(Z))
+    colnames(Z) <- paste("Z", 1:ncol(Z), sep="")
+    g <- reformulate(colnames(Z), colnames(X2), FALSE)
+    h <- reformulate(colnames(Z), NULL, FALSE)
+    dat <- data.frame(cbind(X2, Z))
+    m <- momentModel(g, h, data=dat, vcov=object at vcov,
+                     vcovOptions=object at vcovOptions)
+    v <- vcov(gmmFit(m), !(vcov=object at vcov=="iid"))
+    ev <- eigen(v)$val
+    se <- sum(ev)
+    se2 <- sum(ev^2)
+    me <- max(ev)
+    Feff <- sum(crossprod(X2, Z)^2)/se
+    x <- 1/tau
+    Keff <- se^2*(1+2*x)/(se2+2*x*se*me)
+    vcov <- object at vcov
+    if (vcov=="MDS")
+        vcov <- "HCCM"
+    if (!print)
+        return(c(Feff=Feff, Keff=Keff, x=x))
+    cat("Montiel Olea and Pflueger Test for Weak Instruments\n")
+    cat("****************************************************\n", sep="")
+    cat("Type of LS covariance matrix: ", vcov, "\n", sep="")
+    cat("Number of included Endogenous: ", ncol(X2), "\n", sep="")
+    cat("Effective degrees of freedom: ", Keff, "\n", sep="")
+    cat("x: ", x, "\n", sep="")
+    cat("Statistics: ", formatC(Feff, ...), "\n\n", sep="")
+    invisible()
+}

Modified: pkg/momentfit/man/weakTest.Rd
===================================================================
--- pkg/momentfit/man/weakTest.Rd	2024-02-02 16:25:36 UTC (rev 219)
+++ pkg/momentfit/man/weakTest.Rd	2024-02-02 22:39:34 UTC (rev 220)
@@ -1,6 +1,7 @@
 \name{weakTest}
 \alias{CDtest}
 \alias{SWtest}
+\alias{MOPtest}
 \alias{SYTables}
 \title{
 Tests for weak Instruments
@@ -7,8 +8,9 @@
 }
 \description{
 This is a collection of tests for weak instruments. It includes the
-Cragg and Donald test for the reduced rank hypothesis and the conditional
-F-test of Sanderson and Windmeijer (2016). The critical values of Stock
+Cragg and Donald test for the reduced rank hypothesis, the conditional
+F-test of Sanderson and Windmeijer (2016) and the robust effective F
+test of Montiel Olea and Pflueger (2013). The critical values of Stock
 and Yogo (2005) for the null hypothesis of weak instruments are also
 provided.  
 }
@@ -18,6 +20,8 @@
 
 CDtest(object, print=TRUE, SWcrit=FALSE, ...)
 
+MOPtest(object, tau=0.10, print=TRUE, ...)
+
 SYTables(object, print=TRUE, SWcrit=FALSE)
 
 }
@@ -32,7 +36,9 @@
   is the jth included endogenous variable and \eqn{y_{-j}} is the vector
   of the other included endogenous variables.
   }
-    
+
+  \item{tau}{The desired bias.}
+  
   \item{print}{
     If code{TRUE}, the result is printed and nothing is returned. See
     below for details on what the functions return when it is set to

Modified: pkg/momentfit/vignettes/empir.bib
===================================================================
--- pkg/momentfit/vignettes/empir.bib	2024-02-02 16:25:36 UTC (rev 219)
+++ pkg/momentfit/vignettes/empir.bib	2024-02-02 22:39:34 UTC (rev 220)
@@ -7,6 +7,17 @@
   YEAR={2013}
  } 
 
+ at article{andrews-stock-sun19,
+  AUTHOR={Andrews, I. and Stock, J.H. and Sun, L.},
+  TITLE={Weak Instruments in Intrumental Variables Regression:
+  Theory and Practice},
+  JOURNAL={Annual Review of Economics},
+  VOLUME={11},
+  PAGES={727--753},
+  YEAR={2019}
+ } 
+
+
 @article{sanderson-windmeijer16,
   AUTHOR={Sanderson, E. and Windmeijer, F.},
   TITLE={A weak instrument F-test in linear IV models with multiple

Modified: pkg/momentfit/vignettes/weak.Rmd
===================================================================
--- pkg/momentfit/vignettes/weak.Rmd	2024-02-02 16:25:36 UTC (rev 219)
+++ pkg/momentfit/vignettes/weak.Rmd	2024-02-02 22:39:34 UTC (rev 220)
@@ -518,8 +518,10 @@
 the @stock-yogo05 critical values for models with $l_2-1$ endogeous
 variables and $k_2-l_2+1$ excluded exogenous. This allows us to test
 the intruments for models with 3 endogenous variables without
-generating new tables. This is how the critical values are selected in
-`SWtest`:
+generating new tables. In the following, we can see that the critical
+values are obtained by reducing the number of endogenous variables by
+1 and the number of excluded exogenous variables by 2. Clearly, the
+instruments are weak in this simulated model.
 
 ```{r}
 SWtest(smod)
@@ -528,12 +530,58 @@
 These critical values are obtained by running the function `SYTables`
 with the argument `SWcrit` set to `TRUE`. Note that the authors show
 also that the same critical values can be used if we multiply the
-Cragg and Donald statistic by $k_2/(k_2-l_2+1)$.
+Cragg and Donald statistic by $k_2/(k_2-l_2+1)$. It is therefore
+possible to test for weak instruments in a model with 3 endogenous
+variables using the `CDtest` function, if we set the argument `SWcrit`
+to `TRUE`.
 
 
 ## Testing for weak instrument: @montiel-olea-pflueger13
 
+In most applied economic studies, it is unrealistic to assume that the
+errors are conditionally homoskedastic. When the errors are
+conditionally heteroskedastic, it is recommended by
+ at andrews-stock-sun19 to use the effective F-test of
+ at montiel-olea-pflueger13 (MOP). Assuming that $k_2=1$, which is the
+only option for this test, the procedure is:
 
+- Replace y by $M_1y$, $X_2$ by $M_1X_2$ and $Z_2$ by $M_1Z_2$ and
+  normalize the matrix $Z_2$ so that all vectors are
+  orthonormal. Then, the model become $$y=X_2\beta_2 + u$$ and $$X_2 =
+  Z_2\Pi_2+e\,.$$
+  
+- Obtain the robust covariance matrix estimate of $\hat\Pi_2$, $\hat{W}_2$.
+
+- The test is $F_{eff} = (X_2'Z_2Z_2'X_2)/[n^2tr(\hat{W}_2)]$
+
+This is computed by the function `MOPtest`. For now, no critical
+values are reported. Will be added soon.
+
+```{r}
+MOPtest(mod2)
+```
+
+We can see that it is close to the non-robust F test:
+
+```{r}
+CDtest(mod2, FALSE)
+```
+
+This is because the model `mod2` is defined with
+`vcov="iid"`. Therefore, $\hat{W}_2$ is a non-robust covariance
+matrix. If we want an HCCM estimator, we need to define the model with
+`vcov="MDS"`. It is also possible to compute the test using HAC
+estimator if needed. We use the `update` method to change `vcov` and
+rerun the test. We see that the robust test is a little higher than
+the non-robust. 
+
+```{r}
+mod2 <- update(mod2, vcov="MDS")
+MOPtest(mod2)
+```
+
+
+
 ## Data Generating Process (for later use)
 
 The following function is used to generate dataset with $k$

Modified: pkg/momentfit/vignettes/weak.pdf
===================================================================
(Binary files differ)



More information about the Gmm-commits mailing list