I am quite new to use data.table but I need to optimize the post-processing of large simulations. I would have used ddply as followed to obtain new output with desired calculated parameters based on a customized function (estimate_AUC), that can accommodate different column names (eg TIME and Cc) and different calculation method (eg. last, inf, etc):
AUC_out <- plyr::ddply(sim, c("ID","Dose"), function(x) {
out <- data.frame(AUCinf = estimate_AUC(Time = x$TIME,
Conc = x$Cc,
AUCtype = "inf"),
AUC48 = estimate_AUC(Time = x$TIME[x$TIME<=48],
Conc = x$Cc[x$TIME<=48],
AUCtype = "last")),
Cc48 =approx(x$TIME,x$Cc,48)$y,
stringsAsFactors = FALSE)
})
with sim:
ID Cc TIME Dose
1: 1 0.000000000 0.00 100
2: 1 0.462881773 0.25 100
3: 1 0.625713766 0.50 100
4: 1 0.729046515 0.75 100
5: 1 0.825169830 1.00 100
---
How can I use a customized function using data.table, while being able to provide the method, specific column names in the function arguments
dput(head(sim))
structure(list(ID = c(1, 1, 1, 1, 1, 1), DoseID = c(1L, 1L, 1L,
1L, 1L, 1L), Dose = c(100, 100, 100, 100, 100, 100), nbrDoses = c(1,
1, 1, 1, 1, 1), ExpID = c(1, 1, 1, 1, 1, 1), TrialID = c(1L,
1L, 1L, 1L, 1L, 1L), IndivID = c(1L, 1L, 1L, 1L, 1L, 1L), USUBJID = c(11,
11, 11, 11, 11, 11), TIME = c(0, 0.25, 0.5, 0.75, 1, 1.25), Cc = c(0,
0.462881773273397, 0.625713765604934, 0.729046515431686, 0.825169830220163,
0.92030770178198), PL = c(14.8635310605163, 14.8810310604533,
14.8985310551099, 14.916031006317, 14.9335308009029, 14.9510302005905
), Eff = c(5.19411550856408e-19, 1.18067555547615e-08, 4.21253176904848e-07,
2.63818207596035e-06, 9.25475212778715e-06, 2.43639651038346e-05
)), class = c("data.table", "data.frame"), row.names = c(NA,
-6L), .internal.selfref = <pointer: 0x00000000045e1ef0>)
Here is an example using the PKNCA
package. Since there are just one dose and one ID, there is not much data to be calculated...
library(data.table)
library(PKNCA)
sim <- structure(list(ID = c(1, 1, 1, 1, 1, 1),
DoseID = c(1L, 1L, 1L, 1L, 1L, 1L), Dose = c(100,100, 100, 100, 100, 100), nbrDoses = c(1, 1, 1, 1, 1, 1),
ExpID = c(1, 1, 1, 1, 1, 1),
TrialID = c(1L, 1L, 1L, 1L, 1L, 1L),
IndivID = c(1L, 1L, 1L, 1L, 1L, 1L),
USUBJID = c(11, 11, 11, 11, 11, 11), TIME = c(0, 0.25, 0.5, 0.75, 1, 1.25),
Cc = c(0, 0.462881773273397, 0.625713765604934, 0.729046515431686, 0.825169830220163, 0.92030770178198),
PL = c(14.8635310605163, 14.8810310604533, 14.8985310551099, 14.916031006317, 14.9335308009029, 14.9510302005905),
Eff = c(5.19411550856408e-19, 1.18067555547615e-08, 4.21253176904848e-07, 2.63818207596035e-06, 9.25475212778715e-06, 2.43639651038346e-05)), class = c("data.table", "data.frame"), row.names = c(NA, -6L))
setDT(sim)
sim[, .(AUC.inf = pk.calc.auc(Cc, TIME, interval=c(0, Inf)),
AUC.48 = pk.calc.auc(Cc, TIME, interval=c(0, 48)),
Cc48 = approx(TIME, Cc, 48)$y
), by = c("ID", "Dose")]
#> ID Dose AUC.inf AUC.48 Cc48
#> 1: 1 100 0.7757414 0.7757414 NA
Created on 2020-03-22 by the reprex package (v0.3.0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.