[Gmm-commits] r127 - in pkg/gmm4: R data man vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 28 23:10:11 CEST 2018


Author: chaussep
Date: 2018-06-28 23:10:11 +0200 (Thu, 28 Jun 2018)
New Revision: 127

Added:
   pkg/gmm4/data/Klein.rda
   pkg/gmm4/man/Klein.Rd
Modified:
   pkg/gmm4/R/rsysGmmModels-methods.R
   pkg/gmm4/R/sgmmfit-methods.R
   pkg/gmm4/R/sysGmmModels-methods.R
   pkg/gmm4/vignettes/empir.bib
   pkg/gmm4/vignettes/gmmS4.Rnw
   pkg/gmm4/vignettes/gmmS4.pdf
Log:
Added examples in vignettes and fixed a few bugs

Modified: pkg/gmm4/R/rsysGmmModels-methods.R
===================================================================
--- pkg/gmm4/R/rsysGmmModels-methods.R	2018-06-27 19:57:00 UTC (rev 126)
+++ pkg/gmm4/R/rsysGmmModels-methods.R	2018-06-28 21:10:11 UTC (rev 127)
@@ -234,7 +234,7 @@
                   if (!object at cstSpec$crossEquRest)
                       dgt <- lapply(1:neqn, function(i) evalDMoment(object[i]))
                   else 
-                      dgt <- list(evalDMoment(as(object, "rlinearGmm")))
+                      dgt <- list(neqn*evalDMoment(as(object, "rlinearGmm")))
                   names(dgt) <- modelDims(object)$eqnNames
                   dgt
               })

