From 414fb742a31979b8d466585c63224c02f233245e Mon Sep 17 00:00:00 2001 From: Ron Keizer Date: Thu, 9 Oct 2025 23:46:37 +0000 Subject: [PATCH 1/3] add ofv and ss --- R/misc.R | 16 ++++++++++++++++ R/run_eval_core.R | 10 ++++++++-- man/ss.Rd | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 man/ss.Rd diff --git a/R/misc.R b/R/misc.R index ac76d0e..d0a1ab0 100644 --- a/R/misc.R +++ b/R/misc.R @@ -67,3 +67,19 @@ mape <- function (obs, pred) { mpe <- function (obs, pred) { sum((obs - pred)/obs)/length(obs) } + +#' Weighted sum-of-squares of residuals +#' +#' @inheritParams rmse +#' @param w weights +#' +ss <- function(obs, pred, w = NULL) { + if(is.null(w)) { + w <- rep(1, length(obs)) + } + if (length(obs) != length(pred) || length(obs) != length(w)) { + cli::cli_abort("`obs`, `pred`, and `w` must have the same length") + } + if(sum(w) == 0) return(NA) + sum(w * (obs - pred)^2) +} diff --git a/R/run_eval_core.R b/R/run_eval_core.R index e31fd64..b2c722c 100644 --- a/R/run_eval_core.R +++ b/R/run_eval_core.R @@ -75,6 +75,8 @@ run_eval_core <- function( dv = fit$dv, ipred = fit$ipred, pred = fit$pred, + ofv = fit$fit$value, + ss_w = ss(fit$dv, fit$ipred, weights), `_iteration` = iterations[i], `_grouper` = obs_data$`_grouper` ) @@ -111,6 +113,8 @@ run_eval_core <- function( dv = fit_map$dv, ipred = fit_map$ipred, pred = fit_map$pred, + ofv = fit_map$fit$value, + ss_w = ss(fit_map$dv, fit_map$ipred, NULL), `_iteration` = iterations[i], `_grouper` = obs_data$`_grouper` ) @@ -127,7 +131,9 @@ run_eval_core <- function( dplyr::filter(.data$`_iteration` == 1) |> dplyr::mutate( `_iteration` = 0, - ipred = .data$pred + ipred = .data$pred, + ofv = NA, + ss_w = NA ) |> # set to population parameters, not individual estimates dplyr::select(-!!names(mod_obj$parameters)) |> dplyr::left_join( @@ -144,12 +150,12 @@ run_eval_core <- function( ) |> dplyr::select( "id", "_iteration", "_grouper", "t", "dv", "pred", "map_ipred", + "ofv", "ss_w", "iter_ipred", "apriori", !!names(mod_obj$parameters) ) out - } #' Handle covariate censoring diff --git a/man/ss.Rd b/man/ss.Rd new file mode 100644 index 0000000..aef365c --- /dev/null +++ b/man/ss.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/misc.R +\name{ss} +\alias{ss} +\title{Weighted sum-of-squares of residuals} +\usage{ +ss(obs, pred, w = NULL) +} +\arguments{ +\item{obs}{observations vector} + +\item{pred}{predictions vector} + +\item{w}{weights} +} +\description{ +Weighted sum-of-squares of residuals +} From 46e1d0edb8ceee8bf2d6866362f47dec45ce447e Mon Sep 17 00:00:00 2001 From: Ron Keizer Date: Thu, 9 Oct 2025 23:53:21 +0000 Subject: [PATCH 2/3] add test --- tests/testthat/test-run_eval.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/testthat/test-run_eval.R b/tests/testthat/test-run_eval.R index 7721b17..4f50953 100644 --- a/tests/testthat/test-run_eval.R +++ b/tests/testthat/test-run_eval.R @@ -19,10 +19,24 @@ test_that("Basic run with vanco data + model works", { expect_equal(names(res), c("results", "mod_obj", "data", "sim", "stats_summ", "shrinkage", "bayesian_impact")) expect_s3_class(res$results, c("tbl_df", "tbl", "data.frame")) expect_s3_class(res$stats_summ, c("tbl_df", "tbl", "data.frame")) + expect_equal( + names(res$results), + c("id", "_iteration", "_grouper", "t", "dv", "pred", "map_ipred", + "ofv", "ss_w", "iter_ipred", "apriori", "CL", "V", "TH_CRCL", + "Q", "V2") + ) expect_equal( round(res$results$CL[1:5], 3), c(2.99, 2.685, 2.462, 2.430, 2.439) ) + expect_equal( + round(res$results$ofv[1:5], 3), + c(NA, 6.196, 11.846, 17.046, 21.563) + ) + expect_equal( + round(res$results$ss_w[1:5], 3), + c(NA, 16.839, 11.241, 27.358, 30.944) + ) # Using PKPDsim model library: res_2 <- run_eval( From 1c4546b55f344dac30b77007e90cfd0340adafab Mon Sep 17 00:00:00 2001 From: roninsightrx Date: Fri, 10 Oct 2025 12:11:23 -0700 Subject: [PATCH 3/3] Update R/run_eval_core.R Co-authored-by: Michael McCarthy <51542091+mccarthy-m-g@users.noreply.github.com> --- R/run_eval_core.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/run_eval_core.R b/R/run_eval_core.R index b2c722c..08bbdf9 100644 --- a/R/run_eval_core.R +++ b/R/run_eval_core.R @@ -114,7 +114,7 @@ run_eval_core <- function( ipred = fit_map$ipred, pred = fit_map$pred, ofv = fit_map$fit$value, - ss_w = ss(fit_map$dv, fit_map$ipred, NULL), + ss_w = ss(fit_map$dv, fit_map$ipred, w = NULL), `_iteration` = iterations[i], `_grouper` = obs_data$`_grouper` )