From c6079a807e2b400fe0c772603392aeacd887da2f Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Wed, 8 May 2019 20:57:48 +0200 Subject: Add functionality to plot the error model by plotting squared residuals against predicted values, and showing the variance function used in the fitted error model. Rebuild docs --- docs/reference/mkinerrplot-1.png | Bin 0 -> 35936 bytes docs/reference/mkinerrplot.html | 250 ++++++++++++++++++++++++++++++++++++ docs/reference/mkinfit.html | 28 ++-- docs/reference/mkinmod.html | 2 +- docs/reference/mkinpredict.html | 6 +- docs/reference/mmkin.html | 4 +- docs/reference/plot.mkinfit-1.png | Bin 45182 -> 45207 bytes docs/reference/plot.mkinfit-2.png | Bin 52916 -> 52884 bytes docs/reference/plot.mkinfit-3.png | Bin 43718 -> 51737 bytes docs/reference/plot.mkinfit-4.png | Bin 59800 -> 43712 bytes docs/reference/plot.mkinfit-5.png | Bin 0 -> 59934 bytes docs/reference/plot.mkinfit-6.png | Bin 0 -> 65811 bytes docs/reference/plot.mkinfit.html | 29 +++-- docs/reference/plot.mmkin-2.png | Bin 34829 -> 34725 bytes docs/reference/plot.mmkin-4.png | Bin 0 -> 37126 bytes docs/reference/plot.mmkin.html | 19 ++- docs/reference/summary.mkinfit.html | 6 +- 17 files changed, 308 insertions(+), 36 deletions(-) create mode 100644 docs/reference/mkinerrplot-1.png create mode 100644 docs/reference/mkinerrplot.html create mode 100644 docs/reference/plot.mkinfit-5.png create mode 100644 docs/reference/plot.mkinfit-6.png create mode 100644 docs/reference/plot.mmkin-4.png (limited to 'docs/reference') diff --git a/docs/reference/mkinerrplot-1.png b/docs/reference/mkinerrplot-1.png new file mode 100644 index 00000000..9c663646 Binary files /dev/null and b/docs/reference/mkinerrplot-1.png differ diff --git a/docs/reference/mkinerrplot.html b/docs/reference/mkinerrplot.html new file mode 100644 index 00000000..66da40b5 --- /dev/null +++ b/docs/reference/mkinerrplot.html @@ -0,0 +1,250 @@ + + + + + + + + +Function to plot squared residuals and the error model for an mkin object — mkinerrplot • mkin + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + +
+ +
+
+ + +
+ +

This function plots the squared residuals for the specified subset of the + observed variables from an mkinfit object. In addition, one or more + dashed line(s) show the fitted error model. + A combined plot of the fitted model and this error model plot can be + obtained with plot.mkinfit + using the argument show_errplot = TRUE.

+ +
+ +
mkinerrplot(object,
+    obs_vars = names(object$mkinmod$map),
+    xlim = c(0, 1.1 * max(object$data$predicted)),
+    xlab = "Predicted", ylab = "Squared residual",
+    maxy = "auto", legend= TRUE, lpos = "topright",
+    col_obs = "auto", pch_obs = "auto",
+    ...)
+ +

Arguments

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
object

A fit represented in an mkinfit object.

obs_vars

A character vector of names of the observed variables for which residuals + should be plotted. Defaults to all observed variables in the model

xlim

plot range in x direction.

xlab

Label for the x axis.

ylab

Label for the y axis.

maxy

Maximum value of the residuals. This is used for the scaling of + the y axis and defaults to "auto".

legend

Should a legend be plotted?

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.

further arguments passed to plot.

+ +

Value

+ +

Nothing is returned by this function, as it is called for its side effect, namely to produce a plot.

+ +

See also

+ +

mkinplot, for a way to plot the data and the fitted lines of the + mkinfit object.

+ + +

Examples

+
model <- mkinmod(parent = mkinsub("SFO", "m1"), m1 = mkinsub("SFO"))
#> Successfully compiled differential equation model from auto-generated C code.
fit <- mkinfit(model, FOCUS_2006_D, error_model = "tc", quiet = TRUE)
#> Warning: Observations with value of zero were removed from the data
mkinerrplot(fit)
+
+ +
+ +
+ + +
+

Site built with pkgdown 1.3.0.9000.

