Skip to content

Instantly share code, notes, and snippets.

@mevers
Created February 20, 2018 11:40
Show Gist options
  • Save mevers/2896db9a87c52e6c6fe1cf8fe0ccac78 to your computer and use it in GitHub Desktop.
Save mevers/2896db9a87c52e6c6fe1cf8fe0ccac78 to your computer and use it in GitHub Desktop.
Cox PH with censored data
# Data from Stata manual, p.5:
# "We wish to analyze an experiment testing the ability of emergency
# generators with a new-style bearing to withstand overloads. For this
# experiment, the overload protection circuit was disabled, and
#the generators were run overloaded until they burned up. "
# https://www.stata.com/manuals13/ststcox.pdf
df <- read.table(text =
"failtime load bearings
100 15 0
140 15 1
97 20 0
122 20 1
84 25 0
100 25 1
54 30 0
52 30 1
40 35 0
55 35 1
22 40 0
30 40 1", header = T)
# Add zero time and event status
library(tidyverse);
df <- df %>%
mutate(time = 0, event = TRUE)
# Run Cox PH model
library(survival)
coxph(Surv(time = time, time2 = failtime, event = event) ~ load + bearings, data = df);
#Call:
#coxph(formula = Surv(time = time, time2 = failtime, event = event) ~
# load + bearings, data = df)
#
# coef exp(coef) se(coef) z p
#load 0.4309 1.5386 0.1444 2.98 0.0028
#bearings -2.8107 0.0602 1.1782 -2.39 0.0171
#
#Likelihood ratio test=24 on 2 df, p=6.09e-06
#n= 12, number of events= 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment