Skip to content

Instantly share code, notes, and snippets.

> summary(fit)
Call:
coxph(formula = Surv(time, churn) ~ male + tt(male), data = data,
tt = function(x, t, ...) x * t)
n= 3243, number of events= 1204
coef exp(coef) se(coef) z Pr(>|z|)
male -0.4667783 0.6270191 0.1019114 -4.580 4.64e-06 ***
tt(male) -0.0008827 0.9991177 0.0001251 -7.053 1.75e-12 ***
# Re-model, this time with a time-transform
fit <- coxph(Surv(time, churn) ~ male + tt(male), data = data, tt = function(x, t, ...) x * t)
# Check the results again...
summary(fit)
# Never, ever, ever, ever, ever. Not sure if I have enough ever's memorized.
fit <- coxph(Surv(time, churn) ~ male + male : time, data = data)
# Plot Schoenfield residuals to show the violation...
plot(cox.zph(fit))
> cox.zph(fit)
rho chisq p
male -0.199 51.6 6.64e-13
# Load the survival package
library(survival)
# Read in the CSV
data <- read.csv('C:/users/dbatten/google drive/laptop files/personal/time dependent coefficients/tdc_data.csv')
# Fit a cox model of a survival object on the user's gender
fit <- coxph(Surv(time, churn) ~ male, data = data)
# Have a gander at the results
# Generate a survival fit using our cox results and imaginary customers
plot_fit <- survfit(fit, newdata = new_values, id = id)
# Plot the results!
plot(plot_fit, mark.time = FALSE, col = c('red', 'blue'), ylim = c(.6, 1), xlab = 'Days Since Signing Up', ylab = 'Percent Surviving')
legend(25, .8, legend = c('Contacted Support at Day 500', 'Never Contacted Support'), col = c('red', 'blue'), lty = c(1, 1))
title('Support Interactions and Churn')
# We'll have two "chunks" for customer 1.
# One for customer 2
id <- c(1, 1, 2)
# Customer 1's first chunk starts at day 0,
# his second at day 500. Customer 2 starts at day 0.
start_time <- c(0, 500, 0)
# Customer 1's first chunk ends at day 499.
#Both customer's final chunks end at day 1000.
> summary(fit)
Call:
coxph(formula = Surv(start_time, end_time, churned) ~ contacted_support,
data = data)
n= 2000, number of events= 150
coef exp(coef) se(coef) z Pr(>|z|)
contacted_support 0.6389 1.8943 0.1841 3.47 0.000521 ***
---
# Load the survival package
library(survival)
# Read in the CSV
data <- read.csv('C:/users/dbatten/desktop/time_varying_covariate.csv')
# Fit a cox model of a survival object on whether or not the user contacted support
fit <- coxph(Surv(start_time, end_time, churned) ~ contacted_support, data = data)
# Have a gander at the results