Title: | Methods of Moments |
---|---|
Description: | Several classes for moment-based models are defined. The classes are defined for moment conditions derived from a single equation or a system of equations. The conditions can also be expressed as functions or formulas. Several methods are also offered to facilitate the development of different estimation techniques. The methods that are currently provided are the Generalized method of moments (Hansen 1982; <doi:10.2307/1912775>), for single equations and systems of equation, and the Generalized Empirical Likelihood (Smith 1997; <doi:10.1111/j.0013-0133.1997.174.x>, Kitamura 1997; <doi:10.1214/aos/1069362388>, Newey and Smith 2004; <doi:10.1111/j.1468-0262.2004.00482.x>, and Anatolyev 2005 <doi:10.1111/j.1468-0262.2005.00601.x>). |
Authors: | Pierre Chausse <[email protected]> |
Maintainer: | Pierre Chausse <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.5 |
Built: | 2024-10-29 03:15:58 UTC |
Source: | https://github.com/pchausse/momentfit |
Different subsetting methods for S4 class objects of the package. The subset method returns an new object with observations selected by the second argument. See example.
signature(x = "momentWeights", i = "integer", j = "missing")
It creates a partition from the weighting matrix.
signature(x = "momentWeights", i = "missing", j = "missing")
It generates the whole weighting matrix.
signature(x = "sysMomentWeights", i = "missing", j = "list")
It creates a partition from the weighting matrix. j has no effect here. It creates a partition from the weighting matrix in a systemof equations. i selects the equation and the list j the moments in each equation. Missing i means all equations.
signature(x = "sysMomentWeights", i = "numeric", j =
"missing")
It creates a partition from the weighting matrix. j has no effect here. It creates a partition from the weighting matrix in a systemof equations. i selects the equation and the list j the moments in each equation. Missing j means all moments.
signature(x = "sysMomentWeights", i = "missing", j = "missing")
No effect. It returns x.
signature(x = "snonlinearModel", i = "numeric", j="missing")
It generates a system of equations with a subset of equations selected
by i. If the number of remaining equations is one, it returns an
object of class "nonlinearGmm"
.
signature(x = "slinearModel", i = "numeric", j="missing")
It generates a system of equations with a subset of equations selected
by i. If the number of remaining equations is one, it returns an
object of class "linearModel"
.
signature(x = "rslinearModel", i = "numeric", j="missing")
It is only use to select one equation when no cross-equation restrictions are imposed. Only one equation can be selected.
signature(x = "rsnonlinearModel", i = "numeric", j="missing")
It is only use to select one equation when no cross-equation restrictions are imposed. Only one equation can be selected.
signature(x = "sysMomentModel", i = "numeric", j="list")
It generates a system of equations with a subset of equations selected
by i and a subset of moment conditions selected by j. If the number of
remaining equations is one, it returns an object of class "linearGmm"
.
signature(x = "sysMomentModel", i = "missing", j="missing")
No effect. It returns x.
signature(x = "momentModel", i = "missing", j = "missing")
Returns the model without any change.
signature(x = "functionModel", i = "numeric", j = "missing")
It generates the same model with a subset of the moment conditions.
signature(x = "formulaModel", i = "numeric", j = "missing")
It generates the same model with a subset of the moment conditions.
signature(x = "rfuncionModel", i = "numeric", j = "missing")
It generates the same model with a subset of the moment conditions. j has no effect here.
data(simData) model1 <- momentModel(y~x1+x2, ~x2+x3+z1+z2+z3, data=simData, vcov="MDS") w <- evalWeights(model1, theta=1:3) w[] w[1:3] ## A model with a subset of the instruments model1[1:4] ## Selecting the observations: subset(model1, simData[["x1"]]<3) subset(model1, 1:25)
data(simData) model1 <- momentModel(y~x1+x2, ~x2+x3+z1+z2+z3, data=simData, vcov="MDS") w <- evalWeights(model1, theta=1:3) w[] w[1:3] ## A model with a subset of the instruments model1[1:4] ## Selecting the observations: subset(model1, simData[["x1"]]<3) subset(model1, 1:25)
"allNLModel"
A union class for all nonlinear models. It includes
"nonlinearModel"
, "formulaModel"
, and
"functionModel"
.
A virtual Class: No objects may be created from it.
signature(object = "allNLModel", wObj = "momentWeights")
: ...
showClass("allNLModel")
showClass("allNLModel")
bread
in Package sandwich ~~It computes the bread in the sandwich representation of the covariance matrix of the GMM estimator.
## S4 method for signature 'gmmfit' bread(x, ...) ## S4 method for signature 'sgmmfit' bread(x, ...) ## S4 method for signature 'tsls' bread(x, ...)
## S4 method for signature 'gmmfit' bread(x, ...) ## S4 method for signature 'sgmmfit' bread(x, ...) ## S4 method for signature 'tsls' bread(x, ...)
x |
GMM fit object |
... |
Arguments to pass to other methods |
signature(x = "gmmfit")
signature(x = "sgmmfit")
signature(x = "tsls")
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) res <- gmmFit(model1) m <- meatGmm(res) b <- bread(res) ## Sandwich vcov b ## TSLS model2 <- momentModel(y~x1, ~z1+z2, data=simData, vcov="iid") res <- tsls(model2) bread(res)
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) res <- gmmFit(model1) m <- meatGmm(res) b <- bread(res) ## Sandwich vcov b ## TSLS model2 <- momentModel(y~x1, ~z1+z2, data=simData, vcov="iid") res <- tsls(model2) bread(res)
Panel data on cigarette consumption for the 48 continental US States from 1985–1995.
data("CigarettesSW")
data("CigarettesSW")
A data frame containing 48 observations on 7 variables for 2 periods.
Factor indicating state.
Factor indicating year.
Consumer price index.
State population.
Number of packs per capita.
State personal income (total, nominal).
Average state, federal and average local excise taxes for fiscal year.
Average price during fiscal year, including sales tax.
Average excise taxes for fiscal year, including sales tax.
Online complements to Stock and Watson (2007). The dataset and this help file comes from the AER package.
Stock, J.H. and Watson, M.W. (2007). Introduction to Econometrics, 2nd ed. Boston: Addison Wesley.
Christian Kleiber and Achim Zeileis (2008). Applied Econometrics with R. New York: Springer-Verlag. ISBN 978-0-387-77316-2. URL https://CRAN.R-project.org/package=AER
## Stock and Watson (2007) ## data and transformations 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") ## Equation 12.15 model1 <- momentModel(log(packs)~log(rprice)+log(rincome), ~log(rincome)+tdiff, data = c1995, vcov="MDS") res1 <- gmmFit(model1) ## HC0 robust se (different from the textbook) summary(res1, sandwich=TRUE) ## HC1 robust se (like in the textbook) ## A little harder to get, but is it really worth it ## in the case of GMM? summary(res1, sandwich=TRUE, df.adj=TRUE)@coef ## Equation 12.16 model2<- momentModel(log(packs)~log(rprice)+log(rincome), ~log(rincome)+tdiff+I(tax/cpi), data = c1995, centeredVcov=FALSE, vcov="MDS") res2<- tsls(model2) summary(res2, sandwich=TRUE, df.adj=TRUE) ## Table 12.1 data <- data.frame(dQ=log(c1995$pack/c1985$pack), dP=log(c1995$rprice/c1985$rprice), dTs=c1995$tdiff-c1985$tdiff, dT=c1995$tax/c1995$cpi-c1985$tax/c1985$cpi, dInc=log(c1995$rincome/c1985$rincome)) model1 <- momentModel(dQ~dP+dInc, ~dInc+dTs, vcov="MDS", data=data) model2 <- momentModel(dQ~dP+dInc, ~dInc+dT, vcov="MDS", data=data) model3 <- momentModel(dQ~dP+dInc, ~dInc+dTs+dT, vcov="MDS", data=data) res1 <- tsls(model1) summary(res1, TRUE, TRUE) res2 <- tsls(model2) summary(res2, TRUE, TRUE) res3 <- tsls(model3) summary(res3, TRUE, TRUE)
## Stock and Watson (2007) ## data and transformations 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") ## Equation 12.15 model1 <- momentModel(log(packs)~log(rprice)+log(rincome), ~log(rincome)+tdiff, data = c1995, vcov="MDS") res1 <- gmmFit(model1) ## HC0 robust se (different from the textbook) summary(res1, sandwich=TRUE) ## HC1 robust se (like in the textbook) ## A little harder to get, but is it really worth it ## in the case of GMM? summary(res1, sandwich=TRUE, df.adj=TRUE)@coef ## Equation 12.16 model2<- momentModel(log(packs)~log(rprice)+log(rincome), ~log(rincome)+tdiff+I(tax/cpi), data = c1995, centeredVcov=FALSE, vcov="MDS") res2<- tsls(model2) summary(res2, sandwich=TRUE, df.adj=TRUE) ## Table 12.1 data <- data.frame(dQ=log(c1995$pack/c1985$pack), dP=log(c1995$rprice/c1985$rprice), dTs=c1995$tdiff-c1985$tdiff, dT=c1995$tax/c1995$cpi-c1985$tax/c1985$cpi, dInc=log(c1995$rincome/c1985$rincome)) model1 <- momentModel(dQ~dP+dInc, ~dInc+dTs, vcov="MDS", data=data) model2 <- momentModel(dQ~dP+dInc, ~dInc+dT, vcov="MDS", data=data) model3 <- momentModel(dQ~dP+dInc, ~dInc+dTs+dT, vcov="MDS", data=data) res1 <- tsls(model1) summary(res1, TRUE, TRUE) res2 <- tsls(model2) summary(res2, TRUE, TRUE) res3 <- tsls(model3) summary(res3, TRUE, TRUE)
coef
in Package stats ~~It extract the coefficient estimates of some moment-based models.
signature(object = "gmmfit")
signature(object = "gelfit")
signature(object = "sgmmfit")
signature(object = "momentModel")
signature(object = "rlinearModel")
It gives the unrestricted representation of a restricted model. See examples.
signature(object = "rslinearModel")
It gives the unrestricted representation of a restricted model.
signature(object = "rsnonlinearModel")
It gives the unrestricted representation of a restricted model.
signature(object = "rfunctionModel")
It gives the unrestricted representation of a restricted model. See examples.
signature(object = "rformulaModel")
It gives the unrestricted representation of a restricted model. See examples.
signature(object = "rnonlinearModel")
It gives the unrestricted representation of a restricted nonlinear model.
data(simData) model1 <- momentModel(y~x1+x2+x3+z1, ~x1+x2+z1+z2+z3+z4, data=simData) res1 <- gmmFit(model1) coef(res1) ### Restricted models rmodel1 <- restModel(model1, R=c("x1=1", "x2=2*x3")) res2 <- gmmFit(rmodel1) res2 coef(rmodel1, coef(res2))
data(simData) model1 <- momentModel(y~x1+x2+x3+z1, ~x1+x2+z1+z2+z3+z4, data=simData) res1 <- gmmFit(model1) coef(res1) ### Restricted models rmodel1 <- restModel(model1, R=c("x1=1", "x2=2*x3")) res2 <- gmmFit(rmodel1) res2 coef(rmodel1, coef(res2))
"confint"
A class to store a confidence interval result.
Objects can be created by calls of the form new("confint", ...)
.
It is generated by the "confint"
method (see
confint-methods
).
interval
:Object of class "matrix"
~~
type
:Object of class "character"
~~
level
:Object of class "numeric"
~~
theta
:Object of class "numeric"
~~
signature(x = "confint")
: ...
signature(object = "confint")
: ...
showClass("confint")
showClass("confint")
confint
in Package stats ~~Method to contruct confidence intervals for objects of class
"gmmfit"
and "gelfit"
.
## S4 method for signature 'gmmfit' confint(object, parm, level = 0.95, vcov=NULL, area=FALSE, npoints=50, ...) ## S4 method for signature 'gelfit' confint(object, parm, level = 0.95, lambda = FALSE, type = c("Wald", "invLR", "invLM", "invJ"), fact = 3, corr = NULL, vcov=NULL, area = FALSE, npoints = 20, cores=4, ...) ## S4 method for signature 'numeric' confint(object, parm, level = 0.95, gelType="EL", type = c("Wald", "invLR", "invLM", "invJ"), fact = 3, vcov="iid") ## S4 method for signature 'data.frame' confint(object, parm, level = 0.95, gelType="EL", type = c("Wald", "invLR", "invLM", "invJ"), fact = 3, vcov="iid", npoints=10, cores=4) ## S4 method for signature 'matrix' confint(object, parm, level = 0.95, gelType="EL", type = c("Wald", "invLR", "invLM", "invJ"), fact = 3, vcov="iid", npoints=10, cores=4) ## S4 method for signature 'ANY' confint(object, parm, level = 0.95, ...)
## S4 method for signature 'gmmfit' confint(object, parm, level = 0.95, vcov=NULL, area=FALSE, npoints=50, ...) ## S4 method for signature 'gelfit' confint(object, parm, level = 0.95, lambda = FALSE, type = c("Wald", "invLR", "invLM", "invJ"), fact = 3, corr = NULL, vcov=NULL, area = FALSE, npoints = 20, cores=4, ...) ## S4 method for signature 'numeric' confint(object, parm, level = 0.95, gelType="EL", type = c("Wald", "invLR", "invLM", "invJ"), fact = 3, vcov="iid") ## S4 method for signature 'data.frame' confint(object, parm, level = 0.95, gelType="EL", type = c("Wald", "invLR", "invLM", "invJ"), fact = 3, vcov="iid", npoints=10, cores=4) ## S4 method for signature 'matrix' confint(object, parm, level = 0.95, gelType="EL", type = c("Wald", "invLR", "invLM", "invJ"), fact = 3, vcov="iid", npoints=10, cores=4) ## S4 method for signature 'ANY' confint(object, parm, level = 0.95, ...)
object |
Object of class |
parm |
Vector of integers or characters for selecting the elements for which the intervals should be computed. |
level |
The confidence level. |
lambda |
Should be compute intervals for the Lagrange multipliers? |
type |
The type of confidence intervals. The default is the Wald interval, and the others are computed by inverting the LR, LM or J specification test. |
fact |
For the inversion of the specification tests,
|
corr |
Correction to apply to the specification tests |
vcov |
For Wald intervals, an optional covariance matrix can be
provided. For |
cores |
The number of cores for |
gelType |
Type of GEL confidence interval. |
npoints |
Number of equally spaced points for the confidence region |
area |
If TRUE, a cnnfidence region is computed. The length of
|
... |
signature(object = "ANY")
The method from the stats in used in that case.
signature(object = "gelfit")
Method for any GEL fit class.
signature(object = "gmmfit")
Method for any GMM fit class.
signature(object = "numeric")
It computes the GEL confidence interval for the mean.
signature(object = "data.frame")
It computes the 2D GEL confidence region for the means of two variables.
signature(object = "matrix")
It converts the object into a data.frame and call its method.
Quarterly macroeconomic US data from 1950 to 2000.
data("ConsumptionG")
data("ConsumptionG")
A data frame with 204 observations on the following 14 variables.
YEAR
Year
QTR
Quarter
REALGDP
Read GDP
REALCONS
Real Consumption
REALINVS
Real Investment
REALGOVT
Real public expenditure
REALDPI
ector
CPI_U
CPI
M1
Money stock
TBILRATE
Interest rate
UNEMP
Unemployment rate
POP
Population
INFL
Inflation
REALINT
Real interest rate.
Greene (2012) online resources: (http://pages.stern.nyu.edu/~wgreene/Text/Edition7/tablelist8new.htm)
Green, W.H.. (2012). Econometric Analysis, 7th edition, Prentice Hall.
data(ConsumptionG) ## Get the data ready for Table 8.2 of Greene (2012) Y <- ConsumptionG$REALDPI C <- ConsumptionG$REALCONS n <- nrow(ConsumptionG) Y1 <- Y[-c(1,n)]; Y2 <- Y[-c(n-1,n)]; Y <- Y[-c(1:2)] C1 <- C[-c(1,n)]; C <- C[-(1:2)] dat <- data.frame(Y=Y,Y1=Y1,Y2=Y2,C=C,C1=C1) ## Starting at the NLS estimates (from the table) theta0=c(alpha=468, beta=0.0971, gamma=1.24) ## Greene (2012) seems to assume iid errors (probably wrong assumption here) model <- momentModel(C~alpha+beta*Y^gamma, ~C1+Y1+Y2, data=dat, theta0=theta0, vcov="iid") ### Scaling the parameters increase the speed of convergence res <- gmmFit(model, control=list(parscale=c(1000,.1,1))) ### It also seems that there is a degree of freedom adjustment for the ### estimate of the variance of the error term. summary(res, df.adj=TRUE)@coef
data(ConsumptionG) ## Get the data ready for Table 8.2 of Greene (2012) Y <- ConsumptionG$REALDPI C <- ConsumptionG$REALCONS n <- nrow(ConsumptionG) Y1 <- Y[-c(1,n)]; Y2 <- Y[-c(n-1,n)]; Y <- Y[-c(1:2)] C1 <- C[-c(1,n)]; C <- C[-(1:2)] dat <- data.frame(Y=Y,Y1=Y1,Y2=Y2,C=C,C1=C1) ## Starting at the NLS estimates (from the table) theta0=c(alpha=468, beta=0.0971, gamma=1.24) ## Greene (2012) seems to assume iid errors (probably wrong assumption here) model <- momentModel(C~alpha+beta*Y^gamma, ~C1+Y1+Y2, data=dat, theta0=theta0, vcov="iid") ### Scaling the parameters increase the speed of convergence res <- gmmFit(model, control=list(parscale=c(1000,.1,1))) ### It also seems that there is a degree of freedom adjustment for the ### estimate of the variance of the error term. summary(res, df.adj=TRUE)@coef
Dresiduals
in Package Gmm ~~It returns the matrix of derivatives of the residuals with respect to the coefficients.
signature(object = "linearModel")
signature(object = "nonlinearModel")
signature(object = "rsnonlinearModel")
signature(object = "sysMomentModel")
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) Dresiduals(model1, theta)[1:3,]
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) Dresiduals(model1, theta)[1:3,]
DWH
in Package momentfit ~~It performs the Durbin-Wu-Hausman test on GMM fit models.
## S4 method for signature 'gmmfit,missing' DWH(object1, object2) ## S4 method for signature 'gmmfit,lm' DWH(object1, object2, tol=sqrt(.Machine$double.eps), v1=NULL, v2=NULL, ...) ## S4 method for signature 'gmmfit,gmmfit' DWH(object1, object2, tol=sqrt(.Machine$double.eps), v1=NULL, v2=NULL, ...)
## S4 method for signature 'gmmfit,missing' DWH(object1, object2) ## S4 method for signature 'gmmfit,lm' DWH(object1, object2, tol=sqrt(.Machine$double.eps), v1=NULL, v2=NULL, ...) ## S4 method for signature 'gmmfit,gmmfit' DWH(object1, object2, tol=sqrt(.Machine$double.eps), v1=NULL, v2=NULL, ...)
object1 |
Object of class |
object2 |
Object of class |
v1 |
Alternatively, we can provide a different covariance matrix for object1 |
v2 |
Alternatively, we can provide a different covariance matrix for object2 |
tol |
Tolerance for the Moore-Penrose generalized inverse |
... |
Argument to pass to |
signature(object1 = "gmmfit", object2 = "lm")
signature(object1 = "gmmfit", object2 = "gmmfit")
signature(object1 = "gmmfit", object2 = "missing")
Green, W.H.. (2012). Econometric Analysis, 7th edition, Prentice Hall.
### Exampe 8.7 of Greene (2012) data(ConsumptionG) Y <- ConsumptionG$REALDPI C <- ConsumptionG$REALCONS n <- nrow(ConsumptionG) Y1 <- Y[-n]; Y <- Y[-1] C1 <- C[-n]; C <- C[-1] dat <- data.frame(Y=Y,Y1=Y1,C=C,C1=C1) model1 <- momentModel(C~Y, ~Y, data=dat, vcov="iid") model2 <- momentModel(C~Y, ~Y1+C1, data=dat, vcov="iid") res1 <- tsls(model1) res2 <- tsls(model2) res <- lm(C~Y) ## Exampke 8.7-2. The difference is explained by the rounding ## error in Greene. Only the first the 3 digits of the t-test are used. DWH(res2) ## Example 8.7-1. Not quite the same. DWH(res2, res1) ## using lm object to compare OLS and 2SLS: ## The same adjustment on the vcov must be done (it is by default in lm) ## otherwise the different in the covariance matrices is mostly caused by the ## different ways to compute them. DWH(res2, res, df.adj=TRUE) ## To reproduce the same results as Exampke 8.7-1, ## we need to specify the variance. ## But it is not necessary as the above way is ## asymptotically equivalent X <- model.matrix(model1) Xhat <- qr.fitted(res2@wObj@w, X) s2 <- sum(residuals(res)^2)/(res$df.residual) v1 <- solve(crossprod(Xhat))*s2 v2 <- solve(crossprod(X))*s2 DWH(res2, res, v1=v1, v2=v2)
### Exampe 8.7 of Greene (2012) data(ConsumptionG) Y <- ConsumptionG$REALDPI C <- ConsumptionG$REALCONS n <- nrow(ConsumptionG) Y1 <- Y[-n]; Y <- Y[-1] C1 <- C[-n]; C <- C[-1] dat <- data.frame(Y=Y,Y1=Y1,C=C,C1=C1) model1 <- momentModel(C~Y, ~Y, data=dat, vcov="iid") model2 <- momentModel(C~Y, ~Y1+C1, data=dat, vcov="iid") res1 <- tsls(model1) res2 <- tsls(model2) res <- lm(C~Y) ## Exampke 8.7-2. The difference is explained by the rounding ## error in Greene. Only the first the 3 digits of the t-test are used. DWH(res2) ## Example 8.7-1. Not quite the same. DWH(res2, res1) ## using lm object to compare OLS and 2SLS: ## The same adjustment on the vcov must be done (it is by default in lm) ## otherwise the different in the covariance matrices is mostly caused by the ## different ways to compute them. DWH(res2, res, df.adj=TRUE) ## To reproduce the same results as Exampke 8.7-1, ## we need to specify the variance. ## But it is not necessary as the above way is ## asymptotically equivalent X <- model.matrix(model1) Xhat <- qr.fitted(res2@wObj@w, X) s2 <- sum(residuals(res)^2)/(res$df.residual) v1 <- solve(crossprod(Xhat))*s2 v2 <- solve(crossprod(X))*s2 DWH(res2, res, v1=v1, v2=v2)
estfun
in Package sandwich ~~Estimating equations for moment models.
signature(x = "momentModel")
evalDMoment
in Package momentfit ~~It computes the matrix of derivatives of the sample moments with respect to the coefficients.
## S4 method for signature 'functionModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'rfunctionModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'rnonlinearModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'formulaModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'rformulaModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'regModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'sysModel' evalDMoment(object, theta) ## S4 method for signature 'rslinearModel' evalDMoment(object, theta) ## S4 method for signature 'rsnonlinearModel' evalDMoment(object, theta, impProb=NULL)
## S4 method for signature 'functionModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'rfunctionModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'rnonlinearModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'formulaModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'rformulaModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'regModel' evalDMoment(object, theta, impProb=NULL, lambda=NULL) ## S4 method for signature 'sysModel' evalDMoment(object, theta) ## S4 method for signature 'rslinearModel' evalDMoment(object, theta) ## S4 method for signature 'rsnonlinearModel' evalDMoment(object, theta, impProb=NULL)
object |
An model object |
theta |
A numerical vector of coefficients |
impProb |
If a vector of implied probablities is provided, the sample means are computed using them. If not provided, the means are computed using the uniform weight |
lambda |
A vector of Lagrange multipliers associated with the moment conditions. Its length must therefore match the number of conditions. See details below. |
Without the argument lambda
, the method returns a matrix, where
is the number of coefficients, and
is
the number of moment conditions. That matrix is the derivative of the
sample mean of the moments with respect to the coefficient.
If lambda
is provided, the method returns an
matrix, where
is the sample size. The ith row is
, where $G_i$ is the derivative of the moment
function evaluated at the ith observation. For now, this option is
used to compute robust-to-misspecified standard errors of GEL
estimators.
signature(object = "functionModel")
signature(object = "rfunctionModel")
The theta vector must match the number of coefficients in the restricted model.
signature(object = "formulaModel")
signature(object = "rformulaModel")
The theta vector must match the number of coefficients in the restricted model.
signature(object = "regModel")
signature(object = "sysModel")
signature(object = "rslinearModel")
data(simData) theta <- c(1,1) model1 <- momentModel(y~x1, ~z1+z2, data=simData) G <- evalDMoment(model1, theta) ## A nonlinearModel g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) G <- evalDMoment(model2, c(beta0=1, beta1=2)) ## A functionModel fct <- function(tet, x) { m1 <- (tet[1] - x) m2 <- (tet[2]^2 - (x - tet[1])^2) m3 <- x^3 - tet[1]*(tet[1]^2 + 3*tet[2]^2) f <- cbind(m1, m2, m3) return(f) } dfct <- function(tet, x) { jacobian <- matrix(c( 1, 2*(-tet[1]+mean(x)), -3*tet[1]^2-3*tet[2]^2,0, 2*tet[2], -6*tet[1]*tet[2]), nrow=3,ncol=2) return(jacobian) } X <- rnorm(200) model3 <- momentModel(fct, X, theta0=c(beta0=1, beta1=2), grad=dfct) G <- evalDMoment(model3, c(beta0=1, beta1=2))
data(simData) theta <- c(1,1) model1 <- momentModel(y~x1, ~z1+z2, data=simData) G <- evalDMoment(model1, theta) ## A nonlinearModel g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) G <- evalDMoment(model2, c(beta0=1, beta1=2)) ## A functionModel fct <- function(tet, x) { m1 <- (tet[1] - x) m2 <- (tet[2]^2 - (x - tet[1])^2) m3 <- x^3 - tet[1]*(tet[1]^2 + 3*tet[2]^2) f <- cbind(m1, m2, m3) return(f) } dfct <- function(tet, x) { jacobian <- matrix(c( 1, 2*(-tet[1]+mean(x)), -3*tet[1]^2-3*tet[2]^2,0, 2*tet[2], -6*tet[1]*tet[2]), nrow=3,ncol=2) return(jacobian) } X <- rnorm(200) model3 <- momentModel(fct, X, theta0=c(beta0=1, beta1=2), grad=dfct) G <- evalDMoment(model3, c(beta0=1, beta1=2))
evalGel
in Package modelfit ~~Method to simply evaluate a GEL model at a fixed coefficient vector. It
creates a "gelfit"
object using that fixed vector.
## S4 method for signature 'momentModel' evalGel(model, theta, lambda=NULL, gelType="EL", rhoFct=NULL, lamSlv=NULL, lControl=list(), ...)
## S4 method for signature 'momentModel' evalGel(model, theta, lambda=NULL, gelType="EL", rhoFct=NULL, lamSlv=NULL, lControl=list(), ...)
model |
An object of class |
theta |
A vector of coefficients at which the model is estimated |
lambda |
The Lagrange multiplier vector. If not provided, the optimal vector is obtained for the given theta |
gelType |
The type of GEL. It is either |
rhoFct |
An alternative objective function for GEL. This argument
is only used if we want to fit the model with a different GEL
method. see |
lamSlv |
An alternative solver for the Lagrange multiplier. By
default, either |
lControl |
A list of controls for the Lagrange multiplier algorithm. |
... |
Other arguments to pass. Not used for the moment. |
signature(model = "momentModel")
data(simData) theta <- c(beta0=1,beta1=2) ## A linear model with optimal lambda model1 <- momentModel(y~x1, ~z1+z2, data=simData) evalGel(model1, c(1,1)) ## A nonlinear model with fixed lambda g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) evalGel(model2, theta=c(beta1=2, beta0=0.5), lambda=c(.1,.2,.3), gelType="ET")
data(simData) theta <- c(beta0=1,beta1=2) ## A linear model with optimal lambda model1 <- momentModel(y~x1, ~z1+z2, data=simData) evalGel(model1, c(1,1)) ## A nonlinear model with fixed lambda g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) evalGel(model2, theta=c(beta1=2, beta0=0.5), lambda=c(.1,.2,.3), gelType="ET")
evalGelObj
in Package Gmm ~~~~ Methods to compute the GEL objective function. ~~
## S4 method for signature 'momentModel,numeric,numeric' evalGelObj(object, theta, lambda, gelType, rhoFct=NULL, ...)
## S4 method for signature 'momentModel,numeric,numeric' evalGelObj(object, theta, lambda, gelType, rhoFct=NULL, ...)
object |
An object of class |
theta |
The vector for coefficients. |
lambda |
Vector of Lagrange multiplier. |
gelType |
The type of GEL. It is either |
rhoFct |
An alternative objective function for GEL. This argument
is only used if we want to fit the model with a different GEL
method. see |
... |
Arguments to pass to other methods |
signature(object = "momentModel", theta = "numeric",
lambda = "numeric")
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) evalGelObj(model1, theta, c(.2,.3,.4), gelType="EL")
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) evalGelObj(model1, theta, c(.2,.3,.4), gelType="EL")
evalGmm
in Package modelfit ~~Method to simply evaluate a GMM model at a fixed coefficient vector. It
creates a "gmmfit"
object using that fixed vector.
## S4 method for signature 'momentModel' evalGmm(model, theta, wObj=NULL, ...) ## S4 method for signature 'sysModel' evalGmm(model, theta, wObj=NULL, ...)
## S4 method for signature 'momentModel' evalGmm(model, theta, wObj=NULL, ...) ## S4 method for signature 'sysModel' evalGmm(model, theta, wObj=NULL, ...)
model |
An object of class |
theta |
A vector of coefficients at which the model is estimated |
wObj |
An object of class |
... |
Other arguments to pass. Not used for the moment. |
signature(model = "momentModel")
signature(model = "sysModel")
data(simData) theta <- c(beta0=1,beta1=2) ## A linear model model1 <- momentModel(y~x1, ~z1+z2, data=simData) evalGmm(model1, c(1,1)) ## A nonlinear model g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) evalGmm(model2, theta=c(beta1=2, beta0=0.5)) ## A function model fct <- function(tet, x) { m1 <- (tet[1] - x) m2 <- (tet[2]^2 - (x - tet[1])^2) m3 <- x^3 - tet[1]*(tet[1]^2 + 3*tet[2]^2) f <- cbind(m1, m2, m3) return(f) } dfct <- function(tet, x) { jacobian <- matrix(c( 1, 2*(-tet[1]+mean(x)), -3*tet[1]^2-3*tet[2]^2,0, 2*tet[2], -6*tet[1]*tet[2]), nrow=3,ncol=2) return(jacobian) } model3 <- momentModel(fct, simData$x3, theta0=c(beta0=1, beta1=2), grad=dfct) evalGmm(model3, theta=c(beta1=.1, beta0=0.3))
data(simData) theta <- c(beta0=1,beta1=2) ## A linear model model1 <- momentModel(y~x1, ~z1+z2, data=simData) evalGmm(model1, c(1,1)) ## A nonlinear model g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) evalGmm(model2, theta=c(beta1=2, beta0=0.5)) ## A function model fct <- function(tet, x) { m1 <- (tet[1] - x) m2 <- (tet[2]^2 - (x - tet[1])^2) m3 <- x^3 - tet[1]*(tet[1]^2 + 3*tet[2]^2) f <- cbind(m1, m2, m3) return(f) } dfct <- function(tet, x) { jacobian <- matrix(c( 1, 2*(-tet[1]+mean(x)), -3*tet[1]^2-3*tet[2]^2,0, 2*tet[2], -6*tet[1]*tet[2]), nrow=3,ncol=2) return(jacobian) } model3 <- momentModel(fct, simData$x3, theta0=c(beta0=1, beta1=2), grad=dfct) evalGmm(model3, theta=c(beta1=.1, beta0=0.3))
evalGmmObj
in Package momentfit ~~~~ Methods to compute the GMM objective function. ~~
## S4 method for signature 'momentModel,numeric,momentWeights' evalGmmObj(object, theta, wObj, ...) ## S4 method for signature 'sysModel,list,sysMomentWeights' evalGmmObj(object, theta, wObj, ...)
## S4 method for signature 'momentModel,numeric,momentWeights' evalGmmObj(object, theta, wObj, ...) ## S4 method for signature 'sysModel,list,sysMomentWeights' evalGmmObj(object, theta, wObj, ...)
object |
An object of class |
theta |
The vector for coefficients for single equation, or a list of vector for system of equations. |
wObj |
An object of class |
... |
Arguments to pass to other methods |
signature(object = "momentModel", theta = "numeric",
wObj = "momentWeights")
signature(object = "sysModel", theta = "list",
wObj = "sysMomentWeights")
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) w <- evalWeights(model1, theta) evalGmmObj(model1, theta, w)
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) w <- evalWeights(model1, theta) evalGmmObj(model1, theta, w)
evalMoment
in Package momentfit ~~Method to evaluate the moment matrix at a given coefficient vector.
signature(object = "functionModel")
signature(object = "formulaModel")
signature(object = "regModel")
signature(object = "sysModel")
signature(object = "rsysModel")
data(simData) theta <- c(1,1) model1 <- momentModel(y~x1, ~z1+z2, data=simData) gt <- evalMoment(model1, theta) ## A nonlinearGmm g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) gt <- evalMoment(model2, c(beta0=1, beta1=2)) ## A functionGmm fct <- function(tet, x) { m1 <- (tet[1] - x) m2 <- (tet[2]^2 - (x - tet[1])^2) m3 <- x^3 - tet[1]*(tet[1]^2 + 3*tet[2]^2) f <- cbind(m1, m2, m3) return(f) } dfct <- function(tet, x) { jacobian <- matrix(c( 1, 2*(-tet[1]+mean(x)), -3*tet[1]^2-3*tet[2]^2,0, 2*tet[2], -6*tet[1]*tet[2]), nrow=3,ncol=2) return(jacobian) } X <- rnorm(200) model3 <- momentModel(fct, X, theta0=c(beta0=1, beta1=2), grad=dfct) gt <- evalMoment(model3, c(beta0=1, beta1=2))
data(simData) theta <- c(1,1) model1 <- momentModel(y~x1, ~z1+z2, data=simData) gt <- evalMoment(model1, theta) ## A nonlinearGmm g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) gt <- evalMoment(model2, c(beta0=1, beta1=2)) ## A functionGmm fct <- function(tet, x) { m1 <- (tet[1] - x) m2 <- (tet[2]^2 - (x - tet[1])^2) m3 <- x^3 - tet[1]*(tet[1]^2 + 3*tet[2]^2) f <- cbind(m1, m2, m3) return(f) } dfct <- function(tet, x) { jacobian <- matrix(c( 1, 2*(-tet[1]+mean(x)), -3*tet[1]^2-3*tet[2]^2,0, 2*tet[2], -6*tet[1]*tet[2]), nrow=3,ncol=2) return(jacobian) } X <- rnorm(200) model3 <- momentModel(fct, X, theta0=c(beta0=1, beta1=2), grad=dfct) gt <- evalMoment(model3, c(beta0=1, beta1=2))
evalWeights
in Package Gmm
This is a constructor for objects of class momentWeights
## S4 method for signature 'momentModel' evalWeights(object, theta=NULL, w="optimal", ...) ## S4 method for signature 'sysModel' evalWeights(object, theta = NULL, w="optimal", wObj=NULL) ## S4 method for signature 'rslinearModel' evalWeights(object, theta = NULL, w="optimal", wObj=NULL)
## S4 method for signature 'momentModel' evalWeights(object, theta=NULL, w="optimal", ...) ## S4 method for signature 'sysModel' evalWeights(object, theta = NULL, w="optimal", wObj=NULL) ## S4 method for signature 'rslinearModel' evalWeights(object, theta = NULL, w="optimal", wObj=NULL)
object |
Object of class |
theta |
The vector of coefficients to compute the optimal
weights. If |
w |
A matrix for fixed weights, one of |
wObj |
An object of class |
... |
Arguments to pass to other methods |
signature(object = "momentModel")
signature(object = "sysModel")
signature(object = "rslinearModel")
System of equations with restrictions on the coefficients. It only affects the computation of the weights when there are cross-equation restrictions.
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## Identity weights object wObj1 <- evalWeights(model1, w="ident") ## Identity weights object (an alternative way less efficient) wObj1 <- evalWeights(model1, w=diag(3)) ## Optimal weights wObj1 <- evalWeights(model1, theta, w="optimal")
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## Identity weights object wObj1 <- evalWeights(model1, w="ident") ## Identity weights object (an alternative way less efficient) wObj1 <- evalWeights(model1, w=diag(3)) ## Optimal weights wObj1 <- evalWeights(model1, theta, w="optimal")
"formulaModel"
Class for moment-based models for which moments are expressed using formulas.
Objects can be created by calls of the form new("formulaModel", ...)
.
It is generated my momentModel
.
modelF
:Object of class "data.frame"
~~
vcov
:Object of class "character"
~~
theta0
:Object of class "numeric"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "character"
~~
momNames
:Object of class "character"
~~
fRHS
:Object of class "list"
~~
fLHS
:Object of class "list"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
varNames
:Object of class "character"
~~
isEndo
:Object of class "logical"
~~
isMDE
:Object of class "logical"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "allNLModel"
, directly.
Class "momentModel"
, directly.
signature(x = "formulaModel", i = "numeric", j = "missing")
: ...
signature(object = "formulaModel")
: ...
signature(object = "formulaModel")
: ...
signature(model = "formulaModel")
: ...
signature(object = "formulaModel")
: ...
signature(object = "formulaModel")
: ...
signature(object = "formulaModel")
: ...
signature(x = "formulaModel")
: ...
showClass("formulaModel")
showClass("formulaModel")
"functionModel"
Class for moment-based models for which moment conditions are defined using a function.
Objects can be created by calls of the form new("functionModel", ...)
.
It is generated my momentModel
.
X
:Object of class "ANY"
~~
fct
:Object of class "function"
~~
dfct
:Object of class "functionORNULL"
~~
vcov
:Object of class "character"
~~
theta0
:Object of class "numeric"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "character"
~~
momNames
:Object of class "character"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
varNames
:Object of class "character"
~~
isEndo
:Object of class "logical"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "allNLModel"
, directly.
Class "momentModel"
, directly.
signature(x = "functionModel", i = "numeric", j = "missing")
: ...
signature(object = "functionModel")
: ...
signature(object = "functionModel")
: ...
signature(object = "functionModel")
: ...
signature(object = "functionModel")
: ...
signature(object = "functionModel")
: ...
signature(x = "functionModel")
: ...
showClass("functionModel")
showClass("functionModel")
The main functions and methods to fit any model with GEL. As opposed to
gelFit
, models don't need to be created. It is all done by
the functions. It is meant to be more user friendly.
gel4(g, x=NULL, theta0=NULL,lambda0=NULL, getVcov=FALSE, gelType = c("EL","ET","EEL","HD", "REEL","ETEL","ETHD"), vcov = c("MDS","iid","HAC"), grad=NULL, vcovOptions=list(), centeredVcov = TRUE, cstLHS=NULL, cstRHS=NULL, lamSlv=NULL, rhoFct=NULL, initTheta=c("gmm", "theta0"), data = parent.frame(), coefSlv=c("optim","nlminb","constrOptim"), smooth=FALSE, lControl=list(), tControl=list())
gel4(g, x=NULL, theta0=NULL,lambda0=NULL, getVcov=FALSE, gelType = c("EL","ET","EEL","HD", "REEL","ETEL","ETHD"), vcov = c("MDS","iid","HAC"), grad=NULL, vcovOptions=list(), centeredVcov = TRUE, cstLHS=NULL, cstRHS=NULL, lamSlv=NULL, rhoFct=NULL, initTheta=c("gmm", "theta0"), data = parent.frame(), coefSlv=c("optim","nlminb","constrOptim"), smooth=FALSE, lControl=list(), tControl=list())
g |
A function of the form |
x |
The matrix or vector of data from which the function
|
theta0 |
A |
lambda0 |
The |
getVcov |
Should the method computes the covariance matrices of the coefficients and Lagrange multipliers. |
gelType |
A character string specifying the type of GEL. |
vcov |
Assumption on the properties of the moment conditions. |
grad |
A function of the form |
vcovOptions |
A list of options for the covariance matrix of the
moment conditions. See |
centeredVcov |
Should the moment function be centered when computing its covariance matrix. Doing so may improve inference. |
cstLHS |
The left hand side of the constraints to impose on the
coefficients. See |
cstRHS |
The right hand side of the constraints to impose on the
coefficients. See |
lamSlv |
An alternative solver for the Lagrange multiplier. By
default, either |
rhoFct |
An optional function that return |
initTheta |
Method to obtain the starting values for the
coefficient vector. By default the GMM estimate with identity matrix
is used. The second argument means that |
data |
A required data.frame, in which all variables in g and x can be found. |
smooth |
If TRUE, |
coefSlv |
Minimization solver for the coefficient vector. |
lControl |
A list of controls for the Lagrange multiplier algorithm. |
tControl |
A list of controls for the coefficient algorithm. |
It returns an object of class "gelfit"
Anatolyev, S. (2005), GMM, GEL, Serial Correlation, and Asymptotic Bias. Econometrica, 73, 983-1002.
Andrews DWK (1991), Heteroskedasticity and Autocorrelation Consistent Covariance Matrix Estimation. Econometrica, 59, 817–858.
Kitamura, Yuichi (1997), Empirical Likelihood Methods With Weakly Dependent Processes. The Annals of Statistics, 25, 2084-2102.
Kitamura, Y. and Otsu, T. and Evdokimov, K. (2013), Robustness, Infinitesimal Neighborhoods and Moment Restrictions. Econometrica, 81, 1185-1201.
Newey, W.K. and Smith, R.J. (2004), Higher Order Properties of GMM and Generalized Empirical Likelihood Estimators. Econometrica, 72, 219-255.
Smith, R.J. (2004), GEL Criteria for Moment Condition Models. Working paper, CEMMAP.
data(simData) res <- gel4(y~x1, ~z1+z2, vcov="MDS", gelType="ET", data=simData) res
data(simData) res <- gel4(y~x1, ~z1+z2, vcov="MDS", gelType="ET", data=simData) res
"gelfit"
A class to store fitted models obtained using a GEL method.
Objects can be created by calls of the form new("gelfit", ...)
.
It is created by gelFit
.
theta
:Object of class "numeric"
~~
convergence
:Object of class "numeric"
~~
lambda
:Object of class "numeric"
~~
lconvergence
:Object of class "numeric"
~~
call
:Object of class "callORNULL"
~~
gelType
:Object of class "list"
~~
vcov
:Object of class "list"
~~
model
:Object of class "momentModel"
~~
restrictedLam
:Object of class "integer"
~~
signature(object = "gelfit")
: ...
signature(object = "gelfit")
: ...
signature(object = "gelfit")
: ...
signature(eta = "numeric", object = "gelfit")
: ...
signature(x = "gelfit")
: ...
signature(object = "gelfit")
: ...
signature(object = "gelfit")
: ...
signature(object = "gelfit", which = "missing")
: ...
signature(object = "gelfit")
: ...
signature(object = "gelfit")
: ...
signature(object = "gelfit")
: ...
showClass("gelfit")
showClass("gelfit")
gelFit
in Package momentfit ~~Method to fit a model using GEL, from an object of class
"momentModel"
or its restricted counterpart.
## S4 method for signature 'momentModel' gelFit(model, gelType="EL", rhoFct=NULL, initTheta=c("gmm", "modelTheta0"), theta0=NULL, lambda0=NULL, vcov=FALSE, ...) ## S4 method for signature 'rmomentModel' gelFit(model, gelType="EL", rhoFct=NULL, initTheta=c("gmm", "modelTheta0"), theta0=NULL, lambda0=NULL, vcov=FALSE, ...)
## S4 method for signature 'momentModel' gelFit(model, gelType="EL", rhoFct=NULL, initTheta=c("gmm", "modelTheta0"), theta0=NULL, lambda0=NULL, vcov=FALSE, ...) ## S4 method for signature 'rmomentModel' gelFit(model, gelType="EL", rhoFct=NULL, initTheta=c("gmm", "modelTheta0"), theta0=NULL, lambda0=NULL, vcov=FALSE, ...)
model |
A model class object |
gelType |
The type of GEL. It is either |
rhoFct |
An alternative objective function for GEL. This argument
is only used if we want to fit the model with a different GEL
method. see |
initTheta |
Method to obtain the starting values for the coefficient vector. By default the GMM estimate with identity matrix is used. The second argument means that the theta0 of the object, if any, should be used. |
theta0 |
An optional initial vector for |
lambda0 |
Manual starting values for the Lagrange multiplier. By default, it is a vector of zeros. |
vcov |
Should the method computes the covariance matrices of the coefficients and Lagrange multipliers. |
... |
Arguments to pass to other methods (mostly the optimization algorithm) |
signature(model = "momentModel")
The main method for all moment-based models.
signature(model = "rmomentModel")
The main method for all restricted moment-based models.
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## EL estimate res1 <- gelFit(model1) res1 ## ET estimate res2 <- gelFit(model1, gelType="ET") res2 ## Restricted models by EL ## using the Brent method R <- matrix(c(0,1), ncol=2) q <- 2 rmodel1 <- restModel(model1, R, q) gelFit(rmodel1, tControl=list(method="Brent", lower=-10, upper=10))
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## EL estimate res1 <- gelFit(model1) res1 ## ET estimate res2 <- gelFit(model1, gelType="ET") res2 ## Restricted models by EL ## using the Brent method R <- matrix(c(0,1), ncol=2) q <- 2 rmodel1 <- restModel(model1, R, q) gelFit(rmodel1, tControl=list(method="Brent", lower=-10, upper=10))
getImpProb
in Package momenfit ~~Method to evaluate the implied probabilities of GEL.
signature(object = "gelfit")
getRestrict
in Package momentfit ~~It computes the matrices related to linear and nonlinear contraints. Those matrices are used to perform hypothesis tests.
## S4 method for signature 'rlinearModel' getRestrict(object, theta) ## S4 method for signature 'rslinearModel' getRestrict(object, theta) ## S4 method for signature 'rsnonlinearModel' getRestrict(object, theta) ## S4 method for signature 'rnonlinearModel' getRestrict(object, theta) ## S4 method for signature 'rformulaModel' getRestrict(object, theta) ## S4 method for signature 'momentModel' getRestrict(object, theta, R, rhs=NULL) ## S4 method for signature 'sysModel' getRestrict(object, theta, R, rhs=NULL) ## S4 method for signature 'rfunctionModel' getRestrict(object, theta)
## S4 method for signature 'rlinearModel' getRestrict(object, theta) ## S4 method for signature 'rslinearModel' getRestrict(object, theta) ## S4 method for signature 'rsnonlinearModel' getRestrict(object, theta) ## S4 method for signature 'rnonlinearModel' getRestrict(object, theta) ## S4 method for signature 'rformulaModel' getRestrict(object, theta) ## S4 method for signature 'momentModel' getRestrict(object, theta, R, rhs=NULL) ## S4 method for signature 'sysModel' getRestrict(object, theta, R, rhs=NULL) ## S4 method for signature 'rfunctionModel' getRestrict(object, theta)
object |
Object of class included in |
theta |
A vector of coefficients for the unrestricted model (see examples). |
R |
A matrix, character or list of formulas that specifies the
contraints to impose on the coefficients. See
|
rhs |
The right hand side for the restriction on the
coefficients. See |
signature(object = "momentModel")
A restricted model is created from the constraints, and the restriction matrices are returned. The methods is applied to linear and nonlinear models in a regression form.
signature(object = "sysModel")
A restricted model is created from the constraints, and the restriction matrices are returned. The methods is applied to systems of linear and nonlinear models.
signature(object = "rlinearModel")
The restriction matrices are evaluated at the coefficient vector
theta
of the unrestricted representation.
signature(object = "rslinearModel")
The restriction matrices are evaluated at the coefficient vector
theta
of the unrestricted representation.
signature(object = "rsnonlinearModel")
The restriction matrices are evaluated at the coefficient vector
theta
of the unrestricted representation.
signature(object = "rnonlinearModel")
The restriction matrices are evaluated at the coefficient vector
theta
of the unrestricted representation.
signature(object = "rfunctionModel")
The restriction matrices are evaluated at the coefficient vector
theta
of the unrestricted representation.
data(simData) theta <- c(beta0=1,beta1=2) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3+z1, ~x1+x2+z1+z2+z3+z4, data=simData) ## The restricted model R1 <- c("x1","2*x2+z1=2", "4+x3*5=3") res <- gmmFit(model1) rest <- getRestrict(model1, coef(res), R1) ## it allows to test the restriction g <- rest$R-rest$q v <- rest$dR%*%vcov(res)%*%t(rest$dR) (test <- crossprod(g, solve(v, g))) (pv <- 1-pchisq(test, length(rest$R))) ## Delta Method: ## To impose nonlinear restrictions, we need to convert ## the linear model into a nonlinear one NLmodel <- as(model1, "nonlinearModel") R1 <- c("theta2=2", "theta3=theta4^2") res <- gmmFit(NLmodel) rest <- getRestrict(NLmodel, coef(res), R1) g <- rest$R-rest$q v <- rest$dR%*%vcov(res)%*%t(rest$dR) (test <- crossprod(g, solve(v, g))) (pv <- 1-pchisq(test, length(rest$R))) ## See hypothesisTest method for an easier approach.
data(simData) theta <- c(beta0=1,beta1=2) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3+z1, ~x1+x2+z1+z2+z3+z4, data=simData) ## The restricted model R1 <- c("x1","2*x2+z1=2", "4+x3*5=3") res <- gmmFit(model1) rest <- getRestrict(model1, coef(res), R1) ## it allows to test the restriction g <- rest$R-rest$q v <- rest$dR%*%vcov(res)%*%t(rest$dR) (test <- crossprod(g, solve(v, g))) (pv <- 1-pchisq(test, length(rest$R))) ## Delta Method: ## To impose nonlinear restrictions, we need to convert ## the linear model into a nonlinear one NLmodel <- as(model1, "nonlinearModel") R1 <- c("theta2=2", "theta3=theta4^2") res <- gmmFit(NLmodel) rest <- getRestrict(NLmodel, coef(res), R1) g <- rest$R-rest$q v <- rest$dR%*%vcov(res)%*%t(rest$dR) (test <- crossprod(g, solve(v, g))) (pv <- 1-pchisq(test, length(rest$R))) ## See hypothesisTest method for an easier approach.
The main functions and methods to fit any model with GMM. As opposed to
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
gmm4(g, x, theta0 = NULL, grad = NULL, type = c("twostep", "iter", "cue", "onestep"), vcov = c("iid", "HAC", "MDS", "TrueFixed", "CL"), initW = c("ident", "tsls", "EbyE"), weights = "optimal", itermaxit = 50, cstLHS=NULL, cstRHS=NULL, vcovOptions=list(), survOptions=list(), itertol = 1e-07, centeredVcov = TRUE, data = parent.frame(), ...) ## S4 method for signature 'formula' tsls(model, x, vcov = c("iid", "HAC", "MDS", "CL"), vcovOptions=list(), survOptions=list(), centeredVcov = TRUE, data = parent.frame()) ## S4 method for signature 'list' tsls(model, x=NULL, vcov = c("iid", "HAC", "MDS", "CL"), vcovOptions=list(), survOptions=list(), centeredVcov = TRUE, data = parent.frame()) ## S4 method for signature 'list' ThreeSLS(model, x=NULL, vcov = c("iid", "HAC", "MDS", "CL"), vcovOptions=list(), survOptions=list(), centeredVcov = TRUE, data = parent.frame())
gmm4(g, x, theta0 = NULL, grad = NULL, type = c("twostep", "iter", "cue", "onestep"), vcov = c("iid", "HAC", "MDS", "TrueFixed", "CL"), initW = c("ident", "tsls", "EbyE"), weights = "optimal", itermaxit = 50, cstLHS=NULL, cstRHS=NULL, vcovOptions=list(), survOptions=list(), itertol = 1e-07, centeredVcov = TRUE, data = parent.frame(), ...) ## S4 method for signature 'formula' tsls(model, x, vcov = c("iid", "HAC", "MDS", "CL"), vcovOptions=list(), survOptions=list(), centeredVcov = TRUE, data = parent.frame()) ## S4 method for signature 'list' tsls(model, x=NULL, vcov = c("iid", "HAC", "MDS", "CL"), vcovOptions=list(), survOptions=list(), centeredVcov = TRUE, data = parent.frame()) ## S4 method for signature 'list' ThreeSLS(model, x=NULL, vcov = c("iid", "HAC", "MDS", "CL"), vcovOptions=list(), survOptions=list(), centeredVcov = TRUE, data = parent.frame())
model |
A formula or a list of formulas. |
g |
A function of the form |
x |
The matrix or vector of data from which the function
|
theta0 |
A |
grad |
A function of the form |
type |
What GMM methods should we use? for
|
vcov |
Assumption on the properties of the random vector x. By
default, x is a weakly dependant process. The "iid" option will avoid
using the HAC matrix which will accelerate the estimation if one is
ready to make that assumption. The option "TrueFixed" is used only
when the matrix of weights is provided and it is the optimal one. For
type |
initW |
How should be compute the initial coefficient vector in the first. It only makes a difference for linear models for which the choice is GMM with identity matrix or two-stage least quares. |
weights |
What weighting matrix to use? The
choices are |
itermaxit |
Maximum iterations for iterative GMM |
itertol |
Tolance for the stopping rule in iterative GMM |
centeredVcov |
Should the moment function be centered when computing its covariance matrix. Doing so may improve inference. |
data |
A data.frame or a matrix with column names (Optional). |
cstLHS |
The left hand side of the constraints to impose on the
coefficients. See |
cstRHS |
The right hand side of the constraints to impose on the
coefficients. See |
vcovOptions |
A list of options for the covariance matrix of the
moment conditions. See |
survOptions |
If needed, a list with the type of survey weights and
the weights as a numeric vector, data.frame or formula. The type is either
|
... |
Arguments to pass to |
It returns an object of class "gmmfit"
Zeileis A (2006), Object-oriented Computation of Sandwich Estimators. Journal of Statistical Software, 16(9), 1–16. URL doi:10.18637/jss.v016.i09.
Andrews DWK (1991), Heteroskedasticity and Autocorrelation Consistent Covariance Matrix Estimation. Econometrica, 59, 817–858.
Newey WK & West KD (1987), A Simple, Positive Semi-Definite, Heteroskedasticity and Autocorrelation Consistent Covariance Matrix. Econometrica, 55, 703–708.
Newey WK & West KD (1994), Automatic Lag Selection in Covariance Matrix Estimation. Review of Economic Studies, 61, 631-653.
Hansen, L.P. (1982), Large Sample Properties of Generalized Method of Moments Estimators. Econometrica, 50, 1029-1054,
Hansen, L.P. and Heaton, J. and Yaron, A.(1996), Finite-Sample Properties of Some Alternative GMM Estimators. Journal of Business and Economic Statistics, 14 262-280.
data(simData) res <- gmm4(y~x1, ~z1+z2, vcov="MDS", type="iter", data=simData) res
data(simData) res <- gmm4(y~x1, ~z1+z2, vcov="MDS", type="iter", data=simData) res
"gmmfit"
A class to store a fitted model obtained using GMM.
Objects can be created by calls of the form new("gmmfit", ...)
.
Generated by gmmFit
.
theta
:Object of class "numeric"
~~
convergence
:Object of class "numericORNULL"
~~
convIter
:Object of class "numericORNULL"
~~
call
:Object of class "callORNULL"
~~
type
:Object of class "character"
~~
wObj
:Object of class "momentWeights"
~~
niter
:Object of class "integer"
~~
efficientGmm
:Object of class "logical"
~~
model
:Object of class "momentModel"
~~
signature(x = "gmmfit")
: ...
signature(object = "gmmfit")
: ...
signature(object = "gmmfit")
: ...
signature(object1 = "gmmfit", object2 = "gmmfit")
: ...
signature(object1 = "gmmfit", object2 = "lm")
: ...
signature(object1 = "gmmfit", object2 = "missing")
: ...
signature(object.u = "gmmfit", object.r = "gmmfit")
: ...
signature(object.u = "gmmfit", object.r = "missing")
: ...
signature(object.u = "missing", object.r = "gmmfit")
: ...
signature(object = "gmmfit")
: ...
signature(x = "gmmfit")
: ...
signature(object = "gmmfit")
: ...
signature(object = "gmmfit")
: ...
signature(object = "gmmfit", which = "missing")
: ...
signature(object = "gmmfit", which = "numeric")
: ...
signature(object = "gmmfit")
: ...
signature(object = "gmmfit")
: ...
signature(object = "gmmfit")
: ...
showClass("gmmfit")
showClass("gmmfit")
gmmFit
in Package momentfit ~~Method to fit a model using GMM, from an object of class
"momentModel"
or "sysModel"
.
## S4 method for signature 'momentModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, ...) ## S4 method for signature 'formulaModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, ...) ## S4 method for signature 'sysModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls", "EbyE"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, EbyE=FALSE, ...) ## S4 method for signature 'rnonlinearModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, ...) ## S4 method for signature 'rlinearModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls"), weights="optimal", itermaxit=100, efficientWeights=FALSE, ...) ## S4 method for signature 'rformulaModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, ...) ## S4 method for signature 'rslinearModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls", "EbyE"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, EbyE=FALSE, ...)
## S4 method for signature 'momentModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, ...) ## S4 method for signature 'formulaModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, ...) ## S4 method for signature 'sysModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls", "EbyE"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, EbyE=FALSE, ...) ## S4 method for signature 'rnonlinearModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, ...) ## S4 method for signature 'rlinearModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls"), weights="optimal", itermaxit=100, efficientWeights=FALSE, ...) ## S4 method for signature 'rformulaModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, ...) ## S4 method for signature 'rslinearModel' gmmFit(model, type=c("twostep", "iter","cue", "onestep"), itertol=1e-7, initW=c("ident", "tsls", "EbyE"), weights="optimal", itermaxit=100, efficientWeights=FALSE, theta0=NULL, EbyE=FALSE, ...)
model |
A model class object. |
type |
What GMM methods should we use? for
|
itertol |
Tolance for the stopping rule in iterative GMM |
initW |
How should be compute the initial coefficient vector in
the first. For single equation GMM, it only makes a difference for
linear models for which the choice is GMM with identity matrix or
two-stage least quares. For system of equations, |
weights |
What weighting matrix to use? The choices are
|
itermaxit |
Maximum iterations for iterative GMM |
EbyE |
Should the system be estimated equation by equation? |
efficientWeights |
If |
theta0 |
An optional initial vector for |
... |
Arguments to pass to other methods (mostly the optimization algorithm) |
signature(model = "momentModel")
The main method for all moment-based models.
signature(model = "rnonlinearModel")
It makes a difference only if the number of contraints is equal to the
number of coefficients, in which case, the method evalGmm
is called at the contrained vector. If not, the next method is called.
signature(model = "rformulaModel")
It makes a difference only if the number of contraints is equal to the
number of coefficients, in which case, the method evalGmm
is called at the contrained vector. If not, the next method is called.
signature(model = "rlinearModel")
It makes a difference only if the number of contraints is equal to the
number of coefficients, in which case, the method evalGmm
is called at the contrained vector. If not, the next method is called.
signature(model = "sysModel")
Method to estimate system of equations using GMM methods.
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## Efficient GMM with HAC vcov and tsls as first step. res1 <- gmmFit(model1, init="tsls") ## GMM with identity. Two ways. res2 <- gmmFit(model1, type="onestep") res3 <- gmmFit(model1, weights=diag(3)) ## nonlinear regression with iterative GMM. g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) res4 <- gmmFit(model2, type="iter") ## GMM for with no endogenous vaiables is ## OLS with Robust standard error library(lmtest) model3 <- momentModel(y~x1, ~x1, data=simData, vcov="MDS") resGmm <- gmmFit(model3) resLm <- lm(y~x1, simData) summary(resGmm) coeftest(resLm, vcov=vcovHC(resLm, "HC0")) summary(resGmm, df.adj=TRUE) coeftest(resLm, vcov=vcovHC(resLm, "HC1")) ### All constrained R <- diag(2) q <- c(1,2) rmodel1 <- restModel(model1, R, q) gmmFit(rmodel1) ## Only one constraint R <- matrix(c(0,1), ncol=2) q <- 2 rmodel1 <- restModel(model1, R, q) gmmFit(rmodel1)
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## Efficient GMM with HAC vcov and tsls as first step. res1 <- gmmFit(model1, init="tsls") ## GMM with identity. Two ways. res2 <- gmmFit(model1, type="onestep") res3 <- gmmFit(model1, weights=diag(3)) ## nonlinear regression with iterative GMM. g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) res4 <- gmmFit(model2, type="iter") ## GMM for with no endogenous vaiables is ## OLS with Robust standard error library(lmtest) model3 <- momentModel(y~x1, ~x1, data=simData, vcov="MDS") resGmm <- gmmFit(model3) resLm <- lm(y~x1, simData) summary(resGmm) coeftest(resLm, vcov=vcovHC(resLm, "HC0")) summary(resGmm, df.adj=TRUE) coeftest(resLm, vcov=vcovHC(resLm, "HC1")) ### All constrained R <- diag(2) q <- c(1,2) rmodel1 <- restModel(model1, R, q) gmmFit(rmodel1) ## Only one constraint R <- matrix(c(0,1), ncol=2) q <- 2 rmodel1 <- restModel(model1, R, q) gmmFit(rmodel1)
Labour data on 758 young workers between 16 and 30 years hold. Each observation provides information on one individual at two points in time: in 1980 (variable with 80) and in the year given be the YEAR (variable without 80).
data("Griliches")
data("Griliches")
A data.frame with 758 observations and 20 variables.
Dummy for residency in the southern states
Dummy for marital status (1 if married)
Dummy for residency in metropolitan areas
Mother's education in years
IQ score
"Knowledge of the World of Work" test score
The year of the first observation
Age in years
Completed years of schooling
Experience in years
Tenure im years
log wage
Online complements of Fumio Hayashi (2000)
Griliches, Z. (1976). Wages of Very Young Men. Journal of Political Economy, 84, S69–S85.
Blackburn, M. and Neumark, D. (1992). Unobserved Ability, Efficiency Wages, and Interindustry Wage Differentials. Quarterly Journal of Economics, 107, 1421–1436.
Hayashi, F. (2000). Econometrics, New Jersey: Princeton University Press.
The dataset is used in Greene (2012) and is taken from Riphahn, Wambach, Million (2003).
data("HealthRWM")
data("HealthRWM")
A data frame with 27326 observations on the following 25 variables.
ID
Person-identification number
female
Female=1; male=0
year
Calendar year of the observation
age
Age in years
hsat
Health satisfaction, coded 0 (low) to 10 (high)
handdum
Handicapped=1; otherwise=0
handper
Degree of handicap in percent (0 to 100)
hhninc
Household nominal monthly net income in German marks/10,000
hhkids
Children under age 16 in the household=1; otherwise=0
educ
Years of schooling
married
Married=1; otherwise=0
haupts
Highest schooling degree is Hauptschul degree=1; otherwise=0
reals
Highest schooling degree is Realschul degree=1; otherwise=0
fachhs
Highest schooling degree is Polytechnical degree=1; otherwise=0
abitur
Highest schooling degree is Abitur=1; otherwise=0
univ
Highest schooling degree is university degree=1; otherwise=0
working
Employed=1; otherwise=0
bluec
Blue-collar employee=1; otherwise=0
whitec
White-collar employee=1; otherwise=0
self
Self-employed=1; otherwise=0
beamt
Civil servant=1; otherwise=0
docvis
Number of doctor visits in last three months,
hospvis
Number of hospital visits in last calendar year,
public
Insured in public health insurance=1; otherwise=0
addon
Insured by add-on insurance=1; otherwise=0
On Greene (2012) online resources, and on the Journal of Applied Econometrics website (http://qed.econ.queensu.ca/jae/2003-v18.4/riphahn-wambach-million/).
Riphahn, R.T. and Wambach, A. and Million, A. (2003), Incentive Effects in the Demand for Health Care: A Bivariate Panel Count Data Estimation, Journal of Applied Econometrics, Vol. 18, No. 4, 387–405.
Green, W.H.. (2012). Econometric Analysis, 7th edition, Prentice Hall.
###### Example 13.7 of Greene (2012) #################################### ## Selecting the same data point and scaling income ########## data(HealthRWM) dat88 <- subset(HealthRWM, year==1988 & hhninc>0) dat88$hhninc <- dat88$hhninc/10000 ### A guess start thet0 <- c(b0=log(mean(dat88$hhninc)),b1=0,b2=0,b3=0) ## Table 13.2 First column g <- hhninc~exp(b0+b1*age+b2*educ+b3*female) res0 <- nls(g, dat88, start=thet0, control=list(maxiter=100)) summary(res0)$coef ## Table 13.2 Second column ## Trying very hard to reproduce the results, ## Who is right? h1 <- ~age+educ+female model1 <- momentModel(g, h1, thet0, vcov="MDS", data=dat88) res1 <- gmmFit(model1, control=list(reltol=1e-10, abstol=1e-10)) summary(res1)@coef ## Table 13.2 third column (close enough) ## Here a sandwich vcov is required because it is not ## efficient GMM h2 <- ~age+educ+female+hsat+married model2 <- momentModel(g, h2, thet0, vcov="MDS", data=dat88) res2 <- gmmFit(model2, type="onestep") summary(res2, sandwich=TRUE)@coef ## Table 13.2 fourth column (Can't get closer than that) res3 <- gmmFit(model2) summary(res3)@coef # Lets see what happens if we start on Greene solution update(res3, theta0=c(b0=-1.61192, b1=.00092, b2=.04647, b3=-.01517)) ## No...
###### Example 13.7 of Greene (2012) #################################### ## Selecting the same data point and scaling income ########## data(HealthRWM) dat88 <- subset(HealthRWM, year==1988 & hhninc>0) dat88$hhninc <- dat88$hhninc/10000 ### A guess start thet0 <- c(b0=log(mean(dat88$hhninc)),b1=0,b2=0,b3=0) ## Table 13.2 First column g <- hhninc~exp(b0+b1*age+b2*educ+b3*female) res0 <- nls(g, dat88, start=thet0, control=list(maxiter=100)) summary(res0)$coef ## Table 13.2 Second column ## Trying very hard to reproduce the results, ## Who is right? h1 <- ~age+educ+female model1 <- momentModel(g, h1, thet0, vcov="MDS", data=dat88) res1 <- gmmFit(model1, control=list(reltol=1e-10, abstol=1e-10)) summary(res1)@coef ## Table 13.2 third column (close enough) ## Here a sandwich vcov is required because it is not ## efficient GMM h2 <- ~age+educ+female+hsat+married model2 <- momentModel(g, h2, thet0, vcov="MDS", data=dat88) res2 <- gmmFit(model2, type="onestep") summary(res2, sandwich=TRUE)@coef ## Table 13.2 fourth column (Can't get closer than that) res3 <- gmmFit(model2) summary(res3)@coef # Lets see what happens if we start on Greene solution update(res3, theta0=c(b0=-1.61192, b1=.00092, b2=.04647, b3=-.01517)) ## No...
"hypothesisTest"
A class to store results form an hypothesis test.
Objects can be created by calls of the form new("hypothesisTest", ...)
.
It is created by hypothesisTest
.
test
:Object of class "numeric"
~~
hypothesis
:Object of class "character"
~~
dist
:Object of class "character"
~~
df
:Object of class "integer"
~~
pvalue
:Object of class "numeric"
~~
type
:Object of class "character"
~~
signature(x = "hypothesisTest")
: ...
signature(object = "hypothesisTest")
: ...
showClass("hypothesisTest")
showClass("hypothesisTest")
hypothesisTest
in Package momentfit ~~Performs hypothesis tests on the coefficients estimated by any GMM fit method.
## S4 method for signature 'gmmfit,missing' hypothesisTest(object.u, object.r, R, rhs=NULL, vcov=NULL, ...) ## S4 method for signature 'sgmmfit,missing' hypothesisTest(object.u, object.r, R, rhs=NULL, vcov=NULL, ...) ## S4 method for signature 'gmmfit,gmmfit' hypothesisTest(object.u, object.r, type=c("Wald", "LR", "LM"), sameVcov=TRUE, vcov=NULL, firstStepWeight=FALSE, wObj=NULL, ...) ## S4 method for signature 'sgmmfit,sgmmfit' hypothesisTest(object.u, object.r, type=c("Wald", "LR", "LM"), sameVcov=TRUE, vcov=NULL, firstStepWeight=FALSE, wObj=NULL, ...) ## S4 method for signature 'missing,gmmfit' hypothesisTest(object.u, object.r, wObj=NULL) ## S4 method for signature 'missing,sgmmfit' hypothesisTest(object.u, object.r, wObj=NULL)
## S4 method for signature 'gmmfit,missing' hypothesisTest(object.u, object.r, R, rhs=NULL, vcov=NULL, ...) ## S4 method for signature 'sgmmfit,missing' hypothesisTest(object.u, object.r, R, rhs=NULL, vcov=NULL, ...) ## S4 method for signature 'gmmfit,gmmfit' hypothesisTest(object.u, object.r, type=c("Wald", "LR", "LM"), sameVcov=TRUE, vcov=NULL, firstStepWeight=FALSE, wObj=NULL, ...) ## S4 method for signature 'sgmmfit,sgmmfit' hypothesisTest(object.u, object.r, type=c("Wald", "LR", "LM"), sameVcov=TRUE, vcov=NULL, firstStepWeight=FALSE, wObj=NULL, ...) ## S4 method for signature 'missing,gmmfit' hypothesisTest(object.u, object.r, wObj=NULL) ## S4 method for signature 'missing,sgmmfit' hypothesisTest(object.u, object.r, wObj=NULL)
object.u |
An object of class |
object.r |
An object of class |
R |
If it is an object of class |
rhs |
A vector of right hand sides if |
type |
Should we perform a Wald, LR or LM test? |
sameVcov |
For the LR test, should we use the same estimate of the covariance matrix of the moment conditions? See details below. |
vcov |
For the Wald test, it is possible to provide the method with the covariance matrix of the coefficients. |
wObj |
For the LR test, it is possible to provide the
|
... |
Other argument to pass to |
firstStepWeight |
Should we use the first step weighting matrix to compute the test (By default, the optimal weighting matrix is recomputed using the final vector of coefficient estimates). See details below. |
The LR test is the difference between the J-tests of the restricted
and unrestricted models. It is therefore , where
and
are respectively the restricted and unrestricted
sample mean of the moment conditions, and
and
their respective optimal weigthing matrix. The test is therefore
invalid if either of the weighting matrices does not converge to the
inverse of the covariance matrix of the moment conditions. The
restricted and unrestricted models must therefore be estimated by
efficient GMM. This is not required for the Wald test.
Asymptotically, it makes no difference which consistent estimate of
or
is used. However, it will make a difference in
finite samples.
If sameVcov=TRUE
, both and
are equal to the
the optimal weighting matrix from the unrestricted model if
firstStepWeight=FALSE
, and they are equal to the first step
weighting matrix (or the last step for iteratice GMM) of the
unrestricted model if it is TRUE
. For CUE, the value of
firstStepWeight
makes no difference since the weighting matrix
and coefficients are computed simultaneously. Having
prevents the test to be negative in small samples.
If wObj
is provided, both and
are equal to
it. Of cource,
wObj
must be a consistent estimate of the
optimal weighting matrix for the test to be valid.
signature(object.u = "gmmfit", object.r = "gmmfit")
Used to test a restricted model against an unrestricted one.
signature(object.u = "sgmmfit", object.r = "sgmmfit")
Used to test a restricted model against an unrestricted one (for systems of equations).
signature(object.u = "missing", object.r= "gmmfit")
Used to test a restricted model using the LM test.
signature(object.u = "missing", object.r= "sgmmfit")
Used to test a restricted model using the LM test (for systems of equations).
signature(object.u = "gmmfit", object.r = "missing")
Perform a Wald test using an unrestricted model and a restriction matrix or vector.
signature(object.u = "sgmmfit", object.r = "missing")
Perform a Wald test using an unrestricted model and a restriction matrix or vector in systems of linear equations.
data(simData) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3, ~x2+x3+z1+z2+z3, data=simData, vcov="MDS") res1 <- gmmFit(model1) ## Wald test R <- c("x1=0.5","x2=x3") hypothesisTest(object.u=res1, R=R) ## LR tests rmodel1 <- restModel(model1, R) res2 <- gmmFit(rmodel1) hypothesisTest(object.u=res1, object.r=res2, type="LR") ### LR and Wald should be the same as long as the same weighting ### matrix if used for both GMM fits, for the LR and Wald as well # Unrestricted model and save the weights res1 <- gmmFit(model1) w <- res1@wObj # estimate models with the same weights res2 <- gmmFit(rmodel1, weights=w) # LR test with the same weights hypothesisTest(res1, res2, type="LR", wObj=w) # Wald test with vcov based on the same weights (or the bread) hypothesisTest(object.u=res1, R=R, breadOnly=TRUE) ### Another example with real data data(Mroz) model <- momentModel(log(wage)~educ+exper+I(exper^2), ~exper+I(exper^2)+fatheduc+motheduc, vcov="MDS", data=Mroz, centeredVcov=FALSE) R <- c("educ=0","I(exper^2)=0") rmodel <- restModel(model, R) res1 <- gmmFit(model) res2 <- gmmFit(rmodel, weights=res1@wObj) hypothesisTest(object.u=res1, object.r=res2, type="LR", wObj=res1@wObj) hypothesisTest(object.u=res1, object.r=res2, type="Wald", vcov=vcov(res1, breadOnly=TRUE)) ## LM test (identical to the other two tests as well) hypothesisTest(object.r=res2) # or hypothesisTest(object.u=res1, object.r=res2, type="LM") ## Wald with the Delta Method: ## To impose nonlinear restrictions, we need to convert ## the linear model into a nonlinear one NLmodel <- as(model1, "nonlinearModel") R1 <- c("theta2=2", "theta3=theta4^2") rNLmodel <- restModel(NLmodel, R1) res.u <- gmmFit(NLmodel) res.r <- gmmFit(rNLmodel) hypothesisTest(object.u=res.u, R=R1) ## LM hypothesisTest(object.r=res.r) ## LR hypothesisTest(object.r=res.r, object.u=res.u, type="LR")
data(simData) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3, ~x2+x3+z1+z2+z3, data=simData, vcov="MDS") res1 <- gmmFit(model1) ## Wald test R <- c("x1=0.5","x2=x3") hypothesisTest(object.u=res1, R=R) ## LR tests rmodel1 <- restModel(model1, R) res2 <- gmmFit(rmodel1) hypothesisTest(object.u=res1, object.r=res2, type="LR") ### LR and Wald should be the same as long as the same weighting ### matrix if used for both GMM fits, for the LR and Wald as well # Unrestricted model and save the weights res1 <- gmmFit(model1) w <- res1@wObj # estimate models with the same weights res2 <- gmmFit(rmodel1, weights=w) # LR test with the same weights hypothesisTest(res1, res2, type="LR", wObj=w) # Wald test with vcov based on the same weights (or the bread) hypothesisTest(object.u=res1, R=R, breadOnly=TRUE) ### Another example with real data data(Mroz) model <- momentModel(log(wage)~educ+exper+I(exper^2), ~exper+I(exper^2)+fatheduc+motheduc, vcov="MDS", data=Mroz, centeredVcov=FALSE) R <- c("educ=0","I(exper^2)=0") rmodel <- restModel(model, R) res1 <- gmmFit(model) res2 <- gmmFit(rmodel, weights=res1@wObj) hypothesisTest(object.u=res1, object.r=res2, type="LR", wObj=res1@wObj) hypothesisTest(object.u=res1, object.r=res2, type="Wald", vcov=vcov(res1, breadOnly=TRUE)) ## LM test (identical to the other two tests as well) hypothesisTest(object.r=res2) # or hypothesisTest(object.u=res1, object.r=res2, type="LM") ## Wald with the Delta Method: ## To impose nonlinear restrictions, we need to convert ## the linear model into a nonlinear one NLmodel <- as(model1, "nonlinearModel") R1 <- c("theta2=2", "theta3=theta4^2") rNLmodel <- restModel(NLmodel, R1) res.u <- gmmFit(NLmodel) res.r <- gmmFit(rNLmodel) hypothesisTest(object.u=res.u, R=R1) ## LM hypothesisTest(object.r=res.r) ## LR hypothesisTest(object.r=res.r, object.u=res.u, type="LR")
"momentModel"
classesIt either generates the optimal bandwidth and kernel weights or the smoothed moments of moment based models.
## S4 method for signature 'momentModel' kernapply(x, theta=NULL, smooth=TRUE, ...)
## S4 method for signature 'momentModel' kernapply(x, theta=NULL, smooth=TRUE, ...)
x |
An object of class |
theta |
An optional vector of coefficients. For
|
smooth |
By default, it returns the smoothed moment matrix. If
|
... |
Other arguments to pass. Currently not used |
It return an object of class "sSpec"
.
Anatolyev, S. (2005), GMM, GEL, Serial Correlation, and Asymptotic Bias. Econometrica, 73, 983-1002.
Kitamura, Yuichi (1997), Empirical Likelihood Methods With Weakly Dependent Processes. The Annals of Statistics, 25, 2084-2102.
Smith, R.J. (2011), GEL Criteria for Moment Condition Models. Econometric Theory, 27(6), 1192–1235.
data(simData) theta <- c(beta0=1,beta1=2) ## A linearModel model1 <- momentModel(y~x1, ~z1+z2, data=simData,vcov="HAC",vcovOptions=list(kernel="Bartlett")) ### get the bandwidth ### Notice that the kernel name is the not the same ### That's because a Truncated kernel for smoothing ### lead to a Bartlett kernel for the HAC of the moments ### See Smith (2011) kernapply(model1, smooth=FALSE) ### Adding the kernel option to the model model2 <- momentModel(y~x1, ~z1+z2, data=simData,vcov="HAC",vcovOptions=list(kernel="Bartlett"), smooth=TRUE) kernapply(model2, theta)$smoothx[1:5,]
data(simData) theta <- c(beta0=1,beta1=2) ## A linearModel model1 <- momentModel(y~x1, ~z1+z2, data=simData,vcov="HAC",vcovOptions=list(kernel="Bartlett")) ### get the bandwidth ### Notice that the kernel name is the not the same ### That's because a Truncated kernel for smoothing ### lead to a Bartlett kernel for the HAC of the moments ### See Smith (2011) kernapply(model1, smooth=FALSE) ### Adding the kernel option to the model model2 <- momentModel(y~x1, ~z1+z2, data=simData,vcov="HAC",vcovOptions=list(kernel="Bartlett"), smooth=TRUE) kernapply(model2, theta)$smoothx[1:5,]
The data is used to reproduce examples of Greene (2012)
data("Klein")
data("Klein")
A data frame with 22 observations on the following 10 variables.
YEAR
a numeric vector
C
a numeric vector
P
a numeric vector
WP
a numeric vector
I
a numeric vector
K1
a numeric vector
X
a numeric vector
WG
a numeric vector
G
a numeric vector
T
a numeric vector
On Greene (2012) online resources.
Klein, L. (1950), Economic Fluctuations in the United-States 1921-1941, New York: John Wiley and Sons.
Green, W.H.. (2012). Econometric Analysis, 7th edition, Prentice Hall.
data(Klein)
data(Klein)
A panel data set of 565 individuals from 1976 to 1982 used by Cornwell and Rupert (1988)
data("LabourCR")
data("LabourCR")
A data frame with 4165 observations on the following 12 variables.
EXP
Year of full time experience.
WKS
Weeks worked.
OCC
1 if blue-collar occupation, 0 otherwise.
IND
1 if works in a manufacture industry, 0 otherwise.
SOUTH
1 if resides in the south, 0 otherwise.
SMSA
1 if resides in an SMSA, 0 otherwise.
MS
1 if married, 0 otherwise.
FEM
1 if the individual is a female and 0 otherwise.
UNION
1 if wage is set by a union contract and 0 otherwise.
ED
Years of education.
BLK
1 if the individual is black and 0 otherwise.
LWAGE
Log wage.
Greene (2012) online resources: (http://pages.stern.nyu.edu/~wgreene/Text/Edition7/tablelist8new.htm)
Green, W.H.. (2012). Econometric Analysis, 7th edition, Prentice Hall.
Cornwell, C. and Rupert, P. (1988), Efficient Estimation with Panel Data: An Empirical Comparision of Instrumental Variable Estimators, Journal of Applied Econometrics, No.3, 149–155.
data(LabourCR) ## Table 8.1 of Greene (2012) ## Model with Z2 (iid is assumed in Table 8.1 given the s.e.) model2 <- momentModel(WKS~LWAGE+ED+UNION+FEM, ~IND+ED+UNION+FEM+SMSA, vcov="iid", data=LabourCR) ## Model with Z1 using the subsetting method '[' model1 <- model2[-6L] # Second column res1 <- tsls(model1) summary(res1)@coef # Third column res2 <- tsls(model2) summary(res2)@coef
data(LabourCR) ## Table 8.1 of Greene (2012) ## Model with Z2 (iid is assumed in Table 8.1 given the s.e.) model2 <- momentModel(WKS~LWAGE+ED+UNION+FEM, ~IND+ED+UNION+FEM+SMSA, vcov="iid", data=LabourCR) ## Model with Z1 using the subsetting method '[' model1 <- model2[-6L] # Second column res1 <- tsls(model1) summary(res1)@coef # Third column res2 <- tsls(model2) summary(res2)@coef
The algorithms finds the vector or Lagrange multipliers that maximizes
the GEL objective function for a given vector of coefficient .
Wu_lam(gmat, tol=1e-8, maxiter=50, k=1) EEL_lam(gmat, k=1) REEL_lam(gmat, tol=NULL, maxiter=50, k=1) ETXX_lam(gmat, lambda0, k, gelType, algo, method, control) getLambda(gmat, lambda0=NULL, gelType=NULL, rhoFct=NULL, tol = 1e-07, maxiter = 100, k = 1, method="BFGS", algo = c("nlminb", "optim", "Wu"), control = list(), restrictedLam=integer())
Wu_lam(gmat, tol=1e-8, maxiter=50, k=1) EEL_lam(gmat, k=1) REEL_lam(gmat, tol=NULL, maxiter=50, k=1) ETXX_lam(gmat, lambda0, k, gelType, algo, method, control) getLambda(gmat, lambda0=NULL, gelType=NULL, rhoFct=NULL, tol = 1e-07, maxiter = 100, k = 1, method="BFGS", algo = c("nlminb", "optim", "Wu"), control = list(), restrictedLam=integer())
gmat |
The |
lambda0 |
The |
tol |
A tolerance level for the stopping rule in the Wu algorithm |
maxiter |
The maximum number of iteration in the Wu algorithm |
gelType |
A character string specifying the type of GEL. The
available types are |
rhoFct |
An optional function that return |
k |
A numeric scaling factor that is required when |
method |
This is the method for |
algo |
Which algorithm should be used to maximize the GEL objective
function. If set to |
control |
|
restrictedLam |
A vector of integers indicating which
|
The ETXX_lam
is used for ETEL and ETHD. In general, it
computes lambda using ET, and returns the value of the objective
function determined by the gelType
.
It returns the vector when
derive=0
,
when
derive=1
and when
derive=2
.
Anatolyev, S. (2005), GMM, GEL, Serial Correlation, and Asymptotic Bias. Econometrica, 73, 983-1002.
Kitamura, Yuichi (1997), Empirical Likelihood Methods With Weakly Dependent Processes. The Annals of Statistics, 25, 2084-2102.
Kitamura, Y. and Otsu, T. and Evdokimov, K. (2013), Robustness, Infinitesimal Neighborhoods and Moment Restrictions. Econometrica, 81, 1185-1201.
Newey, W.K. and Smith, R.J. (2004), Higher Order Properties of GMM and Generalized Empirical Likelihood Estimators. Econometrica, 72, 219-255.
Smith, R.J. (2011), GEL Criteria for Moment Condition Models. Econometric Theory, 27(6), 1192–1235.
Wu, C. (2005), Algorithms and R codes for the pseudo empirical likelihood method in survey sampling. Survey Methodology, 31(2), page 239.
"linearModel"
Class for moment-based models for which moment conditions are linear and expressed by a formula.
Objects can be created by calls of the form new("linearModel",
...)
.
It is generated my momentModel
.
modelF
:Object of class "data.frame"
~~
instF
:Object of class "data.frame"
~~
vcov
:Object of class "character"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "character"
~~
momNames
:Object of class "character"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
varNames
:Object of class "character"
~~
isEndo
:Object of class "logical"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "regModel"
, directly.
Class "momentModel"
, directly.
signature(object = "linearModel")
: ...
signature(x = "linearModel", y = "linearModel")
: ...
signature(x = "slinearModel", y = "linearModel")
: ...
signature(object = "linearModel")
: ...
signature(object = "linearModel")
: ...
signature(object = "linearModel")
: ...
signature(object = "linearModel")
: ...
signature(object = "linearModel")
: ...
signature(object = "linearModel")
: ...
signature(object = "linearModel", wObj = "momentWeights")
: ...
signature(model = "linearModel")
: ...
showClass("linearModel")
showClass("linearModel")
The data is used to reproduce examples of Greene (2012)
data("ManufactCost")
data("ManufactCost")
A data frame with 25 observations on the following 10 variables.
Year
a numeric vector
Cost
a numeric vector
K
a numeric vector
L
a numeric vector
E
a numeric vector
M
a numeric vector
Pk
a numeric vector
Pl
a numeric vector
Pe
a numeric vector
Pm
a numeric vector
On Greene (2012) online resources.
Berndt, E. and Wood, D. (1975), Technology, Prices, and the Derived Demand for Energy, Review of Economics and Statistics, Vol. 57, 376–384.
Green, W.H.. (2012). Econometric Analysis, 7th edition, Prentice Hall.
data(ManufactCost)
data(ManufactCost)
"mconfint"
A class to store confidence region.
Objects can be created by calls of the form new("mconfint", ...)
.
It is created by the "confint"
method with the option
area=TRUE
(see confint-methods
).
areaPoints
:Object of class "matrix"
~~
type
:Object of class "character"
~~
level
:Object of class "numeric"
~~
theta
:Object of class "numeric"
~~
signature(x = "mconfint")
: ...
signature(x = "mconfint")
: ...
signature(object = "mconfint")
: ...
showClass("mconfint")
showClass("mconfint")
meatGmm
in Package momentfit ~~It computes the meat in the sandwich representation of the covariance matrix of the GMM estimator.
## S4 method for signature 'gmmfit' meatGmm(object, robust=FALSE) ## S4 method for signature 'sgmmfit' meatGmm(object, robust=FALSE) ## S4 method for signature 'tsls' meatGmm(object, robust=FALSE)
## S4 method for signature 'gmmfit' meatGmm(object, robust=FALSE) ## S4 method for signature 'sgmmfit' meatGmm(object, robust=FALSE) ## S4 method for signature 'tsls' meatGmm(object, robust=FALSE)
object |
GMM fit object |
robust |
If |
If robust=FALSE
, then the meat is , where
and
are respectively the sample mean of the derivatives
and the covariance matrix of the moment conditions. If it is
TRUE
, the meat is , where
is the weighting
matrix.
For tsls
objects, the function makes use of the QR representation
of the weighting matrix. It is simply possible to get the meat in a more
stable way. In that case, . If
robust
is FALSE, V
is assumed to be which is the
inverse of the
bread
. Therefore, a sandwich covariance matrix
with robust=FALSE
will result in a non-sandwich matrix.
For sgmmfit
, the covariance is for the vectorized coefficient
vector of all equations.
signature(object = "gmmfit")
General GMM fit.
signature(object = "tsls")
For model estimated by two-stage least squares.
signature(object = "sgmmfit")
For system of equations.
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) res <- gmmFit(model1) meatGmm(res) ## It is a slightly different because the weighting matrix ## is computed using the first step estimate and the covariance ## matrix of the moment conditions is based on the final estimate. ## They should, however, be asymptotically equivalent. meatGmm(res, robust=TRUE) ## TSLS res2 <- tsls(model1) ## Robust meat meatGmm(res2, TRUE) ## It makes no difference is the model is assumed iid model2 <- momentModel(y~x1, ~z1+z2, data=simData, vcov="iid") res2 <- tsls(model2) meatGmm(res2, FALSE) meatGmm(res2, TRUE)
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) res <- gmmFit(model1) meatGmm(res) ## It is a slightly different because the weighting matrix ## is computed using the first step estimate and the covariance ## matrix of the moment conditions is based on the final estimate. ## They should, however, be asymptotically equivalent. meatGmm(res, robust=TRUE) ## TSLS res2 <- tsls(model1) ## Robust meat meatGmm(res2, TRUE) ## It makes no difference is the model is assumed iid model2 <- momentModel(y~x1, ~z1+z2, data=simData, vcov="iid") res2 <- tsls(model2) meatGmm(res2, FALSE) meatGmm(res2, TRUE)
merge
in Package base ~~It allows to merge momentModel
classes into system objects.
## S4 method for signature 'linearModel,linearModel' merge(x, y, ...) ## S4 method for signature 'nonlinearModel,nonlinearModel' merge(x, y, ...) ## S4 method for signature 'slinearModel,linearModel' merge(x, y, ...) ## S4 method for signature 'snonlinearModel,nonlinearModel' merge(x, y, ...)
## S4 method for signature 'linearModel,linearModel' merge(x, y, ...) ## S4 method for signature 'nonlinearModel,nonlinearModel' merge(x, y, ...) ## S4 method for signature 'slinearModel,linearModel' merge(x, y, ...) ## S4 method for signature 'snonlinearModel,nonlinearModel' merge(x, y, ...)
x |
An object on which the other objects are merged to. |
y |
An object to be merged to |
... |
Other objects of the same class as |
signature(x = "linearModel", y = "linearModel")
Merging linear models into a system of equations.
signature(x = "nonlinearModel", y = "nonlinearModel")
Merging nonlinear models into a system of equations.
signature(x = "slinearModel", y = "linearModel")
Adding linear equations to a system of linear equations.
signature(x = "snonlinearModel", y = "nonlinearModel")
Adding nonlinear equations to a system of nonlinear equations.
data(simData) g1 <- y1~x1+x4; h1 <- ~z1+z2+z3+z4+x4 g2 <- y2~x1+x2+x3; h2 <- ~z1+z2+z3+z4+x3 g3 <- y3~x2+x3+x4; h3 <- ~z2+z3+z4+x3+x4 ## Linear models m1 <- momentModel(g1, h1, data=simData) m2 <- momentModel(g2, h2, data=simData) m3 <- momentModel(g3, h3, data=simData) ## (sys1 <- merge(m1, m2)) ## add an equation to the model (sys2 <- merge(sys1, m3)) ## want to get back the first? sys2[1:2] ## Nonlinear (not really, just written as nonlinear) nlg <- list(y1~theta0+theta1*x1+theta2*x4, y2~alpha0+alpha1*x1+alpha2*x2+alpha3*x3, y3~beta0+beta1*x2+beta2*x3+beta3*x4) theta0 <- list(c(theta0=1,theta1=2,theta2=3), c(alpha0=1,alpha1=2,alpha2=3, alpha3=4), c(beta0=1,beta1=2,beta2=3,beta3=4)) nm1 <- momentModel(nlg[[1]], h1, theta0[[1]], data=simData) nm2 <- momentModel(nlg[[2]], h2, theta0[[2]], data=simData) nm3 <- momentModel(nlg[[3]], h3, theta0[[3]], data=simData) merge(nm1, nm2, nm3)
data(simData) g1 <- y1~x1+x4; h1 <- ~z1+z2+z3+z4+x4 g2 <- y2~x1+x2+x3; h2 <- ~z1+z2+z3+z4+x3 g3 <- y3~x2+x3+x4; h3 <- ~z2+z3+z4+x3+x4 ## Linear models m1 <- momentModel(g1, h1, data=simData) m2 <- momentModel(g2, h2, data=simData) m3 <- momentModel(g3, h3, data=simData) ## (sys1 <- merge(m1, m2)) ## add an equation to the model (sys2 <- merge(sys1, m3)) ## want to get back the first? sys2[1:2] ## Nonlinear (not really, just written as nonlinear) nlg <- list(y1~theta0+theta1*x1+theta2*x4, y2~alpha0+alpha1*x1+alpha2*x2+alpha3*x3, y3~beta0+beta1*x2+beta2*x3+beta3*x4) theta0 <- list(c(theta0=1,theta1=2,theta2=3), c(alpha0=1,alpha1=2,alpha2=3, alpha3=4), c(beta0=1,beta1=2,beta2=3,beta3=4)) nm1 <- momentModel(nlg[[1]], h1, theta0[[1]], data=simData) nm2 <- momentModel(nlg[[2]], h2, theta0[[2]], data=simData) nm3 <- momentModel(nlg[[3]], h3, theta0[[3]], data=simData) merge(nm1, nm2, nm3)
model.matrix
in Package stats ~~Model matrix form momentModel
. It returns the matrix of regressors
or the instruments. In restricted models, it returns the reduced matrix
of regressors.
## S4 method for signature 'linearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'rlinearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'nonlinearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'slinearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'rslinearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'rsnonlinearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'snonlinearModel' model.matrix(object, type=c("regressors","instruments"))
## S4 method for signature 'linearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'rlinearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'nonlinearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'slinearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'rslinearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'rsnonlinearModel' model.matrix(object, type=c("regressors","instruments")) ## S4 method for signature 'snonlinearModel' model.matrix(object, type=c("regressors","instruments"))
object |
Object of class |
type |
Should the function returns the matrix of instruments or the
matrix of regressors. For |
signature(object = "linearModel")
Linear models with not restrictions.
signature(object = "nonlinearModel")
Nonlinear models with not restrictions.
signature(object = "rlinearModel")
linear models with restrictions.
signature(object = "slinearModel")
System of linear equations with no restrictions.
signature(object = "rslinearModel")
System of linear equations with restrictions.
signature(object = "rsnonlinearModel")
System of nonlinear equations with restrictions.
signature(object = "snonlinearModel")
System of nonlinear equations with no restrictions.
data(simData) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3, ~x2+x3+z1+z2, data=simData) model.matrix(model1)[1:3,] ## Restrictions change the response R <- c("x2=2","x3+x1=3") rmodel1 <- restModel(model1, R) rmodel1 model.matrix(rmodel1)[1:3,]
data(simData) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3, ~x2+x3+z1+z2, data=simData) model.matrix(model1)[1:3,] ## Restrictions change the response R <- c("x2=2","x3+x1=3") rmodel1 <- restModel(model1, R) rmodel1 model.matrix(rmodel1)[1:3,]
modelDims
It extracts important information from the model. It is mostly used by
other methods when a modelModel
has been modifed. An example is
when restrictions have been imposed on coefficients.
signature(object = "rlinearModel")
signature(object = "rnonlinearModel")
signature(object = "rfunctionModel")
signature(object = "linearModel")
signature(object = "nonlinearModel")
signature(object = "functionModel")
signature(object = "formulaModel")
signature(object = "rformulaModel")
signature(object = "slinearModel")
signature(object = "rslinearModel")
signature(object = "rsnonlinearModel")
signature(object = "snonlinearModel")
signature(object = "sfunctionModel")
data(simData) model1 <- momentModel(y~x1+x2, ~x2+z1+z2, data=simData) modelDims(model1) ## Unrestricted model rmodel1 <- restModel(model1, R=c("x1+x2=4")) modelDims(rmodel1)
data(simData) model1 <- momentModel(y~x1+x2, ~x2+z1+z2, data=simData) modelDims(model1) ## Unrestricted model rmodel1 <- restModel(model1, R=c("x1+x2=4")) modelDims(rmodel1)
modelResponse
in Package momentfit ~~Return the response vector in models with and without restrictions
signature(object = "linearModel")
For linear models without restrictions on the coefficients.
signature(object = "slinearModel")
For system of linear models without restrictions on the coefficients.
signature(object = "rslinearModel")
For system of linear models with restrictions on the coefficients.
signature(object = "rlinearModel")
For linear models with restrictions on the coefficients.
data(simData) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3, ~x2+x3+z1+z2, data=simData) y <- modelResponse(model1) ## Restrictions change the response R <- c("x2=2","x3=3") rmodel1 <- restModel(model1, R) rmodel1 restY <- modelResponse(rmodel1)
data(simData) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3, ~x2+x3+z1+z2, data=simData) y <- modelResponse(model1) ## Restrictions change the response R <- c("x2=2","x3=3") rmodel1 <- restModel(model1, R) rmodel1 restY <- modelResponse(rmodel1)
"momentModel"
classesIt builds an object class "momentModel"
, which is a
union class for "linearModel"
, "nonlinearModel"
,
"formulaModel"
and "functionModel"
classes. These are
classes for moment based models. This is the first step before running
any estimation algorithm.
momentModel(g, x=NULL, theta0=NULL,grad=NULL, vcov = c("iid", "HAC", "MDS", "CL"), vcovOptions=list(), centeredVcov = TRUE, data=parent.frame(), na.action="na.omit", survOptions=list(), smooth=FALSE)
momentModel(g, x=NULL, theta0=NULL,grad=NULL, vcov = c("iid", "HAC", "MDS", "CL"), vcovOptions=list(), centeredVcov = TRUE, data=parent.frame(), na.action="na.omit", survOptions=list(), smooth=FALSE)
g |
A function of the form |
x |
The matrix or vector of data from which the function
|
theta0 |
A |
grad |
A function of the form |
vcov |
Assumption on the properties of the moment conditions. By
default, they are weakly dependant processes. For |
vcovOptions |
A list of options for the covariance matrix of the
moment conditions. See |
centeredVcov |
Should the moment function be centered when computing its covariance matrix. Doing so may improve inference. |
data |
A data.frame or a matrix with column names (Optional). |
na.action |
Action to take for missing values. If missing values
are present and the option is set to |
survOptions |
If needed, a list with the type of survey weights and
the weights as a numeric vector, data.frame or formula. The type is either
|
smooth |
If |
'momentModel' returns an object of one of the subclasses of "momentModel"
.
Andrews DWK (1991), Heteroskedasticity and Autocorrelation Consistent Covariance Matrix Estimation. Econometrica, 59, 817–858.
Newey WK & West KD (1987), A Simple, Positive Semi-Definite, Heteroskedasticity and Autocorrelation Consistent Covariance Matrix. Econometrica, 55, 703–708.
Newey WK & West KD (1994), Automatic Lag Selection in Covariance Matrix Estimation. Review of Economic Studies, 61, 631-653.
data(simData) theta <- c(beta0=1,beta1=2) ## A linearModel model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## A nonlinearModel g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) ## A functionModel fct <- function(tet, x) { m1 <- (tet[1] - x) m2 <- (tet[2]^2 - (x - tet[1])^2) m3 <- x^3 - tet[1]*(tet[1]^2 + 3*tet[2]^2) f <- cbind(m1, m2, m3) return(f) } dfct <- function(tet, x) { jacobian <- matrix(c( 1, 2*(-tet[1]+mean(x)), -3*tet[1]^2-3*tet[2]^2,0, 2*tet[2], -6*tet[1]*tet[2]), nrow=3,ncol=2) return(jacobian) } model3 <- momentModel(fct, simData$x3, theta0=c(beta0=1, beta1=2), grad=dfct)
data(simData) theta <- c(beta0=1,beta1=2) ## A linearModel model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## A nonlinearModel g <- y~beta0+x1^beta1 h <- ~z1+z2 model2 <- momentModel(g, h, c(beta0=1, beta1=2), data=simData) ## A functionModel fct <- function(tet, x) { m1 <- (tet[1] - x) m2 <- (tet[2]^2 - (x - tet[1])^2) m3 <- x^3 - tet[1]*(tet[1]^2 + 3*tet[2]^2) f <- cbind(m1, m2, m3) return(f) } dfct <- function(tet, x) { jacobian <- matrix(c( 1, 2*(-tet[1]+mean(x)), -3*tet[1]^2-3*tet[2]^2,0, 2*tet[2], -6*tet[1]*tet[2]), nrow=3,ncol=2) return(jacobian) } model3 <- momentModel(fct, simData$x3, theta0=c(beta0=1, beta1=2), grad=dfct)
"momentModel"
A union class for all moment based models. It is created by
momentModel
.
A virtual Class: No objects may be created from it.
signature(x = "momentModel", i = "missing", j = "missing")
: ...
signature(object = "momentModel")
: ...
signature(model = "momentModel")
: ...
signature(object = "momentModel", theta = "numeric", lambda = "numeric")
: ...
signature(model = "momentModel")
: ...
signature(object = "momentModel", theta = "numeric", wObj = "momentWeights")
: ...
signature(object = "momentModel")
: ...
signature(model = "momentModel")
: ...
signature(object = "momentModel")
: ...
signature(model = "momentModel")
: ...
signature(x = "momentModel")
: ...
signature(x = "momentModel")
: ...
signature(object = "momentModel")
: ...
signature(object = "momentModel")
: ...
signature(object = "momentModel")
: ...
signature(object = "momentModel")
: ...
signature(x = "momentModel")
: ...
showClass("momentModel")
showClass("momentModel")
momentStrength
in Package momentfit ~~It produces measures of the strength of the moment conditons.
## S4 method for signature 'linearModel' momentStrength(object, theta, vcovType=c("OLS","HC","HAC","CL"))
## S4 method for signature 'linearModel' momentStrength(object, theta, vcovType=c("OLS","HC","HAC","CL"))
object |
An object of class |
theta |
Coefficient vector at which the strength must be
measured. It does not impact the measure for objects of class
|
vcovType |
Type of covariance matrix used to
compute the F-test of the first-stage regression. For |
signature(object = "functionModel")
Not implemented yet. In that case, we want some measure of the rank of the matrix of derivatives.
signature(object = "formulaModel")
Not implemented yet. In that case, we want some measure of the rank of the matrix of derivatives.
signature(object = "linearModel")
It returns the F-test of the first stage regression. It is a measure of the strength of the instruments.
signature(object = "rlinearModel")
Returns nothing for now.
signature(object = "nonlinearModel")
Not implemented yet.
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) momentStrength(model1)
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) momentStrength(model1)
"momentWeights"
A class to store the weighting matrix of a set of moment conditions.
Objects can be created by calls of the form new("momentWeights", ...)
.
It is created my evalWeights
.
w
:Object of class "ANY"
~~
type
:Object of class "character"
~~
wSpec
:Object of class "list"
~~
signature(x = "momentWeights", i = "missing", j = "missing")
: ...
signature(x = "momentWeights", i = "numeric", j = "missing")
: ...
signature(object = "momentModel", theta = "numeric", wObj = "momentWeights")
: ...
signature(x = "momentWeights")
: ...
signature(w = "momentWeights", x = "matrixORnumeric", y = "matrixORnumeric")
: ...
signature(w = "momentWeights", x = "matrixORnumeric", y = "missing")
: ...
signature(w = "momentWeights", x = "missing", y = "missing")
: ...
signature(object = "momentWeights")
: ...
signature(object = "allNLModel", wObj = "momentWeights")
: ...
signature(object = "linearModel", wObj = "momentWeights")
: ...
showClass("momentWeights")
showClass("momentWeights")
momFct
in Package momentfit
The methods computes the moment matrix. It is use to create special moment functions
## S4 method for signature 'numeric,gelfit' momFct(eta, object)
## S4 method for signature 'numeric,gelfit' momFct(eta, object)
eta |
A vector that includes the coefficient and the Lagrange multipliers |
object |
An object of class |
signature(eta = "numeric", object = "gelfit")
The dataset was used by Mroz (1987) and in examples in Wooldridge (2016)
data("Mroz")
data("Mroz")
A data frame with 753 observations on the following 22 variables.
inlf
=1 if in lab frce, 1975
hours
hours worked, 1975
kidslt6
number of kids < 6 years
kidsge6
number of kids 6-18
age
woman's age in years
educ
years of schooling
wage
Estimated wage from earnings and hours
repwage
reported wage at interview in 1976
hushrs
hours worked by husband, 1975
husage
husband's age
huseduc
husband's years of schooling
huswage
husband's hourly wage, 1975
faminc
family income, 1975
mtr
federal marginal tax rate facing woman
motheduc
mother's years of schooling
fatheduc
father's years of schooling
unem
unemployment rate in county of residence
city
=1 if live in SMSA
exper
actual labor market experience
nwifeinc
(
From Wooldridge (2016) online resources.
Mroz, T.A. (1987), The Sensitivity of an Empirical Model of Married Women's Hours of Work to Economic and Statistical Assumptions, Econometrica, 55, 657–678. 387–405.
Wooldridge, J.M. (2016). Introductory Econometrics, A Modern Approach, 6th edition, Cengage Learning.
## Example 15.1 of Wooldridge (2016) data(Mroz) Mroz <- subset(Mroz, hours>0) ## I guess IID is assumed (That's how we get the same s.e.) ## By default a sandwich vcov is computed because it is ## a just-identified model. res4 <- gmm4(log(wage)~educ, ~fatheduc, vcov="iid", data=Mroz) summary(res4) ## If we adjust the variance of the residuals, however, ## we are a little off (very little) summary(res4, df.adj=TRUE) ## Example 15.5 of Wooldridge (2016) ## Need to adjust for degrees of freedom in order ## to get the same s.e. ## The first stage F-test is very different though ## Cannot get the same even if do it manually ## with the linearHypothesis from the car package model <- momentModel(log(wage)~educ+exper+I(exper^2), ~exper+I(exper^2)+fatheduc+motheduc, vcov="iid", data=Mroz) res <- tsls(model) summary(res, df.adj=TRUE)
## Example 15.1 of Wooldridge (2016) data(Mroz) Mroz <- subset(Mroz, hours>0) ## I guess IID is assumed (That's how we get the same s.e.) ## By default a sandwich vcov is computed because it is ## a just-identified model. res4 <- gmm4(log(wage)~educ, ~fatheduc, vcov="iid", data=Mroz) summary(res4) ## If we adjust the variance of the residuals, however, ## we are a little off (very little) summary(res4, df.adj=TRUE) ## Example 15.5 of Wooldridge (2016) ## Need to adjust for degrees of freedom in order ## to get the same s.e. ## The first stage F-test is very different though ## Cannot get the same even if do it manually ## with the linearHypothesis from the car package model <- momentModel(log(wage)~educ+exper+I(exper^2), ~exper+I(exper^2)+fatheduc+motheduc, vcov="iid", data=Mroz) res <- tsls(model) summary(res, df.adj=TRUE)
"nonlinearModel"
Class for moment-based models for which moment conditions are orthogonality conditions between instruments and the residuals from a nonlinear regression.
Objects can be created by calls of the form new("nonlinearModel",
...)
.
It is generated my momentModel
.
modelF
:Object of class "data.frame"
~~
instF
:Object of class "data.frame"
~~
vcov
:Object of class "character"
~~
theta0
:Object of class "numeric"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "character"
~~
momNames
:Object of class "character"
~~
fRHS
:Object of class "expression"
~~
fLHS
:Object of class "expressionORNULL"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
varNames
:Object of class "character"
~~
isEndo
:Object of class "logical"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "regModel"
, directly.
Class "allNLModel"
, directly.
Class "momentModel"
, directly.
signature(object = "nonlinearModel")
: ...
signature(x = "nonlinearModel", y = "nonlinearModel")
: ...
signature(x = "snonlinearModel", y = "nonlinearModel")
: ...
signature(object = "nonlinearModel")
: ...
signature(object = "nonlinearModel")
: ...
signature(object = "nonlinearModel")
: ...
signature(object = "nonlinearModel")
: ...
signature(object = "nonlinearModel")
: ...
showClass("nonlinearModel")
showClass("nonlinearModel")
plot
from package graphics ~~It plots the confidence region.
## S4 method for signature 'ANY' plot(x, y, ...) ## S4 method for signature 'mconfint' plot(x, y, main=NULL, xlab=NULL, ylab=NULL, pch=21, bg=1, Pcol=1, ylim=NULL, xlim=NULL, add=FALSE, addEstimates=TRUE, ...)
## S4 method for signature 'ANY' plot(x, y, ...) ## S4 method for signature 'mconfint' plot(x, y, main=NULL, xlab=NULL, ylab=NULL, pch=21, bg=1, Pcol=1, ylim=NULL, xlim=NULL, add=FALSE, addEstimates=TRUE, ...)
x |
An object to plot |
y |
On used for |
main |
Optional title |
xlab |
Optional label for the x-axis. |
ylab |
Optional label for the y-axis. |
pch |
Type of points (see |
bg |
Background color for points. |
Pcol |
The color for the points. If col is used, it is passed to
|
xlim |
Optional range for the x-axis. |
ylim |
Optional range for the y-axis. |
add |
If TRUE, the region is added to an existing plot. |
addEstimates |
Should we add the point estimate to the confidence region? This option is only used when add is FALSE. |
... |
Arguments to pass to |
signature(object = "ANY")
It uses the plot
from package graphics
signature(object = "mconfint")
Plot the 2D confidence region.
print
in Package base
Print method for all "momentModel"
, "gmmfit"
,
"summaryGmm"
"hypothesisTest"
and "specTest"
objects.
signature(x = "ANY")
signature(x = "momentModel")
signature(x = "sSpec")
signature(x = "confint")
signature(x = "mconfint")
signature(x = "sysModel")
signature(x = "sysMomentWeights")
signature(x = "gmmfit")
signature(x = "gelfit")
signature(x = "sgmmfit")
signature(x = "summaryGmm")
signature(x = "summaryGel")
signature(x = "summarySysGmm")
signature(x = "specTest")
signature(x = "rlinearModel")
signature(x = "rformulaModel")
signature(x = "rslinearModel")
signature(x = "rsnonlinearModel")
signature(x = "rnonlinearModel")
signature(x = "rfunctionModel")
signature(x = "hypothesisTest")
signature(x = "momentWeights")
printRestrict
in Package momentfit ~~It prints the detailed restrictions imposed on "momentModel"
classes.
signature(object = "rgelModels")
signature(object = "rlinearModel")
signature(object = "rnonlinearModel")
signature(object = "rfunctionModel")
signature(object = "rformulaModel")
signature(object = "rslinearModel")
signature(object = "rsnonlinearModel")
data(simData) theta <- c(beta0=1,beta1=2) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3+z1, ~x1+x2+z1+z2+z3+z4, data=simData) ## restricted model R <- matrix(c(1,1,0,0,0,0,0,2,0,0,0,0,0,1,-1),3,5, byrow=TRUE) q <- c(0,1,3) rmodel1 <- restModel(model1, R, q) printRestrict(rmodel1)
data(simData) theta <- c(beta0=1,beta1=2) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3+z1, ~x1+x2+z1+z2+z3+z4, data=simData) ## restricted model R <- matrix(c(1,1,0,0,0,0,0,2,0,0,0,0,0,1,-1),3,5, byrow=TRUE) q <- c(0,1,3) rmodel1 <- restModel(model1, R, q) printRestrict(rmodel1)
quadra
in Package momentfit ~~~~ Computes the quadratic form, where the center matrix is a class
momentWeights
object ~~
## S4 method for signature 'momentWeights,missing,missing' quadra(w, x, y) ## S4 method for signature 'momentWeights,matrixORnumeric,missing' quadra(w, x, y) ## S4 method for signature 'momentWeights,matrixORnumeric,matrixORnumeric' quadra(w, x, y) ## S4 method for signature 'sysMomentWeights,matrixORnumeric,matrixORnumeric' quadra(w, x, y) ## S4 method for signature 'sysMomentWeights,matrixORnumeric,missing' quadra(w, x, y) ## S4 method for signature 'sysMomentWeights,missing,missing' quadra(w, x, y)
## S4 method for signature 'momentWeights,missing,missing' quadra(w, x, y) ## S4 method for signature 'momentWeights,matrixORnumeric,missing' quadra(w, x, y) ## S4 method for signature 'momentWeights,matrixORnumeric,matrixORnumeric' quadra(w, x, y) ## S4 method for signature 'sysMomentWeights,matrixORnumeric,matrixORnumeric' quadra(w, x, y) ## S4 method for signature 'sysMomentWeights,matrixORnumeric,missing' quadra(w, x, y) ## S4 method for signature 'sysMomentWeights,missing,missing' quadra(w, x, y)
w |
An object of class |
x |
A matrix or numeric vector |
y |
A matrix or numeric vector |
signature(w = "momentWeights", x = "matrixORnumeric", y =
"matrixORnumeric")
It computes , where
is the weighting matrix.
signature(w = "momentWeights", x = "matrixORnumeric", y =
"missing")
It computes , where
is the weighting matrix.
signature(w = "momentWeights", x = "missing", y =
"missing")
It computes , where
is the weighting matrix. When
is the inverse of the covariance matrix of the moment
conditions, it is saved as either a QR decompisition, a Cholesky
decomposition or a covariance matrix into the
momentWeights
object. The quadra
method with no y
and x
is
therefore a way to invert it. The same applies to system of equations
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) gbar <- evalMoment(model1, theta) gbar <- colMeans(gbar) ### Onjective function of GMM with identity matrix wObj <- evalWeights(model1, w="ident") quadra(wObj, gbar) ### Onjective function of GMM with efficient weights wObj <- evalWeights(model1, theta) quadra(wObj, gbar)
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) gbar <- evalMoment(model1, theta) gbar <- colMeans(gbar) ### Onjective function of GMM with identity matrix wObj <- evalWeights(model1, w="ident") quadra(wObj, gbar) ### Onjective function of GMM with efficient weights wObj <- evalWeights(model1, theta) quadra(wObj, gbar)
"regModel"
A union class for "linearModel"
and "nonlinearModel"
classes.
A virtual Class: No objects may be created from it.
signature(x = "regModel", i = "numeric", j = "missing")
: ...
signature(object = "regModel")
: ...
signature(object = "regModel")
: ...
signature(x = "regModel")
: ...
showClass("regModel")
showClass("regModel")
residuals
in Package stats ~~It computes the residual for a given coefficient vector, when the model
is a linear of nonlinear regression with instruments. The method can be
called on a momentModel
class for a given coefficient theta or on a
gmmfit
object.
signature(object = "rsysModel")
signature(object = "linearModel")
signature(object = "nonlinearModel")
signature(object = "gmmfit")
signature(object = "gelfit")
signature(object = "sgmmfit")
signature(object = "sysModel")
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 <- momentModel(y~x, ~z1+z2, data=dat) ## residuals for a given theta e <- residuals(model1, theta) ## residuals of the fit res <- gmmFit(model1) e <- residuals(res)
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 <- momentModel(y~x, ~z1+z2, data=dat) ## residuals for a given theta e <- residuals(model1, theta) ## residuals of the fit res <- gmmFit(model1) e <- residuals(res)
restModel
in Package momentfit ~~It converts momentModel
objects into its restricted
counterpart.
## S4 method for signature 'linearModel' restModel(object, R, rhs=NULL) ## S4 method for signature 'slinearModel' restModel(object, R, rhs=NULL) ## S4 method for signature 'snonlinearModel' restModel(object, R, rhs=NULL) ## S4 method for signature 'nonlinearModel' restModel(object, R, rhs=NULL) ## S4 method for signature 'formulaModel' restModel(object, R, rhs=NULL) ## S4 method for signature 'functionModel' restModel(object, R, rhs=NULL)
## S4 method for signature 'linearModel' restModel(object, R, rhs=NULL) ## S4 method for signature 'slinearModel' restModel(object, R, rhs=NULL) ## S4 method for signature 'snonlinearModel' restModel(object, R, rhs=NULL) ## S4 method for signature 'nonlinearModel' restModel(object, R, rhs=NULL) ## S4 method for signature 'formulaModel' restModel(object, R, rhs=NULL) ## S4 method for signature 'functionModel' restModel(object, R, rhs=NULL)
object |
An object of class |
R |
Either a matrix or a vector of characters for linear models and a list of formulas for nonlinear models |
rhs |
The right hand side of the linear restrictions. It is ignored for nonlinear models. |
signature(object = "linearModel")
Method for object of class linearModel
.
signature(object = "linearGel")
Method for all classes related to linearGel
.
signature(object = "slinearModel")
Method for object of class slinearModel
.
signature(object = "snonlinearModel")
Method for object of class snonlinearModel
.
signature(object = "nonlinearModel")
Method for object of class nonlinearModel
.
signature(object = "nonlinearGel")
Method for object of class nonlinearGel
.
signature(object = "functionModel")
Method for object of class functionModel
.
signature(object = "functionGel")
Method for object of class functionGel
.
signature(object = "formulaModel")
Method for object of class formulaModel
.
signature(object = "formulaGel")
Method for object of class formulaGel
.
data(simData) theta <- c(beta0=1,beta1=2) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3+z1, ~x1+x2+z1+z2+z3+z4, data=simData) ## Using matrix R R <- matrix(c(1,1,0,0,0,0,0,2,0,0,0,0,0,1,-1),3,5, byrow=TRUE) q <- c(0,1,3) rmodel1 <- restModel(model1, R, q) rmodel1 ## Using character ## Many ways to write the constraints R1 <- c("x1","2*x2+z1=2", "4+x3*5=3") rmodel1 <- restModel(model1, R1) rmodel1 ## Works with interaction and identity function I() model1 <- momentModel(y~x1*x2+exp(x3)+I(z1^2), ~x1+x2+z1+z2+z3+z4, data=simData) R1 <- c("x1","exp(x3)+2*x1:x2", "I(z1^2)=3") rmodel1 <- restModel(model1, R1) rmodel1 ## nonlinear constraints on a linear model ## we need to convert the linear model into a nonlinear one model <- momentModel(y~x1+x2+x3+z1, ~x1+x2+z1+z2+z3+z4, data=simData) NLmodel <- as(model, "nonlinearModel") ## To avoid having unconventional parameter names, which happens ## when I() is used or with interaction, the X's and coefficients are ## renamed NLmodel@parNames ## Restriction can be a list of formula or vector of characters ## For the latter, it will be converted into a list of formulas R1 <- c("theta2=2", "theta3=theta4^2") rmod1 <- restModel(NLmodel, R1) res1 <- gmmFit(rmod1) res1 ## recover the orignial form coef(rmod1, coef(res1)) ## with formulas R2 <- list(theta2~2, theta3~1/theta4) rmod2 <- restModel(NLmodel, R2) res2 <- gmmFit(rmod2) res2 coef(rmod2, coef(res2)) ## The same can be done with function based models
data(simData) theta <- c(beta0=1,beta1=2) ## Unrestricted model model1 <- momentModel(y~x1+x2+x3+z1, ~x1+x2+z1+z2+z3+z4, data=simData) ## Using matrix R R <- matrix(c(1,1,0,0,0,0,0,2,0,0,0,0,0,1,-1),3,5, byrow=TRUE) q <- c(0,1,3) rmodel1 <- restModel(model1, R, q) rmodel1 ## Using character ## Many ways to write the constraints R1 <- c("x1","2*x2+z1=2", "4+x3*5=3") rmodel1 <- restModel(model1, R1) rmodel1 ## Works with interaction and identity function I() model1 <- momentModel(y~x1*x2+exp(x3)+I(z1^2), ~x1+x2+z1+z2+z3+z4, data=simData) R1 <- c("x1","exp(x3)+2*x1:x2", "I(z1^2)=3") rmodel1 <- restModel(model1, R1) rmodel1 ## nonlinear constraints on a linear model ## we need to convert the linear model into a nonlinear one model <- momentModel(y~x1+x2+x3+z1, ~x1+x2+z1+z2+z3+z4, data=simData) NLmodel <- as(model, "nonlinearModel") ## To avoid having unconventional parameter names, which happens ## when I() is used or with interaction, the X's and coefficients are ## renamed NLmodel@parNames ## Restriction can be a list of formula or vector of characters ## For the latter, it will be converted into a list of formulas R1 <- c("theta2=2", "theta3=theta4^2") rmod1 <- restModel(NLmodel, R1) res1 <- gmmFit(rmod1) res1 ## recover the orignial form coef(rmod1, coef(res1)) ## with formulas R2 <- list(theta2~2, theta3~1/theta4) rmod2 <- restModel(NLmodel, R2) res2 <- gmmFit(rmod2) res2 coef(rmod2, coef(res2)) ## The same can be done with function based models
"rformulaModel"
A class for restricted moment-based models for which moment conditions are expressed using a list of formulas.
Objects can be created by calls of the form new("rformulaModel", ...)
.
It is created by restModel-methods
.
R
:Object of class "list"
~~
cstSpec
:Object of class "list"
~~
modelF
:Object of class "data.frame"
~~
vcov
:Object of class "character"
~~
theta0
:Object of class "numeric"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "character"
~~
momNames
:Object of class "character"
~~
fRHS
:Object of class "list"
~~
fLHS
:Object of class "list"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
varNames
:Object of class "character"
~~
isEndo
:Object of class "logical"
~~
isMDE
:Object of class "logical"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "formulaModel"
, directly.
Class "rmomentModel"
, directly.
Class "allNLModel"
, by class "formulaModel", distance 2.
Class "momentModel"
, by class "formulaModel", distance 2.
signature(object = "rformulaModel")
: ...
signature(object = "rformulaModel")
: ...
signature(object = "rformulaModel")
: ...
signature(model = "rformulaModel")
: ...
signature(object = "rformulaModel")
: ...
signature(x = "rformulaModel")
: ...
signature(object = "rformulaModel")
: ...
showClass("rformulaModel")
showClass("rformulaModel")
"rfunctionModel"
A restricted moment-based model for which moment conditions are defined by a function.
Objects can be created by calls of the form new("rfunctionModel", ...)
.
It is created by restModel-methods
.
R
:Object of class "list"
~~
cstSpec
:Object of class "list"
~~
X
:Object of class "ANY"
~~
fct
:Object of class "function"
~~
dfct
:Object of class "functionORNULL"
~~
vcov
:Object of class "character"
~~
theta0
:Object of class "numeric"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "character"
~~
momNames
:Object of class "character"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
varNames
:Object of class "character"
~~
isEndo
:Object of class "logical"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "functionModel"
, directly.
Class "rmomentModel"
, directly.
Class "allNLModel"
, by class "functionModel", distance 2.
Class "momentModel"
, by class "functionModel", distance 2.
signature(x = "rfunctionModel", i = "numeric", j = "missing")
: ...
signature(object = "rfunctionModel")
: ...
signature(object = "rfunctionModel")
: ...
signature(object = "rfunctionModel")
: ...
signature(object = "rfunctionModel")
: ...
signature(x = "rfunctionModel")
: ...
signature(object = "rfunctionModel")
: ...
showClass("rfunctionModel")
showClass("rfunctionModel")
Functions that returns the GEL function
and its derivatives.
rhoET(gmat, lambda, derive = 0, k = 1) rhoETEL(gmat, lambda, derive = 0, k = 1) rhoEL(gmat, lambda, derive = 0, k = 1) rhoEEL(gmat, lambda, derive = 0, k = 1) rhoREEL(gmat, lambda, derive = 0, k = 1) rhoHD(gmat, lambda, derive = 0, k = 1) rhoETHD(gmat, lambda, derive = 0, k = 1)
rhoET(gmat, lambda, derive = 0, k = 1) rhoETEL(gmat, lambda, derive = 0, k = 1) rhoEL(gmat, lambda, derive = 0, k = 1) rhoEEL(gmat, lambda, derive = 0, k = 1) rhoREEL(gmat, lambda, derive = 0, k = 1) rhoHD(gmat, lambda, derive = 0, k = 1) rhoETHD(gmat, lambda, derive = 0, k = 1)
gmat |
The |
lambda |
The |
derive |
An integer which indicates which derivative to return |
k |
A numeric scaling factor that is required when |
It returns the vector when
derive=0
,
when
derive=1
and when
derive=2
.
Anatolyev, S. (2005), GMM, GEL, Serial Correlation, and Asymptotic Bias. Econometrica, 73, 983-1002.
Kitamura, Yuichi (1997), Empirical Likelihood Methods With Weakly Dependent Processes. The Annals of Statistics, 25, 2084-2102.
Kitamura, Y. and Otsu, T. and Evdokimov, K. (2013), Robustness, Infinitesimal Neighborhoods and Moment Restrictions. Econometrica, 81, 1185-1201.
Newey, W.K. and Smith, R.J. (2004), Higher Order Properties of GMM and Generalized Empirical Likelihood Estimators. Econometrica, 72, 219-255.
Smith, R.J. (2011), GEL Criteria for Moment Condition Models. Econometric Theory, 27(6), 1192–1235.
"rlinearModel"
A class for restricted moment-based models for which moment conditions are orthogonality conditions between instruments and the residuals from a linear regression.
Objects can be created by calls of the form new("rlinearModel", ...)
.
It is created by restModel-methods
.
cstLHS
:Object of class "matrix"
~~
cstRHS
:Object of class "numeric"
~~
cstSpec
:Object of class "list"
~~
modelF
:Object of class "data.frame"
~~
instF
:Object of class "data.frame"
~~
vcov
:Object of class "character"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "character"
~~
momNames
:Object of class "character"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
varNames
:Object of class "character"
~~
isEndo
:Object of class "logical"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "linearModel"
, directly.
Class "rmomentModel"
, directly.
Class "regModel"
, by class "linearModel", distance 2.
Class "momentModel"
, by class "linearModel", distance 2.
signature(object = "rlinearModel")
: ...
signature(object = "rlinearModel")
: ...
signature(model = "rlinearModel")
: ...
signature(object = "rlinearModel")
: ...
signature(object = "rlinearModel")
: ...
signature(object = "rlinearModel")
: ...
signature(object = "rlinearModel")
: ...
signature(x = "rlinearModel")
: ...
signature(object = "rlinearModel")
: ...
showClass("rlinearModel")
showClass("rlinearModel")
"rmomentModel"
A union class for all restricted moment-based models.
A virtual Class: No objects may be created from it.
signature(model = "rmomentModel")
: ...
showClass("rmomentModel")
showClass("rmomentModel")
"rnonlinearModel"
A class for restricted moment-based models for which moment conditions are orthogonality conditions between instruments and the residuals from a nonlinear regression.
Objects can be created by calls of the form new("rnonlinearModel",
...)
.
It is created by restModel-methods
.
R
:Object of class "list"
~~
cstSpec
:Object of class "list"
~~
modelF
:Object of class "data.frame"
~~
instF
:Object of class "data.frame"
~~
vcov
:Object of class "character"
~~
theta0
:Object of class "numeric"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "character"
~~
momNames
:Object of class "character"
~~
fRHS
:Object of class "expression"
~~
fLHS
:Object of class "expressionORNULL"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
varNames
:Object of class "character"
~~
isEndo
:Object of class "logical"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "nonlinearModel"
, directly.
Class "rmomentModel"
, directly.
Class "regModel"
, by class "nonlinearModel", distance 2.
Class "allNLModel"
, by class "nonlinearModel", distance 2.
Class "momentModel"
, by class "nonlinearModel", distance 2.
signature(object = "rnonlinearModel")
: ...
signature(object = "rnonlinearModel")
: ...
signature(object = "rnonlinearModel")
: ...
signature(model = "rnonlinearModel")
: ...
signature(object = "rnonlinearModel")
: ...
signature(x = "rnonlinearModel")
: ...
signature(object = "rnonlinearModel")
: ...
showClass("rnonlinearModel")
showClass("rnonlinearModel")
"rslinearModel"
A class for restricted system of linear equations.
Objects can be created by calls of the form new("rslinearModel",
...)
.
It is created by restModel-methods
.
cstLHS
:Object of class "matrix"
~~
cstRHS
:Object of class "numeric"
~~
cstSpec
:Object of class "list"
~~
modelT
:Object of class "list"
~~
instT
:Object of class "list"
~~
data
:Object of class "data.frame"
~~
vcov
:Object of class "character"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "list"
~~
momNames
:Object of class "list"
~~
eqnNames
:Object of class "character"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
sameMom
:Object of class "logical"
~~
SUR
:Object of class "logical"
~~
varNames
:Object of class "list"
~~
isEndo
:Object of class "list"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "slinearModel"
, directly.
Class "rsysModel"
, directly.
Class "sysModel"
, by class "slinearModel", distance 2.
signature(x = "rslinearModel", i = "numeric", j = "missing")
: ...
signature(object = "rslinearModel")
: ...
signature(object = "rslinearModel")
: ...
signature(object = "rslinearModel")
: ...
signature(object = "rslinearModel")
: ...
signature(object = "rslinearModel")
: ...
signature(model = "rslinearModel")
: ...
signature(object = "rslinearModel")
: ...
signature(object = "rslinearModel")
: ...
signature(object = "rslinearModel")
: ...
signature(x = "rslinearModel")
: ...
signature(object = "rslinearModel")
: ...
signature(object = "rslinearModel")
: ...
signature(object = "rslinearModel", wObj = "sysMomentWeights")
: ...
signature(model = "rslinearModel")
: ...
showClass("rslinearModel")
showClass("rslinearModel")
"rsnonlinearModel"
A class for restricted systems of nonlinear equations.
Objects can be created by calls of the form new("rsnonlinearModel", ...)
.
It is created by restModel-methods
.
R
:Object of class "list"
~~
cstSpec
:Object of class "list"
~~
data
:Object of class "data.frame"
~~
instT
:Object of class "list"
~~
vcov
:Object of class "character"
~~
theta0
:Object of class "list"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "list"
~~
momNames
:Object of class "list"
~~
fRHS
:Object of class "list"
~~
fLHS
:Object of class "list"
~~
eqnNames
:Object of class "character"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
sameMom
:Object of class "logical"
~~
SUR
:Object of class "logical"
~~
varNames
:Object of class "list"
~~
isEndo
:Object of class "list"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "snonlinearModel"
, directly.
Class "rsysModel"
, directly.
Class "sysModel"
, by class "snonlinearModel", distance 2.
No methods defined with class "rsnonlinearModel" in the signature.
showClass("rsnonlinearModel")
showClass("rsnonlinearModel")
"rsysModel"
A union class for all systems of equations. (see link{systemGmm}
)
A virtual Class: No objects may be created from it.
No methods defined with class "rsysModel" in the signature.
showClass("rsysModel")
showClass("rsysModel")
setCoef
in Package momentfit ~~The method validates the coefficient theta and returns a coefficient object in a format that satisfies the moment model.
## S4 method for signature 'momentModel' setCoef(model, theta) ## S4 method for signature 'sysModel' setCoef(model, theta)
## S4 method for signature 'momentModel' setCoef(model, theta) ## S4 method for signature 'sysModel' setCoef(model, theta)
model |
A moment model object. |
theta |
A coefficient object. The type depends on the model object. See the examples below. |
signature(object = "momentModel")
Methods for all single equation models including the restricted ones.
signature(object = "sysModel")
Methods for all system of equations models including the restricted ones.
### A few system of equation models: data(simData) h <- list(~z1+z2+z3, ~x3+z1+z2+z3+z4, ~x3+x4+z1+z2+z3) nlg <- list(Supply=y1~theta0+theta1*x1+theta2*z2, Demand1=y2~alpha0+alpha1*x1+alpha2*x2+alpha3*x3, Demand2=y3~beta0+beta1*x3+beta2*x4+beta3*z1) g <- list(Supply=y1~x1+z2, Demand1=y2~x1+x2+x3, Demand2=y3~x3+x4+z1) theta0 <- list(c(theta0=1,theta1=2,theta2=3), c(alpha0=1,alpha1=2,alpha2=3, alpha3=4), c(beta0=1,beta1=2,beta2=3,beta3=4)) nlin <- sysMomentModel(nlg, h, theta0, data=simData) lin <- sysMomentModel(g, h, data=simData) ### from numeric vector to the proper format with names: setCoef(nlin, 1:11) ### reorder the equation and name the coefficients setCoef(nlin, list(Demand1=1:4, Supply=1:3, Demand2=1:4)) ### reorder the coefficint to match the order in the model tet <- do.call("c", theta0) set.seed(112233) setCoef(nlin, tet[sample(11)]) ### It validates length and names and provide source of errors ## Not run: setCoef(nlin, list(Demand1=1:4, Supply=1:2, Demand2=1:4)) names(tet)[4] <- "gamma3" setCoef(nlin, tet) setCoef(nlin, list(Demand1=1:4, Supply=1:3, Demand4=1:4)) ## End(Not run) ### a single equation model single <- momentModel(nlg[[1]], h[[1]], theta0[[1]], data=simData) setCoef(single, c(theta1=4, theta0=6, theta2=8)) setCoef(single, 1:3)
### A few system of equation models: data(simData) h <- list(~z1+z2+z3, ~x3+z1+z2+z3+z4, ~x3+x4+z1+z2+z3) nlg <- list(Supply=y1~theta0+theta1*x1+theta2*z2, Demand1=y2~alpha0+alpha1*x1+alpha2*x2+alpha3*x3, Demand2=y3~beta0+beta1*x3+beta2*x4+beta3*z1) g <- list(Supply=y1~x1+z2, Demand1=y2~x1+x2+x3, Demand2=y3~x3+x4+z1) theta0 <- list(c(theta0=1,theta1=2,theta2=3), c(alpha0=1,alpha1=2,alpha2=3, alpha3=4), c(beta0=1,beta1=2,beta2=3,beta3=4)) nlin <- sysMomentModel(nlg, h, theta0, data=simData) lin <- sysMomentModel(g, h, data=simData) ### from numeric vector to the proper format with names: setCoef(nlin, 1:11) ### reorder the equation and name the coefficients setCoef(nlin, list(Demand1=1:4, Supply=1:3, Demand2=1:4)) ### reorder the coefficint to match the order in the model tet <- do.call("c", theta0) set.seed(112233) setCoef(nlin, tet[sample(11)]) ### It validates length and names and provide source of errors ## Not run: setCoef(nlin, list(Demand1=1:4, Supply=1:2, Demand2=1:4)) names(tet)[4] <- "gamma3" setCoef(nlin, tet) setCoef(nlin, list(Demand1=1:4, Supply=1:3, Demand4=1:4)) ## End(Not run) ### a single equation model single <- momentModel(nlg[[1]], h[[1]], theta0[[1]], data=simData) setCoef(single, c(theta1=4, theta0=6, theta2=8)) setCoef(single, 1:3)
"sfunctionModel"
A class for systems of nonlinear equations.
Objects can be created by calls of the form new("sfunctionModel", ...)
.
It is created by momentModel
.
X
:Object of class "ANY"
~~
fct
:Object of class "list"
~~
dfct
:Object of class "list"
~~
vcov
:Object of class "character"
~~
theta0
:Object of class "list"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "list"
~~
momNames
:Object of class "list"
~~
eqnNames
:Object of class "character"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
sameMom
:Object of class "logical"
~~
SUR
:Object of class "logical"
~~
varNames
:Object of class "list"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "sysModel"
, directly.
showClass("sfunctionModel")
showClass("sfunctionModel")
"sgmmfit"
Class to store fitted system of equations obtained using the GMM method.
Objects can be created by calls of the form new("sgmmfit", ...)
.
It is created by gmmFit
.
theta
:Object of class "list"
~~
convergence
:Object of class "numericORNULL"
~~
convIter
:Object of class "numericORNULL"
~~
call
:Object of class "callORNULL"
~~
type
:Object of class "character"
~~
wObj
:Object of class "sysMomentWeights"
~~
niter
:Object of class "integer"
~~
efficientGmm
:Object of class "logical"
~~
model
:Object of class "sysModel"
~~
signature(x = "sgmmfit")
: ...
signature(object = "sgmmfit")
: ...
signature(object.u = "missing", object.r = "sgmmfit")
: ...
signature(object.u = "sgmmfit", object.r = "missing")
: ...
signature(object.u = "sgmmfit", object.r = "sgmmfit")
: ...
signature(object = "sgmmfit")
: ...
signature(x = "sgmmfit")
: ...
signature(object = "sgmmfit")
: ...
signature(object = "sgmmfit")
: ...
signature(object = "sgmmfit", which = "missing")
: ...
signature(object = "sgmmfit")
: ...
signature(object = "sgmmfit")
: ...
showClass("sgmmfit")
showClass("sgmmfit")
show
in Package methods ~~Display method for all objects.
signature(object = "ANY")
signature(object = "confint")
signature(object = "mconfint")
signature(object = "sSpec")
signature(object = "momentModel")
signature(object = "sysModel")
signature(object = "gmmfit")
signature(object = "gelfit")
signature(object = "sgmmfit")
signature(object = "specTest")
signature(object = "summarySysGmm")
signature(object = "summaryGmm")
signature(object = "summaryGel")
signature(object = "hypothesisTest")
signature(object = "momentWeights")
signature(object = "sysMomentWeights")
This dataset is used in several documentation files to illustrate the different functionality of the package.
data("simData")
data("simData")
A data frame with 50 observations on the following 12 variables. See the examples for the method used to generate them.
y
a numeric vector
y1
a numeric vector
y3
a numeric vector
y2
a numeric vector
z1
a numeric vector
x1
a numeric vector
z2
a numeric vector
x2
a numeric vector
z3
a numeric vector
x3
a numeric vector
x4
a numeric vector
z4
a numeric vector
z5
a numeric vector
# Here is how the data was simulated set.seed(1122) n <- 50 x1 <- rchisq(n,5) x2 <- rchisq(n,5) x3 <- rnorm(n) x4 <- rnorm(n) z1 <- .2*x1+rnorm(n) z2 <- .2*x2+rnorm(n) z3 <- rnorm(n) z4 <- rnorm(n) z5 <- rnorm(n) y <- y1 <- x1+rnorm(n) y2 <- 2*x1+rnorm(n) y3 <- 0.5*x2+rnorm(n) simData <- data.frame(y=y, y1=y1,y3=y3,y2=y2, z1=z1,x1=x1,z2=z2,x2=x2,z3=z3,x3=x3, x4=x4,z4=z4,z5=z5)
# Here is how the data was simulated set.seed(1122) n <- 50 x1 <- rchisq(n,5) x2 <- rchisq(n,5) x3 <- rnorm(n) x4 <- rnorm(n) z1 <- .2*x1+rnorm(n) z2 <- .2*x2+rnorm(n) z3 <- rnorm(n) z4 <- rnorm(n) z5 <- rnorm(n) y <- y1 <- x1+rnorm(n) y2 <- 2*x1+rnorm(n) y3 <- 0.5*x2+rnorm(n) simData <- data.frame(y=y, y1=y1,y3=y3,y2=y2, z1=z1,x1=x1,z2=z2,x2=x2,z3=z3,x3=x3, x4=x4,z4=z4,z5=z5)
"slinearModel"
A class for systems of linear equations.
Objects can be created by calls of the form new("slinearModel", ...)
.
It is created by momentModel
.
modelT
:Object of class "list"
~~
instT
:Object of class "list"
~~
data
:Object of class "data.frame"
~~
vcov
:Object of class "character"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "list"
~~
momNames
:Object of class "list"
~~
eqnNames
:Object of class "character"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
sameMom
:Object of class "logical"
~~
SUR
:Object of class "logical"
~~
varNames
:Object of class "list"
~~
isEndo
:Object of class "list"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "sysModel"
, directly.
signature(x = "slinearModel", i = "numeric", j = "missing")
: ...
signature(x = "slinearModel", y = "linearModel")
: ...
signature(object = "slinearModel")
: ...
signature(object = "slinearModel")
: ...
signature(object = "slinearModel")
: ...
signature(object = "slinearModel")
: ...
signature(object = "slinearModel", wObj = "sysMomentWeights")
: ...
signature(model = "slinearModel")
: ...
signature(model = "slinearModel")
: ...
showClass("slinearModel")
showClass("slinearModel")
"snonlinearModel"
A class for systems of nonlinear equations.
Objects can be created by calls of the form new("snonlinearModel", ...)
.
It is created by momentModel
.
data
:Object of class "data.frame"
~~
instT
:Object of class "list"
~~
vcov
:Object of class "character"
~~
theta0
:Object of class "list"
~~
n
:Object of class "integer"
~~
q
:Object of class "integer"
~~
k
:Object of class "integer"
~~
parNames
:Object of class "list"
~~
momNames
:Object of class "list"
~~
fRHS
:Object of class "list"
~~
fLHS
:Object of class "list"
~~
eqnNames
:Object of class "character"
~~
vcovOptions
:Object of class "list"
~~
centeredVcov
:Object of class "logical"
~~
sameMom
:Object of class "logical"
~~
SUR
:Object of class "logical"
~~
varNames
:Object of class "list"
~~
isEndo
:Object of class "list"
~~
omit
:Object of class "integer"
~~
survOptions
:Object of class "list"
~~
sSpec
:Object of class "sSpec"
~~
smooth
:Object of class "logical"
~~
Class "sysModel"
, directly.
signature(x = "snonlinearModel", i = "numeric", j = "missing")
: ...
signature(x = "snonlinearModel", y = "nonlinearModel")
: ...
signature(object = "snonlinearModel")
: ...
signature(object = "snonlinearModel")
: ...
signature(object = "snonlinearModel", wObj = "sysMomentWeights")
: ...
showClass("snonlinearModel")
showClass("snonlinearModel")
solveGel
in Package momentfit ~~It fits a moment-based model using GEL methods.
## S4 method for signature 'momentModel' solveGel(object, gelType="EL", theta0=NULL, lambda0=NULL, lamSlv=NULL, coefSlv=c("optim","nlminb","constrOptim"), rhoFct=NULL, lControl=list(), tControl=list())
## S4 method for signature 'momentModel' solveGel(object, gelType="EL", theta0=NULL, lambda0=NULL, lamSlv=NULL, coefSlv=c("optim","nlminb","constrOptim"), rhoFct=NULL, lControl=list(), tControl=list())
object |
An object of class |
gelType |
The type of GEL. It is either |
theta0 |
The vector of coefficients for the starting values used
in minimization algorithm. If NULL, the starting values in the object is
used. For linear models, it must be provided because
|
lambda0 |
The |
lamSlv |
An alternative solver for the Lagrange multiplier. By
default, either |
coefSlv |
Minimization solver for the coefficient vector. |
rhoFct |
An alternative objective function for GEL. This argument
is only used if we want to fit the model with a different GEL
method. see |
lControl |
A list of controls for the Lagrange multiplier algorithm. |
tControl |
A list of controls for the coefficient algorithm. |
A list with the following:
theta |
The vector of solution |
lambda |
The vector of Lagrange multiplier |
lconvergence |
convergence code for the Lagrange multiplier. 0 means normal convergence. |
convergence |
convergence code for the coefficients. 0 means
normal convergence. For higher numbers, see |
signature(object = "momentModel")
The method applies to all GEL classes.
data(simData) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## Get a good starting value theta0 <- gmmFit(model1)@theta ## EL by default, with Wu algorithm res2 <- solveGel(model1, theta0=theta0) ## Change solver parameters res3 <- solveGel(model1, theta0=theta0, tControl=list(method="Nelder", control=list(maxit=2000)))
data(simData) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## Get a good starting value theta0 <- gmmFit(model1)@theta ## EL by default, with Wu algorithm res2 <- solveGel(model1, theta0=theta0) ## Change solver parameters res3 <- solveGel(model1, theta0=theta0, tControl=list(method="Nelder", control=list(maxit=2000)))
solveGmm
in Package momentfit ~~The main function to get the GMM solution for a given weighting matrix.
## S4 method for signature 'linearModel,momentWeights' solveGmm(object, wObj, theta0=NULL, ...) ## S4 method for signature 'allNLModel,momentWeights' solveGmm(object, wObj, theta0=NULL, algo=c("optim","nlminb"), ...) ## S4 method for signature 'rnonlinearModel,momentWeights' solveGmm(object, wObj, theta0=NULL, ...) ## S4 method for signature 'slinearModel,sysMomentWeights' solveGmm(object, wObj, theta0=NULL) ## S4 method for signature 'rslinearModel,sysMomentWeights' solveGmm(object, wObj, theta0=NULL) ## S4 method for signature 'snonlinearModel,sysMomentWeights' solveGmm(object, wObj, theta0=NULL, ...) ## S4 method for signature 'sfunctionModel,sysMomentWeights' solveGmm(object, wObj, theta0=NULL, ...)
## S4 method for signature 'linearModel,momentWeights' solveGmm(object, wObj, theta0=NULL, ...) ## S4 method for signature 'allNLModel,momentWeights' solveGmm(object, wObj, theta0=NULL, algo=c("optim","nlminb"), ...) ## S4 method for signature 'rnonlinearModel,momentWeights' solveGmm(object, wObj, theta0=NULL, ...) ## S4 method for signature 'slinearModel,sysMomentWeights' solveGmm(object, wObj, theta0=NULL) ## S4 method for signature 'rslinearModel,sysMomentWeights' solveGmm(object, wObj, theta0=NULL) ## S4 method for signature 'snonlinearModel,sysMomentWeights' solveGmm(object, wObj, theta0=NULL, ...) ## S4 method for signature 'sfunctionModel,sysMomentWeights' solveGmm(object, wObj, theta0=NULL, ...)
object |
A moment-based model |
theta0 |
The vector of coefficients for the starting values used
in |
wObj |
An object of class |
algo |
The numerical algorithm to minimize the objective function. |
... |
Arguments to pass to |
A list with the following:
theta |
The vector of solution |
convergence |
convergence code. 0 means normal convergence. For
higher numbers, see |
signature(object = "allNLMoment", wObj = "momentWeights")
Method to solve either nonlinear regressions or models in which moments are computed with a function. The objective is minimized using optim.
signature(object = "rnonlinearModel", wObj = "momentWeights")
Method to solve restricted nonlinear models. It computes the analytical solution.
signature(object = "linearModel", wObj = "momentWeights")
Method to solve linear models. It computes the analytical solution.
signature(object = "slinearModel", wObj = "sysMomentWeights")
Method to solve system of linear models. It computes the analytical solution.
signature(object = "rslinearModel", wObj = "sysMomentWeights")
Method to solve system of linear models in which restrictions have been imposed on the coefficients. It computes the analytical solution.
signature(object = "slinearModel", wObj = "sysMomentWeights")
Method to solve system of nonlinear models. The solution is obtained with optim using the analytical derivatives.
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## A manual two-step GMM w0 <- evalWeights(model1, w="ident") theta0 <- solveGmm(model1, w0)$theta w <- evalWeights(model1, theta0) theta1 <- solveGmm(model1, w)$theta
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## A manual two-step GMM w0 <- evalWeights(model1, w="ident") theta0 <- solveGmm(model1, w0)$theta w <- evalWeights(model1, theta0) theta1 <- solveGmm(model1, w)$theta
"specTest"
A class to store results from a specification test.
Objects can be created by calls of the form new("specTest", ...)
.
It is created my specTest-methods
.
test
:Object of class "matrix"
~~
testname
:Object of class "character"
~~
signature(x = "specTest")
: ...
signature(object = "specTest")
: ...
showClass("specTest")
showClass("specTest")
specTest
in Package momentfit ~~It computes tests of specification for GMM fit.
## S4 method for signature 'gmmfit,missing' specTest(object, which, df.adj=FALSE, wObj=NULL) ## S4 method for signature 'sgmmfit,missing' specTest(object, which, df.adj=FALSE, wObj=NULL) ## S4 method for signature 'gmmfit,numeric' specTest(object, which) ## S4 method for signature 'gelfit,missing' specTest(object, which, type = c("All", "LR", "LM", "J"))
## S4 method for signature 'gmmfit,missing' specTest(object, which, df.adj=FALSE, wObj=NULL) ## S4 method for signature 'sgmmfit,missing' specTest(object, which, df.adj=FALSE, wObj=NULL) ## S4 method for signature 'gmmfit,numeric' specTest(object, which) ## S4 method for signature 'gelfit,missing' specTest(object, which, type = c("All", "LR", "LM", "J"))
object |
GMM or GEL fit object |
which |
Which sub-moment conditions to test. |
df.adj |
Should we adjust the covariance matrix of the moment
conditions for degrees of freedom. If |
wObj |
An object of class |
type |
For GEL, three specification tests are available |
signature(object = "gmmfit", which="missing")
signature(object = "sgmmfit", which="missing")
signature(object = "gmmfit", which="numeric")
Eichenbaum, M. and Hansen L. and Singleton, K. (1985). A time Series Analysis of Representative Agent Models of Consumption and Leisure Choise under Uncertainty. Quarterly Journal of Economics, 103, 51–78.
Hayashi, F. (2000). Econometrics, New Jersey: Princeton University Press.
data(simData) model1 <- momentModel(y~x1, ~z1+z2, data=simData) res <- gmmFit(model1) specTest(res) ## Hayashi Example 3.3 (there is not result in the book but ## that's how we would do it for YEAR=1967 data(Griliches) dat <- subset(Griliches, YEAR==67) model <- momentModel(LW~S+EXPR+IQ, ~S+EXPR+AGE+MED, data=dat, vcov="MDS") res <- gmmFit(model) ## testing the orthogonality conditions of S specTest(res, 2)
data(simData) model1 <- momentModel(y~x1, ~z1+z2, data=simData) res <- gmmFit(model1) specTest(res) ## Hayashi Example 3.3 (there is not result in the book but ## that's how we would do it for YEAR=1967 data(Griliches) dat <- subset(Griliches, YEAR==67) model <- momentModel(LW~S+EXPR+IQ, ~S+EXPR+AGE+MED, data=dat, vcov="MDS") res <- gmmFit(model) ## testing the orthogonality conditions of S specTest(res, 2)
"sSpec"
A class to store the specifications of the kernel used to smooth moment conditions.
Objects can be created by calls of the form new("sSpec", ...)
.
It is created by kernapply-methods
.
k
:Object of class "numeric"
~~
kernel
:Object of class "character"
~~
bw
:Object of class "numeric"
~~
w
:Object of class "tskernel"
~~
bwMet
:Object of class "character"
~~
signature(x = "sSpec")
: ...
signature(object = "sSpec")
: ...
showClass("sSpec")
showClass("sSpec")
"stsls"
A class to store a fitted system of equations obtained using the two-stage least squares method.
Objects can be created by calls of the form new("stsls", ...)
.
It is created my tsls-methods
.
theta
:Object of class "list"
~~
convergence
:Object of class "numericORNULL"
~~
convIter
:Object of class "numericORNULL"
~~
call
:Object of class "callORNULL"
~~
type
:Object of class "character"
~~
wObj
:Object of class "sysMomentWeights"
~~
niter
:Object of class "integer"
~~
efficientGmm
:Object of class "logical"
~~
model
:Object of class "sysModel"
~~
Class "sgmmfit"
, directly.
No methods defined with class "stsls" in the signature.
showClass("stsls")
showClass("stsls")
summary
in Package base ~~Compute several results from a moment based model fit.
## S4 method for signature 'gmmfit' summary(object, testStrength=TRUE, ...) ## S4 method for signature 'gelfit' summary(object, ...) ## S4 method for signature 'sgmmfit' summary(object, testStrength=TRUE, ...)
## S4 method for signature 'gmmfit' summary(object, testStrength=TRUE, ...) ## S4 method for signature 'gelfit' summary(object, ...) ## S4 method for signature 'sgmmfit' summary(object, testStrength=TRUE, ...)
object |
A fit object from the package (GMM and GEL are the only methods for now) |
testStrength |
Should the first stage F-statistics be computed? |
... |
Other arguments to pass to |
signature(object = "gmmfit")
signature(object = "gmmfit")
signature(object = "sgmmfit")
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) res <- gmmFit(model1) summary(res) ## Fixed and True Weights matrix ## Consider the moment of a normal distribution: ## Using the first three non centered moments g <- function(theta, x) { mu <- theta[1] sig2 <- theta[2] m1 <- x-mu m2 <- x^2-mu^2-sig2 m3 <- x^3-mu^3-3*mu*sig2 cbind(m1,m2,m3) } dg <- function(theta, x) { mu <- theta[1] sig2 <- theta[2] G <- matrix(c(-1,-2*mu,-3*mu^2-3*sig2, 0, -1, -3*mu),3,2) } x <- simData$x3 model <- momentModel(g, x, c(mu=.1, sig2=1.5), vcov="iid") res1 <- gmmFit(model) summary(res1) ## Same results (that's because the moment vcov is centered by default) W <- solve(var(cbind(x,x^2,x^3))) res2 <- gmmFit(model, weights=W) res2 ## If is therefore more efficient in this case to do the following: summary(res2, breadOnly=TRUE)
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) res <- gmmFit(model1) summary(res) ## Fixed and True Weights matrix ## Consider the moment of a normal distribution: ## Using the first three non centered moments g <- function(theta, x) { mu <- theta[1] sig2 <- theta[2] m1 <- x-mu m2 <- x^2-mu^2-sig2 m3 <- x^3-mu^3-3*mu*sig2 cbind(m1,m2,m3) } dg <- function(theta, x) { mu <- theta[1] sig2 <- theta[2] G <- matrix(c(-1,-2*mu,-3*mu^2-3*sig2, 0, -1, -3*mu),3,2) } x <- simData$x3 model <- momentModel(g, x, c(mu=.1, sig2=1.5), vcov="iid") res1 <- gmmFit(model) summary(res1) ## Same results (that's because the moment vcov is centered by default) W <- solve(var(cbind(x,x^2,x^3))) res2 <- gmmFit(model, weights=W) res2 ## If is therefore more efficient in this case to do the following: summary(res2, breadOnly=TRUE)
"summaryGel"
Class to store the summary of a model fitted by GEL.
Objects can be created by calls of the form new("summaryGel", ...)
.
It is created by link{summary-methods}
.
coef
:Object of class "matrix"
~~
specTest
:Object of class "specTest"
~~
model
:Object of class "momentModel"
~~
lambda
:Object of class "matrix"
~~
convergence
:Object of class "numeric"
~~
lconvergence
:Object of class "numeric"
~~
impProb
:Object of class "list"
~~
gelType
:Object of class "list"
~~
restrictedLam
:Object of class "integer"
~~
signature(x = "summaryGel")
: ...
signature(object = "summaryGel")
: ...
showClass("summaryGel")
showClass("summaryGel")
"summaryGmm"
A class to store the summary of a model fitted by GMM.
Objects can be created by calls of the form new("summaryGmm", ...)
.
It is created by link{summary-methods}
.
coef
:Object of class "matrix"
~~
specTest
:Object of class "specTest"
~~
strength
:Object of class "list"
~~
model
:Object of class "momentModel"
~~
sandwich
:Object of class "logical"
~~
type
:Object of class "character"
~~
convergence
:Object of class "numericORNULL"
~~
convIter
:Object of class "numericORNULL"
~~
wSpec
:Object of class "list"
~~
niter
:Object of class "integer"
~~
df.adj
:Object of class "logical"
~~
breadOnly
:Object of class "logical"
~~
signature(x = "summaryGmm")
: ...
signature(object = "summaryGmm")
: ...
showClass("summaryGmm")
showClass("summaryGmm")
"summarySysGmm"
A class to store the summary of a system of equations fitted by GMM.
Objects can be created by calls of the form new("summarySysGmm", ...)
.
It is created by summary-methods
.
coef
:Object of class "list"
~~
specTest
:Object of class "specTest"
~~
strength
:Object of class "list"
~~
model
:Object of class "sysModel"
~~
sandwich
:Object of class "logical"
~~
type
:Object of class "character"
~~
convergence
:Object of class "numericORNULL"
~~
convIter
:Object of class "numericORNULL"
~~
wSpec
:Object of class "list"
~~
niter
:Object of class "integer"
~~
df.adj
:Object of class "logical"
~~
breadOnly
:Object of class "logical"
~~
signature(x = "summarySysGmm")
: ...
signature(object = "summarySysGmm")
: ...
showClass("summarySysGmm")
showClass("summarySysGmm")
"sysModel"
A union class for all systems of equations.
A virtual Class: No objects may be created from it.
signature(x = "sysModel", i = "missing", j = "list")
: ...
signature(x = "sysModel", i = "missing", j = "missing")
: ...
signature(x = "sysModel", i = "numeric", j = "list")
: ...
signature(object = "sysModel")
: ...
signature(object = "sysModel")
: ...
signature(object = "sysModel", theta = "list", wObj = "sysMomentWeights")
: ...
signature(object = "sysModel")
: ...
signature(object = "sysModel")
: ...
signature(object = "sysModel")
: ...
signature(model = "sysModel")
: ...
signature(x = "sysModel")
: ...
signature(object = "sysModel")
: ...
signature(object = "sysModel")
: ...
signature(x = "sysModel")
: ...
signature(object = "sysModel")
: ...
showClass("sysModel")
showClass("sysModel")
"sysMomentModel"
classesIt builds the object of either class "slinearModel"
or
"snonlinearModel"
, which are system of equations based on moment
conditions.
sysMomentModel(g, h=NULL, theta0=NULL, grad=NULL, vcov = c("iid", "HAC", "MDS", "CL"), vcovOptions=list(), centeredVcov = TRUE, data=parent.frame(),na.action="na.omit", survOptions=list())
sysMomentModel(g, h=NULL, theta0=NULL, grad=NULL, vcov = c("iid", "HAC", "MDS", "CL"), vcovOptions=list(), centeredVcov = TRUE, data=parent.frame(),na.action="na.omit", survOptions=list())
g |
A list of linear or nonlinear regression formulas for each equation in the system. |
h |
A list of linear formulas for the instruments in each equation in the system. |
theta0 |
A list of vectors of starting values. It is required only when the equations are nonlinear, in which case, it must be a list of named vector, with the names corresponding to the coefficient names in the regression formulas. |
grad |
A list of functions that returns the derivative of the
moment functions. Only used if |
vcov |
Assumption on the properties of the moment conditions. By
default, they are weakly dependant processes. For |
vcovOptions |
A list of options for the covariance matrix of the
moment conditions. See |
centeredVcov |
Should the moment function be centered when computing its covariance matrix. Doing so may improve inference. |
data |
A data.frame or a matrix with column names (Optional). |
na.action |
Action to take for missing values. If missing values
are present and the option is set to |
survOptions |
If needed, a list with the type of survey weights and
the weights as a numeric vector, data.frame or formula. The type is either
|
'sysMomentModel' returns an object of one of the subclasses of
"sysMomentModel"
.
Hayashi, F. (2000). Econometrics, New Jersey: Princeton University Press.
Andrews DWK (1991), Heteroskedasticity and Autocorrelation Consistent Covariance Matrix Estimation. Econometrica, 59, 817–858.
Newey WK & West KD (1987), A Simple, Positive Semi-Definite, Heteroskedasticity and Autocorrelation Consistent Covariance Matrix. Econometrica, 55, 703–708.
Newey WK & West KD (1994), Automatic Lag Selection in Covariance Matrix Estimation. Review of Economic Studies, 61, 631-653.
set.seed(1122) x1 <- rchisq(50,5) x2 <- rchisq(50,5) x3 <- rnorm(50) x4 <- rnorm(50) z1 <- .2*x1+rnorm(50) z2 <- .2*x2+rnorm(50) z3 <- rnorm(50) z4 <- rnorm(50) z5 <- rnorm(50) y1 <- x1+rnorm(50) y2 <- 2*x1+rnorm(50) y3 <- 0.5*x2+rnorm(50) dat <- data.frame(y1=y1,y3=y3,y2=y2, z1=z1,x1=x1,z2=z2,x2=x2,z3=z3,x3=x3, x4=x4,z4=z4,z5=z5) g1 <- y1~x1+x4; h1 <- ~z1+z2+z3+z4+x4 g2 <- y2~x1+x2+x3; h2 <- ~z1+z2+z3+z4+x3 g3 <- y3~x2+x3+x4; h3 <- ~z2+z3+z4+x3+x4 g <- list(g1,g2,g3) h <- list(h1,h2,h3) smodel <- sysMomentModel(g, h, data=dat) ## not really nonlinear nlg <- list(y1~theta0+theta1*x1+theta2*x4, y2~alpha0+alpha1*x1+alpha2*x2+alpha3*x3, y3~beta0+beta1*x2+beta2*x3+beta3*x4) theta0 <- list(c(theta0=1,theta1=2,theta2=3), c(alpha0=1,alpha1=2,alpha2=3, alpha3=4), c(beta0=1,beta1=2,beta2=3,beta3=4)) snmodel <- sysMomentModel(nlg, h, theta0, data=dat)
set.seed(1122) x1 <- rchisq(50,5) x2 <- rchisq(50,5) x3 <- rnorm(50) x4 <- rnorm(50) z1 <- .2*x1+rnorm(50) z2 <- .2*x2+rnorm(50) z3 <- rnorm(50) z4 <- rnorm(50) z5 <- rnorm(50) y1 <- x1+rnorm(50) y2 <- 2*x1+rnorm(50) y3 <- 0.5*x2+rnorm(50) dat <- data.frame(y1=y1,y3=y3,y2=y2, z1=z1,x1=x1,z2=z2,x2=x2,z3=z3,x3=x3, x4=x4,z4=z4,z5=z5) g1 <- y1~x1+x4; h1 <- ~z1+z2+z3+z4+x4 g2 <- y2~x1+x2+x3; h2 <- ~z1+z2+z3+z4+x3 g3 <- y3~x2+x3+x4; h3 <- ~z2+z3+z4+x3+x4 g <- list(g1,g2,g3) h <- list(h1,h2,h3) smodel <- sysMomentModel(g, h, data=dat) ## not really nonlinear nlg <- list(y1~theta0+theta1*x1+theta2*x4, y2~alpha0+alpha1*x1+alpha2*x2+alpha3*x3, y3~beta0+beta1*x2+beta2*x3+beta3*x4) theta0 <- list(c(theta0=1,theta1=2,theta2=3), c(alpha0=1,alpha1=2,alpha2=3, alpha3=4), c(beta0=1,beta1=2,beta2=3,beta3=4)) snmodel <- sysMomentModel(nlg, h, theta0, data=dat)
"sysMomentWeights"
A class to store the weighting matrix of the moment conditions from a system of equations.
Objects can be created by calls of the form new("sysMomentWeights", ...)
.
It is created by the evalWeights
method.
w
:Object of class "ANY"
~~
type
:Object of class "character"
~~
wSpec
:Object of class "list"
~~
Sigma
:Object of class "ANY"
~~
momNames
:Object of class "list"
~~
eqnNames
:Object of class "character"
~~
sameMom
:Object of class "logical"
~~
signature(x = "sysMomentWeights", i = "missing", j = "list")
: ...
signature(x = "sysMomentWeights", i = "numeric", j = "list")
: ...
signature(x = "sysMomentWeights", i = "numeric", j = "missing")
: ...
signature(object = "sysModel", theta = "list", wObj = "sysMomentWeights")
: ...
signature(x = "sysMomentWeights")
: ...
signature(w = "sysMomentWeights", x = "matrixORnumeric", y = "matrixORnumeric")
: ...
signature(w = "sysMomentWeights", x = "matrixORnumeric", y = "missing")
: ...
signature(w = "sysMomentWeights", x = "missing", y = "missing")
: ...
signature(object = "sysMomentWeights")
: ...
signature(object = "rslinearModel", wObj = "sysMomentWeights")
: ...
signature(object = "slinearModel", wObj = "sysMomentWeights")
: ...
signature(object = "snonlinearModel", wObj = "sysMomentWeights")
: ...
showClass("sysMomentWeights")
showClass("sysMomentWeights")
This document is meant to describe how to create system of equations objects, estimating them and peforming hypothesis tests.
Instread of repeating the same example for each method, we are going through all methods and classes for systems of equations.
data(simData) ## first, we create an sysGmm object g1 <- y1~x1+x4; h1 <- ~x4+z1+z2+z3+z4 g2 <- y2~x1+x2+x3; h2 <- ~x3+z1+z2+z3+z4 g3 <- y3~x2+x3+x4; h3 <- ~x3+x4+z1+z2+z3+z4 g <- list(g1,g2,g3) h <- list(h1,h2,h3) smodel <- sysMomentModel(g, h, data=simData, vcov="MDS") ## The show or print method smodel ## The ']' method smodel[1:2] smodel[1] ## becomes a one equation model ## equation by equation 2SLS tsls(smodel) ## or manually lapply(1:3, function(i) coef(tsls(smodel[i]))) ## Fitting the model by two-step GMM res <- gmmFit(smodel) ## testing Overidentifying restrictions specTest(res) ## All info using the summary method ## which includes equation by equation measures of ## the instrument stengths summary(res) ### When the error id iid (homoscedastic), we have a ### FIVE estimator with 2SLS as the first step smodel <- sysMomentModel(g, h, data=simData, vcov="iid") gmmFit(smodel) ### When the error is iid (homoscedastic), ### all instruments are the same, and the first step is 2SLS, ### we have 3SLS smodel <- sysMomentModel(g, ~x4+z1+z2+z3+z4, data=simData, vcov="iid") gmmFit(smodel, initW='tsls') ### When the error is iid (homoscedastic), ### the instruments are the same and are the union of all regressors, ### we have SUR smodel <- sysMomentModel(g, NULL, data=simData, vcov="iid") gmmFit(smodel, initW='tsls') ############ Restricted models ################## ## unrestricted smodel <- sysMomentModel(g, h, data=simData, vcov="MDS") res <- gmmFit(smodel) ## no cross-equation restrictions R1 <- list(c("x1=-12*x4"), character(), c("x2=0.8", "x4=0.3")) rm1 <- restModel(smodel, R1) (res1 <- gmmFit(rm1)) ## Cross equation restrictions R2<- c("Eqn1.x1=1", "Eqn2.x1=Eqn3.x2") rm2 <- restModel(smodel, R2) (es2 <- gmmFit(rm2))## no longer expressed as a system ## testing the restriction hypothesisTest(res, res1, type="LR") hypothesisTest(res, res1, type="LM") hypothesisTest(res, res1, type="Wald")
data(simData) ## first, we create an sysGmm object g1 <- y1~x1+x4; h1 <- ~x4+z1+z2+z3+z4 g2 <- y2~x1+x2+x3; h2 <- ~x3+z1+z2+z3+z4 g3 <- y3~x2+x3+x4; h3 <- ~x3+x4+z1+z2+z3+z4 g <- list(g1,g2,g3) h <- list(h1,h2,h3) smodel <- sysMomentModel(g, h, data=simData, vcov="MDS") ## The show or print method smodel ## The ']' method smodel[1:2] smodel[1] ## becomes a one equation model ## equation by equation 2SLS tsls(smodel) ## or manually lapply(1:3, function(i) coef(tsls(smodel[i]))) ## Fitting the model by two-step GMM res <- gmmFit(smodel) ## testing Overidentifying restrictions specTest(res) ## All info using the summary method ## which includes equation by equation measures of ## the instrument stengths summary(res) ### When the error id iid (homoscedastic), we have a ### FIVE estimator with 2SLS as the first step smodel <- sysMomentModel(g, h, data=simData, vcov="iid") gmmFit(smodel) ### When the error is iid (homoscedastic), ### all instruments are the same, and the first step is 2SLS, ### we have 3SLS smodel <- sysMomentModel(g, ~x4+z1+z2+z3+z4, data=simData, vcov="iid") gmmFit(smodel, initW='tsls') ### When the error is iid (homoscedastic), ### the instruments are the same and are the union of all regressors, ### we have SUR smodel <- sysMomentModel(g, NULL, data=simData, vcov="iid") gmmFit(smodel, initW='tsls') ############ Restricted models ################## ## unrestricted smodel <- sysMomentModel(g, h, data=simData, vcov="MDS") res <- gmmFit(smodel) ## no cross-equation restrictions R1 <- list(c("x1=-12*x4"), character(), c("x2=0.8", "x4=0.3")) rm1 <- restModel(smodel, R1) (res1 <- gmmFit(rm1)) ## Cross equation restrictions R2<- c("Eqn1.x1=1", "Eqn2.x1=Eqn3.x2") rm2 <- restModel(smodel, R2) (es2 <- gmmFit(rm2))## no longer expressed as a system ## testing the restriction hypothesisTest(res, res1, type="LR") hypothesisTest(res, res1, type="LM") hypothesisTest(res, res1, type="Wald")
ThreeSLS
in Package momentfit ~~Method to estimate system of equations by Three-Stage least squares (3SLS) or, as a special case, by Seemingly Unrelatd Regressions (SUR).
## S4 method for signature 'slinearModel' ThreeSLS(model, coefOnly=FALSE, qrZ=NULL, Sigma=NULL) ## S4 method for signature 'rslinearModel' ThreeSLS(model, coefOnly=FALSE, qrZ=NULL, Sigma=NULL)
## S4 method for signature 'slinearModel' ThreeSLS(model, coefOnly=FALSE, qrZ=NULL, Sigma=NULL) ## S4 method for signature 'rslinearModel' ThreeSLS(model, coefOnly=FALSE, qrZ=NULL, Sigma=NULL)
model |
An object of class |
coefOnly |
Should the method return the only the coefficients or
create an object of class |
qrZ |
The qr decomposition of the common instruments. It is
mostly used by |
Sigma |
The covariance matrix of the residuals. If not provided, it is computed using the residuals of the equation by equation two-stage least squares. It should not be used directly unless the user knows what he is doing. |
signature(model = "slinearModel")
The method is specifically for system of linear models with the same instruments and homoscedastic errors. It becomes SUR as a special case when the instruments are the union of all regressors.
signature(model = "rslinearModel")
This method is for restricted models that does not impose cross-equation restrictions. With such restrictions 3SLS is not possible as we can no longer write the model as a system of equations.
"tsls"
Class that contains a fitted model using two-stage least squares
Objects can be created by calls of the form new("tsls", ...)
.
It is created my the
theta
:Object of class "numeric"
~~
convergence
:Object of class "numericORNULL"
~~
convIter
:Object of class "numericORNULL"
~~
call
:Object of class "callORNULL"
~~
type
:Object of class "character"
~~
wObj
:Object of class "momentWeights"
~~
niter
:Object of class "integer"
~~
efficientGmm
:Object of class "logical"
~~
model
:Object of class "momentModel"
~~
Class "gmmfit"
, directly.
showClass("tsls")
showClass("tsls")
tsls
in Package momentfit ~~It estimates a linear model using two-stage least squares.
## S4 method for signature 'linearModel' tsls(model) ## S4 method for signature 'slinearModel' tsls(model)
## S4 method for signature 'linearModel' tsls(model) ## S4 method for signature 'slinearModel' tsls(model)
model |
An object of class |
signature(model = "linearModel")
signature(model = "slinearModel")
2SLS for equation by equation estimation of a system of equations.
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) res <- tsls(model1) summary(res) ## Econometrics, Fumio Hayashi (2000) ## Empirical exercises (b) and (c) data(Griliches) Griliches$YEAR <- as.factor(Griliches$YEAR) model1 <- momentModel(LW~S+IQ+EXPR+TENURE+RNS+SMSA+YEAR-1, ~S+EXPR+TENURE+RNS+SMSA+YEAR+MED+KWW+MRT+AGE-1, data=Griliches, vcov="MDS") res <- tsls(model1) summary(res)
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) res <- tsls(model1) summary(res) ## Econometrics, Fumio Hayashi (2000) ## Empirical exercises (b) and (c) data(Griliches) Griliches$YEAR <- as.factor(Griliches$YEAR) model1 <- momentModel(LW~S+IQ+EXPR+TENURE+RNS+SMSA+YEAR-1, ~S+EXPR+TENURE+RNS+SMSA+YEAR+MED+KWW+MRT+AGE-1, data=Griliches, vcov="MDS") res <- tsls(model1) summary(res)
update
in Package stats ~~The method is used to refit a model with either a different
method or with modifications to the momentModel
.
## S4 method for signature 'gmmfit' update(object, ..., evaluate=TRUE) ## S4 method for signature 'momentModel' update(object, ...) ## S4 method for signature 'gelfit' update(object, newModel=NULL, ..., evaluate=TRUE) ## S4 method for signature 'list' update(object, ...)
## S4 method for signature 'gmmfit' update(object, ..., evaluate=TRUE) ## S4 method for signature 'momentModel' update(object, ...) ## S4 method for signature 'gelfit' update(object, newModel=NULL, ..., evaluate=TRUE) ## S4 method for signature 'list' update(object, ...)
object |
An object produced by |
... |
Arguments to modify the model or the GMM method |
newModel |
When provided, the new model is estimated using the same specification. For example, it is particularly useful to estimate the restricted model using the same optim specification as the unrestricted model. |
evaluate |
The modified |
signature(object = "ANY")
That just calls "update"
from the "stats"
package.
signature(object = "gmmfit")
signature(object = "momentModel")
signature(object = "list")
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 <- momentModel(y~x, ~z1+z2, data=dat) (res <- gmmFit(model1)) ## lets change to iterative update(res, type="iter") ## Let change the HAC specification in the model1 object ## to MDS update(res, vcov="MDS")
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 <- momentModel(y~x, ~z1+z2, data=dat) (res <- gmmFit(model1)) ## lets change to iterative update(res, type="iter") ## Let change the HAC specification in the model1 object ## to MDS update(res, vcov="MDS")
vcov
in Package stats ~~Computes the covariance matrix of the coefficient estimated by GMM or GEL.
## S4 method for signature 'gmmfit' vcov(object, sandwich=NULL, df.adj=FALSE, breadOnly=FALSE, modelVcov=NULL) ## S4 method for signature 'sgmmfit' vcov(object, sandwich=NULL, df.adj=FALSE, breadOnly=FALSE, modelVcov=NULL) ## S4 method for signature 'tsls' vcov(object, sandwich=TRUE, df.adj=FALSE) ## S4 method for signature 'gelfit' vcov(object, withImpProb=FALSE, tol=1e-10, robToMiss=FALSE) ## S4 method for signature 'momentModel' vcov(object, theta) ## S4 method for signature 'sysModel' vcov(object, theta)
## S4 method for signature 'gmmfit' vcov(object, sandwich=NULL, df.adj=FALSE, breadOnly=FALSE, modelVcov=NULL) ## S4 method for signature 'sgmmfit' vcov(object, sandwich=NULL, df.adj=FALSE, breadOnly=FALSE, modelVcov=NULL) ## S4 method for signature 'tsls' vcov(object, sandwich=TRUE, df.adj=FALSE) ## S4 method for signature 'gelfit' vcov(object, withImpProb=FALSE, tol=1e-10, robToMiss=FALSE) ## S4 method for signature 'momentModel' vcov(object, theta) ## S4 method for signature 'sysModel' vcov(object, theta)
object |
A fitted model or a model, For fitted models, it computes the covariance matrix of the estimators. For models, it computes the covariance matrix of the moment conditions, in which case, the coefficient vector must be provided. |
theta |
Coefficient vector to compute the covariance matrix of the moment conditions. |
sandwich |
Should we compute the sandwich covariance matrix. This is
only necessary if the weighting matrix is not the optimal one, or if
we think it is a bad estimate of it. If |
df.adj |
Should we adjust for degrees of freedom. If |
breadOnly |
If |
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,
|
withImpProb |
Should we compute the moments with the implied probabilities |
tol |
Any diagonal less than |
robToMiss |
Should we compute a covariance matrix that is robust to misspecification? |
If sandwich=FALSE
, then it returns , where
and
are respectively the matrix of average derivatives
and the covariance matrix of the moment conditions. If it is
TRUE
, it returns ,
where
is the weighting matrix used to obtain the vector of
estimates.
If breadOnly=TRUE
, it returns ,
where the value of
depends on the type of GMM. For two-step GMM,
it is the first step weighting matrix, for one-step GMM, it is either
the identity matrix or the fixed weighting matrix that was provided when
gmmFit
was called, for iterative GMM, it is the weighting
matrix used in the last step. For CUE, the result is identical to
sandwich=FALSE
and beadOnly=FALSE
, because the
weighting and coefficient estimates are obtained simultaneously, which
makes identical to
.
breadOnly=TRUE
should therefore be used with caution because it
will produce valid standard errors only if the weighting matrix
converges to the the inverse of the covariance matrix of the moment
conditions.
For "tsls"
objects, sandwich
is TRUE by default. If we
assume that the error term is iid, then setting it to FALSE to result in
the usual covariance matrix. If
FALSE
, it returns a robust covariance matrix determined by the
value of vcov
in the momentModel
.
signature(object = "gmmfit")
For any model estimated by any GMM methods.
signature(object = "gelfit")
For any model estimated by any GMM methods.
signature(object = "sgmmfit")
For any system of equations estimated by any GMM methods.
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## optimal matrix res <- gmmFit(model1) vcov(res) ## not the optimal matrix res <- gmmFit(model1, weights=diag(3)) vcov(res, TRUE) ## Model with heteroscedasticity ## MDS is for models with no autocorrelation. ## No restrictions are imposed on the structure of the ## variance of the moment conditions model2 <- momentModel(y~x1, ~z1+z2, data=simData, vcov="MDS") res <- tsls(model2) ## HC0 type of robust variance vcov(res, sandwich=TRUE) ## HC1 type of robust variance vcov(res, sandwich=TRUE, df.adj=TRUE) ## Fixed and True Weights matrix ## Consider the moment of a normal distribution: ## Using the first three non centered moments g <- function(theta, x) { mu <- theta[1] sig2 <- theta[2] m1 <- x-mu m2 <- x^2-mu^2-sig2 m3 <- x^3-mu^3-3*mu*sig2 cbind(m1,m2,m3) } dg <- function(theta, x) { mu <- theta[1] sig2 <- theta[2] G <- matrix(c(-1,-2*mu,-3*mu^2-3*sig2, 0, -1, -3*mu),3,2) } x <- simData$x3 model <- momentModel(g, x, c(mu=.1, sig2=1.5), vcov="iid") res1 <- gmmFit(model) summary(res1) ## Same results (that's because the moment vcov is centered by default) W <- solve(var(cbind(x,x^2,x^3))) res2 <- gmmFit(model, weights=W) res2 ## If is therefore more efficient in this case to do the following: ## the option breadOnly of summary() is passed to vcov() summary(res2, breadOnly=TRUE)
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) ## optimal matrix res <- gmmFit(model1) vcov(res) ## not the optimal matrix res <- gmmFit(model1, weights=diag(3)) vcov(res, TRUE) ## Model with heteroscedasticity ## MDS is for models with no autocorrelation. ## No restrictions are imposed on the structure of the ## variance of the moment conditions model2 <- momentModel(y~x1, ~z1+z2, data=simData, vcov="MDS") res <- tsls(model2) ## HC0 type of robust variance vcov(res, sandwich=TRUE) ## HC1 type of robust variance vcov(res, sandwich=TRUE, df.adj=TRUE) ## Fixed and True Weights matrix ## Consider the moment of a normal distribution: ## Using the first three non centered moments g <- function(theta, x) { mu <- theta[1] sig2 <- theta[2] m1 <- x-mu m2 <- x^2-mu^2-sig2 m3 <- x^3-mu^3-3*mu*sig2 cbind(m1,m2,m3) } dg <- function(theta, x) { mu <- theta[1] sig2 <- theta[2] G <- matrix(c(-1,-2*mu,-3*mu^2-3*sig2, 0, -1, -3*mu),3,2) } x <- simData$x3 model <- momentModel(g, x, c(mu=.1, sig2=1.5), vcov="iid") res1 <- gmmFit(model) summary(res1) ## Same results (that's because the moment vcov is centered by default) W <- solve(var(cbind(x,x^2,x^3))) res2 <- gmmFit(model, weights=W) res2 ## If is therefore more efficient in this case to do the following: ## the option breadOnly of summary() is passed to vcov() summary(res2, breadOnly=TRUE)
vcovHAC
in Package sandwich ~~Methods to compute the HAC covariance matrix of the moment matrix ~~
signature(x = "momentModel")
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) # a warning is given if the model is not set as being HAC vcovHAC(model1, theta) model1 <- momentModel(y~x1, ~z1+z2, data=simData, vcov="HAC",vcovOptions=list(kernel="B")) vcovHAC(model1, theta)
data(simData) theta <- c(beta0=1,beta1=2) model1 <- momentModel(y~x1, ~z1+z2, data=simData) # a warning is given if the model is not set as being HAC vcovHAC(model1, theta) model1 <- momentModel(y~x1, ~z1+z2, data=simData, vcov="HAC",vcovOptions=list(kernel="B")) vcovHAC(model1, theta)