nlmixr2 / rxode2

rxode2
https://nlmixr2.github.io/rxode2/
GNU General Public License v3.0
28 stars 7 forks source link

add.sampling processing time increases disproportionately to number of subjects #724

Open Dstix opened 1 year ago

Dstix commented 1 year ago

When simulating n_subj = 1000 subjects for a weight-based dosing set, the following code was used:

doses_mg_kg_sd <- c(3, 15, 30)

mean_wt <- 3.1
sd_wt <- 0.35

n_subj <- 1000

data_sd <- tibble(
  id = 1:(n_subj * length(doses_mg_kg_sd)),
  dose_mg_kg = rep(doses_mg_kg_sd, each = n_subj)
)

data_sd %<>% mutate(
  weight = rnorm(length(data_sd$id), mean_wt, sd_wt), 
  amt = dose_mg_kg * weight,
  dose = amt
)

data_sd_samp <- et() %>% et(id = 1:(n_subj * length(doses_mg_kg_sd)), amt = data_sd$amt)

tic()
data_sd_samp %<>% add.sampling(seq(0, 168, 0.5))
toc()

80.29 sec elapsed

The length of time it takes to use add.sampling seems to increase disproportionately with n_subj:

n_subj  time
   <dbl> <dbl>
1    100  0.31
2    200  2.28
3    300  7.38
4    400 13.1 
5    500 20.1 
6    600 28.7 
7    800 51   
8   1000 80.29

When I use the following code, it processes much quicker:

doses_mg_kg_sd <- c(3, 15, 30)

mean_wt <- 3.1
sd_wt <- 0.35

n_subj <- 1000

data_sd <- tibble(
  id = 1:(n_subj * length(doses_mg_kg_sd)),
  dose_mg_kg = rep(doses_mg_kg_sd, each = n_subj)
)

data_sd %<>% mutate(
  weight = rnorm(length(data_sd$id), mean_wt, sd_wt), 
  amt = dose_mg_kg * weight,
  dose = amt
)

data_sd_samp <- et() %>% et(id = 1:(n_subj * length(doses_mg_kg_sd)), amt = data_sd$amt)

# * Create new sampling ====

tic()

sampling_times <- seq(0, 168, 0.5)

sampling_times_df <- tibble(
  id = rep(1:(n_subj * length(doses_mg_kg_sd)), length(sampling_times)),
  time = rep(sampling_times, n_subj * length(doses_mg_kg_sd)),
  evid = 0,
  cmt = "(obs)"
)

data_sd_samp %<>% bind_rows(sampling_times_df)

data_sd_samp %<>% arrange(id, time, evid)

toc()

0.14 sec elapsed

It seems like add.sampling shouldn't take this long.

billdenney commented 1 year ago

Yep, almost a minute and a half compared to 0.1 seconds should be improved. I think that this is an issue in the rxode2et package. (We have a spread-out package structure due to some CRAN requirements.) I'm about to move the issue there. I don't have an immediate solution, but hopefully we can figure something out.