From bac62b21b41773c6c55aacfea890276231ee6a54 Mon Sep 17 00:00:00 2001 From: tappek <77916431+tappek@users.noreply.github.com> Date: Thu, 26 Dec 2024 23:20:05 +0100 Subject: [PATCH] pgmm: use FSM() also for transformation = "d" (not fully flexible yet) --- R/est_gmm.R | 7 ++++--- R/tool_vcovG.R | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/R/est_gmm.R b/R/est_gmm.R index 6e0f307..295853f 100755 --- a/R/est_gmm.R +++ b/R/est_gmm.R @@ -516,7 +516,7 @@ pgmm <- function(formula, data, subset, na.action, yX <- yX1 # Compute the first step matrices - if (transformation == "d") A1 <- tcrossprod(diff(diag(1, T - TL1 + 1))) # TODO: to use FSM() as well? + if (transformation == "d") A1 <- FSM(T - TL1, "G") # == tcrossprod(diff(diag(1, T - TL1 + 1))) # TODO: FSM's arg fsm not fully flexible if (transformation == "ld") A1 <- FSM(T - TL2, fsm) # compute the estimator @@ -552,7 +552,8 @@ pgmm <- function(formula, data, subset, na.action, function(x) as.vector(x[ , 1L] - crossprod(t(x[ , -1L, drop = FALSE]), coefficients))) outresid <- lapply(residuals, function(x) outer(x, x)) - + + # A2 is only needed for model = "twosteps", but seems currently needed in vcov.pggm also for "onestep" model A2 <- mapply(function(x, y) crossprod(t(crossprod(x, y)), x), W, outresid, SIMPLIFY = FALSE) A2 <- Reduce("+", A2) minevA2 <- min(eigen(A2)$values) @@ -560,7 +561,7 @@ pgmm <- function(formula, data, subset, na.action, warning("the second-step matrix is singular, a general inverse is used") ginv(A2) } else solve(A2) - + if (model == "twosteps") { coef1s <- coefficients t.CP.WX.A2 <- t(crossprod(WX, A2)) diff --git a/R/tool_vcovG.R b/R/tool_vcovG.R index ccad106..7cc20c1 100644 --- a/R/tool_vcovG.R +++ b/R/tool_vcovG.R @@ -1233,6 +1233,7 @@ vcovHC.pgmm <- function(x, ...) { residuals <- x$residuals } else { + # transformation = "d" yX <- x$model residuals <- x$residuals } @@ -1243,7 +1244,7 @@ vcovHC.pgmm <- function(x, ...) { warning("a general inverse is used") ginv(A2) } else solve(A2) - + if(model == "twosteps") { coef1s <- x$coefficients[[1L]] res1s <- lapply(yX, function(x) x[ , 1L] - crossprod(t(x[ , -1L, drop = FALSE]), coef1s)) @@ -1254,7 +1255,6 @@ vcovHC.pgmm <- function(x, ...) { We <- Reduce("+", mapply(function(x, y) crossprod(x, y), x$W, residuals, SIMPLIFY = FALSE)) B1 <- solve(t(WX) %*% A1 %*% WX) B2 <- vcov(x) - vcov1s <- B1 %*% (t(WX) %*% A1 %*% SA2 %*% A1 %*% WX) %*% B1 for (k in 2:K) { exk <- mapply(