From 65d31e345f9e61e9d05584b24df6a01c6c6ed18d Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Wed, 15 Oct 2014 01:13:48 +0200 Subject: Switch to using the Port algorithm per default --- DESCRIPTION | 8 +-- NEWS.md | 4 ++ R/mkinfit.R | 2 +- README.md | 3 +- man/mkinfit.Rd | 24 ++++----- vignettes/FOCUS_L.html | 135 ++++++++++++++++++++++++++----------------------- vignettes/FOCUS_Z.pdf | Bin 220198 -> 220189 bytes 7 files changed, 95 insertions(+), 81 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a984391c..d36c35cc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Type: Package Title: Routines for Fitting Kinetic Models with One or More State Variables to Chemical Degradation Data Version: 0.9-34 -Date: 2014-10-14 +Date: 2014-10-15 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), email = "jranke@uni-bremen.de"), person("Katrin", "Lindenberger", role = "ctb"), @@ -12,9 +12,9 @@ Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), Description: Calculation routines based on the FOCUS Kinetics Report (2006). Includes a function for conveniently defining differential equation models, model solution based on eigenvalues if possible or using numerical solvers - and a choice of the optimisation methods made available by the FME package - (default is a Levenberg-Marquardt variant). Please note that no warranty is - implied for correctness of results or fitness for a particular purpose. + and a choice of the optimisation methods made available by the FME package. + Please note that no warranty is implied for correctness of results or fitness + for a particular purpose. Depends: minpack.lm, rootSolve Imports: FME, deSolve Suggests: knitr, RUnit diff --git a/NEWS.md b/NEWS.md index 4478be6b..60974b14 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # CHANGES in mkin VERSION 0.9-34 +## NEW FEATURES + +- Switch to using the Port algorithm (using a model/trust region approach) per default. While needing more iterations than the Levenberg-Marquardt algorithm previously used per default, it is less sensitive to starting parameters. + ## MINOR CHANGES - The formatting of differential equations in the summary was further improved diff --git a/R/mkinfit.R b/R/mkinfit.R index a966cea6..6494ea1e 100644 --- a/R/mkinfit.R +++ b/R/mkinfit.R @@ -28,7 +28,7 @@ mkinfit <- function(mkinmod, observed, fixed_initials = names(mkinmod$diffs)[-1], solution_type = "auto", method.ode = "lsoda", - method.modFit = c("Marq", "Port", "SANN", "Nelder-Mead", "BFSG", "CG", "L-BFGS-B"), + method.modFit = c("Port", "Marq", "SANN", "Nelder-Mead", "BFSG", "CG", "L-BFGS-B"), maxit.modFit = "auto", control.modFit = list(), transform_rates = TRUE, diff --git a/README.md b/README.md index dfb735f0..fba6f851 100644 --- a/README.md +++ b/README.md @@ -93,8 +93,7 @@ documentation or the package vignettes referenced from the * Model optimisation with [`mkinfit`](http://kinfit.r-forge.r-project.org/mkin_static/mkinfit.html) internally using the `modFit` function from the `FME` package, - which uses the least-squares Levenberg-Marquardt algorithm from - `minpack.lm` per default. + but using the Port routine `nlminb` per default. * By default, kinetic rate constants and kinetic formation fractions are transformed internally using [`transform_odeparms`](http://kinfit.r-forge.r-project.org/mkin_static/transform_odeparms.html) diff --git a/man/mkinfit.Rd b/man/mkinfit.Rd index 21af9a05..c40dff83 100644 --- a/man/mkinfit.Rd +++ b/man/mkinfit.Rd @@ -7,14 +7,10 @@ This function uses the Flexible Modelling Environment package \code{\link{FME}} to create a function calculating the model cost, i.e. the deviation between the kinetic model and the observed data. This model cost is - then minimised using the Levenberg-Marquardt algorithm \code{\link{nls.lm}}, + then minimised using the Port algorithm \code{\link{nlminb}}, using the specified initial or fixed parameters and starting values. Per default, parameters in the kinetic models are internally transformed in order to better satisfy the assumption of a normal distribution of their estimators. - If fitting with transformed fractions leads to a suboptimal fit, doing a - first run without transforming fractions may help. A final - run using the optimised parameters from the previous run as starting values - can then be performed with transformed fractions. In each step of the optimsation, the kinetic model is solved using the function \code{\link{mkinpredict}}. The variance of the residuals for each observed variable can optionally be iteratively reweighted until convergence @@ -27,7 +23,7 @@ mkinfit(mkinmod, observed, fixed_parms = NULL, fixed_initials = names(mkinmod$diffs)[-1], solution_type = "auto", method.ode = "lsoda", - method.modFit = c("Marq", "Port", "SANN", "Nelder-Mead", "BFSG", "CG", "L-BFGS-B"), + method.modFit = c("Port", "Marq", "SANN", "Nelder-Mead", "BFSG", "CG", "L-BFGS-B"), maxit.modFit = "auto", control.modFit = list(), transform_rates = TRUE, @@ -107,13 +103,17 @@ mkinfit(mkinmod, observed, "lsoda" is performant, but sometimes fails to converge. } \item{method.modFit}{ - The optimisation method passed to \code{\link{modFit}}. The default "Marq" - is the Levenberg Marquardt algorithm \code{\link{nls.lm}} from the package - \code{minpack.lm} and usually needs the least number of iterations. + The optimisation method passed to \code{\link{modFit}}. - For more complex problems where local minima occur, the "Port" algorithm is - recommended as it is less prone to get trapped in local minima and depends - less on starting values for parameters. However, it needs more iterations. + In order to optimally deal with problems where local minima occur, the + "Port" algorithm is now used per default as it is less prone to get trapped + in local minima and depends less on starting values for parameters than + the Levenberg Marquardt variant selected by "Marq". However, "Port" needs + more iterations. + + The former default "Marq" is the Levenberg Marquardt algorithm + \code{\link{nls.lm}} from the package \code{minpack.lm} and usually needs + the least number of iterations. The "Pseudo" algorithm is not included because it needs finite parameter bounds which are currently not supported. diff --git a/vignettes/FOCUS_L.html b/vignettes/FOCUS_L.html index 60c5132a..82bbd2c7 100644 --- a/vignettes/FOCUS_L.html +++ b/vignettes/FOCUS_L.html @@ -244,15 +244,15 @@ summary(m.L1.SFO)
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Tue Oct 14 22:03:33 2014 
-## Date of summary: Tue Oct 14 22:03:33 2014 
+## Date of fit:     Wed Oct 15 00:58:15 2014 
+## Date of summary: Wed Oct 15 00:58:15 2014 
 ## 
 ## Equations:
 ## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 14 model solutions performed in 0.081 s
