From d2c1ab854491ff047135fa8377400a68499e72de Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Thu, 17 Jul 2014 12:53:30 +0200 Subject: Handle non-convergence and maximum number of iterations For details see NEWS.md --- R/mkinfit.R | 117 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 82 insertions(+), 35 deletions(-) (limited to 'R') diff --git a/R/mkinfit.R b/R/mkinfit.R index c6e13b97..d591c42a 100644 --- a/R/mkinfit.R +++ b/R/mkinfit.R @@ -28,7 +28,8 @@ mkinfit <- function(mkinmod, observed, fixed_initials = names(mkinmod$diffs)[-1], solution_type = "auto", method.ode = "lsoda", - method.modFit = "Marq", + method.modFit = c("Marq", "Port", "SANN", "Nelder-Mead", "BFSG", "CG", "L-BFGS-B"), + maxit.modFit = "auto", control.modFit = list(), transform_rates = TRUE, transform_fractions = TRUE, @@ -40,6 +41,16 @@ mkinfit <- function(mkinmod, observed, trace_parms = FALSE, ...) { + # Check optimisation method and set maximum number of iterations if specified + method.modFit = match.arg(method.modFit) + if (maxit.modFit != "auto") { + if (method.modFit == "Marq") control.modFit$maxiter = maxit.modFit + if (method.modFit == "Port") control.modFit$iter.max = maxit.modFit + if (method.modFit %in% c("SANN", "Nelder-Mead", "BFGS", "CG", "L-BFGS-B")) { + control.modFit$maxit = maxit.modFit + } + } + # Get the names of the state variables in the model mod_vars <- names(mkinmod$diffs) @@ -281,48 +292,76 @@ mkinfit <- function(mkinmod, observed, upper[other_fraction_parms] <- 1 } - fit <- modFit(cost, c(state.ini.optim, transparms.optim), - method = method.modFit, control = control.modFit, - lower = lower, upper = upper, ...) - - # Reiterate the fit until convergence of the variance components (IRLS) - # if requested by the user - weight.ini = weight - if (!is.null(err)) weight.ini = "manual" - - if (!is.null(reweight.method)) { - if (reweight.method != "obs") stop("Only reweighting method 'obs' is implemented") - if(!quiet) { - cat("IRLS based on variance estimates for each observed variable\n") - } - if (!quiet) { - cat("Initial variance estimates are:\n") - print(signif(fit$var_ms_unweighted, 8)) - } - reweight.diff = 1 - n.iter <- 0 - if (!is.null(err)) observed$err.ini <- observed[[err]] - err = "err.irls" - while (reweight.diff > reweight.tol & n.iter < reweight.max.iter) { - n.iter <- n.iter + 1 - sigma.old <- sqrt(fit$var_ms_unweighted) - observed[err] <- sqrt(fit$var_ms_unweighted)[as.character(observed$name)] - fit <- modFit(cost, fit$par, method = method.modFit, - control = control.modFit, lower = lower, upper = upper, ...) - reweight.diff = sum((sqrt(fit$var_ms_unweighted) - sigma.old)^2) + # Do the fit and take the time + fit_time <- system.time({ + fit <- modFit(cost, c(state.ini.optim, transparms.optim), + method = method.modFit, control = control.modFit, + lower = lower, upper = upper, ...) + + # Reiterate the fit until convergence of the variance components (IRLS) + # if requested by the user + weight.ini = weight + if (!is.null(err)) weight.ini = "manual" + + if (!is.null(reweight.method)) { + if (reweight.method != "obs") stop("Only reweighting method 'obs' is implemented") + if(!quiet) { + cat("IRLS based on variance estimates for each observed variable\n") + } if (!quiet) { - cat("Iteration", n.iter, "yields variance estimates:\n") + cat("Initial variance estimates are:\n") print(signif(fit$var_ms_unweighted, 8)) - cat("Sum of squared differences to last variance estimates:", - signif(reweight.diff, 2), "\n") + } + reweight.diff = 1 + n.iter <- 0 + if (!is.null(err)) observed$err.ini <- observed[[err]] + err = "err.irls" + while (reweight.diff > reweight.tol & n.iter < reweight.max.iter) { + n.iter <- n.iter + 1 + sigma.old <- sqrt(fit$var_ms_unweighted) + observed[err] <- sqrt(fit$var_ms_unweighted)[as.character(observed$name)] + fit <- modFit(cost, fit$par, method = method.modFit, + control = control.modFit, lower = lower, upper = upper, ...) + reweight.diff = sum((sqrt(fit$var_ms_unweighted) - sigma.old)^2) + if (!quiet) { + cat("Iteration", n.iter, "yields variance estimates:\n") + print(signif(fit$var_ms_unweighted, 8)) + cat("Sum of squared differences to last variance estimates:", + signif(reweight.diff, 2), "\n") + } } } + }) + + # Check for convergence + if (method.modFit == "Marq") { + if (!fit$info %in% c(1, 2, 3)) { + fit$warning = paste0("Optimisation by method ", method.modFit, + " did not converge.\n", + "The message returned by nls.lm is:\n", + fit$message) + warning(fit$warning) + } + } + if (method.modFit %in% c("Port", "SANN", "Nelder-Mead", "BFGS", "CG", "L-BFGS-B")) { + if (fit$convergence != 0) { + fit$warning = paste0("Optimisation by method ", method.modFit, + " did not converge.\n", + "Convergence code is ", fit$convergence, + ifelse(is.null(fit$message), "", + paste0("\nMessage is ", fit$message))) + warning(fit$warning) + } } # We need to return some more data for summary and plotting fit$solution_type <- solution_type fit$transform_rates <- transform_rates fit$transform_fractions <- transform_fractions + fit$method.modFit <- method.modFit + fit$maxit.modFit <- maxit.modFit + fit$calls <- calls + fit$time <- fit_time # We also need the model for summary and plotting fit$mkinmod <- mkinmod @@ -449,6 +488,8 @@ summary.mkinfit <- function(object, data = TRUE, distimes = TRUE, alpha = 0.05, date.fit = object$date, date.summary = date(), solution_type = object$solution_type, + method.modFit = object$method.modFit, + warning = object$warning, use_of_ff = object$mkinmod$use_of_ff, weight.ini = object$weight.ini, reweight.method = object$reweight.method, @@ -461,6 +502,8 @@ summary.mkinfit <- function(object, data = TRUE, distimes = TRUE, alpha = 0.05, cov.scaled = covar * resvar, info = object$info, niter = object$iterations, + calls = object$calls, + time = object$time, stopmess = message, par = param, bpar = bparam) @@ -491,13 +534,17 @@ print.summary.mkinfit <- function(x, digits = max(3, getOption("digits") - 3), . cat("Date of fit: ", x$date.fit, "\n") cat("Date of summary:", x$date.summary, "\n") + if (!is.null(x$warning)) cat("\n\nWarning:", x$warning, "\n\n") + cat("\nEquations:\n") print(noquote(as.character(x[["diffs"]]))) df <- x$df rdf <- df[2] - cat("\nMethod used for solution of differential equation system:\n") - cat(x$solution_type, "\n") + cat("\nModel predictions using solution type", x$solution_type, "\n") + + cat("\nFitted with method", x$method.modFit, + "using", x$calls, "model solutions performed in", x$time[["elapsed"]], "s\n") cat("\nWeighting:", x$weight.ini) if(!is.null(x$reweight.method)) cat(" then iterative reweighting method", -- cgit v1.2.3 From 8def5006fc81c032c3fc99751e062cdb32a81cc1 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Thu, 17 Jul 2014 14:34:20 +0200 Subject: Return complete list of initial states after fitting This is useful for specifying state.ini in a subsequent call to mkinfit --- R/mkinfit.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'R') diff --git a/R/mkinfit.R b/R/mkinfit.R index d591c42a..a8fbfc78 100644 --- a/R/mkinfit.R +++ b/R/mkinfit.R @@ -417,8 +417,11 @@ mkinfit <- function(mkinmod, observed, fit$bparms.optim <- bparms.optim fit$bparms.fixed <- bparms.fixed - # Return ode parameters for further fitting + # Return ode and state parameters for further fitting fit$bparms.ode <- bparms.all[mkinmod$parms] + fit$bparms.state <- c(bparms.all[setdiff(names(bparms.all), names(fit$bparms.ode))], + state.ini.fixed) + names(fit$bparms.state) <- gsub("_0$", "", names(fit$bparms.state)) fit$date <- date() -- cgit v1.2.3 From a1567638a3ba9f4d62fa199525097a94ddfd7912 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Mon, 21 Jul 2014 08:20:44 +0200 Subject: Bugfix, model shorthand, state.ini[[1]] from observed data - The bug occurred when using transform_rates=FALSE for FOMC, DFOP or HS - Make it possible to use mkinfit("SFO", ...) - Take initial mean value at time zero for the variable with the highest value in the observed data - Update of vignette/FOCUS_L - Improve the Makefile to build single vignettes --- DESCRIPTION | 2 +- GNUmakefile | 15 ++- NEWS.md | 6 + R/mkinfit.R | 27 +++- R/transform_odeparms.R | 22 +++- README.md | 5 + man/mkinfit.Rd | 22 +++- man/transform_odeparms.Rd | 2 +- vignettes/FOCUS_L.Rmd | 81 ++++++------ vignettes/FOCUS_L.html | 328 +++++++++++++++++++++++----------------------- vignettes/mkin.pdf | Bin 160326 -> 160326 bytes 11 files changed, 279 insertions(+), 231 deletions(-) (limited to 'R') diff --git a/DESCRIPTION b/DESCRIPTION index fb32b807..bad72501 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-32 -Date: 2014-07-17 +Date: 2014-07-21 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), email = "jranke@uni-bremen.de"), person("Katrin", "Lindenberger", role = "ctb"), diff --git a/GNUmakefile b/GNUmakefile index b8cc5d82..aebc20dd 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -70,10 +70,17 @@ test: install-no-vignettes cd tests;\ "$(RBIN)/Rscript" doRUnit.R -.PHONY: vignettes -vignettes: - "$(RBIN)/Rscript" -e "tools::buildVignettes(dir = '.')" - +vignettes/mkin.pdf: vignettes/mkin.Rnw + "$(RBIN)/Rscript" -e "tools::buildVignette(file = 'vignettes/mkin.Rnw', dir = 'vignettes')" + +vignettes/FOCUS_L.html: vignettes/FOCUS_L.Rmd + "$(RBIN)/Rscript" -e "tools::buildVignette(file = 'vignettes/FOCUS_L.Rmd', dir = 'vignettes')" + +vignettes/FOCUS_Z.pdf: vignettes/FOCUS_Z.Rnw + "$(RBIN)/Rscript" -e "tools::buildVignette(file = 'vignettes/FOCUS_Z.Rnw', dir = 'vignettes')" + +vignettes: vignettes/mkin.pdf vignettes/FOCUS_L.html vignettes/FOCUS_Z.pdf + sd: "$(RBIN)/Rscript" -e "library(staticdocs); build_site()" diff --git a/NEWS.md b/NEWS.md index 031ae954..c10a25b0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,10 @@ ## NEW FEATURES +- The default for the initial value for the first state value is now taken from the mean of the observations at time zero, if available. + +- The kinetic model can alternatively be specified with a shorthand name for parent only degradation models, e.g. `SFO`, or `DFOP`. + - Optimisation method, number of model evaluations and time elapsed during optimisation are given in the summary of mkinfit objects. - The maximum number of iterations in the optimisation algorithm can be specified using the argument `maxit.modFit` to the mkinfit function. @@ -10,6 +14,8 @@ ## BUG FIXES +- `transform_rates=FALSE` in `mkinfit` now also works for FOMC and HS models. + - Initial values for formation fractions were not set in all cases. - No warning was given when the fit did not converge when a method other than the default Levenberg-Marquardt method `Marq` was used. diff --git a/R/mkinfit.R b/R/mkinfit.R index a8fbfc78..39d084cb 100644 --- a/R/mkinfit.R +++ b/R/mkinfit.R @@ -23,7 +23,7 @@ if(getRversion() >= '2.15.1') utils::globalVariables(c("name", "value")) mkinfit <- function(mkinmod, observed, parms.ini = "auto", - state.ini = c(100, rep(0, length(mkinmod$diffs) - 1)), + state.ini = "auto", fixed_parms = NULL, fixed_initials = names(mkinmod$diffs)[-1], solution_type = "auto", @@ -41,6 +41,21 @@ mkinfit <- function(mkinmod, observed, trace_parms = FALSE, ...) { + # Check mkinmod and generate a model for the variable whithe the highest value + # if a suitable string is given + parent_models_available = c("SFO", "FOMC", "DFOP", "HS", "SFORB") + presumed_parent_name = observed[which.max(observed$value), "name"] + if (class(mkinmod) != "mkinmod") { + if (mkinmod[[1]] %in% parent_models_available) { + speclist <- list(list(type = mkinmod, sink = TRUE)) + names(speclist) <- presumed_parent_name + mkinmod <- mkinmod(speclist = speclist) + } else { + stop("Argument mkinmod must be of class mkinmod or a string containing one of\n ", + paste(parent_models_available, collapse = ", ")) + } + } + # Check optimisation method and set maximum number of iterations if specified method.modFit = match.arg(method.modFit) if (maxit.modFit != "auto") { @@ -55,7 +70,7 @@ mkinfit <- function(mkinmod, observed, mod_vars <- names(mkinmod$diffs) # Get the names of observed variables - obs_vars = names(mkinmod$spec) + obs_vars <- names(mkinmod$spec) # Subset observed data with names of observed data in the model observed <- subset(observed, name %in% obs_vars) @@ -137,6 +152,12 @@ mkinfit <- function(mkinmod, observed, } } + # Set default for state.ini if appropriate + if (state.ini[1] == "auto") { + state.ini = c(mean(subset(observed, time == 0 & name == presumed_parent_name)$value), + rep(0, length(mkinmod$diffs) - 1)) + } + # Name the inital state variable values if they are not named yet if(is.null(names(state.ini))) names(state.ini) <- mod_vars @@ -279,7 +300,7 @@ mkinfit <- function(mkinmod, observed, if (!transform_rates) { index_k <- grep("^k_", names(lower)) lower[index_k] <- 0 - other_rate_parms <- intersect(c("alpha", "beta", "k1", "k2"), names(lower)) + other_rate_parms <- intersect(c("alpha", "beta", "k1", "k2", "tb"), names(lower)) lower[other_rate_parms] <- 0 } diff --git a/R/transform_odeparms.R b/R/transform_odeparms.R index 912a5c0a..f518ae32 100644 --- a/R/transform_odeparms.R +++ b/R/transform_odeparms.R @@ -69,7 +69,11 @@ transform_odeparms <- function(parms, mkinmod, # and HS parameter tb if transformation of rates is requested for (pname in c("alpha", "beta", "k1", "k2", "tb")) { if (!is.na(parms[pname])) { - transparms[paste0("log_", pname)] <- ifelse(transform_rates, log(parms[pname]), parms[pname]) + if (transform_rates) { + transparms[paste0("log_", pname)] <- log(parms[pname]) + } else { + transparms[pname] <- parms[pname] + } } } if (!is.na(parms["g"])) { @@ -130,12 +134,16 @@ backtransform_odeparms <- function(transparms, mkinmod, # Transform parameters also for FOMC, DFOP and HS models for (pname in c("alpha", "beta", "k1", "k2", "tb")) { - pname_trans = paste0("log_", pname) - if (!is.na(transparms[pname_trans])) { - parms[pname] <- ifelse(transform_rates, - exp(transparms[pname_trans]), - transparms[pname]) - } + if (transform_rates) { + pname_trans = paste0("log_", pname) + if (!is.na(transparms[pname_trans])) { + parms[pname] <- exp(transparms[pname_trans]) + } + } else { + if (!is.na(transparms[pname])) { + parms[pname] <- transparms[pname] + } + } } if (!is.na(transparms["g_ilr"])) { g_ilr <- transparms["g_ilr"] diff --git a/README.md b/README.md index e171180f..48c667bf 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,11 @@ A very simple usage example would be plot(SFO.fit, show_residuals = TRUE) summary(SFO.fit) +If you have parent only degradation data, you can use a shorthand notation +like `SFO` or `FOMC` for the model without the need to use `mkinmod` + + FOMC.fit <- mkinfit("FOMC", example_data) + A fairly complex usage example using a built-in dataset: data <- mkin_wide_to_long(schaefer07_complex_case, time = "time") diff --git a/man/mkinfit.Rd b/man/mkinfit.Rd index c99e146c..581d63f4 100644 --- a/man/mkinfit.Rd +++ b/man/mkinfit.Rd @@ -17,7 +17,7 @@ \usage{ mkinfit(mkinmod, observed, parms.ini = "auto", - state.ini = c(100, rep(0, length(mkinmod$diffs) - 1)), + state.ini = "auto", fixed_parms = NULL, fixed_initials = names(mkinmod$diffs)[-1], solution_type = "auto", method.ode = "lsoda", @@ -35,7 +35,11 @@ mkinfit(mkinmod, observed, } \arguments{ \item{mkinmod}{ - A list of class \code{\link{mkinmod}}, containing the kinetic model to be fitted to the data. + A list of class \code{\link{mkinmod}}, containing the kinetic model to be + fitted to the data, or one of the shorthand names ("SFO", "FOMC", "DFOP", + "HS", "SFORB"). If a shorthand name is given, a parent only degradation + model is generated for the observation with the highest value in + \code{observed}. } \item{observed}{ The observed data. It has to be in the long format as described in @@ -65,7 +69,8 @@ mkinfit(mkinmod, observed, case the observed variables are represented by more than one model variable, the names will differ from the names of the observed variables (see \code{map} component of \code{\link{mkinmod}}). The default is to set - the initial value of the first model variable to 100 and all others to 0. + the initial value of the first model variable to the mean of the time zero + values for the variable with the maximum observed value, and all others to 0. } \item{fixed_parms}{ The names of parameters that should not be optimised but rather kept at the @@ -124,8 +129,9 @@ mkinfit(mkinmod, observed, model specification used in the fitting for better compliance with the assumption of normal distribution of the estimator. If TRUE, also alpha and beta parameters of the FOMC model are log-transformed, as well - as k1 and k2 rate constants for the DFOP and HS models. - If TRUE, zero is used as a lower bound for the rates in the optimisation. + as k1 and k2 rate constants for the DFOP and HS models and the break point + tb of the HS model. + If FALSE, zero is used as a lower bound for the rates in the optimisation. } \item{transform_fractions}{ Boolean specifying if formation fractions constants should be transformed in the @@ -204,9 +210,13 @@ mkinfit(mkinmod, observed, other GUI derivative of mkin, sponsored by Syngenta. } \author{ - Johannes Ranke + Johannes Ranke } \examples{ +# Use shorthand notation for parent only degradation +fit <- mkinfit("FOMC", FOCUS_2006_C) +summary(fit) + # One parent compound, one metabolite, both single first order. SFO_SFO <- mkinmod( parent = list(type = "SFO", to = "m1", sink = TRUE), diff --git a/man/transform_odeparms.Rd b/man/transform_odeparms.Rd index ea0b5024..ba93af7d 100644 --- a/man/transform_odeparms.Rd +++ b/man/transform_odeparms.Rd @@ -41,7 +41,7 @@ backtransform_odeparms(transparms, mkinmod, assumption of normal distribution of the estimator. If TRUE, also alpha and beta parameters of the FOMC model are log-transformed, as well as k1 and k2 rate constants for the DFOP and HS models and the break point tb - of the HS model + of the HS model. } \item{transform_fractions}{ Boolean specifying if formation fractions constants should be transformed in the diff --git a/vignettes/FOCUS_L.Rmd b/vignettes/FOCUS_L.Rmd index 04d5f831..cd7711f6 100644 --- a/vignettes/FOCUS_L.Rmd +++ b/vignettes/FOCUS_L.Rmd @@ -13,7 +13,7 @@ opts_chunk$set(tidy = FALSE, cache = TRUE) ## Laboratory Data L1 The following code defines example dataset L1 from the FOCUS kinetics -report, p. 284 +report, p. 284: ```{r} library("mkin") @@ -25,27 +25,18 @@ FOCUS_2006_L1 = data.frame( FOCUS_2006_L1_mkin <- mkin_wide_to_long(FOCUS_2006_L1) ``` -The next step is to set up the models used for the kinetic analysis. Note that -the model definitions contain the names of the observed variables in the data. -In this case, there is only one variable called `parent`. +Here we use the assumptions of simple first order (SFO), the case of declining +rate constant over time (FOMC) and the case of two different phases of the +kinetics (DFOP). For a more detailed discussion of the models, please see the +FOCUS kinetics report. -```{r} -SFO <- mkinmod(parent = list(type = "SFO")) -FOMC <- mkinmod(parent = list(type = "FOMC")) -DFOP <- mkinmod(parent = list(type = "DFOP")) -``` - -The three models cover the first assumption of simple first order (SFO), -the case of declining rate constant over time (FOMC) and the case of two -different phases of the kinetics (DFOP). For a more detailed discussion -of the models, please see the FOCUS kinetics report. - -The following two lines fit the model and produce the summary report -of the model fit. This covers the numerical analysis given in the -FOCUS report. +Since mkin version 0.9-32 (July 2014), we can use shorthand notation like `SFO` +for parent only degradation models. The following two lines fit the model and +produce the summary report of the model fit. This covers the numerical analysis +given in the FOCUS report. ```{r} -m.L1.SFO <- mkinfit(SFO, FOCUS_2006_L1_mkin, quiet=TRUE) +m.L1.SFO <- mkinfit("SFO", FOCUS_2006_L1_mkin, quiet=TRUE) summary(m.L1.SFO) ``` @@ -64,32 +55,30 @@ For comparison, the FOMC model is fitted as well, and the chi^2 error level is checked. ```{r} -m.L1.FOMC <- mkinfit(FOMC, FOCUS_2006_L1_mkin, quiet=TRUE) +m.L1.FOMC <- mkinfit("FOMC", FOCUS_2006_L1_mkin, quiet=TRUE) summary(m.L1.FOMC, data = FALSE) ``` Due to the higher number of parameters, and the lower number of degrees of freedom of the fit, the chi^2 error level is actually higher for the FOMC -model (3.6%) than for the SFO model (3.4%). Additionally, the covariance -matrix can not be obtained, indicating overparameterisation of the model. -As a consequence, no standard errors for transformed parameters nor -confidence intervals for backtransformed parameters are available. +model (3.6%) than for the SFO model (3.4%). Additionally, the parameters +`log_alpha` and `log_beta` internally fitted in the model have p-values for the two +sided t-test of 0.18 and 0.125, and their correlation is 1.000, indicating that +the model is overparameterised. The chi^2 error levels reported in Appendix 3 and Appendix 7 to the FOCUS kinetics report are rounded to integer percentages and partly deviate by one percentage point from the results calculated by mkin. The reason for this is not known. However, mkin gives the same chi^2 error levels -as the kinfit package. - -Furthermore, the calculation routines of the kinfit package have been extensively -compared to the results obtained by the KinGUI software, as documented in the -kinfit package vignette. KinGUI is a widely used standard package in this field. -Therefore, the reason for the difference was not investigated further. +as the kinfit package. Furthermore, the calculation routines of the kinfit +package have been extensively compared to the results obtained by the KinGUI +software, as documented in the kinfit package vignette. KinGUI is a widely used +standard package in this field. ## Laboratory Data L2 The following code defines example dataset L2 from the FOCUS kinetics -report, p. 287 +report, p. 287: ```{r} FOCUS_2006_L2 = data.frame( @@ -100,10 +89,10 @@ FOCUS_2006_L2 = data.frame( FOCUS_2006_L2_mkin <- mkin_wide_to_long(FOCUS_2006_L2) ``` -Again, the SFO model is fitted and a summary is obtained. +Again, the SFO model is fitted and a summary is obtained: ```{r} -m.L2.SFO <- mkinfit(SFO, FOCUS_2006_L2_mkin, quiet=TRUE) +m.L2.SFO <- mkinfit("SFO", FOCUS_2006_L2_mkin, quiet=TRUE) summary(m.L2.SFO) ``` @@ -130,7 +119,7 @@ For comparison, the FOMC model is fitted as well, and the chi^2 error level is checked. ```{r fig.height = 8} -m.L2.FOMC <- mkinfit(FOMC, FOCUS_2006_L2_mkin, quiet = TRUE) +m.L2.FOMC <- mkinfit("FOMC", FOCUS_2006_L2_mkin, quiet = TRUE) par(mfrow = c(2, 1)) plot(m.L2.FOMC) mkinresplot(m.L2.FOMC) @@ -144,7 +133,7 @@ experimental error has to be assumed in order to explain the data. Fitting the four parameter DFOP model further reduces the chi^2 error level. ```{r fig.height = 5} -m.L2.DFOP <- mkinfit(DFOP, FOCUS_2006_L2_mkin, quiet = TRUE) +m.L2.DFOP <- mkinfit("DFOP", FOCUS_2006_L2_mkin, quiet = TRUE) plot(m.L2.DFOP) ``` @@ -153,7 +142,7 @@ to a reasonable solution. Therefore the fit is repeated with different starting parameters. ```{r fig.height = 5} -m.L2.DFOP <- mkinfit(DFOP, FOCUS_2006_L2_mkin, +m.L2.DFOP <- mkinfit("DFOP", FOCUS_2006_L2_mkin, parms.ini = c(k1 = 1, k2 = 0.01, g = 0.8), quiet=TRUE) plot(m.L2.DFOP) @@ -180,7 +169,7 @@ FOCUS_2006_L3_mkin <- mkin_wide_to_long(FOCUS_2006_L3) SFO model, summary and plot: ```{r fig.height = 5} -m.L3.SFO <- mkinfit(SFO, FOCUS_2006_L3_mkin, quiet = TRUE) +m.L3.SFO <- mkinfit("SFO", FOCUS_2006_L3_mkin, quiet = TRUE) plot(m.L3.SFO) summary(m.L3.SFO) ``` @@ -191,7 +180,7 @@ does not fit very well. The FOMC model performs better: ```{r fig.height = 5} -m.L3.FOMC <- mkinfit(FOMC, FOCUS_2006_L3_mkin, quiet = TRUE) +m.L3.FOMC <- mkinfit("FOMC", FOCUS_2006_L3_mkin, quiet = TRUE) plot(m.L3.FOMC) summary(m.L3.FOMC, data = FALSE) ``` @@ -202,7 +191,7 @@ Fitting the four parameter DFOP model further reduces the chi^2 error level considerably: ```{r fig.height = 5} -m.L3.DFOP <- mkinfit(DFOP, FOCUS_2006_L3_mkin, quiet = TRUE) +m.L3.DFOP <- mkinfit("DFOP", FOCUS_2006_L3_mkin, quiet = TRUE) plot(m.L3.DFOP) summary(m.L3.DFOP, data = FALSE) ``` @@ -212,10 +201,15 @@ and the correlation matrix suggest that the parameter estimates are reliable, an the DFOP model can be used as the best-fit model based on the chi^2 error level criterion for laboratory data L3. +This is also an example where the standard t-test for the parameter `g_ilr` is +misleading, as it tests for a significant difference from zero. In this case, +zero appears to be the correct value for this parameter, and the confidence +interval for the backtransformed parameter `g` is quite narrow. + ## Laboratory Data L4 The following code defines example dataset L4 from the FOCUS kinetics -report, p. 293 +report, p. 293: ```{r} FOCUS_2006_L4 = data.frame( @@ -227,7 +221,7 @@ FOCUS_2006_L4_mkin <- mkin_wide_to_long(FOCUS_2006_L4) SFO model, summary and plot: ```{r fig.height = 5} -m.L4.SFO <- mkinfit(SFO, FOCUS_2006_L4_mkin, quiet = TRUE) +m.L4.SFO <- mkinfit("SFO", FOCUS_2006_L4_mkin, quiet = TRUE) plot(m.L4.SFO) summary(m.L4.SFO, data = FALSE) ``` @@ -235,14 +229,13 @@ summary(m.L4.SFO, data = FALSE) The chi^2 error level of 3.3% as well as the plot suggest that the model fits very well. -The FOMC model for comparison +The FOMC model for comparison: ```{r fig.height = 5} -m.L4.FOMC <- mkinfit(FOMC, FOCUS_2006_L4_mkin, quiet = TRUE) +m.L4.FOMC <- mkinfit("FOMC", FOCUS_2006_L4_mkin, quiet = TRUE) plot(m.L4.FOMC) summary(m.L4.FOMC, data = FALSE) ``` The error level at which the chi^2 test passes is slightly lower for the FOMC model. However, the difference appears negligible. - diff --git a/vignettes/FOCUS_L.html b/vignettes/FOCUS_L.html index 85fadbfe..614fcf32 100644 --- a/vignettes/FOCUS_L.html +++ b/vignettes/FOCUS_L.html @@ -190,7 +190,7 @@ hr {

Laboratory Data L1

The following code defines example dataset L1 from the FOCUS kinetics -report, p. 284

+report, p. 284:

library("mkin")
 
@@ -207,51 +207,43 @@ report, p. 284

FOCUS_2006_L1_mkin <- mkin_wide_to_long(FOCUS_2006_L1) -

The next step is to set up the models used for the kinetic analysis. Note that -the model definitions contain the names of the observed variables in the data. -In this case, there is only one variable called parent.

+

Here we use the assumptions of simple first order (SFO), the case of declining +rate constant over time (FOMC) and the case of two different phases of the +kinetics (DFOP). For a more detailed discussion of the models, please see the +FOCUS kinetics report.

-
SFO <- mkinmod(parent = list(type = "SFO"))
-FOMC <- mkinmod(parent = list(type = "FOMC"))
-DFOP <- mkinmod(parent = list(type = "DFOP"))
-
- -

The three models cover the first assumption of simple first order (SFO), -the case of declining rate constant over time (FOMC) and the case of two -different phases of the kinetics (DFOP). For a more detailed discussion -of the models, please see the FOCUS kinetics report.

+

Since mkin version 0.9-32 (July 2014), we can use shorthand notation like SFO +for parent only degradation models. The following two lines fit the model and +produce the summary report of the model fit. This covers the numerical analysis +given in the FOCUS report.

-

The following two lines fit the model and produce the summary report -of the model fit. This covers the numerical analysis given in the -FOCUS report.

- -
m.L1.SFO <- mkinfit(SFO, FOCUS_2006_L1_mkin, quiet=TRUE)
+
m.L1.SFO <- mkinfit("SFO", FOCUS_2006_L1_mkin, quiet=TRUE)
 summary(m.L1.SFO)
 
## mkin version:    0.9.32 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 17 12:37:41 2014 
-## Date of summary: Thu Jul 17 12:37:41 2014 
+## Date of fit:     Mon Jul 21 09:14:29 2014 
+## Date of summary: Mon Jul 21 09:14:29 2014 
 ## 
 ## Equations:
 ## [1] d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 14 model solutions performed in 0.087 s
+## Fitted with method Marq using 14 model solutions performed in 0.081 s
 ## 
 ## Weighting: none
 ## 
 ## Starting values for parameters to be optimised:
 ##               value   type
-## parent_0      100.0  state
-## k_parent_sink   0.1 deparm
+## parent_0      89.85  state
+## k_parent_sink  0.10 deparm
 ## 
 ## Starting values for the transformed parameters actually optimised:
-##                     value lower upper
-## parent_0          100.000  -Inf   Inf
-## log_k_parent_sink  -2.303  -Inf   Inf
+##                    value lower upper
+## parent_0          89.850  -Inf   Inf
+## log_k_parent_sink -2.303  -Inf   Inf
 ## 
 ## Fixed parameter values:
 ## None
@@ -259,7 +251,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.15e-20
+## log_k_parent_sink    -2.35     0.0406 -2.43 -2.26   -57.9 5.16e-20
 ##                     Pr(>t)
 ## parent_0          2.17e-21
 ## log_k_parent_sink 2.58e-20
@@ -316,67 +308,70 @@ summary(m.L1.SFO)
 
plot(m.L1.SFO)
 
-

plot of chunk unnamed-chunk-5

+

plot of chunk unnamed-chunk-4

The residual plot can be easily obtained by

mkinresplot(m.L1.SFO, ylab = "Observed", xlab = "Time")
 
-

plot of chunk unnamed-chunk-6

+

plot of chunk unnamed-chunk-5

For comparison, the FOMC model is fitted as well, and the chi2 error level is checked.

-
m.L1.FOMC <- mkinfit(FOMC, FOCUS_2006_L1_mkin, quiet=TRUE)
+
m.L1.FOMC <- mkinfit("FOMC", FOCUS_2006_L1_mkin, quiet=TRUE)
 summary(m.L1.FOMC, data = FALSE)
 
## mkin version:    0.9.32 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 17 12:37:42 2014 
-## Date of summary: Thu Jul 17 12:37:42 2014 
+## Date of fit:     Mon Jul 21 09:14:30 2014 
+## Date of summary: Mon Jul 21 09:14:30 2014 
 ## 
 ## Equations:
 ## [1] d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 45 model solutions performed in 0.266 s
+## Fitted with method Marq using 53 model solutions performed in 0.32 s
 ## 
 ## Weighting: none
 ## 
 ## Starting values for parameters to be optimised:
 ##          value   type
-## parent_0   100  state
-## alpha        1 deparm
-## beta        10 deparm
+## parent_0 89.85  state
+## alpha     1.00 deparm
+## beta     10.00 deparm
 ## 
 ## Starting values for the transformed parameters actually optimised:
-##             value lower upper
-## parent_0  100.000  -Inf   Inf
-## log_alpha   0.000  -Inf   Inf
-## log_beta    2.303  -Inf   Inf
+##            value lower upper
+## parent_0  89.850  -Inf   Inf
+## log_alpha  0.000  -Inf   Inf
+## log_beta   2.303  -Inf   Inf
 ## 
 ## Fixed parameter values:
 ## None
 ## 
 ## Optimised, transformed parameters:
-##           Estimate Std. Error Lower Upper t value Pr(>|t|) Pr(>t)
-## parent_0      92.5         NA    NA    NA      NA       NA     NA
-## log_alpha     25.6         NA    NA    NA      NA       NA     NA
-## log_beta      28.0         NA    NA    NA      NA       NA     NA
+##           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
 ## 
 ## Parameter correlation:
-## Could not estimate covariance matrix; singular system:
+##           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
 ## 
 ## Residual standard error: 3.05 on 15 degrees of freedom
 ## 
 ## Backtransformed parameters:
-##          Estimate Lower Upper
-## parent_0 9.25e+01    NA    NA
-## alpha    1.35e+11    NA    NA
-## beta     1.41e+12    NA    NA
+##          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
 ## 
 ## Chi2 error levels in percent:
 ##          err.min n.optim df
@@ -390,26 +385,24 @@ summary(m.L1.FOMC, data = FALSE)
 
 

Due to the higher number of parameters, and the lower number of degrees of freedom of the fit, the chi2 error level is actually higher for the FOMC -model (3.6%) than for the SFO model (3.4%). Additionally, the covariance -matrix can not be obtained, indicating overparameterisation of the model. -As a consequence, no standard errors for transformed parameters nor -confidence intervals for backtransformed parameters are available.

+model (3.6%) than for the SFO model (3.4%). Additionally, the parameters +log_alpha and log_beta internally fitted in the model have p-values for the two +sided t-test of 0.18 and 0.125, and their correlation is 1.000, indicating that +the model is overparameterised.

The chi2 error levels reported in Appendix 3 and Appendix 7 to the FOCUS kinetics report are rounded to integer percentages and partly deviate by one percentage point from the results calculated by mkin. The reason for this is not known. However, mkin gives the same chi2 error levels -as the kinfit package.

- -

Furthermore, the calculation routines of the kinfit package have been extensively -compared to the results obtained by the KinGUI software, as documented in the -kinfit package vignette. KinGUI is a widely used standard package in this field. -Therefore, the reason for the difference was not investigated further.

+as the kinfit package. Furthermore, the calculation routines of the kinfit +package have been extensively compared to the results obtained by the KinGUI +software, as documented in the kinfit package vignette. KinGUI is a widely used +standard package in this field.

Laboratory Data L2

The following code defines example dataset L2 from the FOCUS kinetics -report, p. 287

+report, p. 287:

FOCUS_2006_L2 = data.frame(
   t = rep(c(0, 1, 3, 7, 14, 28), each = 2),
@@ -419,35 +412,35 @@ report, p. 287

FOCUS_2006_L2_mkin <- mkin_wide_to_long(FOCUS_2006_L2)
-

Again, the SFO model is fitted and a summary is obtained.

+

Again, the SFO model is fitted and a summary is obtained:

-
m.L2.SFO <- mkinfit(SFO, FOCUS_2006_L2_mkin, quiet=TRUE)
+
m.L2.SFO <- mkinfit("SFO", FOCUS_2006_L2_mkin, quiet=TRUE)
 summary(m.L2.SFO)
 
## mkin version:    0.9.32 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 17 12:37:42 2014 
-## Date of summary: Thu Jul 17 12:37:42 2014 
+## Date of fit:     Mon Jul 21 09:14:30 2014 
+## Date of summary: Mon Jul 21 09:14:30 2014 
 ## 
 ## Equations:
 ## [1] d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 32 model solutions performed in 0.357 s
+## Fitted with method Marq using 29 model solutions performed in 0.155 s
 ## 
 ## Weighting: none
 ## 
 ## Starting values for parameters to be optimised:
 ##               value   type
-## parent_0      100.0  state
-## k_parent_sink   0.1 deparm
+## parent_0      93.95  state
+## k_parent_sink  0.10 deparm
 ## 
 ## Starting values for the transformed parameters actually optimised:
-##                     value lower upper
-## parent_0          100.000  -Inf   Inf
-## log_k_parent_sink  -2.303  -Inf   Inf
+##                    value lower upper
+## parent_0          93.950  -Inf   Inf
+## log_k_parent_sink -2.303  -Inf   Inf
 ## 
 ## Fixed parameter values:
 ## None
@@ -487,8 +480,8 @@ summary(m.L2.SFO)
 ## 
 ## Data:
 ##  time variable observed predicted residual
-##     0   parent     96.1  9.15e+01    4.634
-##     0   parent     91.8  9.15e+01    0.334
+##     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
 ##     3   parent     19.3  1.25e+01    6.779
@@ -509,7 +502,7 @@ plot(m.L2.SFO)
 mkinresplot(m.L2.SFO)
 
-

plot of chunk unnamed-chunk-10

+

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 @@ -524,42 +517,42 @@ models generally only implement SFO kinetics.

For comparison, the FOMC model is fitted as well, and the chi2 error level is checked.

-
m.L2.FOMC <- mkinfit(FOMC, FOCUS_2006_L2_mkin, quiet = TRUE)
+
m.L2.FOMC <- mkinfit("FOMC", FOCUS_2006_L2_mkin, quiet = TRUE)
 par(mfrow = c(2, 1))
 plot(m.L2.FOMC)
 mkinresplot(m.L2.FOMC)
 
-

plot of chunk unnamed-chunk-11

+

plot of chunk unnamed-chunk-10

summary(m.L2.FOMC, data = FALSE)
 
## mkin version:    0.9.32 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 17 12:37:43 2014 
-## Date of summary: Thu Jul 17 12:37:43 2014 
+## Date of fit:     Mon Jul 21 09:14:31 2014 
+## Date of summary: Mon Jul 21 09:14:31 2014 
 ## 
 ## Equations:
 ## [1] d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 39 model solutions performed in 0.235 s
+## Fitted with method Marq using 35 model solutions performed in 0.199 s
 ## 
 ## Weighting: none
 ## 
 ## Starting values for parameters to be optimised:
 ##          value   type
-## parent_0   100  state
-## alpha        1 deparm
-## beta        10 deparm
+## parent_0 93.95  state
+## alpha     1.00 deparm
+## beta     10.00 deparm
 ## 
 ## Starting values for the transformed parameters actually optimised:
-##             value lower upper
-## parent_0  100.000  -Inf   Inf
-## log_alpha   0.000  -Inf   Inf
-## log_beta    2.303  -Inf   Inf
+##            value lower upper
+## parent_0  93.950  -Inf   Inf
+## log_alpha  0.000  -Inf   Inf
+## log_beta   2.303  -Inf   Inf
 ## 
 ## Fixed parameter values:
 ## None
@@ -600,62 +593,62 @@ experimental error has to be assumed in order to explain the data.

Fitting the four parameter DFOP model further reduces the chi2 error level.

-
m.L2.DFOP <- mkinfit(DFOP, FOCUS_2006_L2_mkin, quiet = TRUE)
+
m.L2.DFOP <- mkinfit("DFOP", FOCUS_2006_L2_mkin, quiet = TRUE)
 plot(m.L2.DFOP)
 
-

plot of chunk unnamed-chunk-12

+

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 parameters.

-
m.L2.DFOP <- mkinfit(DFOP, FOCUS_2006_L2_mkin, 
+
m.L2.DFOP <- mkinfit("DFOP", FOCUS_2006_L2_mkin, 
   parms.ini = c(k1 = 1, k2 = 0.01, g = 0.8),
   quiet=TRUE)
 plot(m.L2.DFOP)
 
-

plot of chunk unnamed-chunk-13

+

plot of chunk unnamed-chunk-12

summary(m.L2.DFOP, data = FALSE)
 
## mkin version:    0.9.32 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 17 12:37:44 2014 
-## Date of summary: Thu Jul 17 12:37:44 2014 
+## Date of fit:     Mon Jul 21 09:14:31 2014 
+## Date of summary: Mon Jul 21 09:14:31 2014 
 ## 
 ## Equations:
 ## [1] d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 * time)) / (g * exp(-k1 * time) + (1 - g) * exp(-k2 * time))) * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 54 model solutions performed in 0.423 s
+## Fitted with method Marq using 43 model solutions performed in 0.241 s
 ## 
 ## Weighting: none
 ## 
 ## Starting values for parameters to be optimised:
 ##          value   type
-## parent_0 1e+02  state
-## k1       1e+00 deparm
-## k2       1e-02 deparm
-## g        8e-01 deparm
+## parent_0 93.95  state
+## k1        1.00 deparm
+## k2        0.01 deparm
+## g         0.80 deparm
 ## 
 ## Starting values for the transformed parameters actually optimised:
-##             value lower upper
-## parent_0 100.0000  -Inf   Inf
-## log_k1     0.0000  -Inf   Inf
-## log_k2    -4.6052  -Inf   Inf
-## g_ilr      0.9803  -Inf   Inf
+##            value lower upper
+## parent_0 93.9500  -Inf   Inf
+## log_k1    0.0000  -Inf   Inf
+## log_k2   -4.6052  -Inf   Inf
+## g_ilr     0.9803  -Inf   Inf
 ## 
 ## Fixed parameter values:
 ## None
 ## 
 ## Optimised, transformed parameters:
 ##          Estimate Std. Error Lower Upper t value Pr(>|t|) Pr(>t)
-## parent_0   93.900         NA    NA    NA      NA       NA     NA
-## log_k1      4.960         NA    NA    NA      NA       NA     NA
+## parent_0   94.000         NA    NA    NA      NA       NA     NA
+## log_k1      6.160         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
 ## 
@@ -666,8 +659,8 @@ plot(m.L2.DFOP)
 ## 
 ## Backtransformed parameters:
 ##          Estimate Lower Upper
-## parent_0   93.900    NA    NA
-## k1        142.000    NA    NA
+## parent_0   94.000    NA    NA
+## k1        476.000    NA    NA
 ## k2          0.337    NA    NA
 ## g           0.402    NA    NA
 ## 
@@ -678,7 +671,7 @@ plot(m.L2.DFOP)
 ## 
 ## Estimated disappearance times:
 ##        DT50 DT90 DT50_k1 DT50_k2
-## parent   NA   NA 0.00487    2.06
+## parent   NA   NA 0.00146    2.06
 

Here, the DFOP model is clearly the best-fit model for dataset L2 based on the @@ -699,38 +692,38 @@ FOCUS_2006_L3_mkin <- mkin_wide_to_long(FOCUS_2006_L3)

SFO model, summary and plot:

-
m.L3.SFO <- mkinfit(SFO, FOCUS_2006_L3_mkin, quiet = TRUE)
+
m.L3.SFO <- mkinfit("SFO", FOCUS_2006_L3_mkin, quiet = TRUE)
 plot(m.L3.SFO)
 
-

plot of chunk unnamed-chunk-15

+

plot of chunk unnamed-chunk-14

summary(m.L3.SFO)
 
## mkin version:    0.9.32 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 17 12:37:45 2014 
-## Date of summary: Thu Jul 17 12:37:45 2014 
+## Date of fit:     Mon Jul 21 09:14:32 2014 
+## Date of summary: Mon Jul 21 09:14:32 2014 
 ## 
 ## Equations:
 ## [1] d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 44 model solutions performed in 0.241 s
+## Fitted with method Marq using 44 model solutions performed in 0.242 s
 ## 
 ## Weighting: none
 ## 
 ## Starting values for parameters to be optimised:
 ##               value   type
-## parent_0      100.0  state
+## parent_0       97.8  state
 ## k_parent_sink   0.1 deparm
 ## 
 ## Starting values for the transformed parameters actually optimised:
-##                     value lower upper
-## parent_0          100.000  -Inf   Inf
-## log_k_parent_sink  -2.303  -Inf   Inf
+##                    value lower upper
+## parent_0          97.800  -Inf   Inf
+## log_k_parent_sink -2.303  -Inf   Inf
 ## 
 ## Fixed parameter values:
 ## None
@@ -770,7 +763,7 @@ plot(m.L3.SFO)
 ## 
 ## Data:
 ##  time variable observed predicted residual
-##     0   parent     97.8     74.87  22.9273
+##     0   parent     97.8     74.87  22.9274
 ##     3   parent     60.0     69.41  -9.4065
 ##     7   parent     51.0     62.73 -11.7340
 ##    14   parent     43.0     52.56  -9.5634
@@ -785,40 +778,40 @@ does not fit very well. 

The FOMC model performs better:

-
m.L3.FOMC <- mkinfit(FOMC, FOCUS_2006_L3_mkin, quiet = TRUE)
+
m.L3.FOMC <- mkinfit("FOMC", FOCUS_2006_L3_mkin, quiet = TRUE)
 plot(m.L3.FOMC)
 
-

plot of chunk unnamed-chunk-16

+

plot of chunk unnamed-chunk-15

summary(m.L3.FOMC, data = FALSE)
 
## mkin version:    0.9.32 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 17 12:37:45 2014 
-## Date of summary: Thu Jul 17 12:37:45 2014 
+## Date of fit:     Mon Jul 21 09:14:32 2014 
+## Date of summary: Mon Jul 21 09:14:32 2014 
 ## 
 ## Equations:
 ## [1] 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.208 s
+## Fitted with method Marq using 26 model solutions performed in 0.143 s
 ## 
 ## Weighting: none
 ## 
 ## Starting values for parameters to be optimised:
 ##          value   type
-## parent_0   100  state
-## alpha        1 deparm
-## beta        10 deparm
+## parent_0  97.8  state
+## alpha      1.0 deparm
+## beta      10.0 deparm
 ## 
 ## Starting values for the transformed parameters actually optimised:
-##             value lower upper
-## parent_0  100.000  -Inf   Inf
-## log_alpha   0.000  -Inf   Inf
-## log_beta    2.303  -Inf   Inf
+##            value lower upper
+## parent_0  97.800  -Inf   Inf
+## log_alpha  0.000  -Inf   Inf
+## log_beta   2.303  -Inf   Inf
 ## 
 ## Fixed parameter values:
 ## None
@@ -858,42 +851,42 @@ plot(m.L3.FOMC)
 

Fitting the four parameter DFOP model further reduces the chi2 error level considerably:

-
m.L3.DFOP <- mkinfit(DFOP, FOCUS_2006_L3_mkin, quiet = TRUE)
+
m.L3.DFOP <- mkinfit("DFOP", FOCUS_2006_L3_mkin, quiet = TRUE)
 plot(m.L3.DFOP)
 
-

plot of chunk unnamed-chunk-17

+

plot of chunk unnamed-chunk-16

summary(m.L3.DFOP, data = FALSE)
 
## mkin version:    0.9.32 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 17 12:37:46 2014 
-## Date of summary: Thu Jul 17 12:37:46 2014 
+## Date of fit:     Mon Jul 21 09:14:32 2014 
+## Date of summary: Mon Jul 21 09:14:32 2014 
 ## 
 ## Equations:
 ## [1] d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 * time)) / (g * exp(-k1 * time) + (1 - g) * exp(-k2 * time))) * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 37 model solutions performed in 0.338 s
+## Fitted with method Marq using 37 model solutions performed in 0.21 s
 ## 
 ## Weighting: none
 ## 
 ## Starting values for parameters to be optimised:
 ##          value   type
-## parent_0 1e+02  state
-## k1       1e-01 deparm
-## k2       1e-02 deparm
-## g        5e-01 deparm
+## parent_0 97.80  state
+## k1        0.10 deparm
+## k2        0.01 deparm
+## g         0.50 deparm
 ## 
 ## Starting values for the transformed parameters actually optimised:
-##            value lower upper
-## parent_0 100.000  -Inf   Inf
-## log_k1    -2.303  -Inf   Inf
-## log_k2    -4.605  -Inf   Inf
-## g_ilr      0.000  -Inf   Inf
+##           value lower upper
+## parent_0 97.800  -Inf   Inf
+## log_k1   -2.303  -Inf   Inf
+## log_k2   -4.605  -Inf   Inf
+## g_ilr     0.000  -Inf   Inf
 ## 
 ## Fixed parameter values:
 ## None
@@ -936,10 +929,15 @@ and the correlation matrix suggest that the parameter estimates are reliable, an
 the DFOP model can be used as the best-fit model based on the chi2 error
 level criterion for laboratory data L3.

+

This is also an example where the standard t-test for the parameter g_ilr is +misleading, as it tests for a significant difference from zero. In this case, +zero appears to be the correct value for this parameter, and the confidence +interval for the backtransformed parameter g is quite narrow.

+

Laboratory Data L4

The following code defines example dataset L4 from the FOCUS kinetics -report, p. 293

+report, p. 293:

FOCUS_2006_L4 = data.frame(
   t = c(0, 3, 7, 14, 30, 60, 91, 120),
@@ -949,38 +947,38 @@ FOCUS_2006_L4_mkin <- mkin_wide_to_long(FOCUS_2006_L4)
 
 

SFO model, summary and plot:

-
m.L4.SFO <- mkinfit(SFO, FOCUS_2006_L4_mkin, quiet = TRUE)
+
m.L4.SFO <- mkinfit("SFO", FOCUS_2006_L4_mkin, quiet = TRUE)
 plot(m.L4.SFO)
 
-

plot of chunk unnamed-chunk-19

+

plot of chunk unnamed-chunk-18

summary(m.L4.SFO, data = FALSE)
 
## mkin version:    0.9.32 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 17 12:37:46 2014 
-## Date of summary: Thu Jul 17 12:37:46 2014 
+## Date of fit:     Mon Jul 21 09:14:33 2014 
+## Date of summary: Mon Jul 21 09:14:33 2014 
 ## 
 ## Equations:
 ## [1] d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 20 model solutions performed in 0.127 s
+## Fitted with method Marq using 20 model solutions performed in 0.109 s
 ## 
 ## Weighting: none
 ## 
 ## Starting values for parameters to be optimised:
 ##               value   type
-## parent_0      100.0  state
+## parent_0       96.6  state
 ## k_parent_sink   0.1 deparm
 ## 
 ## Starting values for the transformed parameters actually optimised:
-##                     value lower upper
-## parent_0          100.000  -Inf   Inf
-## log_k_parent_sink  -2.303  -Inf   Inf
+##                    value lower upper
+## parent_0          96.600  -Inf   Inf
+## log_k_parent_sink -2.303  -Inf   Inf
 ## 
 ## Fixed parameter values:
 ## None
@@ -1022,42 +1020,42 @@ plot(m.L4.SFO)
 

The chi2 error level of 3.3% as well as the plot suggest that the model fits very well.

-

The FOMC model for comparison

+

The FOMC model for comparison:

-
m.L4.FOMC <- mkinfit(FOMC, FOCUS_2006_L4_mkin, quiet = TRUE)
+
m.L4.FOMC <- mkinfit("FOMC", FOCUS_2006_L4_mkin, quiet = TRUE)
 plot(m.L4.FOMC)
 
-

plot of chunk unnamed-chunk-20

+

plot of chunk unnamed-chunk-19

summary(m.L4.FOMC, data = FALSE)
 
## mkin version:    0.9.32 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 17 12:37:46 2014 
-## Date of summary: Thu Jul 17 12:37:46 2014 
+## Date of fit:     Mon Jul 21 09:14:33 2014 
+## Date of summary: Mon Jul 21 09:14:33 2014 
 ## 
 ## Equations:
 ## [1] 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.355 s
+## Fitted with method Marq using 48 model solutions performed in 0.26 s
 ## 
 ## Weighting: none
 ## 
 ## Starting values for parameters to be optimised:
 ##          value   type
-## parent_0   100  state
-## alpha        1 deparm
-## beta        10 deparm
+## parent_0  96.6  state
+## alpha      1.0 deparm
+## beta      10.0 deparm
 ## 
 ## Starting values for the transformed parameters actually optimised:
-##             value lower upper
-## parent_0  100.000  -Inf   Inf
-## log_alpha   0.000  -Inf   Inf
-## log_beta    2.303  -Inf   Inf
+##            value lower upper
+## parent_0  96.600  -Inf   Inf
+## log_alpha  0.000  -Inf   Inf
+## log_beta   2.303  -Inf   Inf
 ## 
 ## Fixed parameter values:
 ## None
diff --git a/vignettes/mkin.pdf b/vignettes/mkin.pdf
index 9cf1b3e5..b69ddddc 100644
Binary files a/vignettes/mkin.pdf and b/vignettes/mkin.pdf differ
-- 
cgit v1.2.3


From 4c6f29fe2a3ece5a85160b891c89ce0f55299c11 Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Wed, 23 Jul 2014 08:34:59 +0200
Subject: Parallel metabolite formation with formation fractions in mkinerrmin

---
 DESCRIPTION    | 2 +-
 NEWS.md        | 4 ++++
 R/mkinerrmin.R | 7 ++++---
 TODO           | 1 +
 4 files changed, 10 insertions(+), 4 deletions(-)

(limited to 'R')

diff --git a/DESCRIPTION b/DESCRIPTION
index bad72501..de9ff203 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-32
-Date: 2014-07-21
+Date: 2014-07-23
 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), 
                     email = "jranke@uni-bremen.de"),
              person("Katrin", "Lindenberger", role = "ctb"),
diff --git a/NEWS.md b/NEWS.md
index c10a25b0..9d5129c9 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -2,6 +2,8 @@
 
 ## NEW FEATURES
 
+- The number of degrees of freedom is difficult to define in the case of ilr transformation of formation fractions. Now for each source compartment the number of ilr parameters (=number of optimised parameters) is divided by the number of pathways to metabolites (=number of affected data series) which leads to fractional degrees of freedom in some cases.
+
 - The default for the initial value for the first state value is now taken from the mean of the observations at time zero, if available.
 
 - The kinetic model can alternatively be specified with a shorthand name for parent only degradation models, e.g. `SFO`, or `DFOP`.
@@ -14,6 +16,8 @@
 
 ## BUG FIXES
 
+- In the determination of the degrees of freedom in `mkinerrmin`, formation fractions were accounted for multiple times in the case of parallel formation of metabolites. See the new feature described above for the solution.
+
 - `transform_rates=FALSE` in `mkinfit` now also works for FOMC and HS models.
 
 - Initial values for formation fractions were not set in all cases.
diff --git a/R/mkinerrmin.R b/R/mkinerrmin.R
index 9ebac6a4..09724730 100644
--- a/R/mkinerrmin.R
+++ b/R/mkinerrmin.R
@@ -65,11 +65,12 @@ mkinerrmin <- function(fit, alpha = 0.05)
     # Formation fractions are attributed to the target variable, so look
     # for source compartments with formation fractions
     for (source_var in fit$obs_vars) {
+      n.ff.source = length(grep(paste("^f", source_var, sep = "_"),
+                                 names(parms.optim)))
+      n.paths.source = length(fit$mkinmod$spec[[source_var]]$to)
       for (target_var in fit$mkinmod$spec[[source_var]]$to) {
         if (obs_var == target_var) {
-          n.ff.optim <- n.ff.optim +  
-                        length(grep(paste("^f", source_var, sep = "_"),
-                                    names(parms.optim))) 
+          n.ff.optim <- n.ff.optim + n.ff.source/n.paths.source
         }
       }
     }
diff --git a/TODO b/TODO
index 83524220..a9eb8b70 100644
--- a/TODO
+++ b/TODO
@@ -3,6 +3,7 @@ TODO for version 1.0
 - Complete the main package vignette named mkin to include a method description
 - Improve formatting of differential equations in the summary
 - Improve order of parameters in output
+- Write unit tests for mkinerrmin
 
 Nice to have:
 - Calculate confidence intervals for DT50 and DT90 values when only one
-- 
cgit v1.2.3


From 8be01094ecbc65d4bdf1e7f819ef01b7a252b39d Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Thu, 24 Jul 2014 09:58:55 +0200
Subject: Avoid artificial zero residual in mkinresplot

---
 DESCRIPTION     | 2 +-
 NEWS.md         | 2 ++
 R/mkinresplot.R | 3 ++-
 3 files changed, 5 insertions(+), 2 deletions(-)

(limited to 'R')

diff --git a/DESCRIPTION b/DESCRIPTION
index de9ff203..2da7f1c1 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-32
-Date: 2014-07-23
+Date: 2014-07-24
 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), 
                     email = "jranke@uni-bremen.de"),
              person("Katrin", "Lindenberger", role = "ctb"),
diff --git a/NEWS.md b/NEWS.md
index 9d5129c9..02c7c661 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -16,6 +16,8 @@
 
 ## BUG FIXES
 
+- Avoid plotting an artifical 0 residual at time zero in `mkinresplot`
+
 - In the determination of the degrees of freedom in `mkinerrmin`, formation fractions were accounted for multiple times in the case of parallel formation of metabolites. See the new feature described above for the solution.
 
 - `transform_rates=FALSE` in `mkinfit` now also works for FOMC and HS models.
diff --git a/R/mkinresplot.R b/R/mkinresplot.R
index c9a801fd..82ffd2cb 100644
--- a/R/mkinresplot.R
+++ b/R/mkinresplot.R
@@ -36,7 +36,8 @@ mkinresplot <- function (object,
 	col_obs <- pch_obs <- 1:length(obs_vars)
  	names(col_obs) <- names(pch_obs) <- obs_vars
 
-  plot(0,  xlab = xlab, ylab = ylab, 
+  plot(0, type = "n",
+       xlab = xlab, ylab = ylab, 
        xlim = xlim,
        ylim = c(-1.2 * maxabs, 1.2 * maxabs), ...)
 
-- 
cgit v1.2.3


From 9b947f0358d3a1b1fc922bfd0187ca444ce5811d Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Thu, 24 Jul 2014 14:42:59 +0200
Subject: Bump version, better default for state.ini

---
 DESCRIPTION    |  2 +-
 NEWS.md        |  6 ++++++
 R/mkinfit.R    | 10 ++++++++--
 man/mkinfit.Rd |  1 +
 4 files changed, 16 insertions(+), 3 deletions(-)

(limited to 'R')

diff --git a/DESCRIPTION b/DESCRIPTION
index 2da7f1c1..bf0fa09a 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -2,7 +2,7 @@ Package: mkin
 Type: Package
 Title: Routines for fitting kinetic models with one or more state
   variables to chemical degradation data
-Version: 0.9-32
+Version: 0.9-33
 Date: 2014-07-24
 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), 
                     email = "jranke@uni-bremen.de"),
diff --git a/NEWS.md b/NEWS.md
index 02c7c661..4c45a0d1 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,3 +1,9 @@
+# CHANGES in mkin VERSION 0.9-33
+
+## NEW FEATURES
+
+- The initial value (state.ini) for the observed variable with the highest observed residue is set to 100 in case it has no time zero observation and `state.ini = "auto"`
+
 # CHANGES in mkin VERSION 0.9-32
 
 ## NEW FEATURES
diff --git a/R/mkinfit.R b/R/mkinfit.R
index 39d084cb..c98c7586 100644
--- a/R/mkinfit.R
+++ b/R/mkinfit.R
@@ -154,8 +154,14 @@ mkinfit <- function(mkinmod, observed,
 
   # Set default for state.ini if appropriate
   if (state.ini[1] == "auto") {
-    state.ini = c(mean(subset(observed, time == 0 & name == presumed_parent_name)$value), 
-                  rep(0, length(mkinmod$diffs) - 1))
+    presumed_parent_time_0 = subset(observed, 
+                                    time == 0 & name == presumed_parent_name)$value
+    presumed_parent_time_0_mean = mean(presumed_parent_time_0, na.rm = TRUE)
+    if (is.na(presumed_parent_time_0_mean)) {
+      state.ini = c(100, rep(0, length(mkinmod$diffs) - 1))
+    } else {
+      state.ini = c(presumed_parent_time_0_mean, rep(0, length(mkinmod$diffs) - 1))
+    }
   }
 
   # Name the inital state variable values if they are not named yet
diff --git a/man/mkinfit.Rd b/man/mkinfit.Rd
index 581d63f4..4e331e2a 100644
--- a/man/mkinfit.Rd
+++ b/man/mkinfit.Rd
@@ -71,6 +71,7 @@ mkinfit(mkinmod, observed,
     (see \code{map} component of \code{\link{mkinmod}}). The default is to set
     the initial value of the first model variable to the mean of the time zero
     values for the variable with the maximum observed value, and all others to 0.
+    If this variable has no time zero observations, its initial value is set to 100.
   }
   \item{fixed_parms}{
     The names of parameters that should not be optimised but rather kept at the
-- 
cgit v1.2.3


From e5c955f82adf6139d76f842a0b85e5d383685793 Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Fri, 25 Jul 2014 09:36:15 +0200
Subject: Fix internal naming of g for transform_fractions=FALSE

---
 DESCRIPTION            |  2 +-
 NEWS.md                |  4 ++++
 R/transform_odeparms.R | 15 +++++++++++++--
 3 files changed, 18 insertions(+), 3 deletions(-)

(limited to 'R')

diff --git a/DESCRIPTION b/DESCRIPTION
index bf0fa09a..02480e56 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-33
-Date: 2014-07-24
+Date: 2014-07-25
 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), 
                     email = "jranke@uni-bremen.de"),
              person("Katrin", "Lindenberger", role = "ctb"),
diff --git a/NEWS.md b/NEWS.md
index 4c45a0d1..d3c43d60 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -4,6 +4,10 @@
 
 - The initial value (state.ini) for the observed variable with the highest observed residue is set to 100 in case it has no time zero observation and `state.ini = "auto"`
 
+## BUG FIXES
+
+- `mkinfit()`: The internally fitted parameter for `g` was named `g_ilr` even when `transform_fractions=FALSE`
+
 # CHANGES in mkin VERSION 0.9-32
 
 ## NEW FEATURES
diff --git a/R/transform_odeparms.R b/R/transform_odeparms.R
index f518ae32..e64bac59 100644
--- a/R/transform_odeparms.R
+++ b/R/transform_odeparms.R
@@ -76,9 +76,15 @@ transform_odeparms <- function(parms, mkinmod,
       }
     } 
   }
