Heller PHAR7383 wk1 In Class pknca_01_18_25 title: “NCA” output: html_document —

#libraries

rm(list=ls())
library(ggplot2)
library(dplyr)
library(PKNCA)
## Warning: package 'PKNCA' was built under R version 4.4.2

#theme

my_theme<-function(x){theme_bw()+
    theme(text = element_text(size=20))+
    theme(axis.line.y = element_line(size = 2.0))+
    theme(axis.line.x = element_line(size = 2.0))+
    theme(axis.ticks = element_line(size = 1.5,colour="black"))+
    theme(axis.ticks.length=  unit(0.45, "cm"))+
    theme(axis.title.y =element_text(vjust=1.2))+
    theme(axis.title.x =element_text(vjust=-0.2))+
    theme(axis.text=element_text(colour="black"))+
    theme(panel.background = element_rect(fill ="white"))}

#multiple dosing, oral dosing

#pkncadt<-read.csv("C:\\Ayyappa\\PHAR7383\\week1\\pkncadt.csv",stringsAsFactors = F)
pkncadt<-read.csv("C:\\Heller\\PHAR7383\\week1\\pkncadt.csv",stringsAsFactors = F)
pkncadt$DOSEC<-factor(pkncadt$DOSE,labels = c("10 mg","25 mg","50 mg","100 mg"))
pkncadtsum<-pkncadt%>%
  group_by(DOSEC,DAY,NTIME,TIME)%>%
  summarise(cmean=mean(DV),stdev=sd(DV))

#multiple dosing, oral dosing, plots

#Population Plot
ggplot(data=pkncadt,aes(NTIME,DV,color=DOSEC,group=ID))+
geom_line(size=0.5)+
scale_x_continuous(limits = c(0,24),breaks = c(0,1,2,4,6,8,12,24))+
theme_bw()+
my_theme()+
labs(x="Time after dose (hour)",y="Plasma concentration (ng/ml)")+
facet_wrap(vars(DAY))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## i Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## i Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#average plot with standard deviation (+/-1SD)
ggplot(data=pkncadtsum,aes(NTIME,cmean,color=DOSEC))+
geom_line(size=0.5)+
geom_point(size=2)+
scale_x_continuous(limits = c(0,24),breaks = c(0,1,2,4,6,8,12,24))+
geom_errorbar(aes(ymin=cmean-stdev, ymax=cmean+stdev), width=.2)+
theme_bw()+
my_theme()+
labs(x="Time after dose (hour)",y="Plasma concentration (ng/ml)")+
facet_wrap(vars(DAY))

#Noncompartmental analysis- Day 1 Data (code modified from: https://cran.r-project.org/web/packages/PKNCA/vignettes/Introduction-and-Usage.html)

## Load the PK concentration data
pkday1 <-pkncadt%>%filter(DAY==1)
## Generate the dosing data
doseday1 <- pkday1%>%filter(NTIME==0)

## Create a concentration object specifying the concentration, time, and
## subject columns.  (Note that any number of grouping levels is
## supported; you are not restricted to just grouping by subject.)
conc_obj <-
  PKNCAconc(
    pkday1,
    DV~NTIME|DOSEC+DOSE+ID
  )
conc_obj
## Formula for concentration:
##  DV ~ NTIME | DOSEC + DOSE + ID
## Data are dense PK.
## With 48 subjects defined in the 'ID' column.
## Nominal time column is not specified.
## 
## First 6 rows of concentration data:
##  ID DAY NTIME TIME     DV DOSE DOSEC exclude volume duration
##   1   1   0.0  0.0 0.0000   10 10 mg    <NA>     NA        0
##   1   1   0.5  0.5 3.0364   10 10 mg    <NA>     NA        0
##   1   1   1.0  1.0 4.5179   10 10 mg    <NA>     NA        0
##   1   1   1.5  1.5 5.7003   10 10 mg    <NA>     NA        0
##   1   1   2.0  2.0 6.8785   10 10 mg    <NA>     NA        0
##   1   1   4.0  4.0 8.8699   10 10 mg    <NA>     NA        0
## Create a dosing object specifying the dose, time, and subject
## columns.  (Note that the grouping factors should be the same as or a
## subset of the grouping factors for concentration, and the grouping
## columns must have the same names between concentration and dose
## objects.)
dose_obj <-
  PKNCAdose(
    doseday1,
    DOSE~NTIME|DOSEC+ID
  )
