Reputation: 75
I have a data by ID by month, and for each month I have a customer churn probability something like below.
How can I get a churn probability each month conditioned on survival probability of previous month? For example for ID 6800
, in the month ending 4/30
I have 0.54
probability, so survival will be (1-0.54)=0.46
. Hence for month 5/31
I would like probability as 0.46*0.125
. For month 6/30 I should have (1-0.46*0.125)*0.081440443
. I want to do this for every ID for many months.
Is there a automated way I can set this up in a package or something?
ID Date ChurProb cash
1 6800 04/30/2017 0.541440443 35019.86
2 6800 05/31/2017 0.125103719 35047.55
3 6800 06/30/2017 0.081440443 15124.00
4 6801 04/30/2017 0.541440443 35019.86
5 6801 05/31/2017 0.125103719 35047.55
6 6801 06/30/2017 0.081440443 15124.00
Upvotes: 1
Views: 54
Reputation: 7248
I looked for a more elegant solution, but came up empty. Here's a loop that gets the job done:
calc_cond_churn <- function (dta) {
last_id <- -1
last_survival <- 1
CondChurn <- rep(0, nrow(dta))
for (i in 1:nrow(dta)) {
if (dta$ID[i] != last_id) {
last_survival <- 1
last_id <- dta$ID[i]
}
CondChurn[i] <- dta$ChurProb[i] * last_survival
last_survival <- 1 - CondChurn[i]
}
CondChurn
}
You would use it as:
df$ConditionalChurn <- calc_cond_churn(df)
Upvotes: 1