+## Fitted with method Port using 37 model solutions performed in 0.203 s
 ## 
 ## Weighting: none
 ## 
@@ -272,7 +272,7 @@ summary(m.L1.SFO)
 ## Optimised, transformed parameters:
 ##                   Estimate Std. Error Lower Upper t value Pr(>|t|)
 ## parent_0             92.50     1.3700 89.60 95.40    67.6 4.34e-21
-## log_k_parent_sink    -2.35     0.0406 -2.43 -2.26   -57.9 5.16e-20
+## log_k_parent_sink    -2.35     0.0406 -2.43 -2.26   -57.9 5.15e-20
 ##                     Pr(>t)
 ## parent_0          2.17e-21
 ## log_k_parent_sink 2.58e-20
@@ -341,20 +341,31 @@ The residual plot can be easily obtained by

is checked.

m.L1.FOMC <- mkinfit("FOMC", FOCUS_2006_L1_mkin, quiet=TRUE)
-summary(m.L1.FOMC, data = FALSE)
+
+ +
## Warning: Optimisation by method Port did not converge.
+## Convergence code is 1
+
+ +
summary(m.L1.FOMC, data = FALSE)
 
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Tue Oct 14 22:03:34 2014 
-## Date of summary: Tue Oct 14 22:03:34 2014 
+## Date of fit:     Wed Oct 15 00:58:16 2014 
+## Date of summary: Wed Oct 15 00:58:16 2014 
+## 
+## 
+## Warning: Optimisation by method Port did not converge.
+## Convergence code is 1 
+## 
 ## 
 ## Equations:
 ## d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 53 model solutions performed in 0.289 s