+
+  # DFOP parameter g is treated as a fraction
   if (!is.na(parms["g"])) {
     g <- parms["g"]
-    transparms["g_ilr"] <- ifelse(transform_fractions, ilr(c(g, 1 - g)), g)
+    if (transform_fractions) {
+      transparms["g_ilr"] <- ilr(c(g, 1 - g))
+    } else {
+      transparms["g"] <- g
+    }
   }
 
   return(transparms)
@@ -145,9 +151,14 @@ backtransform_odeparms <- function(transparms, mkinmod,
       }
     }
   }
+
+  # DFOP parameter g is treated as a fraction
   if (!is.na(transparms["g_ilr"])) {
     g_ilr <- transparms["g_ilr"]
-    parms["g"] <- ifelse(transform_fractions, invilr(g_ilr)[1], g_ilr)
+    parms["g"] <- invilr(g_ilr)[1]
+  }
+  if (!is.na(transparms["g"])) {
+    parms["g"] <- transparms["g"]
   }
 
   return(parms)
-- 
cgit v1.2.3


From 37a252cb44fed78c4f7a00a2f7874f1c47456468 Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Tue, 19 Aug 2014 17:55:06 +0200
Subject: Improve formatting of differential equations in output

Rebuild of FOCUS_Z vignette with improved formatting
---
 DESCRIPTION           |   5 +++--
 R/mkinfit.R           |   2 +-
 TODO                  |   1 -
 vignettes/FOCUS_Z.Rnw |   1 +
 vignettes/FOCUS_Z.pdf | Bin 214013 -> 220196 bytes
 5 files changed, 5 insertions(+), 4 deletions(-)

