[Gmm-commits] r215 - in pkg/momentfit: . R man vignettes
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jan 24 23:50:04 CET 2024
Author: chaussep
Date: 2024-01-24 23:50:03 +0100 (Wed, 24 Jan 2024)
New Revision: 215
Added:
pkg/momentfit/R/weak.R
pkg/momentfit/man/kclassfit-class.Rd
pkg/momentfit/man/kclassfit.Rd
pkg/momentfit/man/lse-methods.Rd
pkg/momentfit/man/lsefit-class.Rd
pkg/momentfit/man/summaryKclass-class.Rd
pkg/momentfit/vignettes/weak.Rmd
pkg/momentfit/vignettes/weak.pdf
Modified:
pkg/momentfit/DESCRIPTION
pkg/momentfit/NAMESPACE
pkg/momentfit/R/allClasses.R
pkg/momentfit/R/momentData.R
pkg/momentfit/R/rModel-methods.R
pkg/momentfit/R/rsysMomentModel-methods.R
pkg/momentfit/R/sysMomentModel-methods.R
pkg/momentfit/R/sysMomentModel.R
pkg/momentfit/R/validity.R
pkg/momentfit/man/print-methods.Rd
pkg/momentfit/man/restModel-methods.Rd
pkg/momentfit/man/show-methods.Rd
pkg/momentfit/vignettes/gelS4.pdf
pkg/momentfit/vignettes/gmmS4.Rnw
pkg/momentfit/vignettes/gmmS4.pdf
Log:
starting to add tools for weak instruments -- LIML, Fuller, Anderson-Rubin etc.
Modified: pkg/momentfit/DESCRIPTION
===================================================================
--- pkg/momentfit/DESCRIPTION 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/DESCRIPTION 2024-01-24 22:50:03 UTC (rev 215)
@@ -1,19 +1,19 @@
Package: momentfit
Version: 0.6
-Date: 2023-09-17
+Date: 2024-01-24
Title: Methods of Moments
Author: Pierre Chausse <pchausse at uwaterloo.ca>
Maintainer: Pierre Chausse <pchausse at uwaterloo.ca>
-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>).
+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>).Some work is being done to add tools to deal with weak and/or many instruments. This include K-Class estimators (LIML and Fuller), Anderson and Rubin statistics test, etc.
Depends: R (>= 3.0.0), sandwich
Imports: stats, methods, parallel
-Suggests: lmtest, knitr, texreg
+Suggests: lmtest, knitr, texreg, rmarkdown, ivmodel
Collate: 'allClasses.R' 'validity.R' 'momentData.R' 'momentModel-methods.R'
'momentModel.R' 'momentWeights-methods.R' 'gmmfit-methods.R'
'specTest-methods.R' 'summary-methods.R' 'rModel-methods.R'
'hypothesisTest-methods.R' 'sysMomentModel.R'
'sysMomentModel-methods.R' 'rsysMomentModel-methods.R'
- 'sgmmfit-methods.R' 'gmm4.R' 'gel.R' 'gelfit-methods.R' 'gel4.R'
+ 'sgmmfit-methods.R' 'gmm4.R' 'gel.R' 'gelfit-methods.R' 'gel4.R' 'weak.R'
License: GPL (>= 2)
NeedsCompilation: yes
VignetteBuilder: knitr
Modified: pkg/momentfit/NAMESPACE
===================================================================
--- pkg/momentfit/NAMESPACE 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/NAMESPACE 2024-01-24 22:50:03 UTC (rev 215)
@@ -32,7 +32,7 @@
"rsysModel", "formulaModel","rfunctionModel", "sSpec",
"summaryGmm", "specTest", "confint", "mconfint",
"hypothesisTest", "stsls", "sgmmfit", "gelfit", "summaryGel",
- "tsls")
+ "tsls", "lsefit", "kclassfit", "summaryKclass")
exportMethods(print, show, kernapply, coef, model.matrix, bread, summary,
residuals, "[", vcovHAC, subset, update, vcov, plot, confint, merge)
@@ -43,7 +43,7 @@
printRestrict, restModel, getRestrict, gmm4, sysMomentModel, ThreeSLS,
rhoET, rhoEL, rhoEEL, rhoHD, Wu_lam, EEL_lam, REEL_lam, getLambda,
solveGel, rhoETEL, rhoETHD, ETXX_lam, gelFit, evalGel, getImpProb,
- evalGelObj, momFct, gel4, setCoef)
+ evalGelObj, momFct, gel4, setCoef, lse, getK, kclassfit)
### S3 methods ###
Modified: pkg/momentfit/R/allClasses.R
===================================================================
--- pkg/momentfit/R/allClasses.R 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/R/allClasses.R 2024-01-24 22:50:03 UTC (rev 215)
@@ -160,7 +160,6 @@
convIter="numericORNULL", wSpec="list",niter="integer",
df.adj="logical", breadOnly="logical"))
-
## hypothesisTest
setClass("hypothesisTest", representation(test="numeric", hypothesis="character",
@@ -217,7 +216,21 @@
impProb="list", gelType="list",
restrictedLam="integer"))
+## lsefit classes
+setClass("lsefit", slots=list(model="linearModel"), contains="lm")
+
+## K-Class related classes
+
+setClass("kclassfit", slots = list(kappa = "numeric",
+ method = "character", origModel='linearModel'),
+ contains="gmmfit")
+
+setClass("summaryKclass", slots = list(kappa = "numeric",
+ method = "character", origModel='linearModel'),
+ contains="summaryGmm")
+
+
## class converted
setAs("linearModel", "nonlinearModel",
@@ -292,31 +305,24 @@
neqn <- length(eqnNames)
datX <- lapply(1:neqn,
function(i) {
- v <- from at varNames[[i]]
- chk <- "(Intercept)" %in% v
- v <- v[v!="(Intercept)"]
- X <- from at data[,v, drop=FALSE]
- colnames(X) <- paste(eqnNames[[i]],".", v, sep="")
+ X <- model.matrix(from at modelT[[i]], from at data)
+ chk <- attr(from at modelT[[i]], "intercept")==1
if (chk)
- {
- X <- cbind(1, X)
- colnames(X)[1]<-paste(eqnNames[[i]], ".Intercept", sep="")
- }
+ colnames(X)[1] <- "Intercept"
+ colnames(X) <- paste(eqnNames[[i]],".", colnames(X), sep="")
X})
datZ <- lapply(1:neqn,
function(i) {
- v <- all.vars(from at instT[[i]])
+ Z <- model.matrix(from at instT[[i]], from at data)
chk <- attr(from at instT[[i]], "intercept")==1
- Z <- from at data[,v, drop=FALSE]
- colnames(Z) <- paste(eqnNames[[i]],".", v, sep="")
if (chk)
- {
- Z <- cbind(1, Z)
- colnames(Z)[1]<-paste(eqnNames[[i]], ".Intercept", sep="")
- }
+ colnames(Z)[1] <- "Intercept"
+ colnames(Z) <- paste(eqnNames[[i]],".", colnames(Z), sep="")
Z})
nZ <- do.call("c", lapply(datZ, colnames))
+ nZ <- gsub(":", ".", nZ)
nX <- do.call("c", lapply(datX, colnames))
+ nX <- gsub(":", ".", nX)
datZ <- .GListToMat(datZ)
datX <- .GListToMat(datX)
Y <- do.call("c", modelResponse(from))
@@ -326,9 +332,9 @@
dat <- dat[,unique(colnames(dat))]
dat <- data.frame(dat, row.names=1:nrow(datZ))
g <- paste("Y~", paste(nX, collapse="+"), "-1")
- g <- formula(g, .GlobalEnv)
+ g <- formula(g, environment(from at instT[[1]]))
h <- paste("~", paste(nZ, collapse="+"), "-1")
- h <- formula(h, .GlobalEnv)
+ h <- formula(h, environment(from at instT[[1]]))
res <- momentModel(g, h, vcov=from at vcov, vcovOptions=from at vcovOptions,
centeredVcov=from at centeredVcov, data=dat)
})
Modified: pkg/momentfit/R/momentData.R
===================================================================
--- pkg/momentfit/R/momentData.R 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/R/momentData.R 2024-01-24 22:50:03 UTC (rev 215)
@@ -38,7 +38,7 @@
instF <- model.frame(formula, h, na.action="na.pass",
drop.unused.levels=TRUE)
}
- h <- lapply(1:ncol(Y), function(i) formula(attr(instF, "terms"), .GlobalEnv))
+ h <- lapply(1:ncol(Y), function(i) formula(attr(instF, "terms")))
data <- cbind(modelF, instF)
data <- data[,!duplicated(colnames(data))]
return(.slModelData(g,h,data,survOptions, vcovOptions,na.action))
Modified: pkg/momentfit/R/rModel-methods.R
===================================================================
--- pkg/momentfit/R/rModel-methods.R 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/R/rModel-methods.R 2024-01-24 22:50:03 UTC (rev 215)
@@ -513,7 +513,7 @@
R2 <- list()
R <- gsub("=", "~", R, fixed=TRUE)
for (r in R)
- R2 <- c(R2, as.formula(r, .GlobalEnv))
+ R2 <- c(R2, as.formula(r))
R <- R2
} else {
if (!is.list(R))
@@ -543,7 +543,7 @@
R2 <- list()
R <- gsub("=", "~", R, fixed=TRUE)
for (r in R)
- R2 <- c(R2, as.formula(r, .GlobalEnv))
+ R2 <- c(R2, as.formula(r))
R <- R2
} else {
if (!is.list(R))
@@ -573,7 +573,7 @@
R2 <- list()
R <- gsub("=", "~", R, fixed=TRUE)
for (r in R)
- R2 <- c(R2, as.formula(r, .GlobalEnv))
+ R2 <- c(R2, as.formula(r))
R <- R2
} else {
if (!is.list(R))
@@ -628,7 +628,8 @@
stop("Some values in R or dR at theta are either infinite or NAN")
if (qr(dR)$rank < length(R))
stop("The matrix of derivatives of the constraints is not full rank")
- hypo <- sapply(object at R, function(r) capture.output(print(r)))
+ hypo <- sapply(object at R,
+ function(r) capture.output(print(r, FALSE)))
list(dR=dR, R=R, q=rep(0, nrow(dR)), hypo=hypo,
orig.R=object at R, orig.rhs=NULL)
})
Modified: pkg/momentfit/R/rsysMomentModel-methods.R
===================================================================
--- pkg/momentfit/R/rsysMomentModel-methods.R 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/R/rsysMomentModel-methods.R 2024-01-24 22:50:03 UTC (rev 215)
@@ -162,7 +162,7 @@
R2 <- list()
R <- gsub("=", "~", R, fixed=TRUE)
for (r in R)
- R2 <- c(R2, as.formula(r, .GlobalEnv))
+ R2 <- c(R2, as.formula(r))
R <- R2
} else {
if (!is.list(R))
@@ -342,7 +342,7 @@
setMethod("getRestrict", "rsnonlinearModel",
function(object, theta) {
- theta <- setCoef(as(object, "snonlinearModel"), theta)
+ theta <- setCoef(as(object, "snonlinearModel"), theta)
names(theta) <- NULL
theta <- do.call("c", theta)
dR <-numeric()
@@ -363,7 +363,8 @@
stop("Some values in R or dR at theta are either infinite or NAN")
if (qr(dR)$rank < length(R))
stop("The matrix of derivatives of the constraints is not full rank")
- hypo <- sapply(object at R, function(r) capture.output(print(r)))
+ hypo <- sapply(object at R,
+ function(r) capture.output(print(r, FALSE)))
list(dR=dR, R=R, q=rep(0, nrow(dR)), hypo=hypo,
orig.R=object at R, orig.rhs=NULL)
})
Modified: pkg/momentfit/R/sysMomentModel-methods.R
===================================================================
--- pkg/momentfit/R/sysMomentModel-methods.R 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/R/sysMomentModel-methods.R 2024-01-24 22:50:03 UTC (rev 215)
@@ -154,7 +154,6 @@
} else {
f <- reformulate(momNames, NULL, FALSE)
}
- attr(f, ".Environment")<- .GlobalEnv
x at q[s] <- length(momNames)
x at instT[[s]] <- attr(f, "terms")
x at momNames[[s]] <- momNames
Modified: pkg/momentfit/R/sysMomentModel.R
===================================================================
--- pkg/momentfit/R/sysMomentModel.R 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/R/sysMomentModel.R 2024-01-24 22:50:03 UTC (rev 215)
@@ -128,7 +128,7 @@
intercept <- TRUE
reg <- paste(reg, collapse="+", sep="")
reg <- paste("~", reg, ifelse(intercept, "", "-1"), sep="")
- reg <- as.formula(reg, .GlobalEnv)
+ reg <- as.formula(reg)
h <- rep(list(reg), length(g))
} else {
SUR <- FALSE
Modified: pkg/momentfit/R/validity.R
===================================================================
--- pkg/momentfit/R/validity.R 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/R/validity.R 2024-01-24 22:50:03 UTC (rev 215)
@@ -353,7 +353,44 @@
setValidity("functionModel", .checkFunc)
+.checkAllNLRest <- function(object)
+{
+ error <- character()
+ R <- object at R
+ tR <- sapply(R, function(ri) inherits(ri, "formula"))
+ if (!all(tR))
+ {
+ error <- c(error,
+ "R must be a list of formulas")
+ } else {
+ chk <- sapply(R, function(ri) {
+ chk1 <- length(ri) == 3
+ if (!chk1) return(FALSE)
+ ri <- as.character(ri[[2]])
+ if (length(ri) != 1) return(FALSE)
+ ri %in% object at parNames})
+ if (!all(chk))
+ {
+ msg <- paste("Something is wrong with restriction",
+ ifelse(sum(!chk)>1, "s ", " "),
+ paste(which(!chk), collapse=", ", sep=""), ". ",
+ "They must expressed as a formula with one coefficient",
+ " as a function of the others",
+ " and all coefficients in the restriction must be in ",
+ "the nonrestricted model", sep="")
+ error <- c(error, msg)
+ }
+ }
+ if (length(error)==0)
+ TRUE
+ else
+ error
+}
+setValidity("rfunctionModel", .checkAllNLRest)
+setValidity("rnonlinearModel", .checkAllNLRest)
+setValidity("rformulaModel", .checkAllNLRest)
+
.checkMomentWeights <- function(object)
{
error <- character()
Added: pkg/momentfit/R/weak.R
===================================================================
--- pkg/momentfit/R/weak.R (rev 0)
+++ pkg/momentfit/R/weak.R 2024-01-24 22:50:03 UTC (rev 215)
@@ -0,0 +1,155 @@
+## LSE for linearModel objects
+
+setGeneric("lse", function(model, ...) standardGeneric("lse"))
+
+setMethod("lse", "linearModel", function(model) {
+ f <- formula(model at modelF)
+ dat <- model at modelF
+ attr(dat, "terms") <- NULL
+ fit <- lm(f, dat)
+ new("lsefit", fit, model=model)
+})
+
+## Methods for lsefit objects
+
+setMethod("print", "lsefit",
+ function(x, model=TRUE, ...) {
+ if (model)
+ print(x at model)
+ cat("\nEstimation: Least Squares\n")
+ cat(paste(capture.output(print(as(x, "lm"), ...))[-(1:3)],
+ collapse="\n"))
+ invisible()
+ })
+
+setMethod("show","lsefit", function(object) print(object))
+
+
+## K-Class functions
+
+
+getK <- function(object, alpha=1)
+{
+ if (!inherits(object, "linearModel"))
+ stop("object must be a model of class linearModel")
+ spec <- modelDims(object)
+ if (spec$k == spec$q)
+ {
+ k1 <- 1
+ } else {
+ X <- model.matrix(object)
+ endo <- cbind(model.response(object at modelF),
+ X[,object at isEndo])
+ X <- X[,!object at isEndo, drop=FALSE]
+ W <- model.matrix(object, "instruments")
+ e1 <- lm.fit(X, endo)$residuals
+ e2 <- lm.fit(W, endo)$residuals
+ e1 <- crossprod(e1)
+ e2 <- crossprod(e2)
+ k1 <- min(eigen(solve(e2,e1))$val)
+ }
+ k2 <- k1 - alpha/(spec$n-spec$q)
+ c(LIML=k1, Fuller=k2)
+}
+
+
+kclassfit <- function(object, k, type=c("LIML", "Fuller"))
+{
+ if (!inherits(object, "linearModel"))
+ stop("object must be a model of class linearModel")
+ type <- match.arg(type)
+ if (missing(k))
+ {
+ k <- getK(object)[type]
+ method <- type
+ } else {
+ method="K-Class"
+ }
+ if (k==1)
+ return(tsls(object))
+ if (k==0)
+ return(lse(object))
+ spec <- modelDims(object)
+ EndoVars <- !(spec$parNames %in% spec$momNames)
+ exoInst <- spec$momNames %in% spec$parNames
+ if (all(!EndoVars))
+ {
+ warning("This model has no endogenous variables. Returning LSE.")
+ return(lse(object))
+ }
+ g. <- formula(object at modelF)
+ X <- model.matrix(object)
+ Z <- model.matrix(object, "instrument")
+ Z <- X[, EndoVars, drop=FALSE] - as.matrix(k*lm.fit(Z, X[,EndoVars])$residuals)
+ colnames(Z) <- paste("KClass.", colnames(Z), sep="")
+ parNames <- spec$parNames
+ parNames[EndoVars] <- colnames(Z)
+ if (attr(terms(g.), "intercept"))
+ parNames <- parNames[-1]
+ h. <- reformulate(parNames, NULL, attr(terms(g.), "intercept"))
+ dat <- object at modelF
+ attr(dat, "terms") <- NULL
+ dat <- cbind(dat, Z)
+ object2 <- momentModel(g=g., x=h., data=dat,
+ vcov=object at vcov, vcovOptions=object at vcovOptions,
+ survOptions=object at survOptions,
+ centeredVcov=object at centeredVcov, smooth=object at smooth)
+ fit <- gmmFit(object2)
+ new("kclassfit", fit, method=method, kappa=k, origModel = object)
+}
+
+
+## Methods for kclassfit
+
+setMethod("print", "kclassfit",
+ function(x, model=TRUE, ...) {
+ theta <- coef(x)
+ if (model)
+ print(x at origModel)
+ type <- x at method
+ k <- x at kappa
+ spec <- modelDims(x at model)
+ cat("\nEstimation: ", type, sep="")
+ cat(" (k = ", k, ")\n", sep="")
+ cat("coefficients:\n")
+ print.default(format(theta, ...), print.gap=2L, quote=FALSE)
+ })
+setMethod("show","kclassfit", function(object) print(object))
+
+
+setMethod("specTest", c("kclassfit", "missing"),
+ function(object, which, ...) {
+ spec <- modelDims(object at origModel)
+ AR <- log(object at kappa)*spec$n
+ df <- spec$q-spec$k
+ pv <- ifelse(df>0, pchisq(AR, df, lower.tail=FALSE), NA)
+ AR <- cbind(AR, df, pv)
+ dimnames(AR) <- list("Test E(g)=0: ", c("Statistics",
+ "df", "pvalue"))
+ ans <- new("specTest", test = AR, testname = "Anderson and Rubin")
+ ans
+ })
+
+setMethod("summary", "kclassfit",
+ function(object, ...)
+ {
+ ans <- callNextMethod()
+ new("summaryKclass", kappa=object at kappa, method=object at method,
+ origModel=object at origModel, ans)
+ })
+
+
+## Methods for summaryKclass
+
+setMethod("print", "summaryKclass",
+ function(x, ...) {
+ p <- capture.output(print(as(x, "summaryGmm"), ...))
+ type <- x at method
+ k <- x at kappa
+ method <- paste("Estimation: ", type, " (k = ", k, ")", sep="")
+ p[grepl("Estimation:", p)] <- method
+ cat(paste(p, collapse="\n"))
+ cat("\n")
+ invisible()
+ })
+setMethod("show","summaryKclass", function(object) print(object))
Added: pkg/momentfit/man/kclassfit-class.Rd
===================================================================
--- pkg/momentfit/man/kclassfit-class.Rd (rev 0)
+++ pkg/momentfit/man/kclassfit-class.Rd 2024-01-24 22:50:03 UTC (rev 215)
@@ -0,0 +1,51 @@
+\name{kclassfit-class}
+\docType{class}
+\alias{kclassfit-class}
+\alias{print,kclassfit-method}
+\alias{show,kclassfit-method}
+\alias{specTest,kclassfit,missing-method}
+\alias{summary,kclassfit-method}
+
+\title{Class \code{"kclassfit"}}
+\description{
+This is the object that stores the estimation result from the K-Class
+estimation method. The class includes the Limited Information Maximum
+Likelihood (LIML) and its modified version proposed by Fuller (1977).
+}
+\section{Objects from the Class}{
+Objects can be created by calls of the form \code{new("kclassfit", ...)}.
+It is created by the \code{\link{kclassfit}} function.
+}
+\section{Slots}{
+ \describe{
+ \item{\code{kappa}:}{Object of class \code{"numeric"} ~~ }
+ \item{\code{method}:}{Object of class \code{"character"} ~~ }
+ \item{\code{origModel}:}{Object of class \code{"linearModel"} ~~ }
+ \item{\code{theta}:}{Object of class \code{"numeric"} ~~ }
+ \item{\code{convergence}:}{Object of class \code{"numericORNULL"} ~~ }
+ \item{\code{convIter}:}{Object of class \code{"numericORNULL"} ~~ }
+ \item{\code{call}:}{Object of class \code{"callORNULL"} ~~ }
+ \item{\code{type}:}{Object of class \code{"character"} ~~ }
+ \item{\code{wObj}:}{Object of class \code{"momentWeights"} ~~ }
+ \item{\code{niter}:}{Object of class \code{"integer"} ~~ }
+ \item{\code{efficientGmm}:}{Object of class \code{"logical"} ~~ }
+ \item{\code{model}:}{Object of class \code{"momentModel"} ~~ }
+ }
+}
+\section{Extends}{
+Class \code{"\linkS4class{gmmfit}"}, directly.
+}
+\section{Methods}{
+ \describe{
+ \item{print}{\code{signature(x = "kclassfit")}: ... }
+ \item{show}{\code{signature(object = "kclassfit")}: ... }
+ \item{specTest}{\code{signature(object = "kclassfit", which = "missing")}: ... }
+ \item{summary}{\code{signature(object = "kclassfit")}: ... }
+ }
+}
+
+
+\examples{
+showClass("kclassfit")
+}
+\keyword{classes}
Added: pkg/momentfit/man/kclassfit.Rd
===================================================================
--- pkg/momentfit/man/kclassfit.Rd (rev 0)
+++ pkg/momentfit/man/kclassfit.Rd 2024-01-24 22:50:03 UTC (rev 215)
@@ -0,0 +1,51 @@
+\name{kclassfit}
+\alias{kclassfit}
+\alias{getK}
+\title{
+K-Class Estimation Method
+}
+\description{
+It estimates \code{linearModel} objects using the K-Class method. It
+includes LIML, Fuller, TSLS and OLS.
+}
+\usage{
+kclassfit(object, k, type = c("LIML", "Fuller"))
+
+getK(object, alpha = 1)
+}
+
+\arguments{
+ \item{object}{
+ A model of class \code{linearModel}
+ }
+ \item{k}{
+ The numeric value of k for the K-Class estimator. If missing, the value
+ for LIML or Fuller is used.
+ }
+ \item{type}{
+ Which value of \code{k} should we use to fit the model? Only used if
+ \code{k} is missing.
+ }
+ \item{alpha}{A parameter for the Fuller method}
+}
+
+\details{
+ Let the model be \eqn{Y=X\beta+U}{Y=XB+U} and the matrix of instruments
+ be \eqn{Z}{Z}. The K-Class estimator is
+ \eqn{(X'(I-kM_z)X)^{-1}(X'(I-kM_z)Y)}. The function \code{getK} can be
+ used to compute the value of \code{k} for both LIML and Fuller.
+}
+
+\value{
+ The function returns an object of class \code{kclassfit}.
+}
+\examples{
+
+data(simData)
+theta <- c(beta0=1,beta1=2)
+model1 <- momentModel(y~x1, ~z1+z2, data=simData)
+kclassfit(model1, type="LIML")
+}
+
+\keyword{LIML}
+\keyword{Fuller}
Added: pkg/momentfit/man/lse-methods.Rd
===================================================================
--- pkg/momentfit/man/lse-methods.Rd (rev 0)
+++ pkg/momentfit/man/lse-methods.Rd 2024-01-24 22:50:03 UTC (rev 215)
@@ -0,0 +1,26 @@
+\name{lse-methods}
+\docType{methods}
+\alias{lse-methods}
+\alias{lse}
+\alias{lse,linearModel-method}
+\title{Least Squares Methods for Moment Models}
+\description{
+It estimates models defined in the package by least squares. At the
+moment, it only applies to \code{linearModel} objects and it is
+estimated using \code{\link{lm}}.
+}
+\section{Methods}{
+\describe{
+\item{\code{signature(model = "linearModel")}}{
+It ignores the instruments and simply fits the linear model with LSE.
+}
+}}
+
+\examples{
+data(simData)
+mod <- momentModel(y~x1, ~z1+z2, vcov="MDS", data=simData)
+lse(mod)
+}
+
+\keyword{methods}
+\keyword{LSE}
Added: pkg/momentfit/man/lsefit-class.Rd
===================================================================
--- pkg/momentfit/man/lsefit-class.Rd (rev 0)
+++ pkg/momentfit/man/lsefit-class.Rd 2024-01-24 22:50:03 UTC (rev 215)
@@ -0,0 +1,38 @@
+\name{lsefit-class}
+\Rdversion{1.1}
+\docType{class}
+\alias{lsefit-class}
+\alias{print,lsefit-method}
+\alias{show,lsefit-method}
+
+\title{Class \code{"lsefit"}}
+\description{
+A class for least squares estimate of different \code{momentModel}
+objects.
+}
+\section{Objects from the Class}{
+Objects can be created by calls of the form \code{new("lsefit", ...)}.
+It is created by \code{\link{lse}}. It includes the information about
+the model being estimated and the estimation based on \code{\link{lm}}.
+}
+\section{Slots}{
+ \describe{
+ \item{\code{model}:}{Object of class \code{"linearModel"} ~~ }
+ \item{\code{.S3Class}:}{Object of class \code{"character"} ~~ }
+ }
+}
+\section{Extends}{
+Class \code{"\linkS4class{lm}"}, directly.
+Class \code{"\linkS4class{oldClass}"}, by class "lm", distance 2.
+}
+\section{Methods}{
+ \describe{
+ \item{print}{\code{signature(x = "lsefit")}: ... }
+ \item{show}{\code{signature(object = "lsefit")}: ... }
+ }
+}
+
+\examples{
+showClass("lsefit")
+}
+\keyword{classes}
Modified: pkg/momentfit/man/print-methods.Rd
===================================================================
--- pkg/momentfit/man/print-methods.Rd 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/man/print-methods.Rd 2024-01-24 22:50:03 UTC (rev 215)
@@ -26,7 +26,7 @@
\title{ Methods for Function \code{print} in Package \pkg{base}}
\description{
-Print method for all \code{"momentModel"}, \code{"gmmfit"},
+Print methods for all \code{"momentModel"}, \code{"gmmfit"},
\code{"summaryGmm"} \code{"hypothesisTest"} and \code{"specTest"} objects.
}
\section{Methods}{
Modified: pkg/momentfit/man/restModel-methods.Rd
===================================================================
--- pkg/momentfit/man/restModel-methods.Rd 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/man/restModel-methods.Rd 2024-01-24 22:50:03 UTC (rev 215)
@@ -32,7 +32,8 @@
\item{object}{An object of class \code{"momentModel"} or
\code{"sysModel"}.}
\item{R}{Either a matrix or a vector of characters for
- linear models and a list of formulas for nonlinear models}
+ linear models and a list of formulas for nonlinear models. See
+ details below.}
\item{rhs}{The right hand side of the linear restrictions. It is
ignored for nonlinear models.}
}
@@ -79,6 +80,36 @@
Method for object of class \code{formulaGel}.
}
}}
+
+\details{
+For linear models and linear restrictions, R is in general a matrix. In
+that case, the restrictions are in the form \eqn{R\theta=q}, where
+\eqn{\theta} is the vector of coefficients. It is also possible, for
+linear models, to define \code{R} as a character vector with the
+restrictions being expressed explicitly. In that case, the names of the
+coefficients are the names of the variables. For example, if we want the
+sum of the coefficients of the variables \code{x1} and \code{x2} to be
+equal to 0, we can set \code{R} to "x1+x2=0".
+
+Nonlinear restrictions are not allowed for linear models. However, it is
+possible by converting linear models into nonlinear models before
+imposing the nonlinear restrictions. This is done by using the \code{as}
+method. For example, we can convert the linear model \code{mod} to a
+nonlinear model using the command \code{mod <- as(mod, "nonlinearModel")}.
+
+For all other types (nonlinearModel, formulaModel and functionModel),
+restrictions in \code{R} must be in the form: one coefficient as a
+function of the others. We can express the restriction as a formula (or
+a list of \code{formula} for more than one restriction) or a character
+vector. Note that it is the names of the coefficients that appear in the
+\code{R}, not the names of the variables. For example, the following is
+a valid restriction: "theta1=theta2*theta3+1". Although the following is
+the same restriction, it is not a valid entry for \code{R}:
+"theta1-theta2*theta3=1". This condition is part of the validity test
+when restricted model are created. If it is not satisfied, an error
+message is returned.
+}
+
\examples{
data(simData)
theta <- c(beta0=1,beta1=2)
Modified: pkg/momentfit/man/show-methods.Rd
===================================================================
--- pkg/momentfit/man/show-methods.Rd 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/man/show-methods.Rd 2024-01-24 22:50:03 UTC (rev 215)
@@ -16,7 +16,7 @@
\alias{show,momentWeights-method}
\alias{show,sysMomentWeights-method}
\alias{show,sSpec-method}
-\alias{show,hypothesisTest-method}
+\alias{show,hypothesisTest-method}
\title{ ~~ Methods for Function \code{show} in Package \pkg{methods} ~~}
\description{
Display method for all objects.
Added: pkg/momentfit/man/summaryKclass-class.Rd
===================================================================
--- pkg/momentfit/man/summaryKclass-class.Rd (rev 0)
+++ pkg/momentfit/man/summaryKclass-class.Rd 2024-01-24 22:50:03 UTC (rev 215)
@@ -0,0 +1,46 @@
+\name{summaryKclass-class}
+\docType{class}
+\alias{summaryKclass-class}
+\alias{print,summaryKclass-method}
+\alias{show,summaryKclass-method}
+
+\title{Class \code{"summaryKclass"}}
+\description{
+The class that stores the summary statistics of model fitted by K-Class estimators.
+}
+\section{Objects from the Class}{
+Objects can be created by calls of the form \code{new("summaryKclass", ...)}.
+It is the summary statistics of models estimated by \code{\link{kclassfit}}.
+}
+\section{Slots}{
+ \describe{
+ \item{\code{kappa}:}{Object of class \code{"numeric"} ~~ }
+ \item{\code{method}:}{Object of class \code{"character"} ~~ }
+ \item{\code{origModel}:}{Object of class \code{"linearModel"} ~~ }
+ \item{\code{coef}:}{Object of class \code{"matrix"} ~~ }
+ \item{\code{specTest}:}{Object of class \code{"specTest"} ~~ }
+ \item{\code{strength}:}{Object of class \code{"list"} ~~ }
+ \item{\code{model}:}{Object of class \code{"momentModel"} ~~ }
+ \item{\code{sandwich}:}{Object of class \code{"logical"} ~~ }
+ \item{\code{type}:}{Object of class \code{"character"} ~~ }
+ \item{\code{convergence}:}{Object of class \code{"numericORNULL"} ~~ }
+ \item{\code{convIter}:}{Object of class \code{"numericORNULL"} ~~ }
+ \item{\code{wSpec}:}{Object of class \code{"list"} ~~ }
+ \item{\code{niter}:}{Object of class \code{"integer"} ~~ }
+ \item{\code{df.adj}:}{Object of class \code{"logical"} ~~ }
+ \item{\code{breadOnly}:}{Object of class \code{"logical"} ~~ }
+ }
+}
+\section{Extends}{
+Class \code{"\linkS4class{summaryGmm}"}, directly.
+}
+\section{Methods}{
+ \describe{
+ \item{print}{\code{signature(x = "summaryKclass")}: ... }
+ \item{show}{\code{signature(object = "summaryKclass")}: ... }
+ }
+}
+\examples{
+showClass("summaryKclass")
+}
+\keyword{classes}
Modified: pkg/momentfit/vignettes/gelS4.pdf
===================================================================
(Binary files differ)
Modified: pkg/momentfit/vignettes/gmmS4.Rnw
===================================================================
--- pkg/momentfit/vignettes/gmmS4.Rnw 2023-09-19 21:09:27 UTC (rev 214)
+++ pkg/momentfit/vignettes/gmmS4.Rnw 2024-01-24 22:50:03 UTC (rev 215)
@@ -62,7 +62,7 @@
<<echo=FALSE>>=
library(knitr)
opts_chunk$set(size='footnotesize')
-@
+@
\newpage
\tableofcontents
Modified: pkg/momentfit/vignettes/gmmS4.pdf
===================================================================
(Binary files differ)
Added: pkg/momentfit/vignettes/weak.Rmd
===================================================================
--- pkg/momentfit/vignettes/weak.Rmd (rev 0)
+++ pkg/momentfit/vignettes/weak.Rmd 2024-01-24 22:50:03 UTC (rev 215)
@@ -0,0 +1,297 @@
+---
+title: "Instrumental Variables Tools for the Case of Weak or Many Instruments"
+author: "Pierre Chausse^[University of Waterloo, pchausse at uwaterloo.ca]"
+date: ""
+bibliography: empir.bib
+output:
+ pdf_document:
+ number_sections: true
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/gmm -r 215
More information about the Gmm-commits
mailing list