<!-- Generated by pkgdown: do not edit by hand -->
<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Fit a kinetic model to data with one or more state variables — mkinfit • mkin</title>


<!-- jquery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<!-- Bootstrap -->

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script>

<!-- bootstrap-toc -->
<link rel="stylesheet" href="../bootstrap-toc.css">
<script src="../bootstrap-toc.js"></script>

<!-- Font Awesome icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous" />

<!-- clipboard.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script>

<!-- headroom.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script>

<!-- pkgdown -->
<link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script>




<meta property="og:title" content="Fit a kinetic model to data with one or more state variables — mkinfit" />
<meta property="og:description" content="This function maximises the likelihood of the observed data using the Port
algorithm stats::nlminb(), and the specified initial or fixed
parameters and starting values.  In each step of the optimisation, the
kinetic model is solved using the function mkinpredict(), except
if an analytical solution is implemented, in which case the model is solved
using the degradation function in the mkinmod object. The
parameters of the selected error model are fitted simultaneously with the
degradation model parameters, as both of them are arguments of the
likelihood function." />


<meta name="robots" content="noindex">

<!-- mathjax -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script>

<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->



  </head>

  <body data-spy="scroll" data-target="#toc">
    <div class="container template-reference-topic">
      <header>
      <div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">mkin</a>
        <span class="version label label-info" data-toggle="tooltip" data-placement="bottom" title="In-development version">1.0.3.9000</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li>
  <a href="../reference/index.html">Functions and data</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
    Articles
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
    <li>
      <a href="../articles/mkin.html">Introduction to mkin</a>
    </li>
    <li>
      <a href="../articles/FOCUS_D.html">Example evaluation of FOCUS Example Dataset D</a>
    </li>
    <li>
      <a href="../articles/FOCUS_L.html">Example evaluation of FOCUS Laboratory Data L1 to L3</a>
    </li>
    <li>
      <a href="../articles/web_only/FOCUS_Z.html">Example evaluation of FOCUS Example Dataset Z</a>
    </li>
    <li>
      <a href="../articles/web_only/compiled_models.html">Performance benefit by using compiled model definitions in mkin</a>
    </li>
    <li>
      <a href="../articles/twa.html">Calculation of time weighted average concentrations with mkin</a>
    </li>
    <li>
      <a href="../articles/web_only/NAFTA_examples.html">Example evaluation of NAFTA SOP Attachment examples</a>
    </li>
    <li>
      <a href="../articles/web_only/benchmarks.html">Some benchmark timings</a>
    </li>
  </ul>
</li>
<li>
  <a href="../news/index.html">News</a>
</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li>
  <a href="https://github.com/jranke/mkin/">
    <span class="fab fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
      
    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

      

      </header>

<div class="row">
  <div class="col-md-9 contents">
    <div class="page-header">
    <h1>Fit a kinetic model to data with one or more state variables</h1>
    <small class="dont-index">Source: <a href='https://github.com/jranke/mkin/blob/master/R/mkinfit.R'><code>R/mkinfit.R</code></a></small>
    <div class="hidden name"><code>mkinfit.Rd</code></div>
    </div>

    <div class="ref-description">
    <p>This function maximises the likelihood of the observed data using the Port
algorithm <code><a href='https://rdrr.io/r/stats/nlminb.html'>stats::nlminb()</a></code>, and the specified initial or fixed
parameters and starting values.  In each step of the optimisation, the
kinetic model is solved using the function <code><a href='mkinpredict.html'>mkinpredict()</a></code>, except
if an analytical solution is implemented, in which case the model is solved
using the degradation function in the <a href='mkinmod.html'>mkinmod</a> object. The
parameters of the selected error model are fitted simultaneously with the
degradation model parameters, as both of them are arguments of the
likelihood function.</p>
    </div>

    <pre class="usage"><span class='fu'>mkinfit</span><span class='op'>(</span>
  <span class='va'>mkinmod</span>,
  <span class='va'>observed</span>,
  parms.ini <span class='op'>=</span> <span class='st'>"auto"</span>,
  state.ini <span class='op'>=</span> <span class='st'>"auto"</span>,
  err.ini <span class='op'>=</span> <span class='st'>"auto"</span>,
  fixed_parms <span class='op'>=</span> <span class='cn'>NULL</span>,
  fixed_initials <span class='op'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/names.html'>names</a></span><span class='op'>(</span><span class='va'>mkinmod</span><span class='op'>$</span><span class='va'>diffs</span><span class='op'>)</span><span class='op'>[</span><span class='op'>-</span><span class='fl'>1</span><span class='op'>]</span>,
  from_max_mean <span class='op'>=</span> <span class='cn'>FALSE</span>,
  solution_type <span class='op'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='st'>"auto"</span>, <span class='st'>"analytical"</span>, <span class='st'>"eigen"</span>, <span class='st'>"deSolve"</span><span class='op'>)</span>,
  method.ode <span class='op'>=</span> <span class='st'>"lsoda"</span>,
  use_compiled <span class='op'>=</span> <span class='st'>"auto"</span>,
  control <span class='op'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/list.html'>list</a></span><span class='op'>(</span>eval.max <span class='op'>=</span> <span class='fl'>300</span>, iter.max <span class='op'>=</span> <span class='fl'>200</span><span class='op'>)</span>,
  transform_rates <span class='op'>=</span> <span class='cn'>TRUE</span>,
  transform_fractions <span class='op'>=</span> <span class='cn'>TRUE</span>,
  quiet <span class='op'>=</span> <span class='cn'>FALSE</span>,
  atol <span class='op'>=</span> <span class='fl'>1e-08</span>,
  rtol <span class='op'>=</span> <span class='fl'>1e-10</span>,
  error_model <span class='op'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='st'>"const"</span>, <span class='st'>"obs"</span>, <span class='st'>"tc"</span><span class='op'>)</span>,
  error_model_algorithm <span class='op'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='st'>"auto"</span>, <span class='st'>"d_3"</span>, <span class='st'>"direct"</span>, <span class='st'>"twostep"</span>, <span class='st'>"threestep"</span>,
    <span class='st'>"fourstep"</span>, <span class='st'>"IRLS"</span>, <span class='st'>"OLS"</span><span class='op'>)</span>,
  reweight.tol <span class='op'>=</span> <span class='fl'>1e-08</span>,
  reweight.max.iter <span class='op'>=</span> <span class='fl'>10</span>,
  trace_parms <span class='op'>=</span> <span class='cn'>FALSE</span>,
  test_residuals <span class='op'>=</span> <span class='cn'>FALSE</span>,
  <span class='va'>...</span>
