# Cox PH #install.packages("survival", dependencies=TRUE) # Now load the dataset into R with the following command: mydataframe <- read.delim("http://www.robin-beaumont.co.uk/virtualclassroom/book2data/survival_collett_p290.dat", header=TRUE) library(survival) names(mydataframe) attach(mydataframe) mydataframe # Now create the cox regression model: my_coxph<- coxph(Surv(time,cens) ~ treat + age, data=mydataframe) summary(my_coxph) ################### # alternatively if you have carried out the previous chapters r code you # will have a Surv object called mysurv from the line: # mysurv <- Surv(time, cens) # so the above formula could be changed to: # my_coxph<- coxph(mysurv ~ treat + age, data=mydataframe) ################### ### Log Likelihood ratio test my_coxphnull <- coxph(formula = mysurv ~ 1, mydataframe) summary(my_coxphnull) my_coxphnull$loglik -2*my_coxphnull$loglik anova(my_coxph) ### showing iteration details fit0 <- coxph(formula = mysurv ~ treat + age, data=mydataframe, iter=0) fit0 # This indicates that the initial values for both betas are set to 0. # To find the beta values for the first iteration we use the iter=1 setting: fit1 <- coxph(formula = mysurv ~ treat + age, data=mydataframe, iter=1) fit1 fit2 <- coxph(formula = mysurv ~ treat + age, data=mydataframe, iter=1, init=fit1$coef) fit2 fit3 <- coxph(formula = mysurv ~ treat + age, data=mydataframe, iter.max = 50) fit3 ## with iteration instead, explained in the chapter: fit_test <- fit0 for (i in 0:my_coxph$iter) { fit_test2 <- coxph(formula = mysurv ~ treat + age, data=mydataframe, iter=1, init=fit_test$coef) cat(fit_test2$loglik[2]," ", fit_test2$loglik[1]," ",fit_test2$loglik[2] - fit_test2$loglik[1]," ", "\n") fit_test <- fit_test2 }