Skip to content

Commit

Permalink
updated R sources
Browse files Browse the repository at this point in the history
  • Loading branch information
shanmdphd committed Jun 5, 2017
1 parent 5add63e commit e0ca59c
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 132 deletions.
42 changes: 42 additions & 0 deletions R/ConcTime.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,45 @@ ConcTime <- function(Weight, Dose, N = 20){
select(Subject, Time, Conc)
return(ggConc)
}


#' Create a dataset of the concentration-time curve of multiple dosing
#'
#' \code{ConcTimeMulti} will create a dataset of the concentration-time curve of multiple dosing
#'
#' @param Weight Body weight (kg)
#' @param Dose Dose of single caffeine (mg)
#' @param N The number of simulated subjects
#' @param Tau The interval of multiple dosing (hour)
#' @param Repeat The number of dosing
#' @return The dataset of concentration and time of simulated subjects of multiple dosing
#' @export
#' @examples
#' ConcTimeMulti(Weight = 20, Dose = 200, N = 20, Tau = 8, Repeat = 4)
#' ConcTimeMulti(20, 200)
#' @seealso \url{http://asancpt.github.io/CaffeineEdison}
#' @import dplyr

ConcTimeMulti <- function(Weight, Dose, N = 20, Tau = 8, Repeat = 4){
Subject <- seq(1, N, length.out = N) #
Time <- seq(0, 96, length.out = 481) #
Grid <- expand.grid(x = Subject, y = Time) %>% select(Subject=x, Time=y)

ggsuper <- Dataset(Weight, Dose, N) %>% select(CL, V, Ka, Ke) %>%
mutate(Subject = row_number()) %>%
left_join(Grid, by = "Subject") %>%
mutate(Conc = Dose / V * Ka / (Ka - Ke) * (exp(-Ke * Time) - exp(-Ka * Time))) %>%
group_by(Subject) %>%
mutate(ConcOrig = Conc,
ConcTemp = 0)
## Superposition
for (i in 1:Repeat){
Frame <- Tau * 5 * i
ggsuper <- ggsuper %>%
mutate(Conc = Conc + ConcTemp) %>%
mutate(ConcTemp = lag(ConcOrig, n = Frame, default = 0))
}

ggsuper <- ggsuper %>% select(Subject, Time, Conc)
return(ggsuper)
}
40 changes: 0 additions & 40 deletions R/ConcTimeMulti.R

This file was deleted.

41 changes: 41 additions & 0 deletions R/Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,44 @@ Dataset <- function(Weight, Dose, N = 20){
Half_life = 0.693 / Ke) %>% select(Tmax, Cmax, AUC, Half_life, CL, V, Ka, Ke)
return(MVNdata)
}

#' Create a dataset for simulation of multiple dose of caffeine
#'
#' \code{DatasetMulti} will create a dataset for simulation of multiple dose of caffeine
#'
#' @param Weight Body weight (kg)
#' @param Dose Dose of multiple caffeine (mg)
#' @param N The number of simulated subjects
#' @param Tau The interval of multiple dosing (hour)
#' @return The dataset of pharmacokinetic parameters of subjects after multiple caffeine dose following multivariate normal
#' @export
#' @examples
#' DatasetMulti(Weight = 20, Dose = 200, N = 20, Tau = 8)
#' DatasetMulti(20,500)
#' @seealso \url{http://asancpt.github.io/CaffeineEdison}
#' @importFrom mgcv rmvn
#' @import dplyr

DatasetMulti <- function(Weight, Dose, N = 20, Tau = 24){
#set.seed(20140523+1)
MVN <- rmvn(N, CaffMu, CaffSigma);
MVNdata <- data.frame(MVN, stringsAsFactors = FALSE) %>%
select(eta1 = X1, eta2 = X2, eta3 = X3) %>%
mutate(CL = 0.09792 * Weight * exp(eta1), # L/hr
V = 0.7219 * Weight * exp(eta2), # L, TVV =THETA[2] * (1 + ABST*THETA[7]) [1] 0.7218775
Ka = 4.268 * exp(eta3), # /hr
Ke = CL / V,
Half_life = 0.693 / Ke,
Tmax = (log(Ka) - log(Ke)) / (Ka - Ke),
Cmax = Dose / V * Ka / (Ka - Ke) * (exp(-Ke * Tmax) - exp(-Ka * Tmax)),
AUC = Dose / CL
) %>%
mutate(AI = 1/(1-exp(-1*Ke*Tau)),
Aavss = 1.44 * Dose * Half_life / Tau,
Cavss = Dose / (CL * Tau),
Cminss = Dose * exp(-Ke * Tau) / (V * (1 - exp(-Ke * Tau))),
Cmaxss = Dose / (V * (1 - exp(-Ke * Tau)))) %>%
select(TmaxS = Tmax, CmaxS = Cmax, AUCS = AUC, AI, Aavss, Cavss, Cmaxss, Cminss)
return(MVNdata)
}

