From 40e7374bd72a4eae0da535807ecf21afc701af0c Mon Sep 17 00:00:00 2001 From: Ron Keizer Date: Wed, 7 Jan 2026 22:03:49 -0800 Subject: [PATCH 1/2] fix sim when no CMT col in data --- R/run_sim.R | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/R/run_sim.R b/R/run_sim.R index 934bbc6..7ab5e8c 100644 --- a/R/run_sim.R +++ b/R/run_sim.R @@ -101,6 +101,10 @@ run_sim <- function( } else { input_data <- data } + ## Set CMT to NA if not in dataset + if(is.null(input_data[[dictionary$CMT]])) { + input_data[[dictionary$CMT]] <- NA + } if(is.null(covariates)) { # use original dataset if(verbose) cli::cli_alert_info("Using input dataset for simulation") sim_data <- input_data @@ -174,7 +178,8 @@ run_sim <- function( dplyr::arrange(.regimen, ID, TIME, EVID) |> dplyr::group_by(ID) |> tidyr::fill( - dplyr::everything(), + tidyselect::everything(), + -CMT, .direction = "downup" ) if(is.null(t_obs)) { @@ -191,7 +196,8 @@ run_sim <- function( sim_data, t_obs, n_subjects, - dictionary + dictionary, + model ) ## remove old obs and add new sim_data <- sim_data |> @@ -395,15 +401,19 @@ create_obs_records <- function( data, t_obs, n_subjects, - dictionary + dictionary, + model ) { unq_reg <- unique(data[[".regimen"]]) ## create a template row + ## 1st try pull CMT from data. if not available in data, try based on ADVAN cmt <- data |> dplyr::filter(ID == 1 & EVID == 0) |> dplyr::slice(1) |> dplyr::pull(CMT) - if(is.null(cmt)) cmt <- 1 + if(is.null(cmt) || is.na(cmt)) { + cmt <- get_obs_compartment(model) + } obs <- data.frame( ID = 1, TIME = t_obs, From aa742ba20e1315e622278c92e16a62374100023b Mon Sep 17 00:00:00 2001 From: Ron Keizer Date: Wed, 7 Jan 2026 22:10:58 -0800 Subject: [PATCH 2/2] add new test placeholder --- tests/testthat/test_run_sim.R | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/testthat/test_run_sim.R diff --git a/tests/testthat/test_run_sim.R b/tests/testthat/test_run_sim.R new file mode 100644 index 0000000..51221f6 --- /dev/null +++ b/tests/testthat/test_run_sim.R @@ -0,0 +1,9 @@ +## TODO: add tests for simulations +## +## - basic simulations +## - test case for this fix: https://github.com/InsightRX/luna/pull/56 + +test_that( + "dummy check", + expect_true(TRUE) +)