#Relevant libraries
library(survival)
library(frailtyEM)
library(survminer)
library(ggplot2)
library(ggfortify)
library(dplyr)
library(tidyverse)
library(ggpubr)
library(stabledist)
library(statmod)
#Importating the data
data=read.csv('bees data.csv')
data
##EXPLORATORY DATA ANALYSIS

#1. Diagnostics of the Cox model
#1.1 Computing a Cox model
bees.cox=coxph(formula=Surv(time,status)~species + treatment,data=data)
bees.cox
#1.2 Testing the Proportional Hazard assumption
test.ph=cox.zph(bees.cox)
test.ph

#2. Kaplan meier curves per cluster vs overall

#2.1 data set with Kaplan-Meier curves per cluster(10 clusters)
sf=survfit(Surv(time,status) ~ strata(Cage), data)
sf
cluster = as.factor(rep(1:20))data[1:3, ]
cluster
dd=data.frame(time = sf$time, surv = sf$surv, cluster = rep(data[1:3, ]data[1:3, ]sf$strata), sf$strata)) 
dd
startpoint=data.frame(center = unique(dd$cluster), time = 0, surv = 1)

dd=rbind(dd,startpoint)

#2.2 data set with overall Kaplan-Meier curve
sf2=survfit(Surv(time,status) ~ 1, data)
sf2
dd2=data.frame(time = sf2$time, surv = sf2$surv) 

#2.3overall and cluster curves combined
dd %>% 
  ggplot(aes(x = time, y = surv)) + 
  geom_step(aes(group = cluster),  show.legend = FALSE, alpha = 0.2) + 
  geom_step(data = dd2) + 
  theme_gray() + 
  labs(x = "Days", y = "Survival")

#3. Fitting marginal cox PH model,gamma, positive stable and inverse gaussian frailty models

#3.1 Computing a Cox model
bees.cox=coxph(formula=Surv(time,status)~species+treatment,data=data)
summary(bees.cox)
#3.2 Gamma frailty model
bees.gamma=emfrail(Surv(time,status)~species+treatment+cluster(cage),data=data,
                   distribution = emfrail_dist(dist = "gamma"))
summary(bees.gamma)
#3.3 Positive stable frailty model
bees.stable=emfrail(Surv(time,status)~species+treatment+cluster(cage),data=data,
                    distribution = emfrail_dist(dist = "stable"))
summary(bees.stable)
#3.4 PVF-Inverse gaussian, tuining parameter m=-0.5
bees.inversegaus=emfrail(Surv(time,status)~species+treatment+cluster(cage),data=data,
                         distribution = emfrail_dist(dist = "pvf",pvfm=-0.5))
summary(bees.inversegaus)

#4. To illustrate the predicted curves for the different models we take two individuals,
#one from the Apis species and one from the Meliponula species  both under treatment icipe 20

#4.1 Gamma frailty model
#4.1.2 Cumulative Hazard
gamma_cumh_plot1=autoplot(bees.gamma, type = "pred",
                          newdata = data.frame(species = "Apis", treatment = "icipe 20")) +
  ggtitle("Apis") + ylim(c(0, 0.5)) + guides(colour = "legend")

gamma_cumh_plot2=autoplot(bees.gamma, type = "pred",
                          newdata = data.frame(species = "Meliponula", treatment = "icipe 20")) +
  ggtitle("Meliponula") + ylim(c(0, 0.5)) + guides(colour = "legend")
gamma_cumh_plot2
#combining graphs
gamma=ggarrange(gamma_cumh_plot1,gamma_cumh_plot2,nrow=1,ncol=2,common.legend = TRUE,legend="bottom")
#adding title
annotate_figure(gamma, top = text_grob("Gamma", 
                                       color = "black", face = "bold", size = 14))
#4.1.3 Hazard ratio
dat_pred=data.frame(species = c("Apis", "Meliponula"),
                    treatment = c("icipe 20", "icipe 20"))
dat_pred
gamma_hr=autoplot(bees.gamma, type = "hr",
                  newdata = dat_pred) + ggtitle("Gamma") +
  ylim(c(1.8, 1.85))+guides(colour = "legend")
gamma_hr

#4.2 Positive stable frailty model
#4.2.1 Cumulative hazard
stable_cumh_plot1=autoplot(bees.stable, type = "pred",
                           newdata = data.frame(species = "Apis", treatment = "icipe 20")) +
  ggtitle("Apis") + ylim(c(0, 0.6)) + guides(colour = "legend")