<span class='op'>)</span></pre>

    <h2 class="hasAnchor" id="arguments"><a class="anchor" href="#arguments"></a>Arguments</h2>
    <table class="ref-arguments">
    <colgroup><col class="name" /><col class="desc" /></colgroup>
    <tr>
      <th>mkinmod</th>
      <td><p>A list of class <a href='mkinmod.html'>mkinmod</a>, containing the kinetic
model to be fitted to the data, or one of the shorthand names ("SFO",
"FOMC", "DFOP", "HS", "SFORB", "IORE"). If a shorthand name is given, a
parent only degradation model is generated for the variable with the
highest value in <code>observed</code>.</p></td>
    </tr>
    <tr>
      <th>observed</th>
      <td><p>A dataframe with the observed data.  The first column called
"name" must contain the name of the observed variable for each data point.
The second column must contain the times of observation, named "time".
The third column must be named "value" and contain the observed values.
Zero values in the "value" column will be removed, with a warning, in
order to avoid problems with fitting the two-component error model. This
is not expected to be a problem, because in general, values of zero are
not observed in degradation data, because there is a lower limit of
detection.</p></td>
    </tr>
    <tr>
      <th>parms.ini</th>
      <td><p>A named vector of initial values for the parameters,
including parameters to be optimised and potentially also fixed parameters
as indicated by <code>fixed_parms</code>.  If set to "auto", initial values for
rate constants are set to default values.  Using parameter names that are
not in the model gives an error.</p>
<p>It is possible to only specify a subset of the parameters that the model
needs. You can use the parameter lists "bparms.ode" from a previously
fitted model, which contains the differential equation parameters from
this model.  This works nicely if the models are nested. An example is
given below.</p></td>
    </tr>
    <tr>
      <th>state.ini</th>
      <td><p>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 <code>map</code> component of <a href='mkinmod.html'>mkinmod</a>). 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.</p></td>
    </tr>
    <tr>
      <th>err.ini</th>
      <td><p>A named vector of initial values for the error model
parameters to be optimised.  If set to "auto", initial values are set to
default values.  Otherwise, inital values for all error model parameters
must be given.</p></td>
    </tr>
    <tr>
      <th>fixed_parms</th>
      <td><p>The names of parameters that should not be optimised but
rather kept at the values specified in <code>parms.ini</code>. Alternatively,
a named numeric vector of parameters to be fixed, regardless of the values
in parms.ini.</p></td>
    </tr>
    <tr>
      <th>fixed_initials</th>
      <td><p>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.</p></td>
    </tr>
    <tr>
      <th>from_max_mean</th>
      <td><p>If this is set to TRUE, and the model has only one
observed variable, then data before the time of the maximum observed value
(after averaging for each sampling time) are discarded, and this time is
subtracted from all remaining time values, so the time of the maximum
observed mean value is the new time zero.</p></td>
    </tr>
    <tr>
      <th>solution_type</th>
      <td><p>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 <a href='https://rdrr.io/pkg/deSolve/man/ode.html'>ode solver from package deSolve</a> is used. If
set to "analytical", an analytical solution of the model is used. This is
only implemented for relatively simple degradation models.  The default is
"auto", which uses "analytical" if possible, otherwise "deSolve" if a
compiler is present, and "eigen" if no compiler is present and the model
can be expressed using eigenvalues and eigenvectors.</p></td>
    </tr>
    <tr>
      <th>method.ode</th>
      <td><p>The solution method passed via <code><a href='mkinpredict.html'>mkinpredict()</a></code>
