#libraries

library(dplyr)
library(ggplot2)
library(xpose4)
library(tidyr)
library(knitr)

#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"))}

#import data

nelfinavir<-read.csv("C:\\Heller\\PHAR7383\\week12\\nelfinavir.csv",stringsAsFactors = F)

#Exploratory data analysis

#Population Plot Parent
ggplot(data=nelfinavir%>%filter(CMT==2),aes(TIME,DV,group=ID))+
geom_line(size=0.5)+
geom_point(size=1)+
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)")
## 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.

#Individual Plot Parent
ggplot(data=nelfinavir%>%filter(CMT==2),aes(TIME,DV))+
geom_line(size=0.5)+
geom_point(size=1)+
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(ID))

#Population Plot Metab
ggplot(data=nelfinavir%>%filter(CMT==3),aes(TIME,DV,group=ID))+
geom_line(size=0.5)+
geom_point(size=1)+
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)")

#Individual Plot Metab
ggplot(data=nelfinavir%>%filter(CMT==3),aes(TIME,DV))+
geom_line(size=0.5)+
geom_point(size=1)+
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(ID))

#Code1

code1<-xpose.data(1,dir="C:\\Heller\\PHAR7383\\week12")
## 
## Looking for NONMEM table files.
##     Reading C:\Heller\PHAR7383\week12/sdtab1 
##     Reading C:\Heller\PHAR7383\week12/patab1 
##     Reading C:\Heller\PHAR7383\week12/catab1 
##     Reading C:\Heller\PHAR7383\week12/cotab1 
## Table files read.
## 
## Looking for NONMEM simulation table files.
## No simulated table files read.
dv.vs.ipred(code1,type="p",by="CMT")

dv.vs.pred(code1,type="p",by="CMT")

cwres.vs.idv(code1,type="p",by="CMT")

cwres.vs.pred(code1,type="p",by="CMT")

ind.plots(code1,subset="CMT==2")

ind.plots(code1,subset="CMT==3")

ranpar.hist(code1)

#Code2

code2<-xpose.data(2,dir="C:\\Heller\\PHAR7383\\week12")
## 
## Looking for NONMEM table files.
##     Reading C:\Heller\PHAR7383\week12/sdtab2 
##     Reading C:\Heller\PHAR7383\week12/patab2 
##     Reading C:\Heller\PHAR7383\week12/catab2 
##     Reading C:\Heller\PHAR7383\week12/cotab2 
## Table files read.
## 
## Looking for NONMEM simulation table files.
## No simulated table files read.
dv.vs.ipred(code2,type="p",by="CMT")

dv.vs.pred(code2,type="p",by="CMT")

cwres.vs.idv(code2,type="p",by="CMT")

cwres.vs.pred(code2,type="p",by="CMT")

ind.plots(code2,subset="CMT==2")

ind.plots(code2,subset="CMT==3")

ranpar.hist(code2)