dose_obj
## Formula for dosing:
##  DOSE ~ NTIME | DOSEC + ID
## Nominal time column is not specified.
## 
## First 6 rows of dosing data:
##  ID DAY NTIME TIME DV DOSE  DOSEC exclude         route duration
##   1   1     0    0  0   10  10 mg    <NA> extravascular        0
##   2   1     0    0  0   25  25 mg    <NA> extravascular        0
##   3   1     0    0  0   50  50 mg    <NA> extravascular        0
##   4   1     0    0  0  100 100 mg    <NA> extravascular        0
##   5   1     0    0  0   10  10 mg    <NA> extravascular        0
##   6   1     0    0  0   25  25 mg    <NA> extravascular        0
## Combine the concentration and dosing information both to
## automatically define the intervals for NCA calculation and provide
## doses for calculations requiring dose.
data_obj <- PKNCAdata(conc_obj, dose_obj)

## Calculate the NCA parameters
results_obj <- pk.nca(data_obj)
results_obj
## $result
## # A tibble: 672 x 8
##    DOSEC  DOSE    ID start   end PPTESTCD             PPORRES exclude
##    <fct> <int> <int> <dbl> <dbl> <chr>                  <dbl> <chr>  
##  1 10 mg    10     1     0    24 auclast             120.     <NA>   
##  2 10 mg    10     1     0   Inf cmax                  8.87   <NA>   
##  3 10 mg    10     1     0   Inf tmax                  4      <NA>   
##  4 10 mg    10     1     0   Inf tlast                24      <NA>   
##  5 10 mg    10     1     0   Inf clast.obs             3.20   <NA>   
##  6 10 mg    10     1     0   Inf lambda.z              0.0414 <NA>   
##  7 10 mg    10     1     0   Inf r.squared             0.993  <NA>   
##  8 10 mg    10     1     0   Inf adj.r.squared         0.986  <NA>   
##  9 10 mg    10     1     0   Inf lambda.z.time.first  10      <NA>   
## 10 10 mg    10     1     0   Inf lambda.z.n.points     3      <NA>   
## # i 662 more rows
## 
## $data
## Formula for concentration:
##  DV ~ NTIME | DOSEC + DOSE + ID
## Data are dense PK.
## With 48 subjects defined in the 'ID' column.
## Nominal time column is not specified.
## 
## First 6 rows of concentration data:
##  ID DAY NTIME TIME     DV DOSE DOSEC exclude volume duration
##   1   1   0.0  0.0 0.0000   10 10 mg    <NA>     NA        0
##   1   1   0.5  0.5 3.0364   10 10 mg    <NA>     NA        0
##   1   1   1.0  1.0 4.5179   10 10 mg    <NA>     NA        0
##   1   1   1.5  1.5 5.7003   10 10 mg    <NA>     NA        0
##   1   1   2.0  2.0 6.8785   10 10 mg    <NA>     NA        0
##   1   1   4.0  4.0 8.8699   10 10 mg    <NA>     NA        0
## Formula for dosing:
##  DOSE ~ NTIME | DOSEC + ID
## Nominal time column is not specified.
## 
## First 6 rows of dosing data:
##  ID DAY NTIME TIME DV DOSE  DOSEC exclude         route duration
##   1   1     0    0  0   10  10 mg    <NA> extravascular        0
##   2   1     0    0  0   25  25 mg    <NA> extravascular        0
##   3   1     0    0  0   50  50 mg    <NA> extravascular        0
##   4   1     0    0  0  100 100 mg    <NA> extravascular        0
##   5   1     0    0  0   10  10 mg    <NA> extravascular        0
##   6   1     0    0  0   25  25 mg    <NA> extravascular        0
## 
## With 96 rows of interval specifications.
## With imputation: NA
## Options changed from default are:
## $adj.r.squared.factor
## [1] 1e-04
## 
## $max.missing
## [1] 0.5
## 
## $auc.method
## [1] "lin up/log down"
## 
## $conc.na
## [1] "drop"
## 
## $conc.blq
## $conc.blq$first
## [1] "keep"
## 
## $conc.blq$middle
## [1] "drop"
## 
## $conc.blq$last
## [1] "keep"
## 
## 
## $first.tmax
## [1] TRUE
## 
## $allow.tmax.in.half.life
## [1] FALSE
## 
## $keep_interval_cols
## NULL
## 
## $min.hl.points
## [1] 3
## 
## $min.span.ratio
## [1] 2
## 
## $max.aucinf.pext
## [1] 20
## 
## $min.hl.r.squared
## [1] 0.9
## 
## $progress
## [1] TRUE
## 
## $tau.choices
## [1] NA
## 
## $single.dose.aucs
##   start end auclast aucall aumclast aumcall aucint.last aucint.last.dose
## 1     0  24    TRUE  FALSE    FALSE   FALSE       FALSE            FALSE
## 2     0 Inf   FALSE  FALSE    FALSE   FALSE       FALSE            FALSE
##   aucint.all aucint.all.dose    c0  cmax  cmin  tmax tlast tfirst clast.obs
## 1      FALSE           FALSE FALSE FALSE FALSE FALSE FALSE  FALSE     FALSE
## 2      FALSE           FALSE FALSE  TRUE FALSE  TRUE FALSE  FALSE     FALSE
##   cl.last cl.all     f mrt.last mrt.iv.last vss.last vss.iv.last   cav
## 1   FALSE  FALSE FALSE    FALSE       FALSE    FALSE       FALSE FALSE
## 2   FALSE  FALSE FALSE    FALSE       FALSE    FALSE       FALSE FALSE
##   cav.int.last cav.int.all ctrough cstart   ptr  tlag deg.fluc swing  ceoi
## 1        FALSE       FALSE   FALSE  FALSE FALSE FALSE    FALSE FALSE FALSE
## 2        FALSE       FALSE   FALSE  FALSE FALSE FALSE    FALSE FALSE FALSE
##   aucabove.predose.all aucabove.trough.all count_conc totdose    ae clr.last
## 1                FALSE               FALSE      FALSE   FALSE FALSE    FALSE
## 2                FALSE               FALSE      FALSE   FALSE FALSE    FALSE
##   clr.obs clr.pred    fe sparse_auclast sparse_auc_se sparse_auc_df time_above
## 1   FALSE    FALSE FALSE          FALSE         FALSE         FALSE      FALSE
## 2   FALSE    FALSE FALSE          FALSE         FALSE         FALSE      FALSE
##   aucivlast aucivall aucivint.last aucivint.all aucivpbextlast aucivpbextall
## 1     FALSE    FALSE         FALSE        FALSE          FALSE         FALSE
## 2     FALSE    FALSE         FALSE        FALSE          FALSE         FALSE
##   aucivpbextint.last aucivpbextint.all half.life r.squared adj.r.squared
## 1              FALSE             FALSE     FALSE     FALSE         FALSE
## 2              FALSE             FALSE      TRUE     FALSE         FALSE
##   lambda.z lambda.z.time.first lambda.z.n.points clast.pred span.ratio
## 1    FALSE               FALSE             FALSE      FALSE      FALSE
## 2    FALSE               FALSE             FALSE      FALSE      FALSE
##   thalf.eff.last thalf.eff.iv.last kel.last kel.iv.last aucinf.obs aucinf.pred
## 1          FALSE             FALSE    FALSE       FALSE      FALSE       FALSE
## 2          FALSE             FALSE    FALSE       FALSE       TRUE       FALSE
##   aumcinf.obs aumcinf.pred aucint.inf.obs aucint.inf.obs.dose aucint.inf.pred
## 1       FALSE        FALSE          FALSE               FALSE           FALSE
## 2       FALSE        FALSE          FALSE               FALSE           FALSE
##   aucint.inf.pred.dose aucivinf.obs aucivinf.pred aucivpbextinf.obs
## 1                FALSE        FALSE         FALSE             FALSE
## 2                FALSE        FALSE         FALSE             FALSE
##   aucivpbextinf.pred aucpext.obs aucpext.pred cl.obs cl.pred mrt.obs mrt.pred
## 1              FALSE       FALSE        FALSE  FALSE   FALSE   FALSE    FALSE
## 2              FALSE       FALSE        FALSE  FALSE   FALSE   FALSE    FALSE
##   mrt.iv.obs mrt.iv.pred mrt.md.obs mrt.md.pred vz.obs vz.pred vss.obs vss.pred
## 1      FALSE       FALSE      FALSE       FALSE  FALSE   FALSE   FALSE    FALSE
## 2      FALSE       FALSE      FALSE       FALSE  FALSE   FALSE   FALSE    FALSE
##   vss.iv.obs vss.iv.pred vss.md.obs vss.md.pred cav.int.inf.obs
## 1      FALSE       FALSE      FALSE       FALSE           FALSE
## 2      FALSE       FALSE      FALSE       FALSE           FALSE
##   cav.int.inf.pred thalf.eff.obs thalf.eff.pred thalf.eff.iv.obs
## 1            FALSE         FALSE          FALSE            FALSE
## 2            FALSE         FALSE          FALSE            FALSE
##   thalf.eff.iv.pred kel.obs kel.pred kel.iv.obs kel.iv.pred auclast.dn
## 1             FALSE   FALSE    FALSE      FALSE       FALSE      FALSE
## 2             FALSE   FALSE    FALSE      FALSE       FALSE      FALSE
##   aucall.dn aucinf.obs.dn aucinf.pred.dn aumclast.dn aumcall.dn aumcinf.obs.dn
## 1     FALSE         FALSE          FALSE       FALSE      FALSE          FALSE
## 2     FALSE         FALSE          FALSE       FALSE      FALSE          FALSE
##   aumcinf.pred.dn cmax.dn cmin.dn clast.obs.dn clast.pred.dn cav.dn ctrough.dn
## 1           FALSE   FALSE   FALSE        FALSE         FALSE  FALSE      FALSE
## 2           FALSE   FALSE   FALSE        FALSE         FALSE  FALSE      FALSE
## 
## 
## $columns
## $columns$exclude
## [1] "exclude"
## 
## 
## attr(,"class")
## [1] "PKNCAresults" "list"        
## attr(,"provenance")
## Provenance hash fd920ce893dea3446fbd9fd1df7e7d1d generated on 2025-01-26 20:14:38.821369 with R version 4.4.1 (2024-06-14 ucrt).

