Skip to content

Commit

Permalink
updated package entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
shanmdphd committed Aug 10, 2017
1 parent e0ca59c commit 3f446af
Show file tree
Hide file tree
Showing 60 changed files with 1,185 additions and 7,632 deletions.
5 changes: 2 additions & 3 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
^.*\.sh$
^tests$
^data-raw$
^packrat/
^\.Rprofile$
^README.*$
^Figures$
^rd2pdf\.sh$
^assets$
^Makefile$
^docs$
Binary file removed Figures/MyPlotMultiMyConcTimeMulti-1.png
Binary file not shown.
Binary file removed Figures/MyPlotMultiPub-1.png
Binary file not shown.
Binary file removed Figures/MyPlotMyConcTime-1.png
Binary file not shown.
Binary file removed Figures/MyPlotPub-1.png
Binary file not shown.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rd2pdf:
rm caffsim.pdf;R CMD rd2pdf ../caffsim

pkgdown:
rm -rf docs;Rscript -e "Sys.setlocale('LC_ALL', 'C'); pkgdown::build_site()"
12 changes: 6 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Generated by roxygen2: do not edit by hand

export(ConcTime)
export(ConcTimeMulti)
export(Dataset)
export(DatasetMulti)
export(Plot)
export(PlotMulti)
export(caffConcTime)
export(caffConcTimeMulti)
export(caffDataset)
export(caffDatasetMulti)
export(caffPlot)
export(caffPlotMulti)
import(dplyr)
import(ggplot2)
importFrom(mgcv,rmvn)
2 changes: 1 addition & 1 deletion R.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ AlwaysSaveHistory: Yes

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 4
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
Expand Down
90 changes: 46 additions & 44 deletions R/ConcTime.R
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
#' Create a dataset of the concentration-time curve
#' Create a dataset of the concentration-time curve of single oral administration of caffeine
#'
#' \code{ConcTime} will create a dataset of the concentration-time curve
#' \code{caffConcTime} will create a dataset of the concentration-time curve
#'
#' @param Weight Body weight (kg)
#' @param Dose Dose of single caffeine (mg)
#' @param N The number of simulated subjects
#' @return The dataset of concentration and time of simulated subjects
#' @export
#' @examples
#' ConcTime(Weight = 20, Dose = 200, N = 20)
#' ConcTime(20, 200)
#' @seealso \url{http://asancpt.github.io/CaffeineEdison}
#' caffConcTime(Weight = 20, Dose = 200, N = 20)
#' caffConcTime(20, 200)
#' @import dplyr
#' @seealso \url{https://asancpt.github.io/caffsim}

ConcTime <- function(Weight, Dose, N = 20){
#set.seed(Seed)
ggConc <- Dataset(Weight, Dose, N) %>%
select(CL, V, Ka, Ke) %>%
mutate(Subject = row_number()) %>%
left_join(expand.grid(
x = seq(1, N, length.out = N), #Subjecti
y = seq(0,24, by = 0.1)) %>% # Time
select(Subject=x, Time=y), by = "Subject") %>%
mutate(Conc = Dose / V * Ka / (Ka - Ke) * (exp(-Ke * Time) - exp(-Ka * Time))) %>%
select(Subject, Time, Conc)
return(ggConc)
caffConcTime <- function(Weight, Dose, N = 20){
#set.seed(Seed)
ggConc <- caffDataset(Weight, Dose, N) %>%
select(CL, V, Ka, Ke) %>%
mutate(Subject = row_number()) %>%
left_join(expand.grid(
x = seq(1, N, length.out = N), #Subjecti
y = seq(0,24, by = 0.1)) %>% # Time
select(Subject=x, Time=y), by = "Subject") %>%
mutate(Conc = Dose / V * Ka / (Ka - Ke) * (exp(-Ke * Time) - exp(-Ka * Time))) %>%
select(Subject, Time, Conc)
return(ggConc)
}


#' Create a dataset of the concentration-time curve of multiple dosing
#' Create a dataset of the concentration-time curve of multiple dosing of caffeine
#'
#' \code{ConcTimeMulti} will create a dataset of the concentration-time curve of multiple dosing
#' \code{caffConcTimeMulti} will create a dataset of the concentration-time curve of multiple oral administrations of caffeine
#'
#' @param Weight Body weight (kg)
#' @param Dose Dose of single caffeine (mg)
Expand All @@ -40,31 +40,33 @@ ConcTime <- function(Weight, Dose, N = 20){
#' @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}
#' caffConcTimeMulti(Weight = 20, Dose = 200, N = 20, Tau = 8, Repeat = 4)
#' caffConcTimeMulti(20, 200)
#' @import dplyr
#' @seealso \url{https://asancpt.github.io/caffsim}

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)
caffConcTimeMulti <- 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 <- caffDataset(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)
}
63 changes: 31 additions & 32 deletions R/Dataset.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#' Create a dataset for simulation of single dose of caffeine
#'
#' \code{Dataset} will create a dataset for simulation of single dose of caffeine
#' \code{caffDataset} will create a dataset for simulation of single dose of caffeine
#'
#' @param Weight Body weight (kg)
#' @param Dose Dose of single caffeine (mg)
#' @param N The number of simulated subjects
#' @return The dataset of pharmacokinetic parameters of subjects after single caffeine dose following multivariate normal
#' @export
#' @examples
#' Dataset(Weight = 20, Dose = 200, N = 20)
#' Dataset(20,500)
#' @seealso \url{http://asancpt.github.io/CaffeineEdison}
#' caffDataset(Weight = 20, Dose = 200, N = 20)
#' caffDataset(20,500)
#' @importFrom mgcv rmvn
#' @import dplyr
#' @seealso \url{https://asancpt.github.io/caffsim}