to <code><a href='https://rdrr.io/pkg/deSolve/man/ode.html'>deSolve::ode()</a></code> in case the solution type is "deSolve". The default
"lsoda" is performant, but sometimes fails to converge.</p></td>
    </tr>
    <tr>
      <th>use_compiled</th>
      <td><p>If set to <code>FALSE</code>, no compiled version of the
<a href='mkinmod.html'>mkinmod</a> model is used in the calls to <code><a href='mkinpredict.html'>mkinpredict()</a></code> even if a compiled
version is present.</p></td>
    </tr>
    <tr>
      <th>control</th>
      <td><p>A list of control arguments passed to <code><a href='https://rdrr.io/r/stats/nlminb.html'>stats::nlminb()</a></code>.</p></td>
    </tr>
    <tr>
      <th>transform_rates</th>
      <td><p>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 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.  If FALSE, zero is used as
a lower bound for the rates in the optimisation.</p></td>
    </tr>
    <tr>
      <th>transform_fractions</th>
      <td><p>Boolean specifying if formation fractions
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 transformations. If TRUE,
the g parameter of the DFOP model is also transformed. Transformations
are described in <a href='transform_odeparms.html'>transform_odeparms</a>.</p></td>
    </tr>
    <tr>
      <th>quiet</th>
      <td><p>Suppress printing out the current value of the negative
log-likelihood after each improvement?</p></td>
    </tr>
    <tr>
      <th>atol</th>
      <td><p>Absolute error tolerance, passed to <code><a href='https://rdrr.io/pkg/deSolve/man/ode.html'>deSolve::ode()</a></code>. Default
is 1e-8, which is lower than the default in the <code><a href='https://rdrr.io/pkg/deSolve/man/lsoda.html'>deSolve::lsoda()</a></code>
function which is used per default.</p></td>
    </tr>
    <tr>
      <th>rtol</th>
      <td><p>Absolute error tolerance, passed to <code><a href='https://rdrr.io/pkg/deSolve/man/ode.html'>deSolve::ode()</a></code>. Default
is 1e-10, much lower than in <code><a href='https://rdrr.io/pkg/deSolve/man/lsoda.html'>deSolve::lsoda()</a></code>.</p></td>
    </tr>
    <tr>
      <th>error_model</th>
      <td><p>If the error model is "const", a constant standard
deviation is assumed.</p>
<p>If the error model is "obs", each observed variable is assumed to have its
own variance.</p>
<p>If the error model is "tc" (two-component error model), a two component
error model similar to the one described by Rocke and Lorenzato (1995) is
used for setting up the likelihood function.  Note that this model
deviates from the model by Rocke and Lorenzato, as their model implies
that the errors follow a lognormal distribution for large values, not a
normal distribution as assumed by this method.</p></td>
    </tr>
    <tr>
      <th>error_model_algorithm</th>
      <td><p>If "auto", the selected algorithm depends on
the error model.  If the error model is "const", unweighted nonlinear
least squares fitting ("OLS") is selected. If the error model is "obs", or
"tc", the "d_3" algorithm is selected.</p>
<p>The algorithm "d_3" will directly minimize the negative log-likelihood
and independently also use the three step algorithm described below.
The fit with the higher likelihood is returned.</p>
<p>The algorithm "direct" will directly minimize the negative log-likelihood.</p>
<p>The algorithm "twostep" will minimize the negative log-likelihood after an
initial unweighted least squares optimisation step.</p>
<p>The algorithm "threestep" starts with unweighted least squares, then
optimizes only the error model using the degradation model parameters
found, and then minimizes the negative log-likelihood with free
degradation and error model parameters.</p>
<p>The algorithm "fourstep" starts with unweighted least squares, then
optimizes only the error model using the degradation model parameters
found, then optimizes the degradation model again with fixed error model
parameters, and finally minimizes the negative log-likelihood with free
degradation and error model parameters.</p>
<p>The algorithm "IRLS" (Iteratively Reweighted Least Squares) starts with
unweighted least squares, and then iterates optimization of the error
model parameters and subsequent optimization of the degradation model
using those error model parameters, until the error model parameters
converge.</p></td>
    </tr>
    <tr>
      <th>reweight.tol</th>
      <td><p>Tolerance for the convergence criterion calculated from
the error model parameters in IRLS fits.</p></td>
    </tr>
    <tr>
      <th>reweight.max.iter</th>
      <td><p>Maximum number of iterations in IRLS fits.</p></td>
    </tr>
    <tr>
      <th>trace_parms</th>
      <td><p>Should a trace of the parameter values be listed?</p></td>
    </tr>
    <tr>
      <th>test_residuals</th>
      <td><p>Should the residuals be tested for normal distribution?</p></td>
    </tr>
    <tr>
      <th>...</th>
      <td><p>Further arguments that will be passed on to