Processing of Day1 results

#day1 results
pkday1_res<-results_obj$result
pkday1_res_sum<-pkday1_res%>%group_by(DOSEC,PPTESTCD)%>%filter(PPTESTCD=="auclast"|PPTESTCD=="cmax"|PPTESTCD=="tmax")%>%
  summarise(Average=mean(PPORRES),CV=sd(PPORRES)*100/mean(PPORRES))
## `summarise()` has grouped output by 'DOSEC'. You can override using the
## `.groups` argument.
#plotting AUClast
ggplot(pkday1_res%>%filter(PPTESTCD=="auclast"), aes(x=DOSEC, y=PPORRES)) +
geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="AUClast (ng*hr/ml)")

#plotting AUCinf
ggplot(pkday1_res%>%filter(PPTESTCD=="aucinf.obs"), aes(x=DOSEC, y=PPORRES)) +
geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="AUCinf (ng*hr/ml)")

#plotting AUClast/DOSE
ggplot(pkday1_res%>%filter(PPTESTCD=="auclast"), aes(x=DOSEC, y=PPORRES/DOSE)) +
  geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="AUClast (ng*hr/ml)")

#plotting AUCinf/DOSE
ggplot(pkday1_res%>%filter(PPTESTCD=="aucinf.obs"), aes(x=DOSEC, y=PPORRES/DOSE)) +
geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="AUCinf (ng*hr/ml)")