(limited to 'R')

diff --git a/DESCRIPTION b/DESCRIPTION
index 02480e56..3898f69a 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-33
-Date: 2014-07-25
+Date: 2014-08-19
 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), 
                     email = "jranke@uni-bremen.de"),
              person("Katrin", "Lindenberger", role = "ctb"),
@@ -24,5 +24,6 @@ LazyData: yes
 Encoding: UTF-8
 VignetteBuilder: knitr
 BugReports: http://github.com/jranke/mkin/issues
-URL: http://kinfit.r-forge.r-project.org, http://cran.r-project.org/package=mkin, 
+URL: http://cran.r-project.org/package=mkin, 
+     http://kinfit.r-forge.r-project.org/mkin_static, 
      http://github.com/jranke/mkin
diff --git a/R/mkinfit.R b/R/mkinfit.R
index c98c7586..92c8f8bf 100644
--- a/R/mkinfit.R
+++ b/R/mkinfit.R
@@ -567,7 +567,7 @@ print.summary.mkinfit <- function(x, digits = max(3, getOption("digits") - 3), .
   if (!is.null(x$warning)) cat("\n\nWarning:", x$warning, "\n\n")
 
   cat("\nEquations:\n")
-  print(noquote(as.character(x[["diffs"]])))
+  cat(noquote(strwrap(x[["diffs"]], exdent = 11)), fill = TRUE)
   df  <- x$df
   rdf <- df[2]
 
diff --git a/TODO b/TODO
index 2617bf2c..4b73063f 100644
--- a/TODO
+++ b/TODO
@@ -1,7 +1,6 @@
 TODO for version 1.0
 - Think about what a user would expect from version 1.0
 - Complete the main package vignette named mkin to include a method description
-- Improve formatting of differential equations in the summary
 - Improve order of parameters in output
 - Write unit tests for mkinerrmin
 - Calculate confidence intervals for more than one formation fraction using Monte Carlo simulations
diff --git a/vignettes/FOCUS_Z.Rnw b/vignettes/FOCUS_Z.Rnw
index 5b0ee79e..e2a2473e 100644
--- a/vignettes/FOCUS_Z.Rnw
+++ b/vignettes/FOCUS_Z.Rnw
@@ -20,6 +20,7 @@
 <>=
 require(knitr)
 opts_chunk$set(engine='R', tidy=FALSE)
+options(width=70)
 @
 
 \title{Example evaluation of FOCUS dataset Z}
diff --git a/vignettes/FOCUS_Z.pdf b/vignettes/FOCUS_Z.pdf
index f7b0a65a..210ce099 100644
Binary files a/vignettes/FOCUS_Z.pdf and b/vignettes/FOCUS_Z.pdf differ
-- 
cgit v1.2.3


From 81dc12a8c149e11d57190ebf59f3f7e9c7b99af7 Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Wed, 20 Aug 2014 14:42:20 +0200
Subject: Fix mkinfit for a special situation

See NEWS.md: When the parent was not the (only) variable with the
highest value out of all variables in the observed data, it could
happen that the initial value (state.ini) was not initialised.
---
 DESCRIPTION |  2 +-
 NEWS.md     |  2 ++
 R/mkinfit.R | 14 +++++++-------
 3 files changed, 10 insertions(+), 8 deletions(-)

(limited to 'R')

diff --git a/DESCRIPTION b/DESCRIPTION
index 3898f69a..e8b7195d 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-33
-Date: 2014-08-19
+Date: 2014-08-20
 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), 
                     email = "jranke@uni-bremen.de"),
              person("Katrin", "Lindenberger", role = "ctb"),
