1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
|
<!-- 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">0.9.50.4</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 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
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 transformations. If TRUE,
the g parameter of the DFOP and HS models are also transformed, as they
can also be seen as compositional data. The transformation used for these
transformations is the <code><a href='ilr.html'>ilr()</a></code> transformation.</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
<a href='https://doi.org/10.3390/environments6120124'>doi: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'><-</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'>#> mkin version used for fitting: 0.9.50.4
#> R version used for fitting: 4.0.3
#> Date of fit: Thu Nov 5 08:25:42 2020
#> Date of summary: Thu Nov 5 08:25:42 2020
#>
#> Equations:
#> d_parent/dt = - (alpha/beta) * 1/((time/beta) + 1) * parent
#>
#> Model predictions using solution type analytical
#>
#> Fitted using 222 model solutions performed in 0.049 s
#>
#> Error model: Constant variance
#>
#> Error model algorithm: OLS
#>
#> Starting values for parameters to be optimised:
#> value type
#> parent_0 85.1 state
#> alpha 1.0 deparm
#> beta 10.0 deparm
#>
#> Starting values for the transformed parameters actually optimised:
#> value lower upper
#> parent_0 85.100000 -Inf Inf
#> log_alpha 0.000000 -Inf Inf
#> log_beta 2.302585 -Inf Inf
#>
#> Fixed parameter values:
#> None
#>
#> Results:
#>
#> AIC BIC logLik
#> 44.68652 45.47542 -18.34326
#>
#> Optimised, transformed parameters with symmetric confidence intervals:
#> Estimate Std. Error Lower Upper
#> parent_0 85.87000 1.8070 81.23000 90.5200
#> log_alpha 0.05192 0.1353 -0.29580 0.3996
#> log_beta 0.65100 0.2287 0.06315 1.2390
#> sigma 1.85700 0.4378 0.73200 2.9830
#>
#> Parameter correlation:
#> parent_0 log_alpha log_beta sigma
#> parent_0 1.000e+00 -1.565e-01 -3.142e-01 4.758e-08
#> log_alpha -1.565e-01 1.000e+00 9.564e-01 1.007e-07
#> log_beta -3.142e-01 9.564e-01 1.000e+00 8.568e-08
#> sigma 4.758e-08 1.007e-07 8.568e-08 1.000e+00
#>
#> Backtransformed parameters:
#> Confidence intervals for internally transformed parameters are asymmetric.
#> t-test (unrealistically) based on the assumption of normal distribution
#> for estimators of untransformed parameters.
#> Estimate t value Pr(>t) Lower Upper
#> parent_0 85.870 47.530 3.893e-08 81.2300 90.520
#> alpha 1.053 7.393 3.562e-04 0.7439 1.491
#> beta 1.917 4.373 3.601e-03 1.0650 3.451
#> sigma 1.857 4.243 4.074e-03 0.7320 2.983
#>
#> FOCUS Chi2 error levels in percent:
#> err.min n.optim df
#> All data 6.657 3 6
#> parent 6.657 3 6
#>
#> Estimated disappearance times:
#> DT50 DT90 DT50back
#> parent 1.785 15.15 4.56
#>
#> Data:
#> time variable observed predicted residual
#> 0 parent 85.1 85.875 -0.7749
#> 1 parent 57.9 55.191 2.7091
#> 3 parent 29.9 31.845 -1.9452
#> 7 parent 14.6 17.012 -2.4124
#> 14 parent 9.7 9.241 0.4590
#> 28 parent 6.6 4.754 1.8460
#> 63 parent 4.0 2.102 1.8977
#> 91 parent 3.9 1.441 2.4590
#> 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'><-</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'><-</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='mkinsub.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='mkinsub.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'>#> <span class='message'>Successfully compiled differential equation model from auto-generated C code.</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'><-</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='co'># Since mkin 0.9.50.3, we get a warning about non-normality of residuals,</span>
<span class='co'># so we try an alternative error model</span>
<span class='va'>fit.tc</span> <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><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'>#> Likelihood ratio test
#>
#> Model 1: SFO_SFO with error model tc and fixed parameter(s) m1_0
#> Model 2: SFO_SFO with error model const and fixed parameter(s) m1_0
#> #Df LogLik Df Chisq Pr(>Chisq)
#> 1 6 -64.983
#> 2 5 -97.224 -1 64.483 9.737e-16 ***
#> ---
#> 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'><-</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'># This also avoids the warning about non-normality, but the two-component error model</span>
<span class='co'># 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'>#> Likelihood ratio test
#>
#> Model 1: SFO_SFO with error model tc and fixed parameter(s) m1_0
#> Model 2: SFO_SFO with error model obs and fixed parameter(s) m1_0
#> #Df LogLik Df Chisq Pr(>Chisq)
#> 1 6 -64.983
#> 2 6 -96.936 0 63.907 < 2.2e-16 ***
#> ---
#> 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'>#> parent_0 k_parent k_m1 f_parent_to_m1 sigma_low
#> 1.007343e+02 1.005562e-01 5.166712e-03 5.083933e-01 3.049891e-03
#> rsd_high
#> 7.928117e-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'>#> $ff
#> parent_m1 parent_sink
#> 0.5083933 0.4916067
#>
#> $distimes
#> DT50 DT90
#> parent 6.89313 22.89848
#> m1 134.15635 445.65776
#> </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'>#> <span class='message'>Loading required package: rbenchmark</span></div><div class='output co'>#> test relative elapsed
#> 3 analytical 1.000 0.746
#> 1 deSolve_compiled 2.288 1.707
#> 2 eigen 2.708 2.020</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'><-</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='mkinsub.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='mkinsub.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'>#> <span class='message'>Successfully compiled differential equation model from auto-generated C code.</span></div><div class='input'><span class='va'>fit.FOMC_SFO</span> <span class='op'><-</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'><-</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'>#> Likelihood ratio test
#>
#> Model 1: FOMC_SFO with error model tc and fixed parameter(s) m1_0
#> Model 2: SFO_SFO with error model tc and fixed parameter(s) m1_0
#> #Df LogLik Df Chisq Pr(>Chisq)
#> 1 7 -64.829
#> 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'>#> <span class='warning'>Warning: NaNs produced</span></div><div class='output co'>#> <span class='warning'>Warning: NaNs produced</span></div><div class='output co'>#> <span class='warning'>Warning: diag(.) had 0 or NA entries; non-finite result is doubtful</span></div><div class='output co'>#> mkin version used for fitting: 0.9.50.4
#> R version used for fitting: 4.0.3
#> Date of fit: Thu Nov 5 08:25:56 2020
#> Date of summary: Thu Nov 5 08:25:56 2020
#>
#> Equations:
#> d_parent/dt = - (alpha/beta) * 1/((time/beta) + 1) * parent
#> d_m1/dt = + f_parent_to_m1 * (alpha/beta) * 1/((time/beta) + 1) *
#> parent - k_m1 * m1
#>
#> Model predictions using solution type deSolve
#>
#> Fitted using 3611 model solutions performed in 2.673 s
#>
#> Error model: Two-component variance function
#>
#> Error model algorithm: d_3
#> Three-step fitting yielded a higher likelihood than direct fitting
#>
#> Starting values for parameters to be optimised:
#> value type
#> parent_0 100.75 state
#> alpha 1.00 deparm
#> beta 10.00 deparm
#> k_m1 0.10 deparm
#> f_parent_to_m1 0.50 deparm
#> sigma_low 0.10 error
#> rsd_high 0.10 error
#>
#> Starting values for the transformed parameters actually optimised:
#> value lower upper
#> parent_0 100.750000 -Inf Inf
#> log_k_m1 -2.302585 -Inf Inf
#> f_parent_ilr_1 0.000000 -Inf Inf
#> log_alpha 0.000000 -Inf Inf
#> log_beta 2.302585 -Inf Inf
#> sigma_low 0.100000 0 Inf
#> rsd_high 0.100000 0 Inf
#>
#> Fixed parameter values:
#> value type
#> m1_0 0 state
#>
#> Results:
#>
#> AIC BIC logLik
#> 143.658 155.1211 -64.82902
#>
#> Optimised, transformed parameters with symmetric confidence intervals:
#> Estimate Std. Error Lower Upper
#> parent_0 101.600000 2.6390000 96.240000 107.000000
#> log_k_m1 -5.284000 0.0928900 -5.473000 -5.095000
#> f_parent_ilr_1 0.001008 0.0541900 -0.109500 0.111500
#> log_alpha 5.522000 0.0077300 5.506000 5.538000
#> log_beta 7.806000 NaN NaN NaN
#> sigma_low 0.002488 0.0002431 0.001992 0.002984
#> rsd_high 0.079210 0.0093280 0.060180 0.098230
#>
#> Parameter correlation:
#> parent_0 log_k_m1 f_parent_ilr_1 log_alpha log_beta sigma_low
#> parent_0 1.000000 -0.094697 -0.76654 0.70525 NaN 0.016099
#> log_k_m1 -0.094697 1.000000 0.51404 -0.14347 NaN 0.001576
#> f_parent_ilr_1 -0.766543 0.514038 1.00000 -0.61368 NaN 0.015465
#> log_alpha 0.705247 -0.143468 -0.61368 1.00000 NaN 5.871780
#> log_beta NaN NaN NaN NaN 1 NaN
#> sigma_low 0.016099 0.001576 0.01546 5.87178 NaN 1.000000
#> rsd_high 0.006566 -0.011662 -0.05353 0.04845 NaN -0.652554
#> rsd_high
#> parent_0 0.006566
#> log_k_m1 -0.011662
#> f_parent_ilr_1 -0.053525
#> log_alpha 0.048451
#> log_beta NaN
#> sigma_low -0.652554
#> rsd_high 1.000000
#>
#> Backtransformed parameters:
#> Confidence intervals for internally transformed parameters are asymmetric.
#> t-test (unrealistically) based on the assumption of normal distribution
#> for estimators of untransformed parameters.
#> Estimate t value Pr(>t) Lower Upper
#> parent_0 1.016e+02 32.7800 6.312e-26 9.624e+01 1.070e+02
#> k_m1 5.072e-03 10.1200 1.216e-11 4.197e-03 6.130e-03
#> f_parent_to_m1 5.004e-01 20.8300 4.318e-20 4.614e-01 5.394e-01
#> alpha 2.502e+02 0.5624 2.889e-01 2.463e+02 2.542e+02
#> beta 2.455e+03 0.5549 2.915e-01 NA NA
#> sigma_low 2.488e-03 0.4843 3.158e-01 1.992e-03 2.984e-03
#> rsd_high 7.921e-02 8.4300 8.001e-10 6.018e-02 9.823e-02
#>
#> FOCUS Chi2 error levels in percent:
#> err.min n.optim df
#> All data 6.781 5 14
#> parent 7.141 3 6
#> m1 4.640 2 8
#>
#> Resulting formation fractions:
#> ff
#> parent_m1 0.5004
#> parent_sink 0.4996
#>
#> Estimated disappearance times:
#> DT50 DT90 DT50back
#> parent 6.812 22.7 6.834
#> 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'><-</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>
|