#plotting AUClast/DOSE
#plotting cmax/DOSE
#Plotting cmax
ggplot(pkday1_res%>%filter(PPTESTCD=="cmax"), aes(x=DOSEC, y=PPORRES)) +
geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="Cmax (ng/ml)")

#plotting cmax/DOSE
ggplot(pkday1_res%>%filter(PPTESTCD=="cmax"), aes(x=DOSEC, y=PPORRES/DOSE)) +
geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="Cmax (ng/ml)")

#Noncompartmental analysis- Day 14 Data (code modified from: see above ##note by AH See https://cran.r-project.org/web/packages/PKNCA/vignettes/v30-training-session.html#1

## Load the PK concentration data
pkday14 <-pkncadt%>%filter(DAY==14)
## Generate the dosing data
doseday14 <- pkday14%>%filter(NTIME==0)
## Create a concentration object specifying the concentration, time, and
## subject columns.  (Note that any number of grouping levels is
## supported; you are not restricted to just grouping by subject.)
conc14_obj <-
  PKNCAconc(
    pkday14,
    DV~NTIME|DOSEC+DOSE+ID
  )
## Create a dosing object specifying the dose, time, and subject
## columns.  (Note that the grouping factors should be the same as or a
## subset of the grouping factors for concentration, and the grouping
## columns must have the same names between concentration and dose
## objects.)
dose14_obj <-
  PKNCAdose(
    doseday14,
    DOSE~NTIME|DOSEC+ID
  )
