lastdose

R build status

Overview

Calculate the time since and amount of the last dose. Additional (ADDL) dosing records are expanded and included in the calculation.

knitr::opts_chunk$set(comment = '.', message=FALSE, warning = FALSE, 
                      fig.path="man/figures/readme-")

Installation

remotes::install_github("metrumresearchgroup/lastdose")
library(lastdose)
library(tidyverse)
theme_set(theme_bw())

A PK profile

We'll use this PK profile as an example

file <- system.file("csv/data1.csv", package = "lastdose")

df <- read.csv(file) 

head(df)

The dosing runs over 12 weeks and there are 3 epochs, with 3 different doses, most of which are scheduled into the future via ADDL.

df %>% filter(EVID==1) %>% count(TIME,AMT,ADDL)

ggplot(df, aes(TIME,DV)) + geom_line() + theme_bw()

Calculate TAD, TAFD, and LDOS

Use the lastdose() function

df <- lastdose(df)

head(df)

Now we have TAD, TAFD, and LDOS in our data set.

Plot last dose versus time

ggplot(df, aes(TIME,LDOS)) + geom_line()

Plot time after dose versus time

ggplot(df, aes(TIME,TAD)) + geom_line()

Observations before doses at the same time by default

ggplot(df, aes(TIME,TAD)) + geom_line() + 
  scale_x_continuous(breaks = seq(0,72,4), limits=c(0,72)) + 
  scale_y_continuous(breaks = seq(0,24,4), limits=c(0,24)) 

You can also make doses "happen" first

dd <- lastdose(df, addl_ties = "dose_first")

ggplot(dd, aes(TIME,TAD)) + geom_line() + 
  scale_x_continuous(breaks = seq(0,72,4), limits=c(0,72)) + 
  scale_y_continuous(breaks = seq(0,24,4), limits=c(0,24)) 

All doses explicit in the data set

df2 <- mrgsolve::realize_addl(df) %>% lastdose()

ggplot(df2, aes(TIME,TAD)) + geom_line() + 
  scale_x_continuous(breaks = seq(0,72,4), limits = c(0,72)) + 
  scale_y_continuous(breaks = seq(0,24,4))

How does it perform on bigger data?

Same setup as the previous profile, but more individuals.

We have 500K rows and 1000 individuals

file <- system.file("csv/data_big.RDS", package = "lastdose")

big <- readRDS(file)

dim(big)

length(unique(big$ID))

Timing result

system.time(x2 <- lastdose(big))

Compare against the single profile

system.time(x1 <- lastdose(df))

x3 <- filter(x2, big[["ID"]]==1) %>% as.data.frame()

all.equal(x1,x3)

Observations prior to the first dose

When non-dose records happen prior to the first dose, lastdose calculates the time before the first dose (a negative value) for these records.

file <- system.file("csv/data2.csv", package = "lastdose")

df <- read_csv(file)

lastdose(df) %>% head()

The user can alternatively control what happens for these records

lastdose(df, fill = NA_real_, back_calc=FALSE) %>% head()

More info

See inst/doc/about.md for more details.



metrumresearchgroup/lastdose documentation built on April 12, 2025, 3:51 p.m.