+## Fitted with method Port using 188 model solutions performed in 1.011 s
 ## 
 ## Weighting: none
 ## 
@@ -375,23 +386,23 @@ summary(m.L1.FOMC, data = FALSE)
 ## 
 ## Optimised, transformed parameters:
 ##           Estimate Std. Error Lower Upper t value Pr(>|t|)   Pr(>t)
-## parent_0      92.5       1.45 89.40  95.6   63.60 1.17e-19 5.85e-20
-## log_alpha     14.9      10.60 -7.75  37.5    1.40 1.82e-01 9.08e-02
-## log_beta      17.2      10.60 -5.38  39.8    1.62 1.25e-01 6.26e-02
+## parent_0      92.5       1.42  89.4  95.5   65.00 8.32e-20 4.16e-20
+## log_alpha     15.4      15.10 -16.7  47.6    1.02 3.22e-01 1.61e-01
+## log_beta      17.8      15.10 -14.4  49.9    1.18 2.57e-01 1.28e-01
 ## 
 ## Parameter correlation:
 ##           parent_0 log_alpha log_beta
-## parent_0     1.000      0.24    0.238
-## log_alpha    0.240      1.00    1.000
-## log_beta     0.238      1.00    1.000
+## parent_0     1.000     0.113    0.111
+## log_alpha    0.113     1.000    1.000
+## log_beta     0.111     1.000    1.000
 ## 
 ## Residual standard error: 3.05 on 15 degrees of freedom
 ## 
 ## Backtransformed parameters:
 ##          Estimate    Lower    Upper
-## parent_0 9.25e+01 8.94e+01 9.56e+01
-## alpha    2.85e+06 4.32e-04 1.88e+16
-## beta     2.98e+07 4.59e-03 1.93e+17
+## parent_0 9.25e+01 8.94e+01 9.55e+01
+## alpha    5.04e+06 5.51e-08 4.62e+20
+## beta     5.28e+07 5.73e-07 4.86e+21
 ## 
 ## Chi2 error levels in percent:
 ##          err.min n.optim df
@@ -440,15 +451,15 @@ summary(m.L2.SFO)
 
 
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Tue Oct 14 22:03:35 2014 
-## Date of summary: Tue Oct 14 22:03:35 2014 
+## Date of fit:     Wed Oct 15 00:58:17 2014 
+## Date of summary: Wed Oct 15 00:58:17 2014 
 ## 
 ## Equations:
 ## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 29 model solutions performed in 0.154 s
+## Fitted with method Port using 41 model solutions performed in 0.22 s
 ## 
 ## Weighting: none
 ## 
@@ -500,10 +511,10 @@ summary(m.L2.SFO)
 ## 
 ## Data:
 ##  time variable observed predicted residual
-##     0   parent     96.1  9.15e+01    4.635
-##     0   parent     91.8  9.15e+01    0.335
-##     1   parent     41.4  4.71e+01   -5.740
-##     1   parent     38.7  4.71e+01   -8.440
+##     0   parent     96.1  9.15e+01    4.634
+##     0   parent     91.8  9.15e+01    0.334
+##     1   parent     41.4  4.71e+01   -5.739
+##     1   parent     38.7  4.71e+01   -8.439
 ##     3   parent     19.3  1.25e+01    6.779
 ##     3   parent     22.3  1.25e+01    9.779
 ##     7   parent      4.6  8.83e-01    3.717
@@ -522,7 +533,7 @@ plot(m.L2.SFO)
 mkinresplot(m.L2.SFO)
 
