Learn R Programming

mkin (version 0.9-31)

mkinfit: Fit a kinetic model to data with one or more state variables.

Description

This function uses the Flexible Modelling Environment package 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 nls.lm, using the specified initial or fixed parameters and starting values. In each step of the optimsation, the kinetic model is solved using the function mkinpredict. The variance of the residuals for each observed variable can optionally be iteratively reweighted until convergence using the argument reweight.method = "obs".

Usage

mkinfit(mkinmod, observed, 
  parms.ini = "auto",
  state.ini = c(100, rep(0, length(mkinmod$diffs) - 1)), 
  fixed_parms = NULL, fixed_initials = names(mkinmod$diffs)[-1], 
  solution_type = "auto",
  method.ode = "lsoda",
  method.modFit = "Marq",
  control.modFit = list(),
  transform_rates = TRUE,
  transform_fractions = TRUE,
  plot = FALSE, quiet = FALSE, err = NULL, weight = "none", 
  scaleVar = FALSE, 
  atol = 1e-8, rtol = 1e-10, n.outtimes = 100, 
  reweight.method = NULL,
  reweight.tol = 1e-8, reweight.max.iter = 10,
  trace_parms = FALSE, ...)

Arguments

mkinmod
A list of class mkinmod, containing the kinetic model to be fitted to the data.
observed
The observed data. It has to be in the long format as described in modFit, i.e. the first column called "name" must contain the name of the observed variable for each data point. The second column mu
parms.ini
A named vector of initial values for the parameters, including parameters to be optimised and potentially also fixed parameters as indicated by fixed_parms. If set to "auto", initial values for rate constants are set to default v
state.ini
A named vector of initial values for the state variables of the model. In case the observed variables are represented by more than one model variable, the names will differ from the names of the observed variables (see map compone
fixed_parms
The names of parameters that should not be optimised but rather kept at the values specified in parms.ini.
fixed_initials
The names of model variables for which the initial state at time 0 should be excluded from the optimisation. Defaults to all state variables except for the first one.
solution_type
If set to "eigen", the solution of the system of differential equations is based on the spectral decomposition of the coefficient matrix in cases that this is possible. If set to "deSolve", a numerical ode solver from package
method.ode
The solution method passed via mkinpredict to ode in case the solution type is "deSolve". The default "lsoda" is performant, but sometimes fails to c
method.modFit
The optimisation method passed to modFit. The default "Marq" is the Levenberg Marquardt algorithm nls.lm from the package minpack.lm and
control.modFit
Additional arguments passed to the optimisation method used by modFit.
transform_rates
Boolean specifying if kinetic rate constants should be transformed in the 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 th
transform_fractions
Boolean specifying if formation fractions constants should be transformed in the model specification used in the fitting for better compliance with the assumption of normal distribution of the estimator. The default (TRUE) is to do transforma
plot
Should the observed values and the numerical solutions be plotted at each stage of the optimisation?
quiet
Suppress printing out the current model cost after each improvement?
err
either NULL, or the name of the column with the error estimates, used to weigh the residuals (see details of modCost); if NULL, then the residuals are not weighed.
weight
only if err=NULL: how to weight the residuals, one of "none", "std", "mean", see details of modCost.
scaleVar
Will be passed to modCost. Default is not to scale Variables according to the number of observations.
atol
Absolute error tolerance, passed to ode. Default is 1e-8, lower than in lsoda.
rtol
Absolute error tolerance, passed to ode. Default is 1e-10, much lower than in lsoda.
n.outtimes
The length of the dataseries that is produced by the model prediction function mkinpredict. This impacts the accuracy of the numerical solver if that is used (see solution_type argu
reweight.method
The method used for iteratively reweighting residuals, also known as iteratively reweighted least squares (IRLS). Default is NULL, the other method implemented is called "obs", meaning that each observed variable is assumed to have its own var
reweight.tol
Tolerance for convergence criterion for the variance components in IRLS fits.
reweight.max.iter
Maximum iterations in IRLS fits.
trace_parms
Should a trace of the parameter values be listed?
...
Further arguments that will be passed to modFit.

Value

  • A list with "mkinfit" and "modFit" in the class attribute. A summary can be obtained by summary.mkinfit.

Examples

Run this code
# One parent compound, one metabolite, both single first order.
SFO_SFO <- mkinmod(
  parent = list(type = "SFO", to = "m1", sink = TRUE),
  m1 = list(type = "SFO"))
# Fit the model to the FOCUS example dataset D using defaults
fit <- mkinfit(SFO_SFO, FOCUS_2006_D)
summary(fit)

# Use stepwise fitting, using optimised parameters from parent only fit, FOMC
FOMC <- mkinmod(parent = list(type = "FOMC"))
FOMC_SFO <- mkinmod(
  parent = list(type = "FOMC", to = "m1", sink = TRUE),
  m1 = list(type = "SFO"))
# Fit the model to the FOCUS example dataset D using defaults
fit.FOMC_SFO <- mkinfit(FOMC_SFO, FOCUS_2006_D)
# Use starting parameters from parent only FOMC fit
fit.FOMC = mkinfit(FOMC, FOCUS_2006_D, plot=TRUE)
fit.FOMC_SFO <- mkinfit(FOMC_SFO, FOCUS_2006_D, 
  parms.ini = fit.FOMC$bparms.ode, plot=TRUE)

# Use stepwise fitting, using optimised parameters from parent only fit, SFORB
SFORB <- mkinmod(parent = list(type = "SFORB"))
SFORB_SFO <- mkinmod(
  parent = list(type = "SFORB", to = "m1", sink = TRUE),
  m1 = list(type = "SFO"))
# Fit the model to the FOCUS example dataset D using defaults
fit.SFORB_SFO <- mkinfit(SFORB_SFO, FOCUS_2006_D)
# Use starting parameters from parent only SFORB fit (not really needed in this case)
fit.SFORB = mkinfit(SFORB, FOCUS_2006_D)
fit.SFORB_SFO <- mkinfit(SFORB_SFO, FOCUS_2006_D, parms.ini = fit.SFORB$bparms.ode)

# Weighted fits, including IRLS
SFO_SFO.ff <- mkinmod(parent = list(type = "SFO", to = "m1"),
                      m1 = list(type = "SFO"), use_of_ff = "max")
f.noweight <- mkinfit(SFO_SFO.ff, FOCUS_2006_D)
summary(f.noweight)
f.irls <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, reweight.method = "obs")
summary(f.irls)
f.w.mean <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, weight = "mean")
summary(f.w.mean)
f.w.mean.irls <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, weight = "mean",
                         reweight.method = "obs")
summary(f.w.mean.irls)

# Manual weighting
dw <- FOCUS_2006_D
errors <- c(parent = 2, m1 = 1)
dw$err.man <- errors[FOCUS_2006_D$name]
f.w.man <- mkinfit(SFO_SFO.ff, dw, err = "err.man")
summary(f.w.man)
f.w.man.irls <- mkinfit(SFO_SFO.ff, dw, err = "err.man",
                       reweight.method = "obs")
summary(f.w.man.irls)

Run the code above in your browser using DataLab