Skip to content

Commit ce54111

Browse files
roninsightrxclaude
andcommitted
Guard against zero/non-finite sigma_diag in CWRES computation
When residual error variance is zero, negative, or non-finite for any observation (e.g. both error$prop and error$add are 0 for an obs_type), return cwres=NA and vcov=NULL with a warning identifying the offending observations, instead of silently producing Inf/NaN. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 287fb35 commit ce54111

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

R/calc_cwres.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ calc_cwres <- function(
114114
sigma_diag <- error$prop[obs_type]^2 * ipred_transf^2 +
115115
error$add[obs_type]^2
116116

117+
if (any(!is.finite(sigma_diag) | sigma_diag <= 0)) {
118+
bad <- which(!is.finite(sigma_diag) | sigma_diag <= 0)
119+
warning(
120+
"CWRES/vcov computation failed: residual error variance is zero, ",
121+
"negative, or non-finite for observation(s) ",
122+
paste(bad, collapse = ", "),
123+
" (obs_type: ", paste(unique(obs_type[bad]), collapse = ", "), ")."
124+
)
125+
return(list(cwres = rep(NA_real_, n_obs), vcov = NULL))
126+
}
127+
117128
# Omega submatrix for estimated parameters
118129
omega_est <- omega_full[seq_len(n_eta), seq_len(n_eta), drop = FALSE]
119130

0 commit comments

Comments
 (0)