-

plot of chunk unnamed-chunk-9

+

plot of chunk unnamed-chunk-9

In the FOCUS kinetics report, it is stated that there is no apparent systematic error observed from the residual plot up to the measured DT90 (approximately at @@ -543,22 +554,22 @@ plot(m.L2.FOMC) mkinresplot(m.L2.FOMC)

-

plot of chunk unnamed-chunk-10

+

plot of chunk unnamed-chunk-10

summary(m.L2.FOMC, data = FALSE)
 
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Tue Oct 14 22:03:36 2014 
-## Date of summary: Tue Oct 14 22:03:36 2014 
+## Date of fit:     Wed Oct 15 00:58:17 2014 
+## Date of summary: Wed Oct 15 00:58:17 2014 
 ## 
 ## Equations:
 ## d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 35 model solutions performed in 0.192 s
+## Fitted with method Port using 81 model solutions performed in 0.438 s
 ## 
 ## Weighting: none
 ## 
@@ -617,7 +628,7 @@ experimental error has to be assumed in order to explain the data.

plot(m.L2.DFOP)
-

plot of chunk unnamed-chunk-11

+

plot of chunk unnamed-chunk-11

Here, the default starting parameters for the DFOP model obviously do not lead to a reasonable solution. Therefore the fit is repeated with different starting @@ -629,15 +640,15 @@ parameters.

plot(m.L2.DFOP)
-

plot of chunk unnamed-chunk-12

+

plot of chunk unnamed-chunk-12

summary(m.L2.DFOP, data = FALSE)
 
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Tue Oct 14 22:03:36 2014 
-## Date of summary: Tue Oct 14 22:03:36 2014 
+## Date of fit:     Wed Oct 15 00:58:21 2014 
+## Date of summary: Wed Oct 15 00:58:21 2014 
 ## 
 ## Equations:
 ## d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 *
@@ -646,7 +657,7 @@ plot(m.L2.DFOP)
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 43 model solutions performed in 0.24 s
+## Fitted with method Port using 336 model solutions performed in 1.844 s
 ## 
 ## Weighting: none
 ## 
@@ -669,8 +680,8 @@ plot(m.L2.DFOP)
 ## 
 ## Optimised, transformed parameters:
 ##          Estimate Std. Error Lower Upper t value Pr(>|t|) Pr(>t)
-## parent_0   94.000         NA    NA    NA      NA       NA     NA
-## log_k1      6.160         NA    NA    NA      NA       NA     NA
+## parent_0   93.900         NA    NA    NA      NA       NA     NA
+## log_k1      3.120         NA    NA    NA      NA       NA     NA
 ## log_k2     -1.090         NA    NA    NA      NA       NA     NA
 ## g_ilr      -0.282         NA    NA    NA      NA       NA     NA
 ## 
@@ -681,8 +692,8 @@ plot(m.L2.DFOP)
 ## 
 ## Backtransformed parameters:
 ##          Estimate Lower Upper
-## parent_0   94.000    NA    NA
-## k1        476.000    NA    NA
+## parent_0   93.900    NA    NA
+## k1         22.700    NA    NA
 ## k2          0.337    NA    NA
 ## g           0.402    NA    NA
 ## 
@@ -693,7 +704,7 @@ plot(m.L2.DFOP)
 ## 
 ## Estimated disappearance times:
 ##        DT50 DT90 DT50_k1 DT50_k2
-## parent   NA   NA 0.00146    2.06
+## parent   NA   NA  0.0306    2.06
 

Here, the DFOP model is clearly the best-fit model for dataset L2 based on the @@ -718,22 +729,22 @@ FOCUS_2006_L3_mkin <- mkin_wide_to_long(FOCUS_2006_L3) plot(m.L3.SFO) -

plot of chunk unnamed-chunk-14

+

plot of chunk unnamed-chunk-14

summary(m.L3.SFO)
 
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Tue Oct 14 22:03:37 2014 
-## Date of summary: Tue Oct 14 22:03:37 2014 
+## Date of fit:     Wed Oct 15 00:58:22 2014 
+## Date of summary: Wed Oct 15 00:58:22 2014 
 ## 
 ## Equations:
 ## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 44 model solutions performed in 0.237 s
+## Fitted with method Port using 43 model solutions performed in 0.232 s
 ## 
 ## Weighting: none
 ## 
@@ -785,14 +796,14 @@ plot(m.L3.SFO)
 ## 
 ## Data:
 ##  time variable observed predicted residual
-##     0   parent     97.8     74.87  22.9274
-##     3   parent     60.0     69.41  -9.4065
+##     0   parent     97.8     74.87  22.9281
+##     3   parent     60.0     69.41  -9.4061
 ##     7   parent     51.0     62.73 -11.7340
-##    14   parent     43.0     52.56  -9.5634
-##    30   parent     35.0     35.08  -0.0828
-##    60   parent     22.0     16.44   5.5614
-##    91   parent     15.0      7.51   7.4896
-##   120   parent     12.0      3.61   8.3908
+##    14   parent     43.0     52.56  -9.5638
+##    30   parent     35.0     35.08  -0.0839
+##    60   parent     22.0     16.44   5.5602
+##    91   parent     15.0      7.51   7.4887
+##   120   parent     12.0      3.61   8.3903
 

The chi2 error level of 21% as well as the plot suggest that the model @@ -811,15 +822,15 @@ plot(m.L3.FOMC)

## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Tue Oct 14 22:03:37 2014 
-## Date of summary: Tue Oct 14 22:03:37 2014 
+## Date of fit:     Wed Oct 15 00:58:22 2014 
+## Date of summary: Wed Oct 15 00:58:22 2014 
 ## 
 ## Equations:
 ## d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 26 model solutions performed in 0.139 s
+## Fitted with method Port using 83 model solutions performed in 0.442 s
 ## 
 ## Weighting: none
 ## 
@@ -884,8 +895,8 @@ plot(m.L3.DFOP)
 
 
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Tue Oct 14 22:03:37 2014 
-## Date of summary: Tue Oct 14 22:03:37 2014 
+## Date of fit:     Wed Oct 15 00:58:23 2014 
+## Date of summary: Wed Oct 15 00:58:23 2014 
 ## 
 ## Equations:
 ## d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 *
@@ -894,7 +905,7 @@ plot(m.L3.DFOP)
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 37 model solutions performed in 0.207 s
+## Fitted with method Port using 137 model solutions performed in 0.778 s
 ## 
 ## Weighting: none
 ## 
@@ -982,15 +993,15 @@ plot(m.L4.SFO)
 
 
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Tue Oct 14 22:03:38 2014 
-## Date of summary: Tue Oct 14 22:03:38 2014 
+## Date of fit:     Wed Oct 15 00:58:24 2014 
+## Date of summary: Wed Oct 15 00:58:24 2014 
 ## 
 ## Equations:
 ## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 20 model solutions performed in 0.106 s
+## Fitted with method Port using 46 model solutions performed in 0.246 s
 ## 
 ## Weighting: none
 ## 
@@ -1057,15 +1068,15 @@ plot(m.L4.FOMC)
 
 
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Tue Oct 14 22:03:38 2014 
-## Date of summary: Tue Oct 14 22:03:38 2014 
+## Date of fit:     Wed Oct 15 00:58:24 2014 
+## Date of summary: Wed Oct 15 00:58:24 2014 
 ## 
 ## Equations:
 ## d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 48 model solutions performed in 0.26 s
+## Fitted with method Port using 66 model solutions performed in 0.359 s
 ## 
 ## Weighting: none
 ## 
diff --git a/vignettes/FOCUS_Z.pdf b/vignettes/FOCUS_Z.pdf
index b5898b7c..0013cd5e 100644
Binary files a/vignettes/FOCUS_Z.pdf and b/vignettes/FOCUS_Z.pdf differ
-- 
cgit v1.2.1