Dataset <- function(Weight, Dose, N = 20){
MVN <- rmvn(N, CaffMu, CaffSigma);
caffDataset <- function(Weight, Dose, N = 20){
MVN <- mgcv::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
Expand All @@ -31,7 +31,7 @@ Dataset <- function(Weight, Dose, N = 20){

#' Create a dataset for simulation of multiple dose of caffeine
#'
#' \code{DatasetMulti} will create a dataset for simulation of multiple dose of caffeine
#' \code{caffDatasetMulti} will create a dataset for simulation of multiple dose of caffeine
#'
#' @param Weight Body weight (kg)
#' @param Dose Dose of multiple caffeine (mg)
Expand All @@ -40,32 +40,31 @@ Dataset <- function(Weight, Dose, N = 20){
#' @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}
#' caffDatasetMulti(Weight = 20, Dose = 200, N = 20, Tau = 8)
#' caffDatasetMulti(20,500)
#' @importFrom mgcv rmvn
#' @import dplyr
#' @seealso \url{https://asancpt.github.io/caffsim}

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)
caffDatasetMulti <- function(Weight, Dose, N = 20, Tau = 24){
#set.seed(20140523+1)
MVN <- mgcv::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)
}

62 changes: 31 additions & 31 deletions R/Plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
#' @return The concentration-time curve
#' @export
#' @examples
#' Plot(ConcTime(Weight = 20, Dose = 200, N = 20))
#' @seealso \url{http://asancpt.github.io/CaffeineEdison}
#' caffPlot(caffConcTime(Weight = 20, Dose = 200, N = 20))
#' @import dplyr
#' @import ggplot2
#' @seealso \url{https://asancpt.github.io/caffsim}

Plot <- function(ConcTime, log = FALSE){
p <- ggplot(ConcTime, aes(x=Time, y=Conc)) +
xlab("Time (hour)") + ylab("Concentration (mg/L)") +
scale_x_continuous(breaks = seq(from = 0, to = 24, by = 4)) +
#scale_color_gradient2() +
#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)
caffPlot <- function(caffConcTime, log = FALSE){
p <- ggplot(caffConcTime, aes(x=Time, y=Conc)) +
xlab("Time (hour)") + ylab("Concentration (mg/L)") +
scale_x_continuous(breaks = seq(from = 0, to = 24, by = 4)) +
#scale_color_gradient2() +
#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)
}

#' Create concentration-time curve after multiple doses of caffeine
Expand All @@ -37,22 +37,22 @@ Plot <- function(ConcTime, log = FALSE){
#' @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}
#' caffPlotMulti(ConcTimeMulti(Weight = 20, Dose = 200, N = 20, Tau = 8, Repeat = 4))
#' @import dplyr
#' @import ggplot2
#' @seealso \url{https://asancpt.github.io/caffsim}

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)
caffPlotMulti <- function(caffConcTimeMulti, log = FALSE){
p <- ggplot(caffConcTimeMulti, 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)
}
2 changes: 1 addition & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
#' \item{Parameters}{Abbreviated pharmacokinetic parameters}
#' \item{Parameter}{Pharmacokinetic parameters in full name}
#' }
#' @source \url{http://asancpt.github.io/CaffeineEdison}
#' @seealso \url{https://asancpt.github.io/caffsim}

"UnitTable"
Binary file modified R/sysdata.rda
Binary file not shown.
Loading

0 comments on commit 3f446af

Please sign in to comment.