Heller title: “PKNCA PHAR7383 Assignment Wk1 01-20-25” 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)
pkddirtv<-read.csv ("C:\\Heller\\PHAR7383\\week1\\pkddirtv.csv",stringsAsFactors = F)
##pkncadt$DOSEC<-factor(pkncadt$DOSE,labels = c("10 mg","25 mg","50 mg","100 mg"))
pkddirtv$GRPC<-factor(pkddirtv$GRP,labels = c("UNT234+RTV", "UNT234"))
#pkncadtsum<-pkncadt%>%
  #group_by(DOSEC,DAY,NTIME,TIME)%>%
  #summarise(cmean=mean(DV),stdev=sd(DV))
pkddirtv.sum<-pkddirtv%>%
  group_by(GRPC,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))+
ggplot(data=pkddirtv, aes(NTIME,DV,color=GRPC,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=pkddirtv.sum,aes(NTIME,cmean,color=GRPC))+
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)
pkday1<-pkddirtv%>%filter(DAY==1)
## Generate the dosing data
#doseday1 <- pkday1%>%filter(NTIME==0)
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<-
  PKNCAconc(
    pkday1,
    DV~NTIME|GRPC+GRP+ID
  )
conc_obj
## Formula for concentration:
##  DV ~ NTIME | GRPC + GRP + ID
## Data are dense PK.
## With 24 subjects defined in the 'ID' column.
## Nominal time column is not specified.
## 
## First 6 rows of concentration data:
##  ID DAY NTIME TIME     DV GRP       GRPC DOSE exclude volume duration
##   1   1   0.0  0.0 0.0000   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   0.5  0.5 3.1583   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   1.0  1.0 4.8430   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   1.5  1.5 5.9578   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   2.0  2.0 6.8181   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   4.0  4.0 7.9023   1 UNT234+RTV   10    <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<-
  PKNCAdose(
    doseday1,
    GRP~NTIME|GRPC+ID
  )