diff --git a/NEWS.md b/NEWS.md
index a24c31bc..d05c2095 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -8,6 +8,8 @@
 
 - `mkinfit()`: The internally fitted parameter for `g` was named `g_ilr` even when `transform_fractions=FALSE`
 
+- `mkinfit()`: The initial value (state.ini) for the parent compound was not set when the parent was not the (only) variable with the highest value in the observed data.
+
 ## MINOR CHANGES
 
 - The formatting of differential equations in the summary was improved by wrapping overly long lines
diff --git a/R/mkinfit.R b/R/mkinfit.R
index 92c8f8bf..0f488ec8 100644
--- a/R/mkinfit.R
+++ b/R/mkinfit.R
@@ -41,11 +41,11 @@ mkinfit <- function(mkinmod, observed,
   trace_parms = FALSE,
   ...)
 {
-  # Check mkinmod and generate a model for the variable whithe the highest value
+  # Check mkinmod and generate a model for the variable whith the highest value
   # if a suitable string is given
   parent_models_available = c("SFO", "FOMC", "DFOP", "HS", "SFORB") 
-  presumed_parent_name = observed[which.max(observed$value), "name"]
   if (class(mkinmod) != "mkinmod") {
+    presumed_parent_name = observed[which.max(observed$value), "name"]
     if (mkinmod[[1]] %in% parent_models_available) {
       speclist <- list(list(type = mkinmod, sink = TRUE))
       names(speclist) <- presumed_parent_name
@@ -153,14 +153,14 @@ mkinfit <- function(mkinmod, observed,
   }
 
   # Set default for state.ini if appropriate
+  parent_name = names(mkinmod$spec)[[1]]
   if (state.ini[1] == "auto") {
-    presumed_parent_time_0 = subset(observed, 
-                                    time == 0 & name == presumed_parent_name)$value
-    presumed_parent_time_0_mean = mean(presumed_parent_time_0, na.rm = TRUE)
-    if (is.na(presumed_parent_time_0_mean)) {
+    parent_time_0 = subset(observed, time == 0 & name == parent_name)$value
+    parent_time_0_mean = mean(parent_time_0, na.rm = TRUE)
+    if (is.na(parent_time_0_mean)) {
       state.ini = c(100, rep(0, length(mkinmod$diffs) - 1))
     } else {
-      state.ini = c(presumed_parent_time_0_mean, rep(0, length(mkinmod$diffs) - 1))
+      state.ini = c(parent_time_0_mean, rep(0, length(mkinmod$diffs) - 1))
     }
   }
 
-- 
cgit v1.2.3


From f30472ecd2afea6bd2153b8ad2bb2f663f3a2742 Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Mon, 25 Aug 2014 10:39:40 +0200
Subject: Bug fix and unit tests for mkinerrmin

See NEWS.md for details
---
 NEWS.md                           |   7 +++
 R/mkinerrmin.R                    |   9 +--
 TODO                              |   5 --
 inst/unitTests/runit.mkinerrmin.R |  62 +++++++++++++++++++++
 inst/unitTests/runit.mkinfit.R    |  38 +------------
 man/mkinerrmin.Rd                 |  10 ++++
 tests/doRUnit.R                   |   1 -
 vignettes/FOCUS_L.html            | 112 +++++++++++++++++++++-----------------
 vignettes/FOCUS_Z.pdf             | Bin 220196 -> 220177 bytes
 9 files changed, 146 insertions(+), 98 deletions(-)
 create mode 100644 inst/unitTests/runit.mkinerrmin.R

(limited to 'R')

diff --git a/NEWS.md b/NEWS.md
index d05c2095..1ed94c97 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -4,12 +4,19 @@
 
 - The initial value (state.ini) for the observed variable with the highest observed residue is set to 100 in case it has no time zero observation and `state.ini = "auto"`
 
+- A basic unit test for `mkinerrmin()` was written
+
 ## BUG FIXES
 
 - `mkinfit()`: The internally fitted parameter for `g` was named `g_ilr` even when `transform_fractions=FALSE`
 
 - `mkinfit()`: The initial value (state.ini) for the parent compound was not set when the parent was not the (only) variable with the highest value in the observed data.
 
+- `mkinerrmin()`: When checking for degrees of freedom for metabolites, check
+  if their time zero value is fixed instead of checking if the observed value
+  is zero. This ensures correct calculation of degrees of freedom also in cases
+  where the metabolite residue at time zero is greater zero.
+
 ## MINOR CHANGES
 
 - The formatting of differential equations in the summary was improved by wrapping overly long lines
diff --git a/R/mkinerrmin.R b/R/mkinerrmin.R
index 09724730..2697d0a0 100644
--- a/R/mkinerrmin.R
+++ b/R/mkinerrmin.R
@@ -36,10 +36,11 @@ mkinerrmin <- function(fit, alpha = 0.05)
     suffixes = c("_mean", "_pred"))
   errdata <- errdata[order(errdata$time, errdata$name), ]
 
-  # Any value that is set to exactly zero is not really an observed value
-  # Remove those at time 0 - those are caused by the FOCUS recommendation
-  # to set metabolites occurring at time 0 to 0
-  errdata <- subset(errdata, !(time == 0 & value_mean == 0))
+  # Remove values at time zero for variables whose value for state.ini is fixed,
+  # as these will not have any effect in the optimization and should therefore not 
+  # be counted as degrees of freedom.
+  fixed_initials = gsub("_0$", "", rownames(subset(fit$fixed, type = "state")))
+  errdata <- subset(errdata, !(time == 0 & name %in% fixed_initials))
 
   n.optim.overall <- length(parms.optim)
 
diff --git a/TODO b/TODO
index 92a91069..f979d13a 100644
--- a/TODO
+++ b/TODO
@@ -2,11 +2,6 @@ TODO for version 1.0
 - Think about what a user would expect from version 1.0
 - Complete the main package vignette named mkin to include a method description
 - Improve order of parameters in output
-- Write unit tests for mkinerrmin
-- When checking for degrees of freedom for metabolites, check if their time
-  zero value (state.ini) is fixed instead of checking if the observed value is
-  zero (usually in regulatory kinetics it is set to zero anyway, but in the
-  case of known impurities this may not be the case).
 
 Nice to have:
 - Get starting values for formation fractions from data
diff --git a/inst/unitTests/runit.mkinerrmin.R b/inst/unitTests/runit.mkinerrmin.R
new file mode 100644
index 00000000..56a33ff9
--- /dev/null
+++ b/inst/unitTests/runit.mkinerrmin.R
@@ -0,0 +1,62 @@
+# Test SFO_SFO model with FOCUS_2006_D against Schaefer 2007 paper, tolerance = 1% # {{{
+# and check chi2 error values against values obtained with mkin 0.33
+test.FOCUS_2006_D_SFO_SFO <- function()
+{
+  SFO_SFO.1 <- mkinmod(parent = list(type = "SFO", to = "m1"),
+         m1 = list(type = "SFO"), use_of_ff = "min")
+  SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"),
+         m1 = list(type = "SFO"), use_of_ff = "max")
+
+  fit.1.e <- mkinfit(SFO_SFO.1, FOCUS_2006_D)
+  fit.1.d <- mkinfit(SFO_SFO.1, solution_type = "deSolve", FOCUS_2006_D)
+  fit.2.e <- mkinfit(SFO_SFO.2, FOCUS_2006_D)
+  fit.2.d <- mkinfit(SFO_SFO.2, solution_type = "deSolve", FOCUS_2006_D)
+
+  FOCUS_2006_D_results_schaefer07_means <- c(
+    parent_0 = 99.65, DT50_parent = 7.04, DT50_m1 = 131.34)
+
+  r.1.e <- c(fit.1.e$bparms.optim[[1]], endpoints(fit.1.e)$distimes[[1]])
+  r.1.d <- c(fit.1.d$bparms.optim[[1]], endpoints(fit.1.d)$distimes[[1]])
+  r.2.e <- c(fit.2.e$bparms.optim[[1]], endpoints(fit.2.e)$distimes[[1]])
+  r.2.d <- c(fit.2.d$bparms.optim[[1]], endpoints(fit.2.d)$distimes[[1]])
+
+  dev.1.e <- 100 * (r.1.e - FOCUS_2006_D_results_schaefer07_means)/r.1.e 
+  checkIdentical(as.numeric(abs(dev.1.e)) < 1, rep(TRUE, 3))
+  dev.1.d <- 100 * (r.1.d - FOCUS_2006_D_results_schaefer07_means)/r.1.d 
+  checkIdentical(as.numeric(abs(dev.1.d)) < 1, rep(TRUE, 3))
+  dev.2.e <- 100 * (r.2.e - FOCUS_2006_D_results_schaefer07_means)/r.2.e 
+  checkIdentical(as.numeric(abs(dev.2.e)) < 1, rep(TRUE, 3))
+  dev.2.d <- 100 * (r.2.d - FOCUS_2006_D_results_schaefer07_means)/r.2.d 
+  checkIdentical(as.numeric(abs(dev.2.d)) < 1, rep(TRUE, 3))
+
+  round(mkinerrmin(fit.2.e), 4)
+  round(mkinerrmin(fit.2.d), 4)
+
+  errmin.FOCUS_2006_D_rounded = data.frame(
+    err.min = c(0.0640, 0.0646, 0.0469),
+    n.optim = c(4, 2, 2),
+    df = c(15, 7, 8), 
+    row.names = c("All data", "parent", "m1"))
+  checkEqualsNumeric(round(mkinerrmin(fit.2.e), 4),
+                     errmin.FOCUS_2006_D_rounded)
+} # }}}
+
+# Test SFO_SFO model with FOCUS_2006_E against values obtained with mkin 0.33 {{{
+test.FOCUS_2006_E_SFO_SFO <- function()
+{
+  SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"),
+         m1 = list(type = "SFO"), use_of_ff = "max")
+
+  fit.2.e <- mkinfit(SFO_SFO.2, FOCUS_2006_E)
+
+  round(mkinerrmin(fit.2.e), 4)
+  errmin.FOCUS_2006_E_rounded = data.frame(
+    err.min = c(0.1544, 0.1659, 0.1095),
+    n.optim = c(4, 2, 2),
+    df = c(13, 7, 6),
+    row.names = c("All data", "parent", "m1"))
+  checkEqualsNumeric(round(mkinerrmin(fit.2.e), 4),
+                     errmin.FOCUS_2006_E_rounded)
+} # }}}
+
+
diff --git a/inst/unitTests/runit.mkinfit.R b/inst/unitTests/runit.mkinfit.R
index fdbc86e0..8eefb995 100644
--- a/inst/unitTests/runit.mkinfit.R
+++ b/inst/unitTests/runit.mkinfit.R
@@ -1,6 +1,4 @@
-# $Id: runit.mkinfit.R 68 2010-09-09 22:40:04Z jranke $
-
-# Copyright (C) 2010-2013 Johannes Ranke
+# Copyright (C) 2010-2014 Johannes Ranke
 # Contact: jranke@uni-bremen.de
 
 # This file is part of the R package mkin
@@ -189,40 +187,6 @@ test.FOCUS_2006_SFORB <- function()
   checkIdentical(dev.B.SFORB.2 < 1, rep(TRUE, length(dev.B.SFORB.2)))
 } # }}}
 
