# How to do regression on a time series by learning from historical time series?

I have a data set of customer purchases from the day of their registration to 120 days. There is a time series for each customer. However, some new customers do not have a history of 120 days yet. I want to predict how many purchases they will do by the time their history reaches 120 days.

I have created a feature set including frequency of purchase, recency and monetary, and product category (https://en.wikipedia.org/wiki/RFM_(customer_value)).

How can I train the model from the time series to make a regression for each customer?

• It seems you already have a good start. You have the variables and you have the model (a regression), so what is missing? I suppose you "train" your model by estimating it; is there a problem there? What could be improved upon is accounting for seasonality and allowing for time series patterns in model residuals by fitting regression with ARMA errors instead of plain regression. This can be done with functions arima ("stats" package) or auto.arima ("forecast" package) in R. – Richard Hardy Jul 30 '16 at 12:40
• (I edited your question a bit. Please check whether I have not changed the meaning. You may roll back or edit further.) – Richard Hardy Jul 30 '16 at 12:46

How can I train the model from the time series to make a regression for each customer?

There is no need to do regression for every customer. You just need one model to do everything. You can construct the training data as following:

First, select the same time period for every customer (you can choose those customers who purchased at least 120 days)

Second, do some features engineering, like last month purchased moving average, days of week, weekend, holiday, and so on

Finally, you will arrange the training data like this: Customer-Id Purchased timestamp days-of-week moving-average-terms ...

Then, you can do some regression on it, but i will suggest you do something feature selection using tree based models.

1) Here's an approach that will work if you want predictions for days other than the 120th in addition to working for the 120th. If you want to do a true time series regression, you need features to account for trend and seasonality (this essentially acts as the "differencing" you'd need to do if you were making a non-stationary time series stationary).

To do this, add a feature "customer_age_in_days," where you index each and every customer's activity by the # of days since his/her first activity. If a customer starts on 1/1/12, his age in days on 1/2/12 should be 2 (don't zero-index - it could mess things up). If another customer starts on 1/7/14, his age in days on 1/9/14 is 3.

Then, graph this time feature versus your dependent variable (# of purchases) and see what the trend looks like - it might not be linear. Play around with what transformations it might follow - sqrt, log, square, cube, etc.). Could even be a combination of some.

For seasonality, add dummy variables for which day of the week it is. isMon, isTues...isFri where the variable = 1 if it is that day of the week, and 0 if it is not. Delete the one with the least correlation with your dependent variable so as to avoid perfect multicollinearity.

You can then run a regression with customer_age_in_days and your isMon-isFri variables, along with your other features. To get your prediction, put in the data that corresponds to the 120th day.

2) You could do a regression independent of the continuous approach described above if you just want the 120th day. You could just have a lot of other features as the ones you described, and have your dependent variable still being the # of purchases they made by day 120. Then, you just regress on all these other features without having time or seasonality as features. You could add lagged features such as "# of purchases by day x" for x in [10, 20, ...]. The limitation is that x would have to be less than or equal to the minimum age in days of all your customers (since if one customer is 40 days old, and you have a feature of "purchases by day 50," that column will be NaN for that customer and mess everything up.

3) Do a traditional time series. auto.arima() is good, and you could look into Facebook Prophet as well.