From a124d81454e81906890e116a4a3cd629bb9050a9 Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 19 Sep 2022 14:34:10 -0500 Subject: [PATCH] Fix vpcPlot() to work with update vpcSim objects (extract fit) --- R/vpcPlot.R | 28 ++++++++++++++++++---------- tests/testthat/test-plots.R | 2 ++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/R/vpcPlot.R b/R/vpcPlot.R index a0a5a6b..82787cc 100644 --- a/R/vpcPlot.R +++ b/R/vpcPlot.R @@ -62,6 +62,17 @@ vpcPlot <- function(fit, data = NULL, n = 300, bins = "jenks", idv="time", cens=FALSE) { force(idv) rxode2::rxReq("vpc") + # Simulate with VPC + if (inherits(fit, "nlmixr2vpcSim")) { + .sim <- fit + .fit <- attr(class(.sim), "fit") + .cls <- class(.fit) + .attr <- attr(.cls, ".foceiEnv") + .cls <- .cls[-1] + attr(.cls, ".foceiEnv") <- .attr + class(.fit) <- .cls + fit <- .fit + } .ui <- fit$ui .obsLst <- .vpcUiSetupObservationData(fit, data=data, idv=idv) .obs <- .obsLst$obs @@ -82,9 +93,7 @@ vpcPlot <- function(fit, data = NULL, n = 300, bins = "jenks", } } # Simulate with VPC - if (inherits(fit, "nlmixr2vpcSim")) { - .sim <- fit - } else { + if (!inherits(fit, "nlmixr2vpcSim")) { .sim <- nlmixr2est::vpcSim(fit, ..., keep=stratify, n=n, pred=pred_corr, seed=seed) } .sim <- nlmixr2est::vpcSimExpand(fit, .sim, stratify, .obs) @@ -146,13 +155,12 @@ vpcPlot <- function(fit, data = NULL, n = 300, bins = "jenks", stop("this data is not censored") } vpc::vpc_cens(sim=.sim, sim_cols=.simCols, - obs=.obs, obs_cols=.obsCols, - bins=bins, n_bins=n_bins, bin_mid=bin_mid, - show = show, stratify = stratify, - ci = ci, - uloq = uloq, lloq = lloq, - xlab = xlab, ylab = ylab, title = title, smooth = smooth, vpc_theme = vpc_theme, - facet = facet, labeller = labeller, vpcdb = vpcdb, verbose = verbose) + obs=.obs, obs_cols=.obsCols, + bins=bins, n_bins=n_bins, bin_mid=bin_mid, + show = show, stratify = stratify, ci = ci, + uloq = uloq, lloq = lloq, + xlab = xlab, ylab = ylab, title = title, smooth = smooth, vpc_theme = vpc_theme, + facet = facet, labeller = labeller, vpcdb = vpcdb, verbose = verbose) } else { vpc::vpc_vpc(sim=.sim, sim_cols=.simCols, obs=.obs, obs_cols=.obsCols, diff --git a/tests/testthat/test-plots.R b/tests/testthat/test-plots.R index 6ad3cb8..08f11f5 100644 --- a/tests/testthat/test-plots.R +++ b/tests/testthat/test-plots.R @@ -35,6 +35,8 @@ test_that("test plots with vdiffr", { est="focei", table=nlmixr2est::tableControl(npde=TRUE)) + fitSim <- nlmixr2est::vpcSim(fit) + apo <- nlmixr2est::augPred(fit) expect_error(plot(apo), NA) expect_error(vpcPlot(fit), NA)