Kaplan-Meier Analysis
In this exercise you are going to practice Kaplan-Meier Analysis - without and with a categorical covariate.
The survival
package is loaded to your workspace. Also, the survival object survObj
and your data dataNextOrder
are still in the environment. But now, the data contains an additional covariate called voucher
, which you will need in this exercise. This categorical variable tells you if the customer used a voucher in her first order. It contains the value 0 or 1.
This exercise is part of the course
Machine Learning for Marketing Analytics in R
Exercise instructions
- Compute a Kaplan-Meier Analysis (without covariates) using
survfit()
. Store the result in an object calledfitKMSimple
. Remember, the dependent variable (variable to the left of the tilde~
) is again your survival objectsurvObj
. Then, printfitKMSimple
. - Plot the result object
fitKMSimple
and add axis labels (xlab
andylab
arguments). - Now go a step further: Compute a Kaplan-Meier Analysis with the
survObj
as dependent variable and the variablevoucher
as covariate. Don't forget to specify thedata
argument. - Again, plot the result of the new model and add axis labels.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Compute and print fit
fitKMSimple <- ___(___ ~ ___)
___(fitKMSimple)
# Plot fit
plot(___,
conf.int = FALSE, ___ = "Time since first purchase", ___ = "Survival function", main = "Survival function")
# Compute fit with categorical covariate
fitKMCov <- survfit(___ ~ ___, data = ___)
# Plot fit with covariate and add labels
plot(___, lty = 2:3,
___ = "Time since first purchase", ___ = "Survival function", main = "Survival function")
legend(90, .9, c("No", "Yes"), lty = 2:3)