<code><a href='https://rdrr.io/pkg/deSolve/man/ode.html'>deSolve::ode()</a></code>.</p></td>
    </tr>
    </table>

    <h2 class="hasAnchor" id="value"><a class="anchor" href="#value"></a>Value</h2>

    <p>A list with "mkinfit" in the class attribute.</p>
    <h2 class="hasAnchor" id="details"><a class="anchor" href="#details"></a>Details</h2>

    <p>Per default, parameters in the kinetic models are internally transformed in
order to better satisfy the assumption of a normal distribution of their
estimators.</p>
    <h2 class="hasAnchor" id="note"><a class="anchor" href="#note"></a>Note</h2>

    <p>When using the "IORE" submodel for metabolites, fitting with
"transform_rates = TRUE" (the default) often leads to failures of the
numerical ODE solver. In this situation it may help to switch off the
internal rate transformation.</p>
    <h2 class="hasAnchor" id="references"><a class="anchor" href="#references"></a>References</h2>

    <p>Rocke DM and Lorenzato S (1995) A two-component model
for measurement error in analytical chemistry. <em>Technometrics</em> 37(2), 176-184.</p>
<p>Ranke J and Meinecke S (2019) Error Models for the Kinetic Evaluation of Chemical
Degradation Data. <em>Environments</em> 6(12) 124
doi: <a href='https://doi.org/10.3390/environments6120124'>10.3390/environments6120124</a>
.</p>
    <h2 class="hasAnchor" id="see-also"><a class="anchor" href="#see-also"></a>See also</h2>

    <div class='dont-index'><p><a href='summary.mkinfit.html'>summary.mkinfit</a>, <a href='plot.mkinfit.html'>plot.mkinfit</a>, <a href='parms.html'>parms</a> and <a href='https://rdrr.io/pkg/lmtest/man/lrtest.html'>lrtest</a>.</p>
<p>Comparisons of models fitted to the same data can be made using
<code><a href='https://rdrr.io/r/stats/AIC.html'>AIC</a></code> by virtue of the method <code><a href='logLik.mkinfit.html'>logLik.mkinfit</a></code>.</p>
<p>Fitting of several models to several datasets in a single call to
<code><a href='mmkin.html'>mmkin</a></code>.</p></div>
    <h2 class="hasAnchor" id="author"><a class="anchor" href="#author"></a>Author</h2>

    <p>Johannes Ranke</p>

    <h2 class="hasAnchor" id="examples"><a class="anchor" href="#examples"></a>Examples</h2>
    <pre class="examples"><div class='input'>
