Transcript#

This transcript was generated automatically and may contain errors.

Hi, I'm Matt presenting on the blog post by myself and Janice. Today we'll walk through a real dosing problem in R, how to dose a drug for preterm babies when we only have scraps of data.

This talk is from a blog post we wrote together, putting Janice's cat supervised as usual. You'll see all three of us in the section pictures.

The talk has seven parts. First, the dosing question. Second, what a pharmacometric model is. Third, the sparse data we have. Fourth, building the model in nlmixr2. Fifth, checking whether we can believe it. Sixth, using it to compare a flat dose with a weight-based dose. And last, how this fits into the wider R ecosystem.

The dosing question

Let's start with a question. Preterm infants are prone to seizures and phenobarbital is used to treat them. The dose makes the poison. Too little and the seizures continue. Too much and the baby can stop breathing. A doctor read that one flat dose might work for every neonate. That would make giving the drug much simpler, but a flat dose tuned for an average one and a half kilogram baby might not suit our 900 gram baby. And we don't know where that baby would land.

The usual way to check exposure is to take blood samples and measure the drug level. But a preterm baby has less than a teacup of blood in its whole body. So a study with 15 samples per baby isn't possible. What we have is scraps. One sample here, another three days later. So we need a model that learns how weight affects drug clearance from earlier data and then simulates the dose instead.

What a pharmacometric model is

So what is a pharmacometric model? If you fit a mixed model before, you've done half of this already. The difference is the mean function. It isn't a line or a spline. It's the solution to a differential equation. Think of a bucket with a hole in it. Drug goes in with the dose. The volume is how widely the drug spreads and clearance is how fast it drains out. An L-mixer to that bucket is one line of ODE. Each infant gets their own clearance. The typical value plus their own random deviation.

Models like this don't just fit a curve. Their parameters mean something. The loading dose, the first big dose, is set by volume. It's how much drug it takes to fill the bucket. The maintenance doses are set by clearance. They replace what drains out. Because the model separates the two, it can design a whole dosing regimen. It can also predict doses nobody in the study actually got. That's why drug labels and pediatric doses are so often built from models like this.

It can also predict doses nobody in the study actually got. That's why drug labels and pediatric doses are so often built from models like this.

The sparse data

Now let's look at the data. The data are 59 preterm infants given phenobarbital published in 1985. They ship with nlmixr2 as pheno underscore SD. Each row in this plot is one infant sorted by weight. The small ticks are doses and the circles are measured concentrations. There are 589 doses but only 155 measurements. The babies were dosed often and sampled rarely during routine care.

Look closer and it gets worse. Nine infants have exactly one concentration. Forty percent have two or fewer. You can't estimate two parameters for a baby from one data point unless, that is, you let the infants borrow strength from each other. That's exactly what the mixed effects model does.

Building the model in nlmixr2

Let's build the model in nlmixr2. The model is an ordinary R function with two blocks. In the INI block, an arrow sets a fixed effect and a wavy single line sets a random effect. The model block holds the differential equation written the way you'd write it on paper. The last line connects the prediction to the observed data with a proportional error. Notice there's no dosing in the model. Doses come from the data. The rest is plain R and one called nlmixr2 fits it. The base model gives 37 percent between infant variability on clearance and 47 percent on volume. That means two infants on the same dose can easily differ twofold in exposure. That's why a flat dose is risky. These babies range from 0.6 to 3.6 kilograms. When we add body weight with standard allometric scaling, variability falls to about 17 and 18 percent. The objective function drops by about 144 points. More than half of the variability is just body weight.

We didn't have to rewrite the model to add weight. With model piping, we swap the clearance the volume line and set the two new initial estimates. As an experiment in nlmixr2.7.0, we can also skip that step. The new variational autoencoder estimator searches for covariates by itself. Given the base model, it found weight 2. Here's what the VAE search picked. On clearance, it found a power of weight with an exponent of about 0.63 close to the textbook three quarters. On volume, it found a hockey stick that bends at 1.3 kilograms, steeper for the smallest babies. It chose weight for both parameters. Volume variability dropped further to 14 percent. Before we use this model for dosing, we need to check that we can believe it.

