From e09f726134ee273b6b191c1ec6465e6f701b082b Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Mon, 21 Oct 2019 18:52:24 +0200 Subject: Improve some plotting routines, more tests Static documentation rebuilt by pkgdown --- R/mkinresplot.R | 18 +- R/plot.mkinfit.R | 44 ++-- check.log | 8 +- docs/reference/index.html | 2 +- docs/reference/mkinresplot.html | 19 +- docs/reference/plot.mkinfit-2.png | Bin 52842 -> 57593 bytes docs/reference/plot.mkinfit-3.png | Bin 51545 -> 61280 bytes docs/reference/plot.mkinfit-5.png | Bin 59560 -> 59080 bytes docs/reference/plot.mkinfit.html | 12 +- docs/reference/synthetic_data_for_UBA_2014-1.png | Bin 62975 -> 62550 bytes docs/reference/synthetic_data_for_UBA_2014.html | 32 +-- man/mkinresplot.Rd | 32 ++- man/plot.mkinfit.Rd | 15 +- man/synthetic_data_for_UBA_2014.Rd | 10 +- test.log | 10 +- .../plot-errmod-with-sfo-lin-a.svg | 237 +++++++++++++++++++++ .../mkinfit-plot-for-focus-c-with-sep-true.svg | 44 ++-- tests/figs/plotting/plot-err-for-focus-d.svg | 200 +++++++++++++++++ tests/figs/plotting/plot-res-for-focus-d.svg | 196 +++++++++++++++++ tests/testthat/test_error_models.R | 3 + tests/testthat/test_plots_summary_twa.R | 8 +- 21 files changed, 792 insertions(+), 98 deletions(-) create mode 100644 tests/figs/error-model-fitting/plot-errmod-with-sfo-lin-a.svg create mode 100644 tests/figs/plotting/plot-err-for-focus-d.svg create mode 100644 tests/figs/plotting/plot-res-for-focus-d.svg diff --git a/R/mkinresplot.R b/R/mkinresplot.R index 739a80e9..974d0549 100644 --- a/R/mkinresplot.R +++ b/R/mkinresplot.R @@ -1,4 +1,4 @@ -# Copyright (C) 2008-2014 Johannes Ranke +# Copyright (C) 2008-2014,2019 Johannes Ranke # Contact: jranke@uni-bremen.de # This file is part of the R package mkin @@ -21,7 +21,10 @@ mkinresplot <- function (object, obs_vars = names(object$mkinmod$map), xlim = c(0, 1.1 * max(object$data$time)), xlab = "Time", ylab = "Residual", - maxabs = "auto", legend= TRUE, lpos = "topright", ...) + maxabs = "auto", legend= TRUE, lpos = "topright", + col_obs = "auto", pch_obs = "auto", + frame = TRUE, + ...) { obs_vars_all <- as.character(unique(object$data$variable)) @@ -33,10 +36,17 @@ mkinresplot <- function (object, if (maxabs == "auto") maxabs = max(abs(residuals), na.rm = TRUE) - col_obs <- pch_obs <- 1:length(obs_vars) + # Set colors and symbols + if (col_obs[1] == "auto") { + col_obs <- 1:length(obs_vars) + } + + if (pch_obs[1] == "auto") { + pch_obs <- 1:length(obs_vars) + } names(col_obs) <- names(pch_obs) <- obs_vars - plot(0, type = "n", + plot(0, type = "n", frame = frame, xlab = xlab, ylab = ylab, xlim = xlim, ylim = c(-1.2 * maxabs, 1.2 * maxabs), ...) diff --git a/R/plot.mkinfit.R b/R/plot.mkinfit.R index 2a82301f..e39da416 100644 --- a/R/plot.mkinfit.R +++ b/R/plot.mkinfit.R @@ -30,8 +30,9 @@ plot.mkinfit <- function(x, fit = x, show_errplot = FALSE, maxabs = "auto", sep_obs = FALSE, rel.height.middle = 0.9, + row_layout = FALSE, lpos = "topright", inset = c(0.05, 0.05), - show_errmin = FALSE, errmin_digits = 3, + show_errmin = FALSE, errmin_digits = 3, frame = TRUE, ...) { if (add && show_residuals) stop("If adding to an existing plot we can not show residuals") @@ -80,8 +81,9 @@ plot.mkinfit <- function(x, fit = x, # Layout should be restored afterwards oldpar <- par(no.readonly = TRUE) - # If the observed variables are shown separately, do row layout - if (sep_obs) { + # If the observed variables are shown separately, or if requested, do row layout + if (sep_obs | row_layout) { + row_layout <- TRUE n_plot_cols = if (show_residuals | show_errplot) 2 else 1 n_plots = n_plot_rows * n_plot_cols @@ -114,7 +116,7 @@ plot.mkinfit <- function(x, fit = x, ylim_row = ylim } - if (sep_obs) { + if (row_layout) { # Margins for top row of plots when we have more than one row # Reduce bottom margin by 2.1 - hides x axis legend if (plot_row == 1 & n_plot_rows > 1) { @@ -186,24 +188,14 @@ plot.mkinfit <- function(x, fit = x, mtext(chi2_text, cex = 0.7, line = 0.4) } + if (do_layout & !row_layout) { + par(mar = c(5, 4, 0, 2) + 0.1) + } + # Show residuals if requested if (show_residuals) { - residuals <- subset(fit$data, variable %in% row_obs_vars, residual) - if (maxabs == "auto") { - maxabs_row = max(abs(residuals), na.rm = TRUE) - } else { - maxabs_row = maxabs - } - if (!sep_obs) par(mar = c(5, 4, 0, 2) + 0.1) - plot(0, type="n", - xlim = xlim, - ylim = c(-1.2 * maxabs_row, 1.2 * maxabs_row), - xlab = xlab, ylab = "Residuals", frame = frame) - for(obs_var in row_obs_vars){ - residuals_plot <- subset(fit$data, variable == obs_var, c("time", "residual")) - points(residuals_plot, pch = pch_obs[obs_var], col = col_obs[obs_var]) - } - abline(h = 0, lty = 2) + mkinresplot(fit, obs_vars = row_obs_vars, pch_obs = pch_obs[row_obs_vars], col_obs = col_obs[row_obs_vars], + legend = FALSE, frame = frame) } # Show error model plot if requested @@ -216,7 +208,15 @@ plot.mkinfit <- function(x, fit = x, } # Convenience function for switching on some features of mkinfit # that have not been made the default to keep compatibility -plot_sep <- function(fit, sep_obs = TRUE, show_residuals = TRUE, show_errmin = TRUE, ...) { +plot_sep <- function(fit, show_errmin = TRUE, ...) { plot.mkinfit(fit, sep_obs = TRUE, show_residuals = TRUE, - show_errmin = TRUE, ...) + show_errmin = show_errmin, ...) +} +plot_res <- function(fit, sep_obs = FALSE, show_errmin = sep_obs, ...) { + plot.mkinfit(fit, sep_obs = sep_obs, show_errmin = show_errmin, + show_residuals = TRUE, row_layout = TRUE, ...) +} +plot_err <- function(fit, sep_obs = FALSE, show_errmin = sep_obs, ...) { + plot.mkinfit(fit, sep_obs = sep_obs, show_errmin = show_errmin, + show_errplot = TRUE, row_layout = TRUE, ...) } diff --git a/check.log b/check.log index d2a6122d..9ef5c22d 100644 --- a/check.log +++ b/check.log @@ -24,9 +24,7 @@ Maintainer: ‘Johannes Ranke ’ * checking for future file timestamps ... OK * checking ‘build’ directory ... OK * checking DESCRIPTION meta-information ... OK -* checking top-level files ... NOTE -Non-standard file/directory found at top level: - ‘tests_slow.log’ +* checking top-level files ... OK * checking for left-over files ... OK * checking index information ... OK * checking package subdirectories ... OK @@ -61,7 +59,7 @@ Non-standard file/directory found at top level: * checking examples ... NOTE Examples with CPU or elapsed time > 5s user system elapsed -synthetic_data_for_UBA_2014 9.778 0.239 10.019 +synthetic_data_for_UBA_2014 9.755 0.273 10.029 * checking for unstated dependencies in ‘tests’ ... OK * checking tests ... SKIPPED * checking for unstated dependencies in vignettes ... OK @@ -70,7 +68,7 @@ synthetic_data_for_UBA_2014 9.778 0.239 10.019 * checking PDF version of manual ... OK * DONE -Status: 2 NOTEs +Status: 1 NOTE See ‘/home/jranke/git/mkin/mkin.Rcheck/00check.log’ for details. diff --git a/docs/reference/index.html b/docs/reference/index.html index 08ad3d1a..e74a8ad5 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -175,7 +175,7 @@ -

