Skip to content

Commit

Permalink
pgmm: use FSM() also for transformation = "d" (not fully flexible yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
tappek committed Dec 26, 2024
1 parent 0db7fd3 commit bac62b2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions R/est_gmm.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -552,15 +552,16 @@ 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)
A2 <- if (minevA2 < eps) {
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))
Expand Down
4 changes: 2 additions & 2 deletions R/tool_vcovG.R
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,7 @@ vcovHC.pgmm <- function(x, ...) {
residuals <- x$residuals
}
else {
# transformation = "d"
yX <- x$model
residuals <- x$residuals
}
Expand All @@ -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))
Expand All @@ -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(
Expand Down

0 comments on commit bac62b2

Please sign in to comment.