+
+
+
+ + + + + + diff --git a/docs/reference/mkinfit.html b/docs/reference/mkinfit.html index 09329a86..bfca44fc 100644 --- a/docs/reference/mkinfit.html +++ b/docs/reference/mkinfit.html @@ -362,15 +362,15 @@ Per default, parameters in the kinetic models are internally transformed in fit <- mkinfit("FOMC", FOCUS_2006_C, quiet = TRUE) summary(fit)
#> mkin version used for fitting: 0.9.49.4 #> R version used for fitting: 3.6.0 -#> Date of fit: Tue May 7 08:36:16 2019 -#> Date of summary: Tue May 7 08:36:16 2019 +#> Date of fit: Wed May 8 20:50:50 2019 +#> Date of summary: Wed May 8 20:50:50 2019 #> #> Equations: #> d_parent/dt = - (alpha/beta) * 1/((time/beta) + 1) * parent #> #> Model predictions using solution type analytical #> -#> Fitted using 222 model solutions performed in 0.89 s +#> Fitted using 222 model solutions performed in 0.456 s #> #> Error model: #> Constant variance @@ -443,7 +443,7 @@ Per default, parameters in the kinetic models are internally transformed in m1 = mkinsub("SFO"))
#> Successfully compiled differential equation model from auto-generated C code.
# Fit the model to the FOCUS example dataset D using defaults print(system.time(fit <- mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "eigen", quiet = TRUE)))
#> Warning: Observations with value of zero were removed from the data
#> User System verstrichen -#> 2.251 0.000 2.253
coef(fit)
#> NULL
endpoints(fit)
#> $ff +#> 1.488 0.000 1.488
coef(fit)
#> NULL
endpoints(fit)
#> $ff #> parent_sink parent_m1 m1_sink #> 0.485524 0.514476 1.000000 #> @@ -515,7 +515,7 @@ Per default, parameters in the kinetic models are internally transformed in #> Sum of squared residuals at call 126: 371.2134 #> Sum of squared residuals at call 135: 371.2134 #> Negative log-likelihood at call 145: 97.22429
#> Optimisation successfully terminated.
#> User System verstrichen -#> 1.151 0.000 1.152
coef(fit.deSolve)
#> NULL
endpoints(fit.deSolve)
#> $ff +#> 1.086 0.000 1.087
coef(fit.deSolve)
#> NULL
endpoints(fit.deSolve)
#> $ff #> parent_sink parent_m1 m1_sink #> 0.485524 0.514476 1.000000 #> @@ -547,8 +547,8 @@ Per default, parameters in the kinetic models are internally transformed in SFO_SFO.ff <- mkinmod(parent = mkinsub("SFO", "m1"), m1 = mkinsub("SFO"), use_of_ff = "max")
#> Successfully compiled differential equation model from auto-generated C code.
f.noweight <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, quiet = TRUE)
#> Warning: Observations with value of zero were removed from the data
summary(f.noweight)
#> mkin version used for fitting: 0.9.49.4 #> R version used for fitting: 3.6.0 -#> Date of fit: Tue May 7 08:36:33 2019 -#> Date of summary: Tue May 7 08:36:33 2019 +#> Date of fit: Wed May 8 20:51:06 2019 +#> Date of summary: Wed May 8 20:51:06 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -556,7 +556,7 @@ Per default, parameters in the kinetic models are internally transformed in #> #> Model predictions using solution type deSolve #> -#> Fitted using 421 model solutions performed in 1.1 s +#> Fitted using 421 model solutions performed in 1.082 s #> #> Error model: #> Constant variance @@ -665,8 +665,8 @@ Per default, parameters in the kinetic models are internally transformed in #> 120 m1 25.15 28.78984 -3.640e+00 #> 120 m1 33.31 28.78984 4.520e+00
f.obs <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, error_model = "obs", quiet = TRUE)
#> Warning: Observations with value of zero were removed from the data
summary(f.obs)
#> mkin version used for fitting: 0.9.49.4 #> R version used for fitting: 3.6.0 -#> Date of fit: Tue May 7 08:36:35 2019 -#> Date of summary: Tue May 7 08:36:35 2019 +#> Date of fit: Wed May 8 20:51:08 2019 +#> Date of summary: Wed May 8 20:51:08 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -674,7 +674,7 @@ Per default, parameters in the kinetic models are internally transformed in #> #> Model predictions using solution type deSolve #> -#> Fitted using 758 model solutions performed in 1.991 s +#> Fitted using 758 model solutions performed in 1.971 s #> #> Error model: #> Variance unique to each observed variable @@ -795,8 +795,8 @@ Per default, parameters in the kinetic models are internally transformed in #> 120 m1 25.15 28.80430 -3.654e+00 #> 120 m1 33.31 28.80430 4.506e+00
f.tc <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, error_model = "tc", quiet = TRUE)
#> Warning: Observations with value of zero were removed from the data
summary(f.tc)
#> mkin version used for fitting: 0.9.49.4 #> R version used for fitting: 3.6.0 -#> Date of fit: Tue May 7 08:36:39 2019 -#> Date of summary: Tue May 7 08:36:39 2019 +#> Date of fit: Wed May 8 20:51:11 2019 +#> Date of summary: Wed May 8 20:51:11 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -804,7 +804,7 @@ Per default, parameters in the kinetic models are internally transformed in #> #> Model predictions using solution type deSolve #> -#> Fitted using 821 model solutions performed in 3.304 s +#> Fitted using 821 model solutions performed in 3.29 s #> #> Error model: #> Two-component variance function diff --git a/docs/reference/mkinmod.html b/docs/reference/mkinmod.html index d0b2a0eb..51be5465 100644 --- a/docs/reference/mkinmod.html +++ b/docs/reference/mkinmod.html @@ -234,7 +234,7 @@ For the definition of model types and their parameters, the equations given SFO_SFO <- mkinmod( parent = mkinsub("SFO", "m1"), m1 = mkinsub("SFO"), verbose = TRUE)
#> Compilation argument: -#> /usr/lib/R/bin/R CMD SHLIB fileb5a1e31297a.c 2> fileb5a1e31297a.c.err.txt +#> /usr/lib/R/bin/R CMD SHLIB file4bbd307f8763.c 2> file4bbd307f8763.c.err.txt #> Program source: #> 1: #include <R.h> #> 2: diff --git a/docs/reference/mkinpredict.html b/docs/reference/mkinpredict.html index 522cbbfe..8e2e307b 100644 --- a/docs/reference/mkinpredict.html +++ b/docs/reference/mkinpredict.html @@ -328,17 +328,17 @@ c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), solution_type = "eigen")[201,]))
#> time parent m1 #> 201 20 4.978707 27.46227
#> User System verstrichen -#> 0.003 0.000 0.003
system.time( +#> 0.003 0.000 0.004
system.time( print(mkinpredict(SFO_SFO, c(k_parent_m1 = 0.05, k_parent_sink = 0.1, k_m1_sink = 0.01), c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), solution_type = "deSolve")[201,]))
#> time parent m1 #> 201 20 4.978707 27.46227
#> User System verstrichen -#> 0.001 0.000 0.002
system.time( +#> 0.002 0.000 0.001
system.time( print(mkinpredict(SFO_SFO, c(k_parent_m1 = 0.05, k_parent_sink = 0.1, k_m1_sink = 0.01), c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), solution_type = "deSolve", use_compiled = FALSE)[201,]))
#> time parent m1 #> 201 20 4.978707 27.46227
#> User System verstrichen -#> 0.021 0.000 0.022
+#> 0.021 0.000 0.021
# Predict from a fitted model f <- mkinfit(SFO_SFO, FOCUS_2006_C)
#> Ordinary least squares optimisation
#> Sum of squared residuals at call 1: 552.5739 #> Sum of squared residuals at call 3: 552.5739 diff --git a/docs/reference/mmkin.html b/docs/reference/mmkin.html index d0cd748c..3e297eb3 100644 --- a/docs/reference/mmkin.html +++ b/docs/reference/mmkin.html @@ -194,8 +194,8 @@ time_1 <- system.time(fits.4 <- mmkin(models, datasets, cores = 1, quiet = TRUE)) time_default
#> User System verstrichen -#> 0.047 0.033 5.463
time_1
#> User System verstrichen -#> 19.909 0.004 19.967
+#> 0.048 0.024 5.085
time_1
#> User System verstrichen +#> 19.074 0.004 19.089
endpoints(fits.0[["SFO_lin", 2]])
#> $ff #> parent_M1 parent_sink M1_M2 M1_sink #> 0.7340481 0.2659519 0.7505684 0.2494316 diff --git a/docs/reference/plot.mkinfit-1.png b/docs/reference/plot.mkinfit-1.png index e7b3bac7..f70e9e31 100644 Binary files a/docs/reference/plot.mkinfit-1.png and b/docs/reference/plot.mkinfit-1.png differ diff --git a/docs/reference/plot.mkinfit-2.png b/docs/reference/plot.mkinfit-2.png index 15bdba55..6facce0f 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 52de09bd..b53b3134 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-4.png b/docs/reference/plot.mkinfit-4.png index a832ede2..70a936ee 100644 Binary files a/docs/reference/plot.mkinfit-4.png and b/docs/reference/plot.mkinfit-4.png differ diff --git a/docs/reference/plot.mkinfit-5.png b/docs/reference/plot.mkinfit-5.png new file mode 100644 index 00000000..2e208996 Binary files /dev/null and b/docs/reference/plot.mkinfit-5.png differ diff --git a/docs/reference/plot.mkinfit-6.png b/docs/reference/plot.mkinfit-6.png new file mode 100644 index 00000000..1da876b4 Binary files /dev/null and b/docs/reference/plot.mkinfit-6.png differ diff --git a/docs/reference/plot.mkinfit.html b/docs/reference/plot.mkinfit.html index 525200ff..7bfc17f6 100644 --- a/docs/reference/plot.mkinfit.html +++ b/docs/reference/plot.mkinfit.html @@ -36,7 +36,7 @@ from a previous successful call to mkinfit and plots the observed data together with the solution of the fitted model. If the current plot device is a tikz device, - then latex is being used for the formatting of the chi2 error level, + then latex is being used for the formatting of the chi2 error level, if show_errmin = TRUE." /> @@ -137,7 +137,7 @@ If the current plot device is a tikz device, from a previous successful call to mkinfit and plots the observed data together with the solution of the fitted model.