dose_obj
## Formula for dosing:
##  GRP ~ NTIME | GRPC + ID
## Nominal time column is not specified.
## 
## First 6 rows of dosing data:
##  ID DAY NTIME TIME DV GRP       GRPC DOSE exclude         route duration
##   1   1     0    0  0   1 UNT234+RTV   10    <NA> extravascular        0
##   2   1     0    0  0   2     UNT234   10    <NA> extravascular        0
##   3   1     0    0  0   1 UNT234+RTV   10    <NA> extravascular        0
##   4   1     0    0  0   2     UNT234   10    <NA> extravascular        0
##   5   1     0    0  0   1 UNT234+RTV   10    <NA> extravascular        0
##   6   1     0    0  0   2     UNT234   10    <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)
data_obj
## Formula for concentration:
##  DV ~ NTIME | GRPC + GRP + ID
## Data are dense PK.
## With 24 subjects defined in the 'ID' column.
## Nominal time column is not specified.
## 
## First 6 rows of concentration data:
##  ID DAY NTIME TIME     DV GRP       GRPC DOSE exclude volume duration
##   1   1   0.0  0.0 0.0000   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   0.5  0.5 3.1583   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   1.0  1.0 4.8430   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   1.5  1.5 5.9578   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   2.0  2.0 6.8181   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   4.0  4.0 7.9023   1 UNT234+RTV   10    <NA>     NA        0
## Formula for dosing:
##  GRP ~ NTIME | GRPC + ID
## Nominal time column is not specified.
## 
## First 6 rows of dosing data:
##  ID DAY NTIME TIME DV GRP       GRPC DOSE exclude         route duration
##   1   1     0    0  0   1 UNT234+RTV   10    <NA> extravascular        0
##   2   1     0    0  0   2     UNT234   10    <NA> extravascular        0
##   3   1     0    0  0   1 UNT234+RTV   10    <NA> extravascular        0
##   4   1     0    0  0   2     UNT234   10    <NA> extravascular        0
##   5   1     0    0  0   1 UNT234+RTV   10    <NA> extravascular        0
##   6   1     0    0  0   2     UNT234   10    <NA> extravascular        0
## 
## With 48 rows of interval specifications.
## With imputation: NA
## No options are set differently than default.
## Calculate the NCA parameters
results_obj <- pk.nca(data_obj)
results_obj
## $result
## # A tibble: 336 x 8
##    GRPC         GRP    ID start   end PPTESTCD             PPORRES exclude
##    <fct>      <int> <int> <dbl> <dbl> <chr>                  <dbl> <chr>  
##  1 UNT234+RTV     1     1     0    24 auclast             119.     <NA>   
##  2 UNT234+RTV     1     1     0   Inf cmax                  7.90   <NA>   
##  3 UNT234+RTV     1     1     0   Inf tmax                  4      <NA>   
##  4 UNT234+RTV     1     1     0   Inf tlast                24      <NA>   
##  5 UNT234+RTV     1     1     0   Inf clast.obs             3.17   <NA>   
##  6 UNT234+RTV     1     1     0   Inf lambda.z              0.0399 <NA>   
##  7 UNT234+RTV     1     1     0   Inf r.squared             0.998  <NA>   
##  8 UNT234+RTV     1     1     0   Inf adj.r.squared         0.996  <NA>   
##  9 UNT234+RTV     1     1     0   Inf lambda.z.time.first  10      <NA>   
## 10 UNT234+RTV     1     1     0   Inf lambda.z.n.points     3      <NA>   
## # i 326 more rows
## 
## $data
## Formula for concentration:
##  DV ~ NTIME | GRPC + GRP + ID
## Data are dense PK.
## With 24 subjects defined in the 'ID' column.
## Nominal time column is not specified.
## 
## First 6 rows of concentration data:
##  ID DAY NTIME TIME     DV GRP       GRPC DOSE exclude volume duration
##   1   1   0.0  0.0 0.0000   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   0.5  0.5 3.1583   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   1.0  1.0 4.8430   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   1.5  1.5 5.9578   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   2.0  2.0 6.8181   1 UNT234+RTV   10    <NA>     NA        0
##   1   1   4.0  4.0 7.9023   1 UNT234+RTV   10    <NA>     NA        0
## Formula for dosing:
##  GRP ~ NTIME | GRPC + ID
## Nominal time column is not specified.
## 
## First 6 rows of dosing data:
##  ID DAY NTIME TIME DV GRP       GRPC DOSE exclude         route duration
##   1   1     0    0  0   1 UNT234+RTV   10    <NA> extravascular        0
##   2   1     0    0  0   2     UNT234   10    <NA> extravascular        0
##   3   1     0    0  0   1 UNT234+RTV   10    <NA> extravascular        0
##   4   1     0    0  0   2     UNT234   10    <NA> extravascular        0
##   5   1     0    0  0   1 UNT234+RTV   10    <NA> extravascular        0
##   6   1     0    0  0   2     UNT234   10    <NA> extravascular        0
## 
## With 48 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 3e9ea536618cb659e3b3ec30c0e27eb2 generated on 2025-01-20 19:21:27.735524 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"|PP#TESTCD=="tmax")%>%
#summarise(Average=mean(PPORRES),CV=sd(PPORRES)*100/mean(PPORRES))
pkday1_res_sum<-pkday1_res%>%group_by(GRPC,PPTESTCD)%>%filter(PPTESTCD=="auclast"|PPTESTCD=="cmax"|PPTESTCD=="tmax")%>%
summarise(Average=mean(PPORRES),CV=sd(PPORRES)*100/mean(PPORRES))
## `summarise()` has grouped output by 'GRPC'. You can override using the
## `.groups` argument.
#plotting AUClast
ggplot(pkday1_res%>%filter(PPTESTCD=="auclast"), aes(x=GRPC, y=PPORRES)) +
geom_boxplot()+
my_theme()+
labs(x="GROUP",y="Day 1 AUClast (ng*hr/ml)")

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

#plotting AUClast/DOSE
##ggplot(pkday1_res%>%filter(PPTESTCD=="auclast"), aes(x=GRPC, y=PPORRES)) +
 # geom_boxplot()+
#my_theme()+
#labs(x="GROUP",y="Day 1 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 cmax corrected by ahh
ggplot(pkday1_res%>%filter(PPTESTCD=="cmax"), aes(x=GRPC, y=PPORRES)) +
geom_boxplot()+
my_theme()+
labs(x="GROUP",y="day 1 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: https://cran.r-project.org/web/packages/PKNCA/vignettes/Introduction-and-Usage.html)

## Load the PK concentration data
pkday14 <-pkddirtv%>%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|GRPC+GRP+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,
    GRP~NTIME|GRPC+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(GRPC,PPTESTCD)%>%filter(PPTESTCD=="auclast"|PPTESTCD=="cmax"|PPTESTCD=="tmax")%>%
  summarise(Average=mean(PPORRES),CV=sd(PPORRES)*100/mean(PPORRES))
## `summarise()` has grouped output by 'GRPC'. You can override using the
## `.groups` argument.

Processing of Day14 results

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

#plotting AUCinf
ggplot(pkday14_res%>%filter(PPTESTCD=="aucinf.obs"), aes(x=GRPC, y=PPORRES)) +
geom_boxplot()+
my_theme()+
labs(x="GROUP",y="Day 14 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 cmax 
ggplot(pkday14_res%>%filter(PPTESTCD=="cmax"), aes(x=GRPC, y=PPORRES)) +
geom_boxplot()+
my_theme()+
labs(x="GROUP",y="Day 14 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)")