40 changes: 0 additions & 40 deletions R/DatasetMulti.R

This file was deleted.

29 changes: 29 additions & 0 deletions R/Plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,32 @@ Plot <- function(ConcTime, log = FALSE){
if (log == TRUE) p <- p + scale_y_log10() #limits = c(0.1, max(80, ggConc$Conc))))
return(p)
}

#' Create concentration-time curve after multiple doses of caffeine
#'
#' \code{PlotMulti} will create concentration-time curve after multiple doses of caffeine
#'
#' @param ConcTimeMulti Concentration-time dataset having column names Subject, Time, and Conc (case-sensitive)
#' @param log y axis log
#' @return The concentration-time curve
#' @export
#' @examples
#' PlotMulti(ConcTimeMulti(Weight = 20, Dose = 200, N = 20, Tau = 8, Repeat = 4))
#' @seealso \url{http://asancpt.github.io/CaffeineEdison}
#' @import dplyr
#' @import ggplot2

PlotMulti <- function(ConcTimeMulti, log = FALSE){
p <- ggplot(ConcTimeMulti, aes(x=Time, y=Conc)) + #, group=Subject, colour = Conc)) + #Subject)) +
xlab("Time (hour)") + ylab("Concentration (mg/L)") +
scale_x_continuous(breaks = seq(0, 96, 12)) +
#scale_colour_gradient(low="navy", high="red", space="Lab") +
geom_line(aes(group = Subject, colour = Conc)) +
stat_summary(fun.y = "mean", colour = "#F0E442", size = 1, geom = "line") +
geom_hline(yintercept = 80, colour="red") +
geom_hline(yintercept = 40, colour="blue") +
geom_hline(yintercept = 10, colour="green") + theme_linedraw()

if (log == TRUE) p <- p + scale_y_log10() #limits = c(0.1, max(80, ggConc$Conc))))
return(p)
}
28 changes: 0 additions & 28 deletions R/PlotMulti.R

This file was deleted.

Binary file modified R/sysdata.rda
Binary file not shown.
Binary file modified caffsim.pdf
Binary file not shown.
18 changes: 18 additions & 0 deletions data-raw/test.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Tests -------------------------------------------------------------------

Weight <- 20
Dose <- 400
N <- 20
Tau <- 8

edisonlib <- c("mgcv", "psych", "ggplot2", "dplyr", "markdown", "knitr", "tibble")
lapply(edisonlib, function(pkg) {
if (system.file(package = pkg) == '') install.packages(pkg)
})
lapply(edisonlib, library, character.only = TRUE) # if needed # install.packages(mylib, lib = localLibPath)

library(roxygen2)
roxygenize()

remove.packages("caffsim")
devtools::install_github("asancpt/caffsim")
23 changes: 3 additions & 20 deletions data-raw/use_data.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# use_data ----------------------------------------------------------------

UnitTable <- read.csv("data-raw/UnitCSV.csv", as.is = TRUE)

CaffSigma <- matrix(c(0.1599, 6.095e-2, 9.650e-2,
Expand Down Expand Up @@ -37,25 +39,6 @@ devtools::use_data(CaffSigma,
Seed,
round_df,
Get_os,
UnitTable,
internal = TRUE, overwrite = TRUE)
devtools::use_data(UnitTable, overwrite = TRUE)

Weight <- 20
Dose <- 400
N <- 20
Tau <- 8

edisonlib <- c("mgcv", "psych", "ggplot2", "dplyr", "markdown", "knitr", "tibble")
lapply(edisonlib, function(pkg) {
if (system.file(package = pkg) == '') install.packages(pkg)
})
lapply(edisonlib, library, character.only = TRUE) # if needed # install.packages(mylib, lib = localLibPath)

library(roxygen2)
roxygenize()


remove.packages("caffsim")


devtools::install_github("asancpt/caffsim")
2 changes: 1 addition & 1 deletion man/ConcTimeMulti.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/Dataset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/DatasetMulti.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/PlotMulti.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e0ca59c

Please sign in to comment.