<span class='co'># Use shorthand notation for parent only degradation</span>
<span class='va'>fit</span> <span class='op'>&lt;-</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='st'>"FOMC"</span>, <span class='va'>FOCUS_2006_C</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span><span class='op'>)</span>
<span class='fu'><a href='https://rdrr.io/r/base/summary.html'>summary</a></span><span class='op'>(</span><span class='va'>fit</span><span class='op'>)</span>
</div><div class='output co'>#&gt; mkin version used for fitting:    1.0.3.9000 
#&gt; R version used for fitting:       4.0.3 
#&gt; Date of fit:     Mon Feb 15 17:09:39 2021 
#&gt; Date of summary: Mon Feb 15 17:09:39 2021 
#&gt; 
#&gt; Equations:
#&gt; d_parent/dt = - (alpha/beta) * 1/((time/beta) + 1) * parent
#&gt; 
#&gt; Model predictions using solution type analytical 
#&gt; 
#&gt; Fitted using 222 model solutions performed in 0.045 s
#&gt; 
#&gt; Error model: Constant variance 
#&gt; 
#&gt; Error model algorithm: OLS 
#&gt; 
#&gt; Starting values for parameters to be optimised:
#&gt;          value   type
#&gt; parent_0  85.1  state
#&gt; alpha      1.0 deparm
#&gt; beta      10.0 deparm
#&gt; 
#&gt; Starting values for the transformed parameters actually optimised:
#&gt;               value lower upper
#&gt; parent_0  85.100000  -Inf   Inf
#&gt; log_alpha  0.000000  -Inf   Inf
#&gt; log_beta   2.302585  -Inf   Inf
#&gt; 
#&gt; Fixed parameter values:
#&gt; None
#&gt; 
#&gt; Results:
#&gt; 
#&gt;        AIC      BIC    logLik
#&gt;   44.68652 45.47542 -18.34326
#&gt; 
#&gt; Optimised, transformed parameters with symmetric confidence intervals:
#&gt;           Estimate Std. Error    Lower   Upper
#&gt; parent_0  85.87000     1.8070 81.23000 90.5200
#&gt; log_alpha  0.05192     0.1353 -0.29580  0.3996
#&gt; log_beta   0.65100     0.2287  0.06315  1.2390
#&gt; sigma      1.85700     0.4378  0.73200  2.9830
#&gt; 
#&gt; Parameter correlation:
#&gt;             parent_0  log_alpha   log_beta     sigma
#&gt; parent_0   1.000e+00 -1.565e-01 -3.142e-01 4.772e-08
#&gt; log_alpha -1.565e-01  1.000e+00  9.564e-01 1.005e-07
#&gt; log_beta  -3.142e-01  9.564e-01  1.000e+00 8.541e-08
#&gt; sigma      4.772e-08  1.005e-07  8.541e-08 1.000e+00
#&gt; 
#&gt; Backtransformed parameters:
#&gt; Confidence intervals for internally transformed parameters are asymmetric.
#&gt; t-test (unrealistically) based on the assumption of normal distribution
#&gt; for estimators of untransformed parameters.
#&gt;          Estimate t value    Pr(&gt;t)   Lower  Upper
#&gt; parent_0   85.870  47.530 3.893e-08 81.2300 90.520
#&gt; alpha       1.053   7.393 3.562e-04  0.7439  1.491
#&gt; beta        1.917   4.373 3.601e-03  1.0650  3.451
#&gt; sigma       1.857   4.243 4.074e-03  0.7320  2.983
#&gt; 
#&gt; FOCUS Chi2 error levels in percent:
#&gt;          err.min n.optim df
#&gt; All data   6.657       3  6
#&gt; parent     6.657       3  6
#&gt; 
#&gt; Estimated disappearance times:
#&gt;         DT50  DT90 DT50back
#&gt; parent 1.785 15.15     4.56
#&gt; 
#&gt; Data:
#&gt;  time variable observed predicted residual
#&gt;     0   parent     85.1    85.875  -0.7749
#&gt;     1   parent     57.9    55.191   2.7091
#&gt;     3   parent     29.9    31.845  -1.9452
#&gt;     7   parent     14.6    17.012  -2.4124
#&gt;    14   parent      9.7     9.241   0.4590
#&gt;    28   parent      6.6     4.754   1.8460
#&gt;    63   parent      4.0     2.102   1.8977
#&gt;    91   parent      3.9     1.441   2.4590
#&gt;   119   parent      0.6     1.092  -0.4919</div><div class='input'>
<span class='co'># One parent compound, one metabolite, both single first order.</span>
<span class='co'># We remove zero values from FOCUS dataset D in order to avoid warnings</span>
<span class='va'>FOCUS_D</span> <span class='op'>&lt;-</span> <span class='fu'><a href='https://rdrr.io/r/base/subset.html'>subset</a></span><span class='op'>(</span><span class='va'>FOCUS_2006_D</span>, <span class='va'>value</span> <span class='op'>!=</span> <span class='fl'>0</span><span class='op'>)</span>
<span class='co'># Use mkinsub for convenience in model formulation. Pathway to sink included per default.</span>
<span class='va'>SFO_SFO</span> <span class='op'>&lt;-</span> <span class='fu'><a href='mkinmod.html'>mkinmod</a></span><span class='op'>(</span>
  parent <span class='op'>=</span> <span class='fu'><a href='mkinmod.html'>mkinsub</a></span><span class='op'>(</span><span class='st'>"SFO"</span>, <span class='st'>"m1"</span><span class='op'>)</span>,
  m1 <span class='op'>=</span> <span class='fu'><a href='mkinmod.html'>mkinsub</a></span><span class='op'>(</span><span class='st'>"SFO"</span><span class='op'>)</span><span class='op'>)</span>