Modified: pkg/gmm4/R/sgmmfit-methods.R
===================================================================
--- pkg/gmm4/R/sgmmfit-methods.R	2018-06-27 19:57:00 UTC (rev 126)
+++ pkg/gmm4/R/sgmmfit-methods.R	2018-06-28 21:10:11 UTC (rev 127)
@@ -79,7 +79,7 @@
                       }
                   spec <- modelDims(object at model)
                   if (breadOnly) {
-                      vcov <- bread(object)/spec$n
+                      vcov <- bread(object)/object at model@n
                       attr(vcov, "type") <- list(sandwich = FALSE, df.adj = FALSE, 
                                                  breadOnly = TRUE)
                       return(vcov)
@@ -88,17 +88,17 @@
                       sandwich <- !object at efficientGmm
                   meat <- meatGmm(object, sandwich)
                   if (!sandwich) {
-                      vcov <- solve(meat)/spec$n
+                      vcov <- solve(meat)/object at model@n
                   } else {
                       if (all(spec$k == spec$q)) {
                           G <- evalDMoment(object at model, coef(object))
                           v <- momentVcov(object at model, coef(object))
                           b <- lapply(G, solve)
                           b <- .GListToMat(b)
-                          vcov <- b %*% v %*% t(b)/spec$n
+                          vcov <- b %*% v %*% t(b)/object at model@n
                       } else {
                           b <- bread(object)
-                          vcov <- b %*% meat %*% b/spec$n
+                          vcov <- b %*% meat %*% b/object at model@n
                       }
                   }
                   tn <- paste(rep(spec$eqnNames, spec$k), ".", 

Modified: pkg/gmm4/R/sysGmmModels-methods.R
===================================================================
--- pkg/gmm4/R/sysGmmModels-methods.R	2018-06-27 19:57:00 UTC (rev 126)
+++ pkg/gmm4/R/sysGmmModels-methods.R	2018-06-28 21:10:11 UTC (rev 127)
@@ -591,7 +591,8 @@
 
 setMethod("momentVcov", signature("sysGmmModels"),
           function(object, theta, ...){
-              q <- modelDims(object)$q
+              spec <- modelDims(object)
+              q <- spec$q
               if (object at vcov == "MDS")
                   {
                       gt <- evalMoment(object, theta)
@@ -613,7 +614,7 @@
                   } else {
                       stop("not yet implemented for HAC")
                   }
-              wn <- paste(rep(object at eqnNames, q), ".", do.call("c", object at momNames), 
+              wn <- paste(rep(spec$eqnNames, q), ".", do.call("c", spec$momNames), 
                           sep = "")
               dimnames(w) <- list(wn,wn)
               w

Added: pkg/gmm4/data/Klein.rda
===================================================================
(Binary files differ)


Property changes on: pkg/gmm4/data/Klein.rda
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: pkg/gmm4/man/Klein.Rd
===================================================================
--- pkg/gmm4/man/Klein.Rd	                        (rev 0)
+++ pkg/gmm4/man/Klein.Rd	2018-06-28 21:10:11 UTC (rev 127)
@@ -0,0 +1,39 @@
+\name{Klein}
+\alias{Klein}
+\docType{data}
+\title{
+Klein (1950) macro data. 
+}
+\description{
+The data is used to reproduce examples of Greene (2012)
+}
+\usage{data("Klein")}
+\format{
+  A data frame with 22 observations on the following 10 variables.
+  \describe{
+    \item{\code{YEAR}}{a numeric vector}
+    \item{\code{C}}{a numeric vector}
+    \item{\code{P}}{a numeric vector}
+    \item{\code{WP}}{a numeric vector}
+    \item{\code{I}}{a numeric vector}
+    \item{\code{K1}}{a numeric vector}
+    \item{\code{X}}{a numeric vector}
+    \item{\code{WG}}{a numeric vector}
+    \item{\code{G}}{a numeric vector}
+    \item{\code{T}}{a numeric vector}
+  }
+}
+
+\source{
+  On Greene (2012) online resources.
+}
+\references{
+  Klein, L. (1950), \emph{Economic Fluctuations in the United-States
+  1921-1941}, New York: John Wiley and Sons.
+  
+Green, W.H.. (2012). \emph{Econometric Analysis, 7th edition}, Prentice Hall.    
+}
+\examples{
+data(Klein)
+}
+\keyword{datasets}

Modified: pkg/gmm4/vignettes/empir.bib
===================================================================
--- pkg/gmm4/vignettes/empir.bib	2018-06-27 19:57:00 UTC (rev 126)
+++ pkg/gmm4/vignettes/empir.bib	2018-06-28 21:10:11 UTC (rev 127)
@@ -505,6 +505,13 @@
 year = {2001},
 }
 
+ at Book{klein50,
+author = {Klein, L.},
+publisher = {New York: John Wiley and Sons},
+title = {Economic Fluctuations in the United-States 1921-1941},
+year = {1950},
+}
+
 @article{carrasco-chernov-florens-ghysels07,
 NUMBER={140},
 AUTHOR = {Carrasco, M. and Chernov, M. and Florens, J. P. and Ghysels, E.},

Modified: pkg/gmm4/vignettes/gmmS4.Rnw
===================================================================
--- pkg/gmm4/vignettes/gmmS4.Rnw	2018-06-27 19:57:00 UTC (rev 126)
+++ pkg/gmm4/vignettes/gmmS4.Rnw	2018-06-28 21:10:11 UTC (rev 127)
@@ -1566,8 +1566,91 @@
 @ 
 
 \subsection{Textbooks Applications} \label{sec:sgmmmodels-app}
+\subsubsection{Greene}
 
+In this section, we kind of reproduce results from \cite{greene12}. Textbook with GMM estimation of systems of equations are not common.
 
+In Table 10.3, the author estimates the following system of equations:
+\begin{eqnarray*}
+s_k &=& \beta_k + \delta_{kk} \log\left(\frac{p_k}{p_m}\right) +  \delta_{kl} \log\left(\frac{p_l}{p_m}\right) +  \delta_{ke} \log\left(\frac{p_e}{p_m}\right) + u_k\\
+s_l &=& \beta_l + \delta_{lk} \log\left(\frac{p_k}{p_m}\right) +  \delta_{ll} \log\left(\frac{p_l}{p_m}\right) +  \delta_{le} \log\left(\frac{p_e}{p_m}\right) + u_l\\
+s_e &=& \beta_e + \delta_{ek} \log\left(\frac{p_k}{p_m}\right) +  \delta_{el} \log\left(\frac{p_l}{p_m}\right) +  \delta_{ee} \log\left(\frac{p_e}{p_m}\right) + u_e
+\end{eqnarray*}
+
+where $k$, $l$, $e$  and $m$ stand for capital, labor, energy and materials. The dependent variables are shares and the regressors are prices. The equation for materials is omitted because it is equal to one minus the sum of the other three. The system with all four would be singular. Without any restriction, this is just a multivariate regression in which all equation are just identified. Any GMM estimation would therefore be identical to OLS. If we impose restrictions on the coefficients, however, some equations become over-identified and GMM deviates from OLS. In particular, if we assume iid errors, efficient GMM becomes SUR since all regressors are considered exogenous. It turns out that the theory behind the model implies that $\delta_{kl}=\delta_{lk}$, $\delta_{le}=\delta_{el}$ and $\delta_{ke}=\delta_{ek}$. SUR estimation of the restricted model should lead to results that are close to Table 10.3 which were generated by restricted feasible GLS. 
+
+First, we normalize the prices and take the log.
+<<>>=
+data(ManufactCost)
+price <- c("Pk","Pl","Pe")
+ManufactCost[,price] <- log(ManufactCost[,price]/ManufactCost$Pm)
+@ 
+
+In the dataset, the shares are labeled $K$, $L$ and $E$. The unrestricted model can be defined as follows.
+
+<<>>=
+g <- list(Sk=K~Pk+Pl+Pe,
+          Sl=L~Pk+Pl+Pe,
+          Se=E~Pk+Pl+Pe)
+mod <- sysGmmModel(g, NULL, data=ManufactCost, vcov="iid")
+@ 
+
+Notice that the second argument is NULL because we want the instruments to be the regressors. We can now create the restricted model by adding the equation names to each coefficient names. 
+
+<<>>=
+R <- c("Sk.Pl=Sl.Pk", "Sk.Pe=Se.Pk", "Sl.Pe=Se.Pl")
+rmod <- restGmmModel(mod, R=R)
+@ 
+
+We can then estimate the model and print the coefficient matrix:
+
+<<>>=
+res <- gmmFit(rmod)
+summary(res)@coef
+@ 
+
+We can also test the restriction:
+
+<<>>=
+res.u <- gmmFit(mod)
+hypothesisTest(res.u, res)
+@ 
+
+In Table 10.5, the author estimates the macro model of \cite{klein50}:
+\begin{eqnarray*}
+C_t &=& \theta_{c0} + \theta_{c1} P_t + \theta_{c2} P_{t-1} + \theta_{c3} (W_t^p+W_t^g) + \epsilon_{ct}\\
+I_t &=& \theta_{i0} + \theta_{i1} P_t + \theta_{i2} P_{t-1} + \theta_{i3} K_{t-1} + \epsilon_{it}\\
+W^D_t &=& \theta_{w0} + \theta_{w1} X_t + \theta_{w2} X_{t-1} + \theta_{w3} A_t + \epsilon_{wt}
+\end{eqnarray*}
+The exogenous and predetermined variables that are used as instruments in each equation are $Z_t=\{G_t, T_t, W_t^g, A_t, K_{t-1}, P_{t-1}, X_{t-1}\}$. The data are annual observations from 1920 to 1941. We therefore have only 22 observations (21 because of the lags). 
+
+The table reports the results for many estimation method. We can reproduce 2SLS and 3SLS, but we only consider the latter because we have covered 2SLS cases in Section \ref{sec:gmmmodels-app}. First we arrange the data to get lags and $A_t$.
+
+<<>>=
+data(Klein)
+Klein1 <- Klein[-22,]
+Klein <- Klein[-1,]
+dimnames(Klein1) <- list(rownames(Klein), paste(colnames(Klein),"1",sep=""))
+Klein <- cbind(Klein, Klein1)
+Klein$A <- (Klein$YEAR-1931)
+@ 
+
+We can then estimate it by 3SLS. To reproduce the same standard errors, we need to use the bread only. In other words, using the final estimate to compute the weights leads to slightly different standard errors. In other words, the covariance matrix must be estimated using
+\[
+\left[G'\left(\tilde{\Sigma}^{-1}\otimes (Z'Z/n)^{-1}\right)G\right]^{-1}/n,
+\]
+where $\tilde{\Sigma}$ is computed using the 2SLS estimates. By default, the package updates $\tilde{\Sigma}$ using the final estimates. The following is identical to Table 10.5, section 3SLS of \cite{greene12}.
+
+<<>>=
+g <- list(C=C~P+P1+I(WP+WG),
+          I=I~P+P1+K1,
+          Wp=WP~X+X1+A)
+h <- ~G+T+WG+A+K1+P1+X1          
+res <- ThreeSLS(g, h, vcov="iid", data=Klein)
+summary(res, breadOnly=TRUE)@coef
+@ 
+
+
 \bibliography{empir}
 \pagebreak
 \Large{\textbf{Appendix}}

Modified: pkg/gmm4/vignettes/gmmS4.pdf
===================================================================
(Binary files differ)



More information about the Gmm-commits mailing list