Answer To: Assignment 3 Survival Analysis SVA 2020 Due date: 11.55pm 18 May 2020 Survival Analysis Assignment...
Bezawada Arun answered on May 23 2021
#installing the required packages
install.packages("survminer")
library(dplyr)
library(survival)
library(survminer)
#reading the dataset
framing <- read.csv('framing3.csv')
head(framing)
#Exploratory Data Analytics
miss <- function(x) return(sum(is.na(x)))
t(data.frame(apply(framing,2,miss)))
mi_re <- function(x,fuc)
{
y = x[!is.na(x)]
if(fuc=='mode')
{
m = unique(y)[which.max(tabulate(match(y,unique(y))))]
x = ifelse(is.na(x)==TRUE,m,x)
return(x)
}else if(fuc=='mean')
{
x = ifelse(is.na(x)==TRUE,mean(y),x)
return(x)
}
}
#passing connection to replace null values with mean and mode
framing1 = framing %>% mutate(totchol = mi_re(totchol,'mean'),
age = mi_re(age,'mean'),
cursmoke1 = mi_re(cursmoke1,'mode'),
cursmoke2 = mi_re(cursmoke2,'mode'),
time1 = rep(0.01,nrow(framing)),
time2 = mi_re(time2,'mean'),
cursmoke3 = mi_re(cursmoke3,'mode'),
time3 = mi_re(time3,'mean'))
head(framing1)
# ---------------------- 1.A ---------------------------------------------
#Building the model for time period 1
model.cox1 <- coxph(Surv(time1,cursmoke1)~age+sex+totchol,data = framing1)
model.cox1
summary(model.cox1)
survfit(model.cox1)
#Building model for time period 2
model.cox2 <- coxph(Surv(time2,cursmoke2)~age+sex+totchol,data = framing1)
model.cox2
summary(model.cox2)
survfit(model.cox2)
#Building model for time period 3
model.cox3 <- coxph(Surv(time3,cursmoke3)~age+sex+totchol,data =...