Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Comment on lines +71 to +85
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for something more concise (you can take or leave). vctrs::vec_recycle_common() will throw an informative error if any of the lengths are different, e.g.:

ss(obs = 1:5, pred = 5:1, w = 1:3)
#> Error in `ss()`:
#> ! Can't recycle `obs` (size 5) to match `w` (size 3).
Suggested change
#' 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)
}
#' Weighted sum-of-squares of residuals
#'
#' @inheritParams rmse
#' @param w A numeric vector of weights the same size as `obs` and `pred`.
#' Vectors of size 1 will be recycled to the size of `obs` and `pred`.
#'
ss <- function(obs, pred, w = 1) {
args <- vctrs::vec_recycle_common(obs = obs, pred = pred, w = w)
if(sum(args[["w"]]) == 0) return(NA)
sum(args[["w"]] * (args[["obs"]] - args[["pred"]])^2)
}

Here are some tests you could add for this too:

test_that("recycling + incompatible size error works", {
  expect_equal(ss(obs = 1:3, pred = 3:1, w = 1), 8)
  expect_equal(ss(obs = 1:3, pred = 3:1, w = c(1, 1, 0.5)), 6)
  expect_error(
    ss(obs = 1:5, pred = 5:1, w = 1:3), class = "vctrs_error_incompatible_size"
  )
})

10 changes: 8 additions & 2 deletions R/run_eval_core.R
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)
Expand Down Expand Up @@ -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, w = NULL),
`_iteration` = iterations[i],
`_grouper` = obs_data$`_grouper`
)
Expand All @@ -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(
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions man/ss.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test-run_eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down