Compute time after dose in a poppk dataset

I have a “normal” multiple dose poppk dataset with a dose record for each dose (no addl doses) (one analyte) already assembled with time (time since first dose), id, evid, dv, amt, cmt, etc. What it lacks is time since the last dose (TSLD). Does anyone have Pumas/Julia code that will allow me to compute TSLD from the dataset of this format?

I normally do this by taking advantage of the evid column. I group by ID, then use a cumulative sum function on evid to create a doseid variable which groups all events that occur between 2 doses together. From there I group by id and doseid, and use the lag function from the ShiftedArrays package to take the difference between the current row and the previous row for every entry in each group. lag will return missing for the first row (i.e., the dosing event) by default, but you can use the coalesce function to replace that missing value with zero. This assumes that the first event in any group is always a dosing event (evid=1) and thus TSLD=0 for the first row.

I’ve included a small example if you’d like to tinker with the code first.

using ShiftedArrays
#example df
mydf = DataFrame(
    id = repeat(1:2, inner = 4), 
    evid = repeat([1,0], outer = 4),
    nomtime = repeat([0,6,12,18], outer = 2)

# test
@chain mydf begin
    # optional sort by id, time, evid; rev=true ensures that doses come before samples recorded at the same time (1/0)
    sort(_, [:id, :nomtime, order(:evid, rev=true)])
    # create doseid
    transform(groupby(_, :id), :evid => cumsum => :doseid)
    # calculate tsld
    transform(groupby(_, [:id, :doseid]), :nomtime => (t -> coalesce.(t .- ShiftedArrays.lag(t), 0)) => :tsld)

Note that this isn’t a foolproof solution because if you have evids other than 0/1 you can’t count on your doses being labeled correctly. If you still wanted to use this approach you’d need to create a separate column to flag dosing events (something like: if isadose then 1 else 0 and save as doseflag col) and then call cumsum on the new col.

Hope that helps!

1 Like

Presumably the dosenum column could be useful here? It indicates what dose interval you’re in.