From 3b25a9e25aabbfe0b3f864e27e4ccb8389bda886 Mon Sep 17 00:00:00 2001 From: tappek <77916431+tappek@users.noreply.github.com> Date: Sat, 28 Dec 2024 00:29:44 +0100 Subject: [PATCH] re-arranged code for sake of clarity --- R/est_gmm.R | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/R/est_gmm.R b/R/est_gmm.R index 970524c..5376450 100755 --- a/R/est_gmm.R +++ b/R/est_gmm.R @@ -514,12 +514,6 @@ pgmm <- function(formula, data, subset, na.action, W <- W1 yX <- yX1 - - # Compute the first step matrices - 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 ## WX <- mapply(function(x, y) crossprod(x, y), W, yX, SIMPLIFY = FALSE) ## WX <- Reduce("+", WX) @@ -529,6 +523,14 @@ pgmm <- function(formula, data, subset, na.action, WX <- mapply(function(x, y) crossprod(x, y), W, yX, SIMPLIFY = FALSE) Wy <- lapply(WX, function(x) x[ , 1L]) WX <- lapply(WX, function(x) x[ , -1L, drop = FALSE]) + WX <- Reduce("+", WX) + Wy <- Reduce("+", Wy) + + # Compute the first step matrices + A1 <- switch(fsm, + "d" = FSM(T - TL1, "G"), # == tcrossprod(diff(diag(1, T - TL1 + 1))) # TODO: FSM's arg fsm not fully flexible + "ld" = FSM(T - TL2, fsm)) + A1 <- lapply(W, function(x) crossprod(t(crossprod(x, A1)), x)) A1 <- Reduce("+", A1) minevA1 <- min(eigen(A1)$values) @@ -538,9 +540,7 @@ pgmm <- function(formula, data, subset, na.action, ginv(A1) } else solve(A1) A1 <- A1 * length(W) - - WX <- Reduce("+", WX) - Wy <- Reduce("+", Wy) + t.CP.WX.A1 <- t(crossprod(WX, A1)) B1 <- solve(crossprod(WX, t.CP.WX.A1)) Y1 <- crossprod(t.CP.WX.A1, Wy) @@ -693,7 +693,7 @@ extract.data <- function(data, as.matrix = TRUE){ # the previous version is *very* slow because : # 1. split works wrong on pdata.frame # 2. model.matrix is lapplied ! - ### -> using collpse's fast *split functions / 2024-12-27 + ### -> using collapse's fast *split functions / 2024-12-27 form <- attr(data, "formula") trms <- terms(form) has.response <- attr(trms, 'response') == 1