-# Test SFO_SFO model with FOCUS_2006_D against Schaefer 2007 paper, tolerance = 1% # {{{
-test.FOCUS_2006_D_SFO_SFO <- function()
-{
-  SFO_SFO.1 <- mkinmod(parent = list(type = "SFO", to = "m1"),
-         m1 = list(type = "SFO"), use_of_ff = "min")
-  SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"),
-         m1 = list(type = "SFO"), use_of_ff = "max")
-
-  fit.1.e <- mkinfit(SFO_SFO.1, FOCUS_2006_D)
-  fit.1.d <- mkinfit(SFO_SFO.1, solution_type = "deSolve", FOCUS_2006_D)
-  fit.2.e <- mkinfit(SFO_SFO.2, FOCUS_2006_D)
-  SFO <- mkinmod(parent = list(type = "SFO"))
-  f.SFO <- mkinfit(SFO, FOCUS_2006_D)
-  fit.2.d <- mkinfit(SFO_SFO.2, solution_type = "deSolve", FOCUS_2006_D)
-  fit.2.e <- mkinfit(SFO_SFO.2, FOCUS_2006_D)
-
-  FOCUS_2006_D_results_schaefer07_means <- c(
-    parent_0 = 99.65, DT50_parent = 7.04, DT50_m1 = 131.34)
-
-  r.1.e <- c(fit.1.e$bparms.optim[[1]], endpoints(fit.1.e)$distimes[[1]])
-  r.1.d <- c(fit.1.d$bparms.optim[[1]], endpoints(fit.1.d)$distimes[[1]])
-  r.2.e <- c(fit.2.e$bparms.optim[[1]], endpoints(fit.2.e)$distimes[[1]])
-  r.2.d <- c(fit.2.d$bparms.optim[[1]], endpoints(fit.2.d)$distimes[[1]])
-
-  dev.1.e <- 100 * (r.1.e - FOCUS_2006_D_results_schaefer07_means)/r.1.e 
-  checkIdentical(as.numeric(abs(dev.1.e)) < 1, rep(TRUE, 3))
-  dev.1.d <- 100 * (r.1.d - FOCUS_2006_D_results_schaefer07_means)/r.1.d 
-  checkIdentical(as.numeric(abs(dev.1.d)) < 1, rep(TRUE, 3))
-  dev.2.e <- 100 * (r.2.e - FOCUS_2006_D_results_schaefer07_means)/r.2.e 
-  checkIdentical(as.numeric(abs(dev.2.e)) < 1, rep(TRUE, 3))
-  dev.2.d <- 100 * (r.2.d - FOCUS_2006_D_results_schaefer07_means)/r.2.d 
-  checkIdentical(as.numeric(abs(dev.2.d)) < 1, rep(TRUE, 3))
-} # }}}
-
 # Test eigenvalue based fit to Schaefer 2007 data against solution from conference paper {{{
 test.mkinfit.schaefer07_complex_example <- function()
 {
diff --git a/man/mkinerrmin.Rd b/man/mkinerrmin.Rd
index c43d87a1..78ab414e 100644
--- a/man/mkinerrmin.Rd
+++ b/man/mkinerrmin.Rd
@@ -34,6 +34,16 @@ mkinerrmin(fit, alpha = 0.05)
 \details{
     This function is used internally by \code{\link{summary.mkinfit}}.
 }
+\examples{
+SFO_SFO = mkinmod(parent = list(type = "SFO", to = "m1"),
+                  m1 = list(type = "SFO"),
+                  use_of_ff = "max")
+
+fit_FOCUS_D = mkinfit(SFO_SFO, FOCUS_2006_D, quiet = TRUE)
+round(mkinerrmin(fit_FOCUS_D), 4)
+fit_FOCUS_E = mkinfit(SFO_SFO, FOCUS_2006_E, quiet = TRUE)
+round(mkinerrmin(fit_FOCUS_E), 4)
+}
 \references{ 
   FOCUS (2006) \dQuote{Guidance Document on Estimating Persistence and
   Degradation Kinetics from Environmental Fate Studies on Pesticides in EU
diff --git a/tests/doRUnit.R b/tests/doRUnit.R
index f0f82812..9faee940 100644
--- a/tests/doRUnit.R
+++ b/tests/doRUnit.R
@@ -1,4 +1,3 @@
-# $Id: doRUnit.R 96 2011-04-29 11:10:40Z jranke $
 # Adapted from a version around 2.9 of the rcdk package by Rajarshi Guha
 if(require("RUnit", quietly=TRUE)) {
  
diff --git a/vignettes/FOCUS_L.html b/vignettes/FOCUS_L.html
index ab7ccaee..2dd186de 100644
--- a/vignettes/FOCUS_L.html
+++ b/vignettes/FOCUS_L.html
@@ -193,7 +193,13 @@ hr {
 report, p. 284:

library("mkin")
-FOCUS_2006_L1 = data.frame(
+
+ +
## Loading required package: minpack.lm
+## Loading required package: rootSolve
+
+ +
FOCUS_2006_L1 = data.frame(
   t = rep(c(0, 1, 2, 3, 5, 7, 14, 21, 30), each = 2),
   parent = c(88.3, 91.4, 85.6, 84.5, 78.9, 77.6, 
              72.0, 71.9, 50.3, 59.4, 47.0, 45.1,
@@ -215,17 +221,17 @@ given in the FOCUS report. 

summary(m.L1.SFO)
-
## mkin version:    0.9.32 
+
## mkin version:    0.9.33 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 24 10:32:09 2014 
-## Date of summary: Thu Jul 24 10:32:09 2014 
+## Date of fit:     Mon Aug 25 10:34:14 2014 
+## Date of summary: Mon Aug 25 10:34:14 2014 
 ## 
 ## Equations:
-## [1] d_parent = - k_parent_sink * parent
+## 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 Marq using 14 model solutions performed in 0.083 s
 ## 
 ## Weighting: none
 ## 
@@ -318,17 +324,17 @@ is checked.

summary(m.L1.FOMC, data = FALSE)
-
## mkin version:    0.9.32 
+
## mkin version:    0.9.33 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 24 10:32:10 2014 
-## Date of summary: Thu Jul 24 10:32:11 2014 
+## Date of fit:     Mon Aug 25 10:34:17 2014 
+## Date of summary: Mon Aug 25 10:34:17 2014 
 ## 
 ## Equations:
-## [1] d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
+## 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.321 s
+## Fitted with method Marq using 53 model solutions performed in 0.3 s
 ## 
 ## Weighting: none
 ## 
@@ -412,17 +418,17 @@ FOCUS_2006_L2_mkin <- mkin_wide_to_long(FOCUS_2006_L2)
 summary(m.L2.SFO)
 
-
## mkin version:    0.9.32 
+
## mkin version:    0.9.33 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 24 10:32:11 2014 
-## Date of summary: Thu Jul 24 10:32:11 2014 
+## Date of fit:     Mon Aug 25 10:34:17 2014 
+## Date of summary: Mon Aug 25 10:34:17 2014 
 ## 
 ## Equations:
-## [1] d_parent = - k_parent_sink * parent
+## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 29 model solutions performed in 0.196 s
+## Fitted with method Marq using 29 model solutions performed in 0.184 s
 ## 
 ## Weighting: none
 ## 
@@ -522,17 +528,17 @@ mkinresplot(m.L2.FOMC)
 
summary(m.L2.FOMC, data = FALSE)
 
-
## mkin version:    0.9.32 
+
## mkin version:    0.9.33 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 24 10:32:11 2014 
-## Date of summary: Thu Jul 24 10:32:11 2014 
+## Date of fit:     Mon Aug 25 10:34:17 2014 
+## Date of summary: Mon Aug 25 10:34:17 2014 
 ## 
 ## Equations:
-## [1] d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
+## 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.223 s
+## Fitted with method Marq using 35 model solutions performed in 0.2 s
 ## 
 ## Weighting: none
 ## 
@@ -608,17 +614,19 @@ plot(m.L2.DFOP)
 
summary(m.L2.DFOP, data = FALSE)
 
-
## mkin version:    0.9.32 
+
## mkin version:    0.9.33 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 24 10:32:12 2014 
-## Date of summary: Thu Jul 24 10:32:12 2014 
+## Date of fit:     Mon Aug 25 10:34:18 2014 
+## Date of summary: Mon Aug 25 10:34:18 2014 
 ## 
 ## Equations:
-## [1] d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 * time)) / (g * exp(-k1 * time) + (1 - g) * exp(-k2 * time))) * parent
+## d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 * 
+##            time)) / (g * exp(-k1 * time) + (1 - g) * exp(-k2 * 
+##            time))) * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 43 model solutions performed in 0.271 s
+## Fitted with method Marq using 43 model solutions performed in 0.26 s
 ## 
 ## Weighting: none
 ## 
@@ -695,17 +703,17 @@ plot(m.L3.SFO)
 
summary(m.L3.SFO)
 
-
## mkin version:    0.9.32 
+
## mkin version:    0.9.33 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 24 10:32:14 2014 
-## Date of summary: Thu Jul 24 10:32:14 2014 
+## Date of fit:     Mon Aug 25 10:34:18 2014 
+## Date of summary: Mon Aug 25 10:34:18 2014 
 ## 
 ## Equations:
-## [1] d_parent = - k_parent_sink * parent
+## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 44 model solutions performed in 0.251 s
+## Fitted with method Marq using 44 model solutions performed in 0.252 s
 ## 
 ## Weighting: none
 ## 
@@ -781,17 +789,17 @@ plot(m.L3.FOMC)
 
summary(m.L3.FOMC, data = FALSE)
 
-
## mkin version:    0.9.32 
+
## mkin version:    0.9.33 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 24 10:32:14 2014 
-## Date of summary: Thu Jul 24 10:32:14 2014 
+## Date of fit:     Mon Aug 25 10:34:19 2014 
+## Date of summary: Mon Aug 25 10:34:19 2014 
 ## 
 ## Equations:
-## [1] d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
+## 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.154 s
+## Fitted with method Marq using 26 model solutions performed in 0.148 s
 ## 
 ## Weighting: none
 ## 
@@ -854,17 +862,19 @@ plot(m.L3.DFOP)
 
summary(m.L3.DFOP, data = FALSE)
 
-
## mkin version:    0.9.32 
+
## mkin version:    0.9.33 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 24 10:32:14 2014 
-## Date of summary: Thu Jul 24 10:32:14 2014 
+## Date of fit:     Mon Aug 25 10:34:19 2014 
+## Date of summary: Mon Aug 25 10:34:19 2014 
 ## 
 ## Equations:
-## [1] d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 * time)) / (g * exp(-k1 * time) + (1 - g) * exp(-k2 * time))) * parent
+## d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 * 
+##            time)) / (g * exp(-k1 * time) + (1 - g) * exp(-k2 * 
+##            time))) * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 37 model solutions performed in 0.228 s
+## Fitted with method Marq using 37 model solutions performed in 0.236 s
 ## 
 ## Weighting: none
 ## 
@@ -950,17 +960,17 @@ plot(m.L4.SFO)
 
summary(m.L4.SFO, data = FALSE)
 
-
## mkin version:    0.9.32 
+
## mkin version:    0.9.33 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 24 10:32:15 2014 
-## Date of summary: Thu Jul 24 10:32:15 2014 
+## Date of fit:     Mon Aug 25 10:34:19 2014 
+## Date of summary: Mon Aug 25 10:34:19 2014 
 ## 
 ## Equations:
-## [1] d_parent = - k_parent_sink * parent
+## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 20 model solutions performed in 0.141 s
+## Fitted with method Marq using 20 model solutions performed in 0.123 s
 ## 
 ## Weighting: none
 ## 
@@ -1025,17 +1035,17 @@ plot(m.L4.FOMC)
 
summary(m.L4.FOMC, data = FALSE)
 
-
## mkin version:    0.9.32 
+
## mkin version:    0.9.33 
 ## R version:       3.1.1 
-## Date of fit:     Thu Jul 24 10:32:15 2014 
-## Date of summary: Thu Jul 24 10:32:15 2014 
+## Date of fit:     Mon Aug 25 10:34:20 2014 
+## Date of summary: Mon Aug 25 10:34:20 2014 
 ## 
 ## Equations:
-## [1] d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
+## 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.296 s
+## Fitted with method Marq using 48 model solutions performed in 0.281 s
 ## 
 ## Weighting: none
 ## 
diff --git a/vignettes/FOCUS_Z.pdf b/vignettes/FOCUS_Z.pdf
index 210ce099..ca6d2506 100644
Binary files a/vignettes/FOCUS_Z.pdf and b/vignettes/FOCUS_Z.pdf differ
-- 
cgit v1.2.3


From 2dc4f2f87048c28252992fe70055e0d7652a61ef Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Fri, 29 Aug 2014 12:33:26 +0200
Subject: Avoid an unnecessary warning

---
 NEWS.md          | 8 ++++----
 R/plot.mkinfit.R | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

(limited to 'R')

diff --git a/NEWS.md b/NEWS.md
index 1ed94c97..0d690b37 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -12,10 +12,10 @@
 
 - `mkinfit()`: The initial value (state.ini) for the parent compound was not set when the parent was not the (only) variable with the highest value in the observed data.
 
-- `mkinerrmin()`: When checking for degrees of freedom for metabolites, check
-  if their time zero value is fixed instead of checking if the observed value
-  is zero. This ensures correct calculation of degrees of freedom also in cases
-  where the metabolite residue at time zero is greater zero.
+- `mkinerrmin()`: When checking for degrees of freedom for metabolites, check if their time zero value is fixed instead of checking if the observed value is zero. This ensures correct calculation of degrees of freedom also in cases where the metabolite residue at time zero is greater zero.
+
+- `plot.mkinfit()`: Avoid a warning message about only using the first component of ylim that occurred when ylim was specified explicitly
+
 
 ## MINOR CHANGES
 
diff --git a/R/plot.mkinfit.R b/R/plot.mkinfit.R
index 4132d96c..31746fb8 100644
--- a/R/plot.mkinfit.R
+++ b/R/plot.mkinfit.R
@@ -31,7 +31,7 @@ plot.mkinfit <- function(x, fit = x,
 {
   if (add && show_residuals) stop("If adding to an existing plot we can not show residuals")
 
-  if (ylim == "default") {
+  if (ylim[[1]] == "default") {
     ylim = c(0, max(subset(fit$data, variable %in% obs_vars)$observed, na.rm = TRUE))
   }
 
-- 
cgit v1.2.3


From 45942cc099c258fa07f42691cf2e2ee163f3b6d6 Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Mon, 6 Oct 2014 20:17:17 +0200
Subject: Avoid a warning produced by initial fits in gmkin

---
 NEWS.md     |  3 ++-
 R/mkinfit.R | 16 +++++++++-------
 2 files changed, 11 insertions(+), 8 deletions(-)

(limited to 'R')

diff --git a/NEWS.md b/NEWS.md
index 0d690b37..19745300 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -16,13 +16,14 @@
 
 - `plot.mkinfit()`: Avoid a warning message about only using the first component of ylim that occurred when ylim was specified explicitly
 
-
 ## MINOR CHANGES
 
 - The formatting of differential equations in the summary was improved by wrapping overly long lines
 
 - The FOCUS_Z vignette was rebuilt with the above improvement and using a width of 70 to avoid output outside of the grey area
 
+- `print.summary.mkinfit()`: Avoid a warning that occurred when gmkin showed summaries ofinitial fits without iterations
+
 # CHANGES in mkin VERSION 0.9-32
 
 ## NEW FEATURES
diff --git a/R/mkinfit.R b/R/mkinfit.R
index 0f488ec8..5c1de846 100644
--- a/R/mkinfit.R
+++ b/R/mkinfit.R
@@ -594,13 +594,15 @@ print.summary.mkinfit <- function(x, digits = max(3, getOption("digits") - 3), .
   cat("\nOptimised, transformed parameters:\n")
   print(signif(x$par, digits = digits))
 
-  cat("\nParameter correlation:\n")
-  if (!is.null(x$cov.unscaled)){
-    Corr <- cov2cor(x$cov.unscaled)
-    rownames(Corr) <- colnames(Corr) <- rownames(x$par)
-    print(Corr, digits = digits, ...)
-  } else {
-    cat("Could not estimate covariance matrix; singular system:\n")
+  if (x$niter != 0) {
+    cat("\nParameter correlation:\n")
+    if (!is.null(x$cov.unscaled)){
+      Corr <- cov2cor(x$cov.unscaled)
+      rownames(Corr) <- colnames(Corr) <- rownames(x$par)
+      print(Corr, digits = digits, ...)
+    } else {
+      cat("Could not estimate covariance matrix; singular system:\n")
+    }
   }
 
   cat("\nResidual standard error:",
-- 
cgit v1.2.3


From 4510a609159216041f10a33146534f5a8366ac76 Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Tue, 14 Oct 2014 22:04:54 +0200
Subject: Further formatting improvement for differential equations

---
 DESCRIPTION            |   2 +-
 NEWS.md                |   4 +++
 R/mkinfit.R            |   2 +-
 vignettes/FOCUS_L.html |  96 ++++++++++++++++++++++++++-----------------------
 vignettes/FOCUS_Z.pdf  | Bin 213325 -> 220198 bytes
 5 files changed, 57 insertions(+), 47 deletions(-)

(limited to 'R')

diff --git a/DESCRIPTION b/DESCRIPTION
index 6c4545c4..a984391c 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-11
+Date: 2014-10-14
 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), 
                     email = "jranke@uni-bremen.de"),
              person("Katrin", "Lindenberger", role = "ctb"),
diff --git a/NEWS.md b/NEWS.md
index 55a11818..4478be6b 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,5 +1,9 @@
 # CHANGES in mkin VERSION 0.9-34
 
+## MINOR CHANGES
+
+- The formatting of differential equations in the summary was further improved
+
 # CHANGES in mkin VERSION 0.9-33
 
 ## NEW FEATURES
diff --git a/R/mkinfit.R b/R/mkinfit.R
index 5c1de846..a966cea6 100644
--- a/R/mkinfit.R
+++ b/R/mkinfit.R
@@ -567,7 +567,7 @@ print.summary.mkinfit <- function(x, digits = max(3, getOption("digits") - 3), .
   if (!is.null(x$warning)) cat("\n\nWarning:", x$warning, "\n\n")
 
   cat("\nEquations:\n")
-  cat(noquote(strwrap(x[["diffs"]], exdent = 11)), fill = TRUE)
+  writeLines(strwrap(x[["diffs"]], exdent = 11))
   df  <- x$df
   rdf <- df[2]
 
diff --git a/vignettes/FOCUS_L.html b/vignettes/FOCUS_L.html
index c0430358..60c5132a 100644
--- a/vignettes/FOCUS_L.html
+++ b/vignettes/FOCUS_L.html
@@ -214,7 +214,13 @@ hr {
 report, p. 284:

library("mkin")
-FOCUS_2006_L1 = data.frame(
+
+ +
## Loading required package: minpack.lm
+## Loading required package: rootSolve
+
+ +
FOCUS_2006_L1 = data.frame(
   t = rep(c(0, 1, 2, 3, 5, 7, 14, 21, 30), each = 2),
   parent = c(88.3, 91.4, 85.6, 84.5, 78.9, 77.6, 
              72.0, 71.9, 50.3, 59.4, 47.0, 45.1,
@@ -236,17 +242,17 @@ given in the FOCUS report. 

summary(m.L1.SFO)
-
## mkin version:    0.9.33 
+
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Sat Oct 11 11:06:43 2014 
-## Date of summary: Sat Oct 11 11:06:43 2014 
+## Date of fit:     Tue Oct 14 22:03:33 2014 
+## Date of summary: Tue Oct 14 22:03:33 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.083 s
+## Fitted with method Marq using 14 model solutions performed in 0.081 s
 ## 
 ## Weighting: none
 ## 
@@ -338,17 +344,17 @@ is checked.

summary(m.L1.FOMC, data = FALSE)
-
## mkin version:    0.9.33 
+
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Sat Oct 11 11:06:44 2014 
-## Date of summary: Sat Oct 11 11:06:44 2014 
+## Date of fit:     Tue Oct 14 22:03:34 2014 
+## Date of summary: Tue Oct 14 22:03:34 2014 
 ## 
 ## 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.314 s
+## Fitted with method Marq using 53 model solutions performed in 0.289 s
 ## 
 ## Weighting: none
 ## 
@@ -432,17 +438,17 @@ FOCUS_2006_L2_mkin <- mkin_wide_to_long(FOCUS_2006_L2)
 summary(m.L2.SFO)
 
-
## mkin version:    0.9.33 
+
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Sat Oct 11 11:06:44 2014 
-## Date of summary: Sat Oct 11 11:06:44 2014 
+## Date of fit:     Tue Oct 14 22:03:35 2014 
+## Date of summary: Tue Oct 14 22:03:35 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.173 s
+## Fitted with method Marq using 29 model solutions performed in 0.154 s
 ## 
 ## Weighting: none
 ## 
@@ -542,17 +548,17 @@ mkinresplot(m.L2.FOMC)
 
summary(m.L2.FOMC, data = FALSE)
 
-
## mkin version:    0.9.33 
+
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Sat Oct 11 11:06:46 2014 
-## Date of summary: Sat Oct 11 11:06:47 2014 
+## Date of fit:     Tue Oct 14 22:03:36 2014 
+## Date of summary: Tue Oct 14 22:03:36 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.206 s
+## Fitted with method Marq using 35 model solutions performed in 0.192 s
 ## 
 ## Weighting: none
 ## 
@@ -628,19 +634,19 @@ plot(m.L2.DFOP)
 
summary(m.L2.DFOP, data = FALSE)
 
-
## mkin version:    0.9.33 
+
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Sat Oct 11 11:06:47 2014 
-## Date of summary: Sat Oct 11 11:06:47 2014 
+## Date of fit:     Tue Oct 14 22:03:36 2014 
+## Date of summary: Tue Oct 14 22:03:36 2014 
 ## 
 ## Equations:
-## d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 * 
-##            time)) / (g * exp(-k1 * time) + (1 - g) * exp(-k2 * 
+## d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 *
+##            time)) / (g * exp(-k1 * time) + (1 - g) * exp(-k2 *
 ##            time))) * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 43 model solutions performed in 0.265 s
+## Fitted with method Marq using 43 model solutions performed in 0.24 s
 ## 
 ## Weighting: none
 ## 
@@ -717,17 +723,17 @@ plot(m.L3.SFO)
 
summary(m.L3.SFO)
 
-
## mkin version:    0.9.33 
+
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Sat Oct 11 11:06:48 2014 
-## Date of summary: Sat Oct 11 11:06:48 2014 
+## Date of fit:     Tue Oct 14 22:03:37 2014 
+## Date of summary: Tue Oct 14 22:03:37 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.261 s
+## Fitted with method Marq using 44 model solutions performed in 0.237 s
 ## 
 ## Weighting: none
 ## 
@@ -803,17 +809,17 @@ plot(m.L3.FOMC)
 
summary(m.L3.FOMC, data = FALSE)
 
-
## mkin version:    0.9.33 
+
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Sat Oct 11 11:06:48 2014 
-## Date of summary: Sat Oct 11 11:06:48 2014 
+## Date of fit:     Tue Oct 14 22:03:37 2014 
+## Date of summary: Tue Oct 14 22:03:37 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.159 s
+## Fitted with method Marq using 26 model solutions performed in 0.139 s
 ## 
 ## Weighting: none
 ## 
@@ -876,19 +882,19 @@ plot(m.L3.DFOP)
 
summary(m.L3.DFOP, data = FALSE)
 
-
## mkin version:    0.9.33 
+
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Sat Oct 11 11:06:50 2014 
-## Date of summary: Sat Oct 11 11:06:50 2014 
+## Date of fit:     Tue Oct 14 22:03:37 2014 
+## Date of summary: Tue Oct 14 22:03:37 2014 
 ## 
 ## Equations:
-## d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 * 
-##            time)) / (g * exp(-k1 * time) + (1 - g) * exp(-k2 * 
+## d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 *
+##            time)) / (g * exp(-k1 * time) + (1 - g) * exp(-k2 *
 ##            time))) * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Marq using 37 model solutions performed in 0.225 s
+## Fitted with method Marq using 37 model solutions performed in 0.207 s
 ## 
 ## Weighting: none
 ## 
@@ -974,17 +980,17 @@ plot(m.L4.SFO)
 
summary(m.L4.SFO, data = FALSE)
 
-
## mkin version:    0.9.33 
+
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Sat Oct 11 11:06:51 2014 
-## Date of summary: Sat Oct 11 11:06:51 2014 
+## Date of fit:     Tue Oct 14 22:03:38 2014 
+## Date of summary: Tue Oct 14 22:03:38 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.119 s
+## Fitted with method Marq using 20 model solutions performed in 0.106 s
 ## 
 ## Weighting: none
 ## 
@@ -1049,17 +1055,17 @@ plot(m.L4.FOMC)
 
summary(m.L4.FOMC, data = FALSE)
 
-
## mkin version:    0.9.33 
+
## mkin version:    0.9.34 
 ## R version:       3.1.1 
-## Date of fit:     Sat Oct 11 11:06:51 2014 
-## Date of summary: Sat Oct 11 11:06:51 2014 
+## Date of fit:     Tue Oct 14 22:03:38 2014 
+## Date of summary: Tue Oct 14 22:03:38 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.283 s
+## Fitted with method Marq using 48 model solutions performed in 0.26 s
 ## 
 ## Weighting: none
 ## 
diff --git a/vignettes/FOCUS_Z.pdf b/vignettes/FOCUS_Z.pdf
index 426aa0df..b5898b7c 100644
Binary files a/vignettes/FOCUS_Z.pdf and b/vignettes/FOCUS_Z.pdf differ
-- 
cgit v1.2.3


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(-)

(limited to 'R')

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.3


From c0803a4e6660a6ad3a3a219476b6269bab528bfe Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Wed, 22 Oct 2014 09:18:41 +0200
Subject: Always include 0 on y axis when plotting during the fit

---
 DESCRIPTION | 2 +-
 NEWS.md     | 2 ++
 R/mkinfit.R | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)

(limited to 'R')

diff --git a/DESCRIPTION b/DESCRIPTION
index d36c35cc..da7037a6 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-15
+Date: 2014-10-22
 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), 
                     email = "jranke@uni-bremen.de"),
              person("Katrin", "Lindenberger", role = "ctb"),
diff --git a/NEWS.md b/NEWS.md
index 60974b14..837293bb 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -8,6 +8,8 @@
 
 - The formatting of differential equations in the summary was further improved
 
+- Always include 0 on y axis when plotting during the fit
+
 # CHANGES in mkin VERSION 0.9-33
 
 ## NEW FEATURES
diff --git a/R/mkinfit.R b/R/mkinfit.R
index 6494ea1e..e7cab1ee 100644
--- a/R/mkinfit.R
+++ b/R/mkinfit.R
@@ -281,7 +281,7 @@ mkinfit <- function(mkinmod, observed,
                                 atol = atol, rtol = rtol, ...)
 
         plot(0, type="n", 
-          xlim = range(observed$time), ylim = range(observed$value, na.rm=TRUE),
+          xlim = range(observed$time), ylim = c(0, max(observed$value, na.rm=TRUE)),
           xlab = "Time", ylab = "Observed")
         col_obs <- pch_obs <- 1:length(obs_vars)
         lty_obs <- rep(1, length(obs_vars))
-- 
cgit v1.2.3


From 3516b626be1aeb639d0735e79449424d2e987d7a Mon Sep 17 00:00:00 2001
From: Johannes Ranke 
Date: Wed, 12 Nov 2014 12:46:15 +0100
Subject: Fix a typo in the list of possible arguments

Thanks to Michael Brauer for the hint
---
 R/mkinfit.R | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'R')

diff --git a/R/mkinfit.R b/R/mkinfit.R
index e7cab1ee..59598631 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("Port", "Marq", "SANN", "Nelder-Mead", "BFSG", "CG", "L-BFGS-B"),
+  method.modFit = c("Port", "Marq", "SANN", "Nelder-Mead", "BFGS", "CG", "L-BFGS-B"),
   maxit.modFit = "auto",
   control.modFit = list(),
   transform_rates = TRUE,
-- 
cgit v1.2.3