Checking the model

First, let's look at individual profiles. Infants 56 and 28 gave one measurement each, and both still get a full concentration curve. Their individual and population curves nearly overlap. With one data point, there's nothing to pull the infant away from typical, and that's the right behavior. Infants 2 and 8 have three samples each, and their curves separate. Now there's enough information to say this baby isn't typical.

Second, a prediction-corrected visual predictive check. We simulate the study many times from the fitted model and ask whether the real data look like one of those draws. The observed median and 5th and 95th percentiles, the solid and dashed lines, stay inside the simulated bands. With only 155 observations, the percentiles are noisy, but the model isn't misspecified.

Comparing flat versus weight-based dosing

Now we can answer the question we started with. We compare two regimens. The flat one gives every infant a 30 mg loading dose, then 1.9 mg every 12 hours. That's right for a 1.5 kg baby. The weight-based one gives 20 mg per kg to load, then 1.25 per kg. The simulation passes the fitted object straight to RxSolve. It samples 400 new infants at each weight from the estimated variability. The model we simulate from is exactly the model we fit.

Here's the result. The gray band is the 15 to 40 mg per liter window. The flat regimen, in red, hits the window at 1.5 kg because that's what it was designed for, but it slopes down across the weight range. Too high for the smallest babies, too low for the largest. The weight-based regimen, in teal, stays flat and inside the window at every weight. Here are the numbers. At 1.5 kg, the flat dose puts almost every infant in the window. At 0.7 kg, the median infant is at 41, above the window. At 3.1 kg, the median has dropped to 12, and fewer than 4% of infants reach the window at all. With weight-based dosing, 96% or more are in the window at every weight, and that weight was in the dataset all along.

With weight-based dosing, 96% or more are in the window at every weight, and that weight was in the dataset all along.

To be clear, this is a teaching example on a 1985 dataset. We kept it to one compartment and one covariate on purpose. It is not dosing guidance. Real neonatal dosing also accounts for postnatal age and maturation, and it's decided by a qualified doctor and pharmacist. The workflow, though, is the real one.

nlmixr2 in the wider R ecosystem

Finally, where does all this sit in R? Nothing we did required leaving R. The fit object is a data frame, so every plot here is plain ggplot2, and every summary is plain dplyr. There's no export step and no intermediate file format. That's not just convenient. Analyses usually break at the boundaries between tools, and this workflow doesn't have any. Add renv to PIM packages and targets to rerun only what changed, and someone else can rebuild the dosing recommendation from the raw data.

nlmixr2 isn't a walled garden. That's unusual in this field, and it may be the strongest practical reason to use it. babelmixr2 runs the same model object through NONMEM or Monolix, so you can fit in nlmixr2 and confirm in NONMEM. It also connects to PopED for optimal experimental design. Going the other way, NONMEM to R, X, and Monolix to Rx bring finished runs into R, so years of legacy models can be simulated and checked with the same tools. Underneath it all, RxODE2 solves both the fits and the simulations, and there's no license to negotiate, so any collaborator can run your analysis today.

Getting started is a smaller step than it looks. Everything in this talk is about 40 lines of code. Install nlmixr2 from CRAN, and on Windows, install RTools2. From there, the nlmixr2 website has the model, language, and worked examples. nlmixr2lib has starting models. If you're coming from NONMEM, NONMEM to Rx will translate a model you already trust, and nlmixr2 7.0 makes FOCEI solve in parallel.

To sum up, 155 scattered concentrations were enough to model 59 infants. Body weight explains more than half of the variability between them. A flat dose is right only for the baby it was designed for, while weight-based dosing keeps nearly every simulated infant in the window. And the whole analysis is one R document that also connects to the rest of the field. Whether your data come from sports analytics, supply chains, or neonatal wards, the shape of the problem is the same. Few observations per subject, a mechanistic model, and a decision someone has to make. Thanks for listening. From Matt, Janice, and Pudding.