[Gmm-commits] r122 - in pkg/gmm4: . R man vignettes
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jun 21 23:29:15 CEST 2018
Author: chaussep
Date: 2018-06-21 23:29:15 +0200 (Thu, 21 Jun 2018)
New Revision: 122
Modified:
pkg/gmm4/DESCRIPTION
pkg/gmm4/R/gmm4.R
pkg/gmm4/R/gmmfit-methods.R
pkg/gmm4/R/sgmmfit-methods.R
pkg/gmm4/R/sysGmmModels-methods.R
pkg/gmm4/R/tsls-methods.R
pkg/gmm4/man/gmm4.Rd
pkg/gmm4/man/specTest-methods.Rd
pkg/gmm4/man/vcov-methods.Rd
pkg/gmm4/vignettes/empir.bib
pkg/gmm4/vignettes/gmmS4.Rnw
pkg/gmm4/vignettes/gmmS4.pdf
Log:
worked on the vignette and add functionalities
Modified: pkg/gmm4/DESCRIPTION
===================================================================
--- pkg/gmm4/DESCRIPTION 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/DESCRIPTION 2018-06-21 21:29:15 UTC (rev 122)
@@ -9,13 +9,13 @@
welcome. See the vignette for more details.
Depends: R (>= 2.10.0), sandwich
Imports: stats, methods
-Suggests: lmtest, knitr
+Suggests: lmtest, knitr, texreg
Collate: 'allClasses.R' 'validity.R' 'gmmData.R' 'gmmModels-methods.R'
'gmmfit-methods.R' 'tsls-methods.R' 'specTest-methods.R'
'summaryGmm-methods.R' 'gmmWeights-methods.R' 'gmmModel.R'
- 'rGmmModel-methods.R' 'gmm4.R' 'hypothesisTest-methods.R'
+ 'rGmmModel-methods.R' 'hypothesisTest-methods.R'
'sysGmmModel.R' 'sysGmmModels-methods.R' 'rsysGmmModels-methods.R'
- 'sgmmfit-methods.R'
+ 'sgmmfit-methods.R' 'gmm4.R'
License: GPL (>= 2)
NeedsCompilation: no
VignetteBuilder: knitr
Modified: pkg/gmm4/R/gmm4.R
===================================================================
--- pkg/gmm4/R/gmm4.R 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/R/gmm4.R 2018-06-21 21:29:15 UTC (rev 122)
@@ -1,26 +1,110 @@
-################### the main gmm function ###################
+################### the main gmm functions ###################
+########## These functions ar to avoid having to builf model objects
-gmm4 <- function(g, x, tet0=NULL,grad=NULL, type=c("twostep", "iter","cue", "onestep"),
- vcov = c("HAC", "MDS", "iid", "TrueFixed"),
- initW=c("tsls","ident"), weights="optimal", itermaxit=50,
- kernel = c("Quadratic Spectral", "Truncated", "Bartlett", "Parzen",
- "Tukey-Hanning"), crit = 1e-06,
- bw = "Andrews", prewhite = 1L, ar.method = "ols", approx = "AR(1)",
- kerntol = 1e-07, itertol=1e-7, centeredVcov = TRUE,
- data=parent.frame(), ...)
- {
- Call <- match.call()
- vcov <- match.arg(vcov)
- kernel <- match.arg(kernel)
- type <- match.arg(type)
- initW <- match.arg(initW)
- model <- gmmModel(g, x, tet0, grad, vcov, kernel, crit = 1e-06,
- bw, prewhite, ar.method, approx, kerntol, centeredVcov, data)
-
- fit <- gmmFit(model, type, itertol, initW, weights,
- itermaxit=100, ...)
- fit at call <- Call
- fit
- }
+gmm4 <- function (g, x, tet0 = NULL, grad = NULL,
+ type = c("twostep", "iter", "cue", "onestep"),
+ vcov = c("MDS", "HAC", "iid", "TrueFixed"),
+ initW = c("ident", "tsls", "EbyE"), weights = "optimal",
+ itermaxit = 50, cstLHS=NULL, cstRHS=NULL,
+ kernel = c("Quadratic Spectral", "Truncated",
+ "Bartlett", "Parzen", "Tukey-Hanning"), crit = 1e-06,
+ bw = "Andrews", prewhite = 1L, ar.method = "ols", approx = "AR(1)",
+ kerntol = 1e-07, itertol = 1e-07, centeredVcov = TRUE,
+ data = parent.frame(), ...)
+{
+ Call <- match.call()
+ vcov <- match.arg(vcov)
+ kernel <- match.arg(kernel)
+ type <- match.arg(type)
+ initW <- match.arg(initW)
+ if (vcov == "TrueFixed")
+ {
+ if (!is.matrix(weights) ||
+ !(class(weights) %in% c("gmmWeights", "sysGmmWeigths")))
+ stop("With TrueFixed vcov the weights must be provided")
+ efficientWeights <- TRUE
+ } else {
+ efficientWeights <- FALSE
+ }
+ if (is.list(g))
+ {
+ model <- sysGmmModel(g=g, h=x, tet0=tet0, vcov=vcov,
+ kernel=kernel, crit=crit, bw=bw, prewhite=prewhite,
+ ar.method=ar.method, approx=approx, tol=kerntol,
+ centeredVcov=centeredVcov, data=data)
+ } else {
+ model <- gmmModel(g=g, x=x, tet0=tet0, grad=grad, vcov=vcov,
+ kernel=kernel, crit=crit, bw=bw, prewhite=prewhite,
+ ar.method=ar.method, approx=approx, tol=kerntol,
+ centeredVcov=centeredVcov, data=data)
+ if (initW == "EbyE")
+ {
+ warning("initW cannot be EbyE for single equations, initW set to ident")
+ initW="ident"
+ }
+ }
+ if (!is.null(cstLHS))
+ model <- restGmmModel(model, cstLHS, cstRHS)
+
+ fit <- gmmFit(object=model, type=type, itertol=itertol, initW=initW,
+ weights=weights, itermaxit=itermaxit,
+ efficientWeights=efficientWeights, ...)
+ fit at call <- Call
+ fit
+}
+
+
+setMethod("tsls", "formula",
+ function(object, x, vcov = c("iid", "HAC", "MDS"),
+ kernel = c("Quadratic Spectral", "Truncated", "Bartlett",
+ "Parzen", "Tukey-Hanning"), crit = 1e-06, bw = "Andrews",
+ prewhite = 1L, ar.method = "ols", approx = "AR(1)", kerntol = 1e-07,
+ centeredVcov = TRUE, data = parent.frame())
+ {
+ vcov <- match.arg(vcov)
+ kernel <- match.arg(kernel)
+ model <- gmmModel(g = object, x = x, vcov = vcov,
+ kernel = kernel, crit = crit, bw = bw,
+ prewhite = prewhite, ar.method = ar.method,
+ approx = approx, tol = kerntol,
+ centeredVcov = centeredVcov, data = data)
+ tsls(model)
+ })
+
+
+setMethod("tsls", "list",
+ function(object, x=NULL, vcov = c("iid", "HAC", "MDS"),
+ kernel = c("Quadratic Spectral", "Truncated", "Bartlett",
+ "Parzen", "Tukey-Hanning"), crit = 1e-06, bw = "Andrews",
+ prewhite = 1L, ar.method = "ols", approx = "AR(1)", kerntol = 1e-07,
+ centeredVcov = TRUE, data = parent.frame())
+ {
+ vcov <- match.arg(vcov)
+ kernel <- match.arg(kernel)
+ model <- sysGmmModel(g = object, h = x, vcov = vcov,
+ kernel = kernel, crit = crit, bw = bw,
+ prewhite = prewhite, ar.method = ar.method,
+ approx = approx, tol = kerntol,
+ centeredVcov = centeredVcov, data = data)
+ tsls(model)
+ })
+
+
+setMethod("ThreeSLS", "list",
+ function(object, x=NULL, vcov = c("iid", "HAC", "MDS"),
+ kernel = c("Quadratic Spectral", "Truncated", "Bartlett",
+ "Parzen", "Tukey-Hanning"), crit = 1e-06, bw = "Andrews",
+ prewhite = 1L, ar.method = "ols", approx = "AR(1)", kerntol = 1e-07,
+ centeredVcov = TRUE, data = parent.frame())
+ {
+ vcov <- match.arg(vcov)
+ kernel <- match.arg(kernel)
+ model <- sysGmmModel(g = object, h = x, vcov = vcov,
+ kernel = kernel, crit = crit, bw = bw,
+ prewhite = prewhite, ar.method = ar.method,
+ approx = approx, tol = kerntol,
+ centeredVcov = centeredVcov, data = data)
+ ThreeSLS(model)
+ })
Modified: pkg/gmm4/R/gmmfit-methods.R
===================================================================
--- pkg/gmm4/R/gmmfit-methods.R 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/R/gmmfit-methods.R 2018-06-21 21:29:15 UTC (rev 122)
@@ -45,7 +45,16 @@
setGeneric("vcov")
setMethod("vcov", "gmmfit",
- function(object, sandwich=NULL, df.adj=FALSE, breadOnly=FALSE) {
+ function(object, sandwich=NULL, df.adj=FALSE, breadOnly=FALSE,
+ modelVcov=NULL) {
+ if (!is.null(modelVcov))
+ {
+ if (modelVcov != object at model@vcov)
+ {
+ slot(object at model, "vcov") <- modelVcov
+ sandwich <- TRUE
+ }
+ }
spec <- modelDims(object at model)
if (breadOnly)
{
@@ -132,9 +141,9 @@
ans <- new("specTest", test=j, testname=J_test)
ans})
-setMethod("specTest", signature("gmmfit", "integer"),
+setMethod("specTest", signature("gmmfit", "numeric"),
function(object, which) {
- which <- sort(unique(which))
+ which <- sort(unique(as.integer(which)))
spec <- modelDims(object at model)
q <- spec$q
if (!all(which%in%(1:q)))
Modified: pkg/gmm4/R/sgmmfit-methods.R
===================================================================
--- pkg/gmm4/R/sgmmfit-methods.R 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/R/sgmmfit-methods.R 2018-06-21 21:29:15 UTC (rev 122)
@@ -67,8 +67,16 @@
setMethod("vcov", "sgmmfit",
function (object, sandwich = NULL, df.adj = FALSE,
- breadOnly = FALSE)
+ breadOnly = FALSE, modelVcov=NULL)
{
+ if (!is.null(modelVcov))
+ {
+ if (modelVcov != object at model@vcov)
+ {
+ slot(object at model, "vcov") <- modelVcov
+ sandwich <- TRUE
+ }
+ }
spec <- modelDims(object at model)
if (breadOnly) {
vcov <- bread(object)/spec$n
Modified: pkg/gmm4/R/sysGmmModels-methods.R
===================================================================
--- pkg/gmm4/R/sysGmmModels-methods.R 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/R/sysGmmModels-methods.R 2018-06-21 21:29:15 UTC (rev 122)
@@ -649,8 +649,7 @@
stop("3SLS is for slinearGmm classes")
if (!object at sameMom)
stop("For 3SLS, the instruments must be the same in each equation")
- if (object at vcov != "iid")
- stop("For 3SLS, the error must be homoscedastic")
+ efficientGmm <- object at vcov == "iid"
spec <- modelDims(object)
n <- spec$n
neqn <- length(object at eqnNames)
@@ -699,7 +698,7 @@
HAC=list(), sameMom=TRUE, eqnNames=object at eqnNames)
new("sgmmfit", theta=theta, convergence=NULL,
convIter=rep(NULL, neqn), call=Call, type=type, wObj=wObj,
- niter=2L, efficientGmm=TRUE, model=object)
+ niter=2L, efficientGmm=efficientGmm, model=object)
})
Modified: pkg/gmm4/R/tsls-methods.R
===================================================================
--- pkg/gmm4/R/tsls-methods.R 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/R/tsls-methods.R 2018-06-21 21:29:15 UTC (rev 122)
@@ -7,8 +7,10 @@
function(x, ...) {
sig <- sum(residuals(x)^2)
X <- model.matrix(x at model)
- Xhat <- qr.fitted(x at wObj@w, X)
- sig*chol2inv(qr.R(qr(Xhat)))
+ Xhat <- qr(qr.fitted(x at wObj@w, X))
+ v <- matrix(ncol=ncol(X), nrow=ncol(X))
+ v[Xhat$pivot, Xhat$pivot] <- chol2inv(qr.R(Xhat))
+ sig*v
})
## meat
Modified: pkg/gmm4/man/gmm4.Rd
===================================================================
--- pkg/gmm4/man/gmm4.Rd 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/man/gmm4.Rd 2018-06-21 21:29:15 UTC (rev 122)
@@ -1,37 +1,62 @@
\name{gmm4}
\alias{gmm4}
+\alias{tsls,formula-method}
+\alias{tsls,list-method}
+\alias{ThreeSLS,list-method}
\title{GMM estimation
}
\description{
-The main function to fit any model with GMM. As opposed to
-\code{\link{gmmFit}}, the arguments are more specific.
+The main functions and methods to fit any model with GMM. As opposed to
+\code{\link{gmmFit}}, models don't need to be created. It is all done by
+the functions. It is meant to be more user friendly. This document
+needs to changed. It is just a copy and paste from the gmm package
}
\usage{
-gmm4(g, x, tet0 = NULL, grad = NULL, type = c("twostep", "iter", "cue",
-"onestep"), vcov = c("HAC", "MDS", "iid", "TrueFixed"), initW =
-c("tsls", "ident"), weights = "optimal", itermaxit = 50,
-kernel = c("Quadratic Spectral", "Truncated", "Bartlett", "Parzen",
-"Tukey-Hanning"), crit = 1e-06, bw = "Andrews", prewhite = 1L,
-ar.method = "ols", approx = "AR(1)", kerntol = 1e-07,
-itertol = 1e-07, centeredVcov = TRUE, data = parent.frame(), ...)
+gmm4(g, x, tet0 = NULL, grad = NULL,
+ type = c("twostep", "iter", "cue", "onestep"),
+ vcov = c("MDS", "HAC", "iid", "TrueFixed"),
+ initW = c("ident", "tsls", "EbyE"), weights = "optimal",
+ itermaxit = 50, cstLHS=NULL, cstRHS=NULL,
+ kernel = c("Quadratic Spectral", "Truncated",
+ "Bartlett", "Parzen", "Tukey-Hanning"), crit = 1e-06,
+ bw = "Andrews", prewhite = 1L, ar.method = "ols", approx = "AR(1)",
+ kerntol = 1e-07, itertol = 1e-07, centeredVcov = TRUE,
+ data = parent.frame(), ...)
+
+\S4method{tsls}{formula}(object, x, vcov = c("iid", "HAC", "MDS"),
+ kernel = c("Quadratic Spectral", "Truncated", "Bartlett",
+ "Parzen", "Tukey-Hanning"), crit = 1e-06, bw = "Andrews",
+ prewhite = 1L, ar.method = "ols", approx = "AR(1)", kerntol = 1e-07,
+ centeredVcov = TRUE, data = parent.frame())
+
+\S4method{tsls}{list}(object, x=NULL, vcov = c("iid", "HAC", "MDS"),
+ kernel = c("Quadratic Spectral", "Truncated", "Bartlett",
+ "Parzen", "Tukey-Hanning"), crit = 1e-06, bw = "Andrews",
+ prewhite = 1L, ar.method = "ols", approx = "AR(1)", kerntol = 1e-07,
+ centeredVcov = TRUE, data = parent.frame())
+
+\S4method{ThreeSLS}{list}(object, x=NULL, vcov = c("iid", "HAC", "MDS"),
+ kernel = c("Quadratic Spectral", "Truncated", "Bartlett",
+ "Parzen", "Tukey-Hanning"), crit = 1e-06, bw = "Andrews",
+ prewhite = 1L, ar.method = "ols", approx = "AR(1)", kerntol = 1e-07,
+ centeredVcov = TRUE, data = parent.frame())
}
\arguments{
+\item{object}{A formula or a list of formulas.}
\item{g}{A function of the form \eqn{g(\theta,x)} and which returns a
\eqn{n \times q} matrix with typical element \eqn{g_i(\theta,x_t)} for
\eqn{i=1,...q} and \eqn{t=1,...,n}. This matrix is then used to build
the q sample moment conditions. It can also be a formula if the model
- is linear (see detailsbelow).}
+ is linear or nonlinear, or a list of formulas for systems of equations.}
\item{x}{The matrix or vector of data from which the function
\eqn{g(\theta,x)} is computed. If "g" is a formula, it is an \eqn{n
\times Nh} matrix of instruments or a formula (see details below).}
-
\item{tet0}{A \eqn{k \times 1} vector of starting values. It is required
- only when "g" is a function because only then a numerical algorithm is
- used to minimize the objective function. If the dimension of
- \eqn{\theta} is one, see the argument "optfct".}
+ only when "g" is a function or a nonlinear equation defined by a
+ formula, in which case, it must be a named vector}
\item{grad}{A function of the form \eqn{G(\theta,x)} which returns a
\eqn{q\times k} matrix of derivatives of \eqn{\bar{g}(\theta)} with
@@ -100,8 +125,15 @@
computing its covariance matrix. Doing so may improve inference.}
\item{data}{A data.frame or a matrix with column names (Optional). }
-
- \item{\dots}{Arguments to pass to other methods. Not used for now.}
+
+ \item{cstLHS}{The left hand side of the constraints to impose on the
+ coefficients. See \code{\link{restGmmModel}} for more details.}
+
+ \item{cstRHS}{The right hand side of the constraints to impose on the
+ coefficients. See \code{\link{restGmmModel}} for more details.}
+
+ \item{\dots}{Arguments to pass to \code{\link{optim}} when the model
+ is nonlinear.}
}
\value{
Modified: pkg/gmm4/man/specTest-methods.Rd
===================================================================
--- pkg/gmm4/man/specTest-methods.Rd 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/man/specTest-methods.Rd 2018-06-21 21:29:15 UTC (rev 122)
@@ -4,7 +4,7 @@
\alias{specTest-methods}
\alias{specTest,gmmfit,missing-method}
\alias{specTest,sgmmfit,missing-method}
-\alias{specTest,gmmfit,integer-method}
+\alias{specTest,gmmfit,numeric-method}
\title{ ~~ Methods for Function \code{specTest} in Package \pkg{gmm4} ~~}
\description{
It computes tests of specification for GMM fit.
@@ -14,7 +14,7 @@
\S4method{specTest}{sgmmfit,missing}(object, which, df.adj=FALSE, wObj=NULL)
-\S4method{specTest}{gmmfit,integer}(object, which)
+\S4method{specTest}{gmmfit,numeric}(object, which)
}
\arguments{
\item{object}{GMM fit object}
@@ -42,7 +42,7 @@
\item{\code{signature(object = "sgmmfit", which="missing")}}{
}
-\item{\code{signature(object = "gmmfit", which="integer")}}{
+\item{\code{signature(object = "gmmfit", which="numeric")}}{
}
}}
@@ -57,13 +57,8 @@
}
\examples{
-x <- rchisq(200,5)
-z1 <- rnorm(200)
-z2 <- .2*x+rnorm(200)
-y <- x+rnorm(200)
-dat <- data.frame(y=y,z1=z1,x=x,z2=z2)
-theta <- c(beta0=1,beta1=2)
-model1 <- gmmModel(y~x, ~z1+z2, data=dat)
+data(simData)
+model1 <- gmmModel(y~x1, ~z1+z2, data=simData)
res <- gmmFit(model1)
specTest(res)
@@ -75,7 +70,7 @@
model <- gmmModel(LW~S+EXPR+IQ, ~S+EXPR+AGE+MED, data=dat, vcov="MDS")
res <- gmmFit(model)
## testing the orthogonality conditions of S
-specTest(res, 2L)
+specTest(res, 2)
}
\keyword{methods}
Modified: pkg/gmm4/man/vcov-methods.Rd
===================================================================
--- pkg/gmm4/man/vcov-methods.Rd 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/man/vcov-methods.Rd 2018-06-21 21:29:15 UTC (rev 122)
@@ -10,10 +10,10 @@
}
\usage{
\S4method{vcov}{gmmfit}(object, sandwich=NULL, df.adj=FALSE,
-breadOnly=FALSE)
+breadOnly=FALSE, modelVcov=NULL)
\S4method{vcov}{sgmmfit}(object, sandwich=NULL, df.adj=FALSE,
-breadOnly=FALSE)
+breadOnly=FALSE, modelVcov=NULL)
\S4method{vcov}{tsls}(object, sandwich=TRUE, df.adj=FALSE)
@@ -33,6 +33,10 @@
HC0.}
\item{breadOnly}{If \code{TRUE}, the covariance matrix is set to the
bread (see details below).}
+ \item{modelVcov}{Should be one of "iid", "MDS" or "HAC". It is meant
+ to change the way the variance of the moments is computed. If it is
+ set to a different specification included in the model,
+ \code{sandwich} is set to \code{TRUE}. }
}
\section{Methods}{
\describe{
Modified: pkg/gmm4/vignettes/empir.bib
===================================================================
--- pkg/gmm4/vignettes/empir.bib 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/vignettes/empir.bib 2018-06-21 21:29:15 UTC (rev 122)
@@ -73,6 +73,41 @@
year = {2002},
}
+ at Book{greene12,
+author = {Greene, W. H.},
+title = {Econometric Analysis, 7th edition},
+publisher = {Prentice Hall},
+year = {2012},
+}
+
+ at Book{hayashi00,
+author = {Hayashi, FH.},
+title = {Econometrics},
+publisher = {Princeton University Press},
+year = {2000},
+}
+
+ at Book{hayashi00,
+author = {Hayashi, FH.},
+title = {Econometrics},
+publisher = {Princeton University Press},
+year = {2000},
+}
+
+ at Book{stock-watson15,
+author = {Stock, J.H. and Watson, M.W.},
+title = {Introduction to Econometrics},
+publisher = {Pearson},
+year = {2015},
+}
+
+ at Book{wooldridge16,
+author = {Wooldridge, J. M.},
+title = {Introductory Econometrics, A Modern Approach, 6th edition},
+publisher = {Cengage Learning},
+year = {2016},
+}
+
@Book{cochrane01,
author = {Cochrane, J. H.},
title = {Asset Pricing},
@@ -814,3 +849,15 @@
title = {Time Series Analysis},
year = {1994},
}
+
+
+ @Article{leifeld13,
+ title = {{texreg}: Conversion of Statistical Model Output in {R} to {\LaTeX} and {HTML} Tables},
+ author = {Philip Leifeld},
+ journal = {Journal of Statistical Software},
+ year = {2013},
+ volume = {55},
+ number = {8},
+ pages = {1--24},
+ url = {http://www.jstatsoft.org/v55/i08/},
+ }
\ No newline at end of file
Modified: pkg/gmm4/vignettes/gmmS4.Rnw
===================================================================
--- pkg/gmm4/vignettes/gmmS4.Rnw 2018-06-20 21:00:48 UTC (rev 121)
+++ pkg/gmm4/vignettes/gmmS4.Rnw 2018-06-21 21:29:15 UTC (rev 122)
@@ -448,7 +448,7 @@
wObj <- evalWeights(model, theta=1:2)
@
-Let compute $G$ and $\bar{g(\theta)}$
+Let compute $G$ and $\bar{g}(\theta)$
<<>>=
G <- evalDMoment(model, theta=1:2)
@@ -619,18 +619,24 @@
\item Any other sandwich estimator: $(G'WG)^{-1}G'W\hat{V}WG(G'WG)^{-1}/n$.
\item The argument ``breadonly'' is set to TRUE: $(G'WG)^{-1}/n$. For efficient GMM, it is asymptotically equivalent to $(G'\hat{V}^{-1}G)^{-1}/n$. It is particularly useful for efficient and fixed weighting matrices.
\end{itemize}
- The method is flexible enough that you may hand up with a non-valid covariance matrix if not careful. For example, setting ``sandwich'' to FALSE would lead to non valid covariance matrix if the model was not estimated by efficient GMM. It is important to understand that we assume here that the specifications of the model are valid. If you set ``vcov'' to iid and that the errors are heteroscedastic, there is nothing you can do to get valid standard errors. You need to recreate a new gmmModels object. An example will be given below using two-stage least squares.
+ The method is flexible enough that you may hand up with a non-valid covariance matrix if not careful. For example, setting ``sandwich'' to FALSE would lead to non valid covariance matrix if the model was not estimated by efficient GMM. It is important to understand that we assume here that the specifications of the model are valid. If you set ``vcov'' to iid and that the errors are heteroscedastic, GMM is no longer efficient and a sandwich matrix is required. But event if you set ``sandwich'' to TRUE, it will not solve the problem because the meat will be computed assuming the errors are iid. You can, however, set the argument ``modelVcov'' to ``MDS'' which will set ``sandwich'' to TRUE and compute the meat properly.
The argument ``df.adj'' can be set to TRUE if degrees of freedom adjustment is needed. In that case, the covariance matrix is multiplied by $n/(n-k)$. It is only included in the package to reproduce textbook examples. This adjustment is not really justified in the GMM context.
\item \textit{specTest}: It tests the null hypothesis $\E[g_i(\theta)]=0$ using the J-test. The statistics is $n\bar{g}'\hat{V}^{-1}\bar{g}$ and it is asymptotically distributed as a $\chi^2_{q-k}$ under the null. The model must have been estimated by efficient GMM for this test to be valid. The method returns an S4 class object.
<<>>=
-mod <- gmmModel(y~x1, ~z1+z2, data=simData, vcov="MDS")
+mod <- gmmModel(y~x1, ~z1+z2+z3, data=simData, vcov="MDS")
res <- gmmFit(mod)
specTest(res)
@
+It is also possible to test subsets of instruments. Suppose we suspect $z_2$ to be invalid. We would estimate the model without $z_2$ and compute the difference between the J-tests $(J_1-J_2)$, where $J_1$ is the J-test with $z_2$ and $J_2$ is the test without. The distribution is the number of instruments that we want to test, which is one in this example. To test it using the \textit{specTest} method, we specify which instrument we want to test ($z_2$ is the third instrument if we include the intercept):
+
+<<>>=
+specTest(res, 3)
+@
+
\item \textit{summary}: It computes important information about the estimated model. It is an S4 class object with a \textit{print} method that shows the results in the usual way.
<<>>=
@@ -713,8 +719,207 @@
e.r <- residuals(res.r)
@
-\item \textit{DWH}: It performs the Durbin-Wu-Hausman test.
+\item \textit{DWH}: It performs the Durbin-Wu-Hausman test. In general, the purpose of the test is to compare an efficient estimator, $\hat{\theta}$, with an inefficient one, $\tilde{\theta}$. Under the null hypothesis, both are consistent estimators of $\theta$ and under the alternative only $\tilde{\theta}$ is consistent. It is well known in the linear GMM setup as a way of comparing OLS with GMM. We want to test if it is worth instrumenting the suspected endogenous vaiables among th regressors. The method with signature $\{gmmfit, lm\}$ performs such test.
+
+<<>>=
+mod <- gmmModel(y~x1, ~z1+z2, data=simData, vcov="iid")
+res1 <- gmmFit(mod)
+res2 <- lm(y~x1, simData)
+DWH(res1,res2)
+@
+
+Used this way, the test is defined as $(\theta_{ols}-\theta_{gmm})'\Sigma (\theta_{ols}-\theta_{gmm})$, where $\Sigma$ is the generalized inverse of $[\widehat{\Var(\theta_{gmm})}-\widehat{\Var(\theta_{ols})}]$. The degrees of freedom is the rank of difference between the two covariance matrices. The argument ``tol'' is the tolerance level for the Moore-Penrose generalized inverse (for singular values less than ``tol'', their inverse is set to zero). The degrees of freedom should be 1 here because there is only one endogenous variable. That approach is therefore not too stable. Below, we consider a regression approach. The method with signature $\{gmmfit, gmmfit\}$ is used to compare two GMM estimators applied on the same regression model, using the same approach.
+For the signature $\{gmmfit, missing\}$, the test is done using an auxiliary regression. The fitted endogenous regressors are added to the regression model and a joint significance test on their coefficients is performed. For the example we have here, we would regress $x_1$ on $z_1$ and $z_2$ with an intercept, regress $y$ on $x_1$ and the fitted value $\hat{x}_1$ and test the coefficient of $\hat{x}_1$. Using \textit{DWH} we obtain:
+
+<<>>=
+DWH(res1)
+@
+
+Notice that the Wald test is robust in the sense that the covariance matrix is based on the specification of the ``gmmModels''. For example, if ``vcov'' was set to ``MDS'', an HCCM covariance matrix would be used.
+
+\subsection{The \textit{tsls} method}
+
+This method is to estimate linear models with two-stage least squares. It returns a ``tsls'' class object which inherits from ``gmmfit''. Most ``gmmfit'' methods are the same with the eception of \textit{bread}, \textit{meatGmm} and \textit{vcov}. They just use the structure of 2LSL to make them more computationally efficient. They may be removed in future version and included in the main ``gmmfit'' methods.
+
+If the model has iid error, \textit{gmmFit} and \textit{tsls} are numerically identical. In fact, the function is called by \textit{gmmFit} in that case. The main reason for using it is if we have a more complex variance structure but want to avoid using a fully efficient GMM, which may have worse small sample properties. Therefore, ``sandwich'' is set to TRUE in the \textit{vcov} method for ``tsls'' objects. In the following example, errors are assumed heteroscedastic, and the model is estimated by 2SLS. The \textit{summary} method returns, however, robust standard errors because ``sandwich=TRUE'' is the default in the \textit{vcov} method of ``tsls''.
+
+<<>>=
+mod <- gmmModel(y~x1, ~z1+z2+z3, data=simData, vcov="MDS")
+res <- tsls(mod)
+summary(res)@coef
+@
+
+\subsection{\textit{gmm4}: A function to fit them all}
+
+If you still think that the \textit{gmmFit} method is not simple enough because you have to crate a model first, the \textit{gmm4} function will do everything for you. It is the function that looks the most like its ancestor function \textit{gmm} from the gmm package. It is still required to specify the structure of variance for the moment conditions. In fact, it combines all arguments of the \textit{gmmModel} constructor and \textit{gmmFit} method. Here are a few examples.
+
+You want to estimate
+\[
+y = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + \epsilon
+\]
+using the instruments $\{x_2, z_1, z_2, z_3\}$. We do not want to assume homoscedasticity, so we want to set ``vcov'' to ``MDS''. We want to estimate the model by two-step GMM.
+
+<<>>=
+res1 <- gmm4(y~x1+x2, ~x2+z1+z2+z3, type="twostep", vcov="MDS", data=simData)
+res1
+@
+
+We want to compare it with iterative GMM:
+
+<<>>=
+res2 <- gmm4(y~x1+x2, ~x2+z1+z2+z3, type="iter", vcov="MDS", data=simData)
+@
+
+Now, we want to estimate the model with the restrictions $\theta_1=\theta_2$
+
+<<>>=
+res1.r <- gmm4(y~x1+x2, ~x2+z1+z2+z3, type="twostep", vcov="MDS",
+ data=simData, cstLHS="x1=x2")
+res1.r
+@
+
+Since the function returns a ``gmmfit'' object, all methods work with the output. We for example test the restriction:
+
+<<>>=
+hypothesisTest(res1, res1.r, type="LR")
+@
+
+There is also a \textit{tsls} method for ``formula'', which works the same way:
+
+<<>>=
+res3 <- tsls(y~x1+x2, ~x2+z1+z2+z3, vcov="MDS", data=simData)
+res3
+@
+
+It is still important to specify the variance structure in order to obtain the appropriate coefficient standard errors. To estimate a nonlinear model, \textit{gmm4} will recognize it by the way the formula is set along with the names vector ``tet0''.
+
+<<>>=
+res3 <- gmm4(y~theta0+exp(theta1*x1+theta2*x2), ~x2+z1+z2+z3+z4, vcov="iid",
+ tet0=c(theta0=1, theta1=0, theta2=0), data=simData)
+res3
+@
+
+
+\subsection{Textbooks Applications}
+
+In this section, we cover a few examples from major textbooks. Since it is meant to help users who care less about the structure of the package, we use, when possible, the quicker functions that we just intruduced in the last section.
+
+\subsubsection{Stock-Watson}
+In this section, we cover examples from \cite{stock-watson15}. In Chapter 12, the demand for cigarettes is estimated for 1985 using a panel. The following data change is required
+
+<<>>=
+data(CigarettesSW)
+CigarettesSW$rprice <- with(CigarettesSW, price/cpi)
+CigarettesSW$rincome <- with(CigarettesSW, income/population/cpi)
+CigarettesSW$tdiff <- with(CigarettesSW, (taxs - tax)/cpi)
+c1985 <- subset(CigarettesSW, year == "1985")
+c1995 <- subset(CigarettesSW, year == "1995")
+@
+
+In equation 12.15, the demand is estimated using sales tax as an instrument for price. In order to get the same standard errors, we need to assume ``MDS'', and use a sandwich matrix with degrees of freedom adjustment.
+
+<<>>=
+res1 <- gmm4(log(packs)~log(rprice)+log(rincome),
+ ~log(rincome)+tdiff, data = c1995, vcov="MDS")
+summary(res1, sandwich=TRUE, df.adj=TRUE)@coef
+@
+
+Equation 12.16, for which both cigarettes and sales taxes are used as instruments, can be reproduced using the same specifications. We also have to set ``centeredVcov'' to FALSE. We have not seen that argument yet. When set to TRUE, the moments are centered before computing the weights. For more details on when it should be centered, see \cite{hall05}.
+
+<<>>=
+res2<- tsls(log(packs)~log(rprice)+log(rincome),
+ ~log(rincome)+tdiff+I(tax/cpi), data = c1995,
+ centeredVcov=FALSE, vcov="MDS")
+summary(res2, sandwich=TRUE, df.adj=TRUE)@coef
+@
+
+In Table 12.1, the long-run demand elasticity is estimated over a 10 year period. They compare a model with only sales tax as instrument, a model with cigarettes tax only and one with both.
+
+<<echo=FALSE, message=FALSE, warning=FALSE>>=
+library(texreg)
+setMethod("extract", "gmmfit",
+ function(model, includeJTest=TRUE, includeFTest=TRUE, ...)
+ {
+ s <- summary(model, ...)
+ spec <- modelDims(model at model)
+ coefs <- s at coef
+ names <- rownames(coefs)
+ coef <- coefs[, 1]
+ se <- coefs[, 2]
+ pval <- coefs[, 4]
+ n <- model at model@n
+ gof <- numeric()
+ gof.names <- character()
+ gof.decimal <- logical()
+ if (includeJTest) {
+ if (spec$k == spec$q)
+ {
+ obj.fcn <- NA
+ obj.pv <- NA
+ } else {
+ obj.fcn <- s at specTest@test[1]
+ obj.pv <- s at specTest@test[3]
+ }
+ gof <- c(gof, obj.fcn, obj.pv)
+ gof.names <- c(gof.names, "J-test Statistics", "J-test p-value")
+ gof.decimal <- c(gof.decimal, TRUE, TRUE)
+ }
+ if (includeFTest) {
+ str <- s at strength$strength
+ if (is.null(str))
+ {
+ gof <- c(gof, NA)
+ gof.names <- c(gof.names, "First Stage F-stats")
+ gof.decimal <- c(gof.decimal, TRUE)
+ } else {
+ for (i in 1:nrow(str))
+ {
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/gmm -r 122
More information about the Gmm-commits
mailing list