From d28ce9f8ad6f9573e403ebd8eb637ecd5e5b0e02 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Mon, 21 Dec 2020 06:02:10 +0100 Subject: plot.mixed: Possibility to overlay predictions --- R/plot.mixed.mmkin.R | 37 +++++++++++++++---- docs/dev/pkgdown.yml | 2 +- docs/dev/reference/Rplot001.png | Bin 1011 -> 19395 bytes docs/dev/reference/plot.mixed.mmkin-3.png | Bin 163488 -> 163536 bytes docs/dev/reference/plot.mixed.mmkin-4.png | Bin 0 -> 166687 bytes docs/dev/reference/plot.mixed.mmkin.html | 23 +++++++++--- docs/dev/reference/saem.html | 57 +++++++++++++++--------------- man/plot.mixed.mmkin.Rd | 13 ++++++- man/saem.Rd | 5 +++ 9 files changed, 95 insertions(+), 42 deletions(-) create mode 100644 docs/dev/reference/plot.mixed.mmkin-4.png diff --git a/R/plot.mixed.mmkin.R b/R/plot.mixed.mmkin.R index db29376e..109df283 100644 --- a/R/plot.mixed.mmkin.R +++ b/R/plot.mixed.mmkin.R @@ -8,6 +8,8 @@ utils::globalVariables("ds") #' @inheritParams plot.mkinfit #' @param standardized Should the residuals be standardized? Only takes effect if #' `resplot = "time"`. +#' @param pred_over Named list of alternative predictions as obtained +#' from [mkinpredict] with a compatible [mkinmod]. #' @param rel.height.legend The relative height of the legend shown on top #' @param rel.height.bottom The relative height of the bottom plot row #' @param ymax Vector of maximum y axis values @@ -37,8 +39,15 @@ utils::globalVariables("ds") #' f_nlme <- nlme(f, control = list(pnlsMaxIter = 120, tolerance = 1e-3)) #' plot(f_nlme) #' -#' f_saem <- saem(f) +#' f_saem <- saem(f, transformations = "saemix") #' plot(f_saem) +#' +#' # We can overlay the two variants if we generate predictions +#' pred_nlme <- mkinpredict(dfop_sfo, +#' f_nlme$bparms.optim[-1], +#' c(parent = f_nlme$bparms.optim[[1]], A1 = 0), +#' seq(0, 180, by = 0.2)) +#' plot(f_saem, pred_over = list(nlme = pred_nlme)) #' } #' @export plot.mixed.mmkin <- function(x, @@ -48,6 +57,7 @@ plot.mixed.mmkin <- function(x, xlab = "Time", xlim = range(x$data$time), resplot = c("predicted", "time"), + pred_over = NULL, ymax = "auto", maxabs = "auto", ncol.legend = ifelse(length(i) <= 3, length(i) + 1, ifelse(length(i) <= 8, 3, 4)), nrow.legend = ceiling((length(i) + 1) / ncol.legend), @@ -174,12 +184,19 @@ plot.mixed.mmkin <- function(x, par(mar = c(0.1, 2.1, 0.6, 2.1)) + # Empty plot with legend + if (!is.null(pred_over)) lty_over <- seq(2, length.out = length(pred_over)) + else lty_over <- NULL + n_pop <- 1 + length(lty_over) + lty_pop <- c(1, lty_over) + plot(0, type = "n", axes = FALSE, ann = FALSE) legend("center", bty = "n", ncol = ncol.legend, - legend = c("Population", ds_names[i]), - lty = c(1, lty_ds), lwd = c(2, rep(1, length(i))), - col = c(1, col_ds), - pch = c(NA, pch_ds)) + legend = c("Population", names(pred_over), ds_names[i]), + lty = c(lty_pop, lty_ds), + lwd = c(rep(2, n_pop), rep(1, length(i))), + col = c(rep(1, n_pop), col_ds), + pch = c(rep(NA, n_pop), pch_ds)) resplot <- match.arg(resplot) @@ -206,10 +223,18 @@ plot.mixed.mmkin <- function(x, } plot(pred_pop$time, pred_pop[[obs_var]], - type = "l", lwd = 2, + type = "l", lwd = 2, lty = lty_pop, xlim = xlim, ylim = ylim_row, xlab = xlab, ylab = obs_var, frame = frame) + if (!is.null(pred_over)) { + for (i_over in seq_along(pred_over)) { + pred_frame <- as.data.frame(pred_over[[i_over]]) + lines(pred_frame$time, pred_frame[[obs_var]], + lwd = 2, lty = lty_over[i_over]) + } + } + for (ds_i in seq_along(i)) { points(subset(observed_row, ds == ds_names[ds_i], c("time", "value")), col = col_ds[ds_i], pch = pch_ds[ds_i]) diff --git a/docs/dev/pkgdown.yml b/docs/dev/pkgdown.yml index b3124589..85a358df 100644 --- a/docs/dev/pkgdown.yml +++ b/docs/dev/pkgdown.yml @@ -10,7 +10,7 @@ articles: web_only/NAFTA_examples: NAFTA_examples.html web_only/benchmarks: benchmarks.html web_only/compiled_models: compiled_models.html -last_built: 2020-12-19T11:24Z +last_built: 2020-12-21T04:57Z urls: reference: https://pkgdown.jrwb.de/mkin/reference article: https://pkgdown.jrwb.de/mkin/articles diff --git a/docs/dev/reference/Rplot001.png b/docs/dev/reference/Rplot001.png index 17a35806..bca41e2c 100644 Binary files a/docs/dev/reference/Rplot001.png and b/docs/dev/reference/Rplot001.png differ diff --git a/docs/dev/reference/plot.mixed.mmkin-3.png b/docs/dev/reference/plot.mixed.mmkin-3.png index 3055c0c9..5e00afe6 100644 Binary files a/docs/dev/reference/plot.mixed.mmkin-3.png and b/docs/dev/reference/plot.mixed.mmkin-3.png differ diff --git a/docs/dev/reference/plot.mixed.mmkin-4.png b/docs/dev/reference/plot.mixed.mmkin-4.png new file mode 100644 index 00000000..6a5f3b9c Binary files /dev/null and b/docs/dev/reference/plot.mixed.mmkin-4.png differ diff --git a/docs/dev/reference/plot.mixed.mmkin.html b/docs/dev/reference/plot.mixed.mmkin.html index 90ba9184..55c411e7 100644 --- a/docs/dev/reference/plot.mixed.mmkin.html +++ b/docs/dev/reference/plot.mixed.mmkin.html @@ -121,7 +121,7 @@