If the current plot device is a tikz device, - then latex is being used for the formatting of the chi2 error level, + then latex is being used for the formatting of the chi2 error level, if show_errmin = TRUE.

@@ -151,7 +151,9 @@ plot(x, fit = x, col_obs = 1:length(obs_vars), pch_obs = col_obs, lty_obs = rep(1, length(obs_vars)), add = FALSE, legend = !add, - show_residuals = FALSE, maxabs = "auto", + show_residuals = FALSE, + show_errplot = FALSE, + maxabs = "auto", sep_obs = FALSE, rel.height.middle = 0.9, lpos = "topright", inset = c(0.05, 0.05), show_errmin = FALSE, errmin_digits = 3, …) @@ -213,10 +215,17 @@ plot_sep(fit, sep_obs = TRUE, show_residuals = TRUE, show_errmin = TRUE, … show_residuals

Should residuals be shown? If only one plot of the fits is shown, the - residual plot is in the lower third of the plot? Otherwise, i.e. if + residual plot is in the lower third of the plot. Otherwise, i.e. if "sep_obs" is given, the residual plots will be located to the right of the plots of the fitted curves.

+ + show_errplot +

Should squared residuals and the error model be shown? If only one plot of + the fits is shown, this plot is in the lower third of the plot. + Otherwise, i.e. if "sep_obs" is given, the residual plots will be located + to the right of the plots of the fitted curves.

+ maxabs

Maximum absolute value of the residuals. This is used for the scaling of @@ -263,14 +272,18 @@ plot_sep(fit, sep_obs = TRUE, show_residuals = TRUE, show_errmin = TRUE, …

# One parent compound, one metabolite, both single first order, path from # parent to sink included 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)
#> Warning: Observations with value of zero were removed from the data
plot(fit)
plot(fit, show_residuals = 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(fit, show_residuals = TRUE)
plot(fit, show_errplot = TRUE)
# Show the observed variables separately -plot(fit, sep_obs = TRUE, lpos = c("topright", "bottomright"))
+plot(fit, sep_obs = TRUE, lpos = c("topright", "bottomright"))
# Show the observed variables separately, with residuals plot(fit, sep_obs = TRUE, show_residuals = TRUE, lpos = c("topright", "bottomright"), - show_errmin = TRUE)
+ show_errmin = TRUE)
# The same can be obtained with less typing, using the convenience function plot_sep -plot_sep(fit, lpos = c("topright", "bottomright"))
+plot_sep(fit, lpos = c("topright", "bottomright")) + +# Show the observed variables separately, with the error model +plot(fit, sep_obs = TRUE, show_errplot = TRUE, lpos = c("topright", "bottomright"), + show_errmin = TRUE)
# S3 method for mmkin
-plot(x, main = "auto", legends = 1, errmin_var = "All data", errmin_digits = 3,
-              cex = 0.7, rel.height.middle = 0.9, ...)
+plot(x, main = "auto", legends = 1, + resplot = c("time", "errmod"), errmin_var = "All data", errmin_digits = 3, + cex = 0.7, rel.height.middle = 0.9, ...)

Arguments

@@ -159,6 +160,12 @@ If the current plot device is a tikz device, + + + + @@ -187,15 +194,17 @@ If the current plot device is a tikz device,

Examples

-
# Only use one core not to offend CRAN checks +
# Only use one core not to offend CRAN checks fits <- mmkin(c("FOMC", "HS"), list("FOCUS B" = FOCUS_2006_B, "FOCUS C" = FOCUS_2006_C), # named list for titles - cores = 1, quiet = TRUE) + cores = 1, quiet = TRUE, error_model = "tc") plot(fits[, "FOCUS C"])
plot(fits["FOMC", ])
# We can also plot a single fit, if we like the way plot.mmkin works, but then the plot # height should be smaller than the plot width (this is not possible for the html pages # generated by pkgdown, as far as I know). - plot(fits["FOMC", "FOCUS C"]) # same as plot(fits[1, 2])
+ plot(fits["FOMC", "FOCUS C"]) # same as plot(fits[1, 2])
+ # Show the error models + plot(fits["FOMC", ], resplot = "errmod")
legends

An index for the fits for which legends should be shown.

resplot

Should the residuals plotted against time, using mkinresplot, + or as squared residuals against predicted values, with the error model, + using mkinerrplot.

errmin_var

The variable for which the FOCUS chi2 error value should be shown.