</div><div class='output co'>#&gt; <span class='message'>Temporary DLL for differentials generated and loaded</span></div><div class='input'>
<span class='co'># Fit the model quietly to the FOCUS example dataset D using defaults</span>
<span class='va'>fit</span> <span class='op'>&lt;-</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='va'>SFO_SFO</span>, <span class='va'>FOCUS_D</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span><span class='op'>)</span>
<span class='fu'><a href='plot.mkinfit.html'>plot_sep</a></span><span class='op'>(</span><span class='va'>fit</span><span class='op'>)</span>
</div><div class='img'><img src='mkinfit-1.png' alt='' width='700' height='433' /></div><div class='input'><span class='co'># As lower parent values appear to have lower variance, we try an alternative error model</span>
<span class='va'>fit.tc</span> <span class='op'>&lt;-</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='va'>SFO_SFO</span>, <span class='va'>FOCUS_D</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span>, error_model <span class='op'>=</span> <span class='st'>"tc"</span><span class='op'>)</span>
<span class='co'># This avoids the warning, and the likelihood ratio test confirms it is preferable</span>
<span class='fu'><a href='https://rdrr.io/pkg/lmtest/man/lrtest.html'>lrtest</a></span><span class='op'>(</span><span class='va'>fit.tc</span>, <span class='va'>fit</span><span class='op'>)</span>
</div><div class='output co'>#&gt; Likelihood ratio test
#&gt; 
#&gt; Model 1: SFO_SFO with error model tc and fixed parameter(s) m1_0
#&gt; Model 2: SFO_SFO with error model const and fixed parameter(s) m1_0
#&gt;   #Df  LogLik Df  Chisq Pr(&gt;Chisq)    
#&gt; 1   6 -64.983                         
#&gt; 2   5 -97.224 -1 64.483  9.737e-16 ***
#&gt; ---
#&gt; Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1</div><div class='input'><span class='co'># We can also allow for different variances of parent and metabolite as error model</span>
<span class='va'>fit.obs</span> <span class='op'>&lt;-</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='va'>SFO_SFO</span>, <span class='va'>FOCUS_D</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span>, error_model <span class='op'>=</span> <span class='st'>"obs"</span><span class='op'>)</span>
<span class='co'># The two-component error model has significantly higher likelihood</span>
<span class='fu'><a href='https://rdrr.io/pkg/lmtest/man/lrtest.html'>lrtest</a></span><span class='op'>(</span><span class='va'>fit.obs</span>, <span class='va'>fit.tc</span><span class='op'>)</span>
</div><div class='output co'>#&gt; Likelihood ratio test
#&gt; 
#&gt; Model 1: SFO_SFO with error model tc and fixed parameter(s) m1_0
#&gt; Model 2: SFO_SFO with error model obs and fixed parameter(s) m1_0
#&gt;   #Df  LogLik Df  Chisq Pr(&gt;Chisq)    
#&gt; 1   6 -64.983                         
#&gt; 2   6 -96.936  0 63.907  &lt; 2.2e-16 ***
#&gt; ---
#&gt; Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1</div><div class='input'><span class='fu'><a href='parms.html'>parms</a></span><span class='op'>(</span><span class='va'>fit.tc</span><span class='op'>)</span>
</div><div class='output co'>#&gt;       parent_0       k_parent           k_m1 f_parent_to_m1      sigma_low 
#&gt;   1.007343e+02   1.005562e-01   5.166712e-03   5.083933e-01   3.049883e-03 
#&gt;       rsd_high 
#&gt;   7.928118e-02 </div><div class='input'><span class='fu'><a href='endpoints.html'>endpoints</a></span><span class='op'>(</span><span class='va'>fit.tc</span><span class='op'>)</span>
</div><div class='output co'>#&gt; $ff
#&gt;   parent_m1 parent_sink 
#&gt;   0.5083933   0.4916067 
#&gt; 
#&gt; $distimes
#&gt;             DT50      DT90
#&gt; parent   6.89313  22.89848
#&gt; m1     134.15634 445.65772
#&gt; </div><div class='input'>
<span class='co'># We can show a quick (only one replication) benchmark for this case, as we</span>
<span class='co'># have several alternative solution methods for the model. We skip</span>
<span class='co'># uncompiled deSolve, as it is so slow. More benchmarks are found in the</span>
<span class='co'># benchmark vignette</span>
<span class='co'># \dontrun{</span>
<span class='kw'>if</span><span class='op'>(</span><span class='kw'><a href='https://rdrr.io/r/base/library.html'>require</a></span><span class='op'>(</span><span class='va'><a href='http://rbenchmark.googlecode.com'>rbenchmark</a></span><span class='op'>)</span><span class='op'>)</span> <span class='op'>{</span>
  <span class='fu'><a href='https://rdrr.io/pkg/rbenchmark/man/benchmark.html'>benchmark</a></span><span class='op'>(</span>replications <span class='op'>=</span> <span class='fl'>1</span>, order <span class='op'>=</span> <span class='st'>"relative"</span>, columns <span class='op'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span><span class='op'>(</span><span class='st'>"test"</span>, <span class='st'>"relative"</span>, <span class='st'>"elapsed"</span><span class='op'>)</span>,
    deSolve_compiled <span class='op'>=</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='va'>SFO_SFO</span>, <span class='va'>FOCUS_D</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span>, error_model <span class='op'>=</span> <span class='st'>"tc"</span>,
      solution_type <span class='op'>=</span> <span class='st'>"deSolve"</span>, use_compiled <span class='op'>=</span> <span class='cn'>TRUE</span><span class='op'>)</span>,
    eigen <span class='op'>=</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='va'>SFO_SFO</span>, <span class='va'>FOCUS_D</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span>, error_model <span class='op'>=</span> <span class='st'>"tc"</span>,
      solution_type <span class='op'>=</span> <span class='st'>"eigen"</span><span class='op'>)</span>,
    analytical <span class='op'>=</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='va'>SFO_SFO</span>, <span class='va'>FOCUS_D</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span>, error_model <span class='op'>=</span> <span class='st'>"tc"</span>,
      solution_type <span class='op'>=</span> <span class='st'>"analytical"</span><span class='op'>)</span><span class='op'>)</span>
<span class='op'>}</span>
</div><div class='output co'>#&gt;               test relative elapsed
#&gt; 3       analytical    1.000   0.563
#&gt; 1 deSolve_compiled    1.702   0.958
#&gt; 2            eigen    2.597   1.462</div><div class='input'><span class='co'># }</span>

