# Dealing with spikes in data

A company sells chocolates. Demand is recorded weekly. The future demand is estimated using the sales for every week in the previous 3 years. But the sales pattern is corrupted by promotions that have been run by the marketing department from time to time. Typically such promotions last 2 weeks and result in temporary spurts in sales.

The objective is to remove and smooth such spikes that occur (spread over 2 weeks or less).

The possible ways I can think of are KDE (Kernel Density Approximation) and LWR . What should be the best approach?

• Should the excursions with length $8$ or $9$ weeks be filtered out so that the signal would be nearly here piecewise constant with level $\approx 3$ for time $\le 75$ and $5$ after? Maybe you could tell when promotions occured or even better show them with vertical lines/bands or rugs.
– Yves
May 1, 2015 at 14:52
• no , only remove if length is 2 weeks or less.. May 2, 2015 at 3:50
– Tim
Sep 25, 2017 at 19:39

Intervention analysis via autoregressive and moving-average time series models is suited for this kind of analysis. This allows you to measure the effect of an intervention at a know time point (e.g. marketing campaign, policy change,...). The answer to this post given by @forecaster gives a good introduction. In that and other answers you can see some illustrations.

Different patterns can be considered for the intervention effect. The intervention may have a permanent or a transitory effect or affect just one observation. See for example this post.

• Thanks for the answer. I am using python, is there any library similar to tsoutliers of R ? I am just wandering over different links and am yet to figure out :( May 2, 2015 at 3:54
• @Sword I don't know about similar packages in python. As a possible solution, I have heard about the Rpy module. It seems that Rpy can be used to call R functions from python, but I have no experience with it. May 2, 2015 at 7:22
• Actually it is just a small model I am creating. So I have installed R and am going with it for now. I have had some experience with R before, could you let me know what kind of an input would be suitable for the date? A Timestamp, or just an integer for no of weeks? May 2, 2015 at 10:36
• @Sword The package zoo provides a flexible interface for indexing time series. If you are going to use the package tsoutliers, be aware that it requires as input a ts object (not a zoo object). Assuming you have 52 observations each year (you may need to do some arrangement with leap years), you can define a ts object related to a weekly time series as follows ts(x, frequency=x), where x is a numeric vector containing the data. May 2, 2015 at 13:34
• @Sword Here you can find another example of an intervention analysis. May 2, 2015 at 13:34

You have use holtz winters forecast where you can use the seasonality and the trend component and also weigh more attention to the recent data thus giving you accurate forecast.

• The Holt and Winters filter can be used to get an overall trend or cyclical components. But how would you deal with specific events like promotions, as mentioned by the OP? I don't think it would be straightforward to adapt this filter and use it to smooth the effect of this kind of events. May 1, 2015 at 12:27