plot(<mkinfit>) plot_sep()

+

plot(<mkinfit>) plot_sep() plot_res() plot_err()

Plot the observed data and the fitted model of an mkinfit object

diff --git a/docs/reference/mkinresplot.html b/docs/reference/mkinresplot.html index d841909f..eea5b218 100644 --- a/docs/reference/mkinresplot.html +++ b/docs/reference/mkinresplot.html @@ -146,7 +146,10 @@ obs_vars = names(object$mkinmod$map), xlim = c(0, 1.1 * max(object$data$time)), xlab = "Time", ylab = "Residual", - maxabs = "auto", legend = TRUE, lpos = "topright", ...) + maxabs = "auto", legend = TRUE, lpos = "topright", + col_obs = "auto", pch_obs = "auto", + frame = TRUE, + ...)

Arguments

@@ -184,7 +187,19 @@ + legend.

+ + + + + + + + + + + + diff --git a/docs/reference/plot.mkinfit-2.png b/docs/reference/plot.mkinfit-2.png index 84bec348..6910ae47 100644 Binary files a/docs/reference/plot.mkinfit-2.png and b/docs/reference/plot.mkinfit-2.png differ diff --git a/docs/reference/plot.mkinfit-3.png b/docs/reference/plot.mkinfit-3.png index 557046da..8d399598 100644 Binary files a/docs/reference/plot.mkinfit-3.png and b/docs/reference/plot.mkinfit-3.png differ diff --git a/docs/reference/plot.mkinfit-5.png b/docs/reference/plot.mkinfit-5.png index 36fcff6a..20f30221 100644 Binary files a/docs/reference/plot.mkinfit-5.png and b/docs/reference/plot.mkinfit-5.png differ diff --git a/docs/reference/plot.mkinfit.html b/docs/reference/plot.mkinfit.html index 795262f8..df01a00b 100644 --- a/docs/reference/plot.mkinfit.html +++ b/docs/reference/plot.mkinfit.html @@ -159,9 +159,12 @@ If the current plot device is a tikz device, show_errplot=FALSE, maxabs="auto", sep_obs=FALSE, rel.height.middle=0.9, + row_layout=FALSE, lpos="topright", inset=c(0.05, 0.05), show_errmin=FALSE, errmin_digits=3, frame=TRUE, ...) -plot_sep(fit, sep_obs=TRUE, show_residuals=TRUE, show_errmin=TRUE, ...) +plot_sep(fit, show_errmin=TRUE, ...) +plot_res(fit, sep_obs=FALSE, show_errmin=sep_obs, ...) +plot_err(fit, sep_obs=FALSE, show_errmin=sep_obs, ...)

