# General approach on time series for customer retention/churn in retail

I have a time series of data in the following form:

| purchase_date    |    cutomer_id  |   num_purchases | churned |
2018-10-31            id1              39             0
2018-11-31            id1              0              0
2019-01-31            id1              6              1
2019-03-31            id2              300            0
2018-04-31            id2               2             1
...


I grouped the data by month and summed num_purchases by month. The churned column for user id1 for example represents in which month customer churned. So id1 in my case churned in January. Before this, to label who has churned or not, we sampled customers based on 2 months of inactivity period from the churn date. I need to predict if a user is going to churn in a 2 months from now. I am not sure what is the best approach for this.

• Q1: Should I be grouping customers like I am doing, on a monthly basis or I have to group them on a 2-month basis since that is how they were labeled?
• Q2: Also, how do I model this? Do I keep customer_id as a feature of the model or not? Is the gap in dates for each customer relevant and how should I deal with it (if)? The dates repeat for different users, should I create index out of a date but it won't be unique or should I create index out of customer_id?
• Q3: If I need to predict whether the user is going to churn by the end of the year for example or in the next 6 months, would that change how I group/arrange my date and model this?

I plan to add more features to this dataframe (both categorical and numerical).

• If anyone could point me in the right direction, that would be great. If not, tell me if I should add more information or post this in some other forum. – Michael Oct 8 '19 at 13:54

OK, let me try to dissect your questions:

Q1: Should I be grouping customers like I am doing, on a monthly basis or I have to group them on a 2-month basis since that is how they were labeled?

This depends a bit on what your goal is and what kind of features you want to use. I think, with the limited things I know from your question, I would go for a daily prognosis for the next two months (so if you're doing daily you can forecast a period of two months daily data for each day). Reason being that otherwise you might loose some inactivity "features" that are implicitly part of your data. As an alternative you can group your data and create features that help you introduce the information anyway. For example: activity_in_last_5d, activity_in_last_10d, etc.

Q2: Also, how do I model this? Do I keep customer_id as a feature of the model or not?

Customer_id doesn't seem to be a feature to me but rather the key for identifying which features to feed into your model.

Is the gap in dates for each customer relevant and how should I deal with it (if)?

You are basically asking about imputing your data (see this for more: https://towardsdatascience.com/6-different-ways-to-compensate-for-missing-values-data-imputation-with-examples-6022d9ca0779). Gaps can cause problems in your modeling. Some models (for example ARIMA for time series) won't work at all if you have gaps that aren't handled. Looking at your use case, I think taking the last known value for a gap should work fine since a gap means your customer didn't churn on that day.

The dates repeat for different users, should I create index out of a date but it won't be unique or should I create index out of customer_id?

Probably go for indexing the customer_id and creating features based on the date! Maybe there are some seasonality features interesting as well (for example how many people churned on the same day last year?)

Q3: If I need to predict whether the user is going to churn by the end of the year for example or in the next 6 months, would that change how I group/arrange my date and model this?

You would have to change the size of the period you are predicting for. Changing features, grouping, etc. might be useful as well but that really depends on what features you are choosing aka are important and how your model is going to work.

• Awesome, thanks! How do I know which models won't work with gaps as ARIMA that you mentioned? Also, if I index by customer_id, I will have repeating ids. Is that ok, or do they need to be unique (which means I need to group by customer_id)? – Michael Oct 8 '19 at 15:21
• Can I use LogisticRegression or RandomForestClassifier or similar for this or it has to be time series model like ARIMA? – Michael Oct 8 '19 at 15:43
• You'll have to read about what your model does, what prerequisites does it have (some have prerequisites about the residuals, some about format of data, etc.) and what their use and limitations are to get all out of the algorithm. I wouldn't group...repeating id's should be fine. Although hard to say without doing the project myself. Doesn't have to be time series...especially ARIMA would solve a regression problem not a classification one like yours. LogisticRegression and RF should work fine. – Philipp Oct 9 '19 at 17:14
• Thanks. If you have any good links or books on the similar problem as mine, I would appreciate it. Everything I find online is either too simple or with a clean already generated features and data. Nobody seem to struggle with this, it all looks so easy in articles I find online. – Michael Oct 10 '19 at 10:00