<span class='co'># Use stepwise fitting, using optimised parameters from parent only fit, FOMC-SFO</span>
<span class='co'># \dontrun{</span>
<span class='va'>FOMC_SFO</span> <span class='op'>&lt;-</span> <span class='fu'><a href='mkinmod.html'>mkinmod</a></span><span class='op'>(</span>
  parent <span class='op'>=</span> <span class='fu'><a href='mkinmod.html'>mkinsub</a></span><span class='op'>(</span><span class='st'>"FOMC"</span>, <span class='st'>"m1"</span><span class='op'>)</span>,
  m1 <span class='op'>=</span> <span class='fu'><a href='mkinmod.html'>mkinsub</a></span><span class='op'>(</span><span class='st'>"SFO"</span><span class='op'>)</span><span class='op'>)</span>
</div><div class='output co'>#&gt; <span class='message'>Temporary DLL for differentials generated and loaded</span></div><div class='input'><span class='va'>fit.FOMC_SFO</span> <span class='op'>&lt;-</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='va'>FOMC_SFO</span>, <span class='va'>FOCUS_D</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span><span class='op'>)</span>
<span class='co'># Again, we get a warning and try a more sophisticated error model</span>
<span class='va'>fit.FOMC_SFO.tc</span> <span class='op'>&lt;-</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='va'>FOMC_SFO</span>, <span class='va'>FOCUS_D</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span>, error_model <span class='op'>=</span> <span class='st'>"tc"</span><span class='op'>)</span>
<span class='co'># This model has a higher likelihood, but not significantly so</span>
<span class='fu'><a href='https://rdrr.io/pkg/lmtest/man/lrtest.html'>lrtest</a></span><span class='op'>(</span><span class='va'>fit.tc</span>, <span class='va'>fit.FOMC_SFO.tc</span><span class='op'>)</span>
</div><div class='output co'>#&gt; Likelihood ratio test
#&gt; 
#&gt; Model 1: FOMC_SFO with error model tc and fixed parameter(s) m1_0
#&gt; Model 2: SFO_SFO with error model tc and fixed parameter(s) m1_0
#&gt;   #Df  LogLik Df  Chisq Pr(&gt;Chisq)
#&gt; 1   7 -64.829                     
#&gt; 2   6 -64.983 -1 0.3075     0.5792</div><div class='input'><span class='co'># Also, the missing standard error for log_beta and the t-tests for alpha</span>
<span class='co'># and beta indicate overparameterisation</span>
<span class='fu'><a href='https://rdrr.io/r/base/summary.html'>summary</a></span><span class='op'>(</span><span class='va'>fit.FOMC_SFO.tc</span>, data <span class='op'>=</span> <span class='cn'>FALSE</span><span class='op'>)</span>
</div><div class='output co'>#&gt; <span class='warning'>Warning: NaNs produced</span></div><div class='output co'>#&gt; <span class='warning'>Warning: NaNs produced</span></div><div class='output co'>#&gt; <span class='warning'>Warning: diag(.) had 0 or NA entries; non-finite result is doubtful</span></div><div class='output co'>#&gt; mkin version used for fitting:    1.0.3.9000 
#&gt; R version used for fitting:       4.0.3 
#&gt; Date of fit:     Mon Feb 15 17:09:50 2021 
#&gt; Date of summary: Mon Feb 15 17:09:50 2021 
#&gt; 
#&gt; Equations:
#&gt; d_parent/dt = - (alpha/beta) * 1/((time/beta) + 1) * parent
#&gt; d_m1/dt = + f_parent_to_m1 * (alpha/beta) * 1/((time/beta) + 1) *
#&gt;            parent - k_m1 * m1
#&gt; 
#&gt; Model predictions using solution type deSolve 
#&gt; 
#&gt; Fitted using 3729 model solutions performed in 2.815 s
#&gt; 
#&gt; Error model: Two-component variance function 
#&gt; 
#&gt; Error model algorithm: d_3 
#&gt; Direct fitting and three-step fitting yield approximately the same likelihood 
#&gt; 
#&gt; Starting values for parameters to be optimised:
#&gt;                 value   type
#&gt; parent_0       100.75  state
#&gt; alpha            1.00 deparm
#&gt; beta            10.00 deparm
#&gt; k_m1             0.10 deparm
#&gt; f_parent_to_m1   0.50 deparm
#&gt; sigma_low        0.10  error
#&gt; rsd_high         0.10  error
#&gt; 
#&gt; Starting values for the transformed parameters actually optimised:
#&gt;                      value lower upper
#&gt; parent_0        100.750000  -Inf   Inf
#&gt; log_k_m1         -2.302585  -Inf   Inf
#&gt; f_parent_qlogis   0.000000  -Inf   Inf
#&gt; log_alpha         0.000000  -Inf   Inf
#&gt; log_beta          2.302585  -Inf   Inf
#&gt; sigma_low         0.100000     0   Inf
#&gt; rsd_high          0.100000     0   Inf
#&gt; 
#&gt; Fixed parameter values:
#&gt;      value  type
#&gt; m1_0     0 state
#&gt; 
#&gt; Results:
#&gt; 
#&gt;       AIC      BIC    logLik
#&gt;   143.658 155.1211 -64.82902
#&gt; 
#&gt; Optimised, transformed parameters with symmetric confidence intervals:
#&gt;                   Estimate Std. Error     Lower      Upper
#&gt; parent_0        101.600000  2.6400000 96.240000 107.000000
#&gt; log_k_m1         -5.284000  0.0929100 -5.474000  -5.095000
#&gt; f_parent_qlogis   0.001426  0.0767000 -0.155000   0.157800
#&gt; log_alpha         5.522000  0.0077320  5.506000   5.538000
#&gt; log_beta          7.806000        NaN       NaN        NaN
#&gt; sigma_low         0.002488  0.0002431  0.001992   0.002984
#&gt; rsd_high          0.079210  0.0093280  0.060180   0.098230
#&gt; 
#&gt; Parameter correlation:
#&gt;                  parent_0  log_k_m1 f_parent_qlogis log_alpha log_beta
#&gt; parent_0         1.000000 -0.095226        -0.76678   0.70544      NaN
#&gt; log_k_m1        -0.095226  1.000000         0.51432  -0.14387      NaN
#&gt; f_parent_qlogis -0.766780  0.514321         1.00000  -0.61396      NaN
#&gt; log_alpha        0.705444 -0.143872        -0.61396   1.00000      NaN
#&gt; log_beta              NaN       NaN             NaN       NaN        1
#&gt; sigma_low        0.016073  0.001586         0.01548   5.87007      NaN
#&gt; rsd_high         0.006626 -0.011700        -0.05357   0.04849      NaN
#&gt;                 sigma_low  rsd_high
#&gt; parent_0         0.016073  0.006626
#&gt; log_k_m1         0.001586 -0.011700
#&gt; f_parent_qlogis  0.015476 -0.053566
#&gt; log_alpha        5.870075  0.048487
#&gt; log_beta              NaN       NaN
#&gt; sigma_low        1.000000 -0.652558
#&gt; rsd_high        -0.652558  1.000000
#&gt; 
#&gt; Backtransformed parameters:
#&gt; Confidence intervals for internally transformed parameters are asymmetric.
#&gt; t-test (unrealistically) based on the assumption of normal distribution
#&gt; for estimators of untransformed parameters.
#&gt;                 Estimate t value    Pr(&gt;t)     Lower     Upper
#&gt; parent_0       1.016e+02 32.7800 6.311e-26 9.624e+01 1.070e+02
#&gt; k_m1           5.072e-03 10.1200 1.216e-11 4.196e-03 6.130e-03
#&gt; f_parent_to_m1 5.004e-01 20.8300 4.317e-20 4.613e-01 5.394e-01
#&gt; alpha          2.502e+02  0.5624 2.889e-01 2.463e+02 2.542e+02
#&gt; beta           2.455e+03  0.5549 2.915e-01        NA        NA
#&gt; sigma_low      2.488e-03  0.4843 3.158e-01 1.992e-03 2.984e-03
#&gt; rsd_high       7.921e-02  8.4300 8.001e-10 6.018e-02 9.823e-02
#&gt; 
#&gt; FOCUS Chi2 error levels in percent:
#&gt;          err.min n.optim df
#&gt; All data   6.781       5 14
#&gt; parent     7.141       3  6
#&gt; m1         4.640       2  8
#&gt; 
#&gt; Resulting formation fractions:
#&gt;                 ff
#&gt; parent_m1   0.5004
#&gt; parent_sink 0.4996
#&gt; 
#&gt; Estimated disappearance times:
#&gt;           DT50  DT90 DT50back
#&gt; parent   6.812  22.7    6.834
#&gt; m1     136.661 454.0       NA</div><div class='input'>
<span class='co'># We can easily use starting parameters from the parent only fit (only for illustration)</span>
<span class='va'>fit.FOMC</span> <span class='op'>=</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='st'>"FOMC"</span>, <span class='va'>FOCUS_2006_D</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span>, error_model <span class='op'>=</span> <span class='st'>"tc"</span><span class='op'>)</span>
<span class='va'>fit.FOMC_SFO</span> <span class='op'>&lt;-</span> <span class='fu'>mkinfit</span><span class='op'>(</span><span class='va'>FOMC_SFO</span>, <span class='va'>FOCUS_D</span>, quiet <span class='op'>=</span> <span class='cn'>TRUE</span>,
  parms.ini <span class='op'>=</span> <span class='va'>fit.FOMC</span><span class='op'>$</span><span class='va'>bparms.ode</span>, error_model <span class='op'>=</span> <span class='st'>"tc"</span><span class='op'>)</span>
<span class='co'># }</span>
</div></pre>
  </div>
  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
    <nav id="toc" data-toggle="toc" class="sticky-top">
      <h2 data-toc-skip>Contents</h2>
    </nav>
  </div>
</div>


      <footer>
      <div class="copyright">
  <p>Developed by Johannes Ranke.</p>
</div>

<div class="pkgdown">
  <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.6.1.</p>
</div>

      </footer>
   </div>

  


  </body>
</html>