stable_cumh_plot2=autoplot(bees.stable, type = "pred",
                           newdata = data.frame(species = "Meliponula", treatment = "icipe 20")) +
  ggtitle("Meliponula") + ylim(c(0, 0.6)) + guides(colour = "legend")
#Combining graphs
PS=ggarrange(stable_cumh_plot1,stable_cumh_plot2,nrow=1,ncol=2,common.legend = TRUE,legend="bottom")
#adding title
annotate_figure(PS, top = text_grob("Positive Stable", 
                                    color = "black", face = "bold", size = 14))
#4.2.2 HAZARD RATIO
dat_pred=data.frame(species = c("Apis", "Meliponula"),
                    treatment = c("icipe 20", "icipe 20"))
dat_pred
stable_hr=autoplot(bees.stable, type = "hr",
                   newdata = dat_pred) + ggtitle("Positive Stable") +
  ylim(c(1.8,1.95)) + guides(colour = "legend")
stable_hr

#4.3 Inverse Gaussian frailty model
#4.3.1 Cumulative hazard
IG_cumh_plot1=autoplot(bees.IG, type = "pred",
                       newdata = data.frame(species = "Apis", treatment = "icipe 20")) +
  ggtitle("Apis") + ylim(c(0, 0.6)) + guides(colour = "legend")

IG_cumh_plot2=autoplot(bees.inversegaus, type = "pred",
                       newdata = data.frame(species = "Meliponula", treatment = "icipe 20")) +
  ggtitle("Meliponula") + ylim(c(0, 0.6)) + guides(colour = "legend")
#combining graphs
IG=ggarrange(IG_cumh_plot1,IG_cumh_plot2,nrow=1,ncol=2,common.legend = TRUE,legend="bottom")
#adding main title 
annotate_figure(IG, top = text_grob("Inverse Gaussian", 
                                    color = "black", face = "bold", size = 14))
#4.3.2 Hazard ratio
dat_pred=data.frame(species = c("Apis", "Meliponula"),
                    treatment = c("icipe 20", "icipe 20"))
dat_pred
IG_hr=autoplot(bees.inversegaus, type = "hr",
               newdata = dat_pred) + ggtitle("Inverse Gausssian") + 
  ylim(c(1.8, 1.85)) + guides(colour = "legend")
IG_hr

#5. SIMULATION STUDY
sim_run=function() {
  #number of clusters
  ni=10
  ni
  #clusters size
  size=10
  size
  #Sample size
  n=ni * size
  n
  #covariates
  Xij<- matrix(rbinom(n, 1, 0.5), nrow=1,ncol=n, byrow=TRUE)
  Xij
  #True beta
  B=-0.5
  B
  # generates the frailty 
  zi<-rlnorm(ni,1,2)  
  zi
  # this generates the exponential parameters that includes the frailty and covariate
  lambdaij<-rep(0, ni)
  lambdaij
  for(k in 1:ni) {
    for(j in (1+(k-1)*size):(size+(k-1)*size)){lambdaij[j]<-zi[k]*exp(Xij[,j]%*%B) }}
  lambdaij
  # generate the response variable using the lambdaij
  yi=rexp(n, rate=lambdaij) 
  yi
  # use a transformation to give it a baseline hazard
  time=100*sqrt(yi)   
  time
  clusters = rep(1:ni, each = size)
  clusters
  treatment=Xij[1,]
  treatment
  status=rbinom(n, 1, 0.6)
  status
  id = 1:n
  id
  simudata<-data.frame(id,time,status,treatment,clusters)
  simudata
  
  mod.gamma=emfrail(Surv(time,status)~treatment+cluster(clusters),data=simudata,
                    emfrail_dist(dist = "pvf"))
  summary(mod.gamma)
  
  est=mod.gamma$coefficients
  theta=var(mod.gamma$frail)
  
  
  c(est,theta)
}
set.seed(2020)
simulations=1000
#simulated beta estimates
parameters=replicate(simulations, sim_run())
parameters
###simulated betas
betas_i=parameters[1,]
betas_i
bias_beta=(sum(betas_i)/simulations)-(-0.5)
bias_beta
MSE_beta=(bias_beta)^2+var(betas_i)
MSE_beta
###simulated theta 
theta_i=parameters[2,]
theta_i
bias_theta=(sum(theta_i)/simulations)-(2)
bias_theta
MSE_theta=(bias_theta)^2+var(theta_i)
MSE_theta

