# Fitting a smoothed curve to a noisy data

I have a variable with sales data over time. It is very noisy at a disaggregate level but if you look at it as a whole, you can see a smoothing curve that follows a polynomial pattern. Is there a way to fit such curve and actually generate values of it over time in the dataset?

I tried many different options here including a moving average (which leaves a lot of noise still); I tried lowess smoother in Stata, which is on the opposite side - too neutral and leaves out important curve patterns. I also tried fitting a nonlinear function nl but to no avail.

The original goal of this task is to determine any infection points that the curve can have. In my mind, I am looking for a curve that would run in the middle of all the noise.

• Do you need the smoothed function to be represented as a polynomial, or would you accept any function that smooths out the noise (not necessarily expressed in polynomial form). – Underminer Jun 15 '16 at 15:44
• Have you tried filtering, through the Fourier transform/convolution? – Firebug Jun 15 '16 at 19:28

Since you mention the "polynomial pattern" in your question, try to fit your data using polynomial least squares fitting.

I tried to reproduce your data (more or less) and plotted a third degree least squares fit on the data. The result is in the graph below.

Actually, I used two goniometric functions to generate the data. The period and amplitude of the base cycle can be estimated by the fitted polynomial: half the period is the distance between the two extremes. The amplitude is half of the difference between the two extremes.

The advantage of this approach is that is it very easy to understand, calculate and apply (maybe at the cost of some mathematical inaccuracy).

There are two disadvantages of using a moving average over any fitting approach:

• the moving average is always lagging. This results in shift in the direction of the past data (in the example below: the moving average is above the polynomial fit line; the trend is going down, so the moving average is lagging upwards in this case).
• the moving average moves along with any spike in the data. This results in a more "wobbely" line compared to the fitting approach.

These effects can be clearly seen in the picture below (red = polynomial fitting; black = 20 period moving average).

• Because this is a time series, with an obvious seasonal structure and some serial correlation, caution would seem advisable. In particular a "classic" polynomial least squares fit would have several disadvantages compared to methods that are more suitable to time series or are more robust. – whuber Jun 15 '16 at 21:24
• The OP mentions polynomial-like patterns. Doing a moving polynomial least squares fit is a logical thing to try, I'd say. – agtoever Jun 15 '16 at 21:33
• Possibly--but fitting it via ordinary least squares may be problematic. It certainly would produce incorrect standard errors of the parameters. – whuber Jun 15 '16 at 22:02
• This is being automatically flagged as low quality, probably because it is so short. At present it is more of a comment than an answer by our standards. Can you expand on it? You can also turn it into a comment. – gung Jun 15 '16 at 22:02
• Moving averages are not always lagging. If you use points equally spaced on either side of the point (sometimes called 'central moving average') then the moving average is more representative of the current trend. – Underminer Jun 20 '16 at 12:52

Sounds like you just need to adjust the smoothing parameters (sometimes called bandwidth) to your liking. Either of these methods should be able to be tuned appropriately.

Moving average can be adjusted to account for more points (increasing smoothness).

Similarly, lowess smoothers have a smoothing parameter to increase or decrease smoothness.