Skip to content

Commit f4df117

Browse files
roninsightrxclaude
andcommitted
Guard FOCE vcov against weight_prior_var <= 0
When the prior is flat (weight_prior=0) the scaled omega is undefined and the FOCE Hessian H = (Omega/w)^-1 + F'Sigma^-1*F is not meaningful. Return vcov = NULL in that case so the caller falls back to omega$full cleanly. CWRES itself is unaffected (uses unscaled omega). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 7b8a385 commit f4df117

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

R/calc_cwres.R

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,20 @@ calc_cwres <- function(
154154
# estimation. vcov of etas = H^{-1}.
155155
# This reuses the Jacobian F already computed for CWRES, so no extra
156156
# simulations are needed (replaces the numDeriv::hessian computation).
157-
omega_scaled <- omega_est / weight_prior_var
158-
omega_scaled_inv <- tryCatch(solve(omega_scaled), error = function(e) NULL)
157+
# With weight_prior_var <= 0 the prior is effectively flat (LS fit), so
158+
# the FOCE vcov is not meaningful; skip and let the caller fall back.
159159
vcov <- NULL
160-
if (!is.null(omega_scaled_inv)) {
161-
H_foce <- omega_scaled_inv +
162-
t(F_matrix) %*% diag(1 / sigma_diag, nrow = n_obs) %*% F_matrix
163-
vcov <- tryCatch(solve(H_foce), error = function(e) {
164-
warning("FOCE variance-covariance computation failed.")
165-
NULL
166-
})
160+
if (weight_prior_var > 0) {
161+
omega_scaled <- omega_est / weight_prior_var
162+
omega_scaled_inv <- tryCatch(solve(omega_scaled), error = function(e) NULL)
163+
if (!is.null(omega_scaled_inv)) {
164+
H_foce <- omega_scaled_inv +
165+
t(F_matrix) %*% diag(1 / sigma_diag, nrow = n_obs) %*% F_matrix
166+
vcov <- tryCatch(solve(H_foce), error = function(e) {
167+
warning("FOCE variance-covariance computation failed.")
168+
NULL
169+
})
170+
}
167171
}
168172

169173
list(cwres = cwres, vcov = vcov)

0 commit comments

Comments
 (0)