Tutorial 2: a transit delay, then absorption: what do the data determine?¶
A drug that passes a transit delay before it is absorbed is described by two quantities: the mean transit time (MTT) and the absorption rate constant (ka). Both slow the rise of the concentration, so the question every modeller meets is whether the data can tell them apart. This tutorial answers it for simulated studies, where the true values are known:
- the model of TenQSP's first tutorial, and studies simulated from it;
- a sparse design (samples from 2 h on): the doctor finds MTT and ka moving together, and a profile shows what the data do determine, the mean absorption time MTT + 1/ka;
- a rich design (samples from 15 min on): both are estimated, but the interval for ka from its standard error is not the one to trust;
- ten studies of the rich design: the estimates of ka scatter, and the profile intervals contain the true value where the standard-error intervals sometimes do not.
import warnings
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tenqsp as tq
import tenqsp_fit as tf
TRUE = {"MTT": 1.5, "ka": 3.0, "CL": 3.0, "V": 30.0}
1. The model and the studies¶
As in TenQSP's tutorial 1:
the drug in transit is a field u(z) along an axis from 0 (just dosed) to 1 (reaching the
absorption compartment Aa), crossed in MTT on average. Four cells of the erlang scheme
are exactly three transit compartments. The dose D starts in the first cell.
model = tq.Model.from_text("""
model transit
axis z in [0, 1] n=4 kind=one_way
param MTT=1.5, ka=3, CL=3, V=30, D=100
field u(z)
d_t(u) = -d_z(u/MTT)
at z=1: outflow
d_t(Aa) = u(z=1)/MTT - ka*Aa
d_t(Ac) = ka*Aa - CL/V*Ac
u(0) = D*point(z, at=0.125)
observable conc = Ac/V
""")
SPARSE = [2, 4, 8, 24]
RICH = [0.25, 0.5, 0.75, 1, 1.25, 1.5, 2, 3, 4, 8, 24]
def study(times, seed, subjects=12, cv=0.15):
"""Concentrations of `subjects` subjects at `times`, from the true values, with a
measurement error of `cv` (lognormal)."""
true = np.asarray(model.solve((0, 24), times=times, rtol=1e-10, atol=1e-12)["conc"])
rng = np.random.default_rng(seed)
rows = [
{"ID": i, "TIME": t, "DV": c * np.exp(cv * rng.standard_normal())}
for i in range(1, subjects + 1)
for t, c in zip(times, true, strict=True)
]
return pd.DataFrame(rows)
t = np.linspace(0, 24, 241)
fine = np.asarray(model.solve((0, 24), times=t)["conc"])
fig, ax = plt.subplots(1, 2, figsize=(10, 3.5))
for a, times, name in [(ax[0], SPARSE, "sparse"), (ax[1], RICH, "rich")]:
d = study(times, seed=2)
a.plot(d.TIME, d.DV, "o", ms=3, alpha=0.5)
a.plot(t, fine, "k-", lw=1, label="true")
a.set_xlim(0, 12)
a.set_title(f"{name} design: {len(times)} samples per subject")
a.set_xlabel("time (h)")
a.set_ylabel("concentration")
a.legend()
Every subject has the same parameters here, to keep the question about the design; parameters that vary between subjects come in the next tutorials.
The fit estimates MTT, ka, CL and V, and a proportional error. ka gets a lower bound, 0.3 per hour, three times the elimination rate CL/V: with oral data alone, a slow absorption and a fast elimination fit as well as the reverse (flip-flop kinetics), and only an intravenous dose could tell them apart. The bound says which we mean.
def problem(data, checks="full"):
p = tf.Problem(model, name="transit")
p.data(data)
p.unit("ID")
p.observe("conc", "DV")
p.estimate("MTT", 1.0)
p.estimate("ka", 2.0, bounds=(0.3, 100))
p.estimate("CL", 2.0)
p.estimate("V", 20.0)
p.fix("D", 100)
p.error("conc", tf.proportional(0.2))
p.method("optimize")
p.options(starts=4, seed=1, checks=checks, progress="none")
return p
2. The sparse design¶
Four samples per subject, the first at 2 h, when the concentration is near its peak.
r_sparse = tf.fit(problem(study(SPARSE, seed=2)))
print(r_sparse)
transit: method optimize (L-BFGS-B), converged; -2LL -22.9813 (48 measurements)
starts: 4; 4 at the best fit (within 0.1); redrawn 0; failed runs during the fit 0; 21 s
quantity estimate SE RSE % 95 % CI also
MTT 1.3438 1.206 89.7 (0.2315, 7.799)
ka 1.8826 4.635 246 (0.3047, 84.67)
CL 2.9655 0.06577 2.22 (2.839, 3.097)
V 28.447 1.222 4.29 (26.15, 30.95)
conc.proportional 0.13771 0.01432 10.4 (0.1123, 0.1688)
doctor: 1 warning, 4 notes
warning weakly determined: MTT (its 95 % interval spans a factor 33.7), which moves with ka (correlation 1.00); and ka (its 95 % interval spans a factor 1.55e+04), which moves with MTT (correlation 1.00).
what to do: a written prior, or fix the quantity and say so; or measurements that inform it.
note determination: condition number of the curvature 3.35e+04.
note optima: all 4 starts reached the same fit.
note tolerance check: at rtol 1e-08 the -2LL changes by 7.6e-10 and the estimates by less than 0.001 SE; rtol 1e-06 is enough.
note gradient check: exact gradients agree with differences (at rtol 1e-08) to 0.034 % (3 directions).
The estimates of CL and V are fine (2-4 % RSE). The doctor warns that MTT and ka are weakly determined and move together (a correlation of 1.00): the data are fitted almost equally well by a long delay with fast absorption as by a short delay with slower absorption.
A profile of ka shows what that means. Each point holds ka at one value and refits the rest; the 95 % interval is where −2LL stays within the cut-off of its minimum. Here ka can be anywhere from about 0.9 to the upper bound: the upper end is not reached.
prof = r_sparse.profile("ka")
print(prof)
prof.plot()
<Profile of ka: estimate 1.883, 95% interval (0.9146, not reached), 14 points; cut-off 4.24 (small sample: n 48, p 4, F(1, 44))>
<Axes: title={'center': 'ka: 95% interval (0.9146, not reached)'}, xlabel='ka', ylabel='-2LL above the minimum'>
What stays fixed along the profile is what the data determine. MTT + 1/ka is the mean
time from the dose until the drug reaches the circulation (the mean absorption time,
MAT): the mean time in transit plus the mean time in Aa. Within the profile's interval,
ka goes from about 0.9 to the bound, a factor of a hundred, while MAT stays at 1.87 h (it
moves by less than 0.01 h); it is close to the true 1.5 + 1/3 = 1.83 h.
d = prof.table.copy()
d["MAT"] = d.MTT + 1 / d.value
inside = d[d["-2LL"] <= prof.m2ll + prof.cut]
print(inside[["value", "-2LL", "MTT", "MAT"]].round(3).to_string(index=False))
print(f"MAT within the interval: {inside.MAT.min():.3f} to {inside.MAT.max():.3f} h")
assert r_sparse.doctor().count("warning") >= 1
assert "weakly determined" in str(r_sparse.doctor())
assert np.isnan(prof.interval[1]) # the upper end is not reached
assert inside.MAT.max() / inside.MAT.min() < 1.15 # MAT moves by less than 15 %
assert abs(inside.MAT.median() / (TRUE["MTT"] + 1 / TRUE["ka"]) - 1) < 0.05
value -2LL MTT MAT 1.883 -22.981 1.344 1.875 6.447 -22.980 1.720 1.875 22.075 -22.963 1.829 1.874 75.591 -22.954 1.859 1.873 100.000 -22.953 1.862 1.872 MAT within the interval: 1.872 to 1.875 h
3. The rich design¶
Eleven samples per subject, from 15 minutes on, so the rise is sampled.
r_rich = tf.fit(problem(study(RICH, seed=100)))
print(r_rich)
prof_rich = r_rich.profile("ka")
print(prof_rich)
prof_rich.plot()
transit: method optimize (L-BFGS-B), converged; -2LL -365.9181 (132 measurements) starts: 4; 4 at the best fit (within 0.1); redrawn 0; failed runs during the fit 0; 2 s quantity estimate SE RSE % 95 % CI also MTT 1.4615 0.1356 9.28 (1.218, 1.753) ka 2.7369 1.011 37 (1.373, 5.737) CL 2.8649 0.05703 1.99 (2.755, 2.979) V 30.034 0.7326 2.44 (28.63, 31.5) conc.proportional 0.1399 0.008777 6.27 (0.1237, 0.1582) doctor: 4 notes note determination: condition number of the curvature 4.8e+03; no ridge, every quantity determined within a factor 10. note optima: all 4 starts reached the same fit. note tolerance check: at rtol 1e-08 the -2LL changes by 6e-05 and the estimates by less than 0.001 SE; rtol 1e-06 is enough. note gradient check: exact gradients agree with differences (at rtol 1e-08) to 0.039 % (3 directions).
<Profile of ka: estimate 2.737, 95% interval (1.606, 4.931), 24 points; cut-off 3.98 (small sample: n 132, p 4, F(1, 128))>
<Axes: title={'center': 'ka: 95% interval (1.606, 4.931)'}, xlabel='ka', ylabel='-2LL above the minimum'>
Both MTT and ka are now estimated, and the doctor no longer finds them weakly determined; all four starts reached the same fit.
There are two intervals for ka. The one in the table comes from the standard error: it assumes that −2LL is a parabola around the estimate (on the log scale of ka). The profile follows −2LL itself. For this study they differ; which to trust is the subject of the next section.
row = r_rich.table().set_index("quantity").loc["ka"]
print(f"ka {r_rich.estimates['ka']:.3g}: from its SE ({row['CI 2.5 %']:.3g}, "
f"{row['CI 97.5 %']:.3g}); from the profile ({prof_rich.interval[0]:.3g}, "
f"{prof_rich.interval[1]:.3g}); true value {TRUE['ka']}") # fmt: skip
assert "weakly determined" not in str(r_rich.doctor())
assert prof_rich.interval[0] <= TRUE["ka"] <= prof_rich.interval[1]
ka 2.74: from its SE (1.37, 5.74); from the profile (1.61, 4.93); true value 3.0
4. Ten studies¶
Is the profile's interval the one to trust? Simulate ten studies of the rich design, fit each, and count how often each interval contains the true ka. (A check of the 95 % intervals would need hundreds of studies; ten show the pattern. The quick checks of the doctor are enough here.)
rows = []
with warnings.catch_warnings():
warnings.simplefilter("ignore") # warnings of weakly determined fits, for some studies
for seed in range(100, 110):
r = tf.fit(problem(study(RICH, seed), checks="quick"))
ci = r.table().set_index("quantity").loc["ka"]
pr = r.profile("ka")
rows.append({"study": seed - 99, "ka": r.estimates["ka"],
"SE interval": (round(ci["CI 2.5 %"], 3), round(ci["CI 97.5 %"], 3)),
"profile interval": (round(pr.interval[0], 3), round(pr.interval[1], 3)),
"SE covers": ci["CI 2.5 %"] <= TRUE["ka"] <= ci["CI 97.5 %"],
"profile covers": pr.interval[0] <= TRUE["ka"] <= pr.interval[1]}) # fmt: skip
studies = pd.DataFrame(rows)
print(studies.to_string(index=False))
print(f"true ka {TRUE['ka']}: inside the SE interval in {studies['SE covers'].sum()} of 10, "
f"inside the profile interval in {studies['profile covers'].sum()} of 10") # fmt: skip
fig, ax = plt.subplots(figsize=(6, 3.5))
for i, s in studies.iterrows():
ax.plot(s["SE interval"], [i - 0.12] * 2, color="#888888", lw=2)
ax.plot(s["profile interval"], [i + 0.12] * 2, color="#1f3b73", lw=2)
ax.plot(s["ka"], i, "k.", ms=6)
ax.plot([], [], color="#888888", lw=2, label="from the SE")
ax.plot([], [], color="#1f3b73", lw=2, label="profile")
ax.axvline(TRUE["ka"], color="#d62728", ls="--", lw=1, label="true ka")
ax.set_xscale("log")
ax.set_xlabel("ka (1/h)")
ax.set_ylabel("study")
ax.legend(fontsize="small")
assert studies["profile covers"].all()
assert studies.ka.max() / studies.ka.min() > 1.5 # the estimates scatter
study ka SE interval profile interval SE covers profile covers
1 2.736928 (1.373, 5.737) (1.606, 4.931) True True
2 2.677430 (1.576, 4.688) (1.678, 4.572) True True
3 2.762051 (0.486, 25.791) (1.474, 5.473) True True
4 2.734314 (1.51, 5.137) (1.63, 4.649) True True
5 2.699737 (1.67, 4.471) (1.732, 4.317) True True
6 5.620810 (3.703, 8.528) (1.073, 8.159) False True
7 2.762279 (0.835, 10.896) (1.575, 5.541) True True
8 2.770167 (0.766, 12.338) (1.545, 5.535) True True
9 5.410804 (3.28, 8.93) (1.026, 8.355) False True
10 4.548052 (2.74, 7.596) (1.293, 6.829) True True
true ka 3.0: inside the SE interval in 8 of 10, inside the profile interval in 10 of 10
The estimates of ka scatter between studies of the same design, from 1.4 to 5.4 per hour. The intervals from the standard error are sometimes far too wide (study 3: 0.5 to 26) and sometimes too narrow and in the wrong place, missing the true value (studies 6 and 9). The profile intervals contain it in every study.
What this shows¶
- With samples only around and after the peak, the data determine the mean absorption time MTT + 1/ka, not MTT and ka apart. The doctor says so (weakly determined, moving together), and the profile shows the trade-off.
- Sampling the rise determines both, but ka's likelihood is far from a parabola: its
interval should come from a profile (
result.profile("ka")), not from its standard error. - Oral data alone cannot tell absorption from elimination (flip-flop); a bound on ka says which one is meant.