Arguments

lpos

Where should the legend be placed? Default is "topright". Will be passed on to - legend.

col_obs

Colors for the observed variables.

pch_obs

Symbols to be used for the observed variables.

frame

Should a frame be drawn around the plots?

...
@@ -244,6 +247,11 @@ If the current plot device is a tikz device, + + + +
rel.height.middle

The relative height of the middle plot, if more than two rows of plots are shown.

row_layout

Should we use a row layout where the residual plot or the error model plot is shown + to the right?

lpos

Position(s) of the legend(s). Passed to legend as the first argument. @@ -281,7 +289,7 @@ If the current plot device is a tikz device, # parent to sink included # \dontrun{ SFO_SFO <- mkinmod(parent = mkinsub("SFO", "m1", full = "Parent"), - m1 = mkinsub("SFO", full = "Metabolite M1" ))

#> Successfully compiled differential equation model from auto-generated C code.
fit <- mkinfit(SFO_SFO, FOCUS_2006_D, quiet = TRUE, error_model = "tc")
#> Warning: Observations with value of zero were removed from the data
plot(fit)
plot(fit, show_residuals = TRUE)
plot(fit, show_errplot = TRUE)
+ m1 = mkinsub("SFO", full = "Metabolite M1" ))
#> Successfully compiled differential equation model from auto-generated C code.
fit <- mkinfit(SFO_SFO, FOCUS_2006_D, quiet = TRUE, error_model = "tc")
#> Warning: Observations with value of zero were removed from the data
plot(fit)
plot_res(fit)
plot_err(fit)
# Show the observed variables separately plot(fit, sep_obs = TRUE, lpos = c("topright", "bottomright"))
# Show the observed variables separately, with residuals diff --git a/docs/reference/synthetic_data_for_UBA_2014-1.png b/docs/reference/synthetic_data_for_UBA_2014-1.png index 9d8c0931..201ef0fd 100644 Binary files a/docs/reference/synthetic_data_for_UBA_2014-1.png and b/docs/reference/synthetic_data_for_UBA_2014-1.png differ diff --git a/docs/reference/synthetic_data_for_UBA_2014.html b/docs/reference/synthetic_data_for_UBA_2014.html index a639151b..ddc88c21 100644 --- a/docs/reference/synthetic_data_for_UBA_2014.html +++ b/docs/reference/synthetic_data_for_UBA_2014.html @@ -278,12 +278,13 @@ Compare also the code in the example section to see the degradation models." /> # This is just one example of an evaluation using the kinetic model used for # the generation of the data -fit <- mkinfit(m_synth_SFO_lin, synthetic_data_for_UBA_2014[[1]]$data, - quiet = TRUE) -plot_sep(fit)
summary(fit)
#> mkin version used for fitting: 0.9.49.6 +# \dontrun{ + fit <- mkinfit(m_synth_SFO_lin, synthetic_data_for_UBA_2014[[1]]$data, + quiet = TRUE) + plot_sep(fit)
summary(fit)
#> mkin version used for fitting: 0.9.49.6 #> R version used for fitting: 3.6.1 -#> Date of fit: Thu Sep 19 12:48:50 2019 -#> Date of summary: Thu Sep 19 12:48:50 2019 +#> Date of fit: Mon Oct 21 18:52:20 2019 +#> Date of summary: Mon Oct 21 18:52:20 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -292,21 +293,20 @@ Compare also the code in the example section to see the degradation models." /> #> #> Model predictions using solution type deSolve #> -#> Fitted using 847 model solutions performed in 2.49 s +#> Fitted using 847 model solutions performed in 2.42 s #> #> Error model: Constant variance #> #> Error model algorithm: OLS #> #> Starting values for parameters to be optimised: -#> value type -#> parent_0 101.350000 state -#> k_parent 0.100000 deparm -#> k_M1 0.100100 deparm -#> k_M2 0.100200 deparm -#> f_parent_to_M1 0.500000 deparm -#> f_M1_to_M2 0.500000 deparm -#> sigma 2.273126 error +#> value type +#> parent_0 101.3500 state +#> k_parent 0.1000 deparm +#> k_M1 0.1001 deparm +#> k_M2 0.1002 deparm +#> f_parent_to_M1 0.5000 deparm +#> f_M1_to_M2 0.5000 deparm #> #> Starting values for the transformed parameters actually optimised: #> value lower upper @@ -316,7 +316,6 @@ Compare also the code in the example section to see the degradation models." /> #> log_k_M2 -2.300587 -Inf Inf #> f_parent_ilr_1 0.000000 -Inf Inf #> f_M1_ilr_1 0.000000 -Inf Inf -#> sigma 2.273126 0 Inf #> #> Fixed parameter values: #> value type @@ -424,7 +423,8 @@ Compare also the code in the example section to see the degradation models." /> #> 90 M2 10.6 1.013e+01 0.47130 #> 90 M2 10.8 1.013e+01 0.67130 #> 120 M2 9.8 5.521e+00 4.27893 -#> 120 M2 3.3 5.521e+00 -2.22107
+#> 120 M2 3.3 5.521e+00 -2.22107
# } +