## Combine the concentration and dosing information both to
## automatically define the intervals for NCA calculation and provide
## doses for calculations requiring dose.
data14_obj <- PKNCAdata(conc14_obj, dose14_obj)

## Calculate the NCA parameters
results14_obj <- pk.nca(data14_obj)

##extracting parameters from the list
pkday14_res<-results14_obj$result
pkday14_res_sum<-pkday14_res%>%group_by(DOSEC,PPTESTCD)%>%filter(PPTESTCD=="auclast"|PPTESTCD=="cmax"|PPTESTCD=="tmax")%>%
  summarise(Average=mean(PPORRES),CV=sd(PPORRES)*100/mean(PPORRES))
## `summarise()` has grouped output by 'DOSEC'. You can override using the
## `.groups` argument.

Processing of Day14 results

#plotting AUClast
ggplot(pkday14_res%>%filter(PPTESTCD=="auclast"), aes(x=DOSEC, y=PPORRES)) +
geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="AUClast (ng*hr/ml)")

#plotting AUCinf
ggplot(pkday14_res%>%filter(PPTESTCD=="aucinf.obs"), aes(x=DOSEC, y=PPORRES)) +
geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="AUCinf (ng*hr/ml)")

#plotting AUClast/DOSE
ggplot(pkday14_res%>%filter(PPTESTCD=="auclast"), aes(x=factor(DOSE), y=PPORRES/DOSE)) +
  geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="AUClast (ng*hr/ml)")

#plotting AUCinf/DOSE
ggplot(pkday14_res%>%filter(PPTESTCD=="aucinf.obs"), aes(x=DOSEC, y=PPORRES/DOSE)) +
geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="AUCinf (ng*hr/ml)")

#plotting AUClast/DOSE AH - not correct
#plotting cmax
ggplot(pkday14_res%>%filter(PPTESTCD=="cmax"), aes(x=DOSEC, y=PPORRES)) +
geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="Cmax (ng/ml)")

#plotting cmax/DOSE
ggplot(pkday14_res%>%filter(PPTESTCD=="cmax"), aes(x=DOSEC, y=PPORRES/DOSE)) +
geom_boxplot()+
my_theme()+
labs(x="Dose (mg)",y="Cmax (ng/ml)")