<div></div>
<p>Time series data is an important area of analysis, especially if you do a lot of web analytics. To be able to analyse time series effectively, it helps to understand how the interaction between general seasonality in activity and the underlying trend.</p>
<p>The interactions between trend and seasonality are typically classified as either additive or multiplicative. This post looks at how we can classify a given time series as one or the other to facilitate further processing.</p>
<h2>Additive or multiplicative?</h2>
<p>It’s important to understand what the difference between a multiplicative time series and an additive one before we go any further.</p>
<p>There are three components to a time series:<br>
– <strong>trend</strong> how things are overall changing<br>
– <strong>seasonality</strong> how things change within a given period e.g. a year, month, week, day<br>
– <strong>error</strong>/<strong>residual</strong>/<strong>irregular</strong> activity not explained by the trend or the seasonal value</p>
<p>How these three components interact determines the difference between a multiplicative and an additive time series.</p>
<p>In a multiplicative time series, the components multiply together to make the time series. If you have an increasing trend, the amplitude of seasonal activity increases. Everything becomes more exaggerated. This is common when you’re looking at web traffic.</p>
<p>In an additive time series, the components added together to make the time series.If you have an increasing trend, you still see roughly the same size peaks and troughs. This is often seen in indexed time series where the absolute value is growing but changes stay relative.</p>
<blockquote><p>
You can have a time series that is somewhere in between the two, a statistician’s “it depends”, but I’m interested in attaining a quick classification so I won’t be handling this complication here.
</p>
</blockquote>
<h2>There’s a package for that</h2>
<p>When I first started doing time series analysis, the only way to visualise how a time series splits into different components was to use base R. About the time I was feeling the pain, someone released a ggplot2 time series extension! I’ll be using <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=ggseas">ggseas</a> where I can.</p>
<p>We’ll use the <code>nzbop</code> data set from ggseas to, first of all, examine a single time series and then process all the time series in the dataset to determine if they’re multiplicative or additive.</p>
<pre><code>sample_ts&lt;-nzdata[Account == &quot;Current account&quot; &amp; Category==&quot;Services; Exports total&quot;,
                  .(TimePeriod, Value)]
</code></pre>
<table>
<thead>
<tr>
<th align="left">TimePeriod</th>
<th align="right">Value</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">1971-06-30</td>
<td align="right">55</td>
</tr>
<tr>
<td align="left">1971-09-30</td>
<td align="right">56</td>
</tr>
<tr>
<td align="left">1971-12-31</td>
<td align="right">60</td>
</tr>
<tr>
<td align="left">1972-03-31</td>
<td align="right">65</td>
</tr>
<tr>
<td align="left">1972-06-30</td>
<td align="right">65</td>
</tr>
<tr>
<td align="left">1972-09-30</td>
<td align="right">63</td>
</tr>
</tbody>
</table>
<blockquote><p>
I’ll be using other packages (like data.table) and will only show relevant code snippets as I go along. You can get the whole script in a <a target="_blank" rel="nofollow" href="https://gist.github.com/stephlocke/682f618e36b07945aa48871e0563b3fa">GIST</a>.
</p>
</blockquote>
<h2>Decomposing the data</h2>
<p>To be able to determine if the time series is additive or multiplicative, the time series has to be split into its components.</p>
<blockquote><p>
Existing functions to decompose the time series include <code>decompose()</code>, which allows you pass whether the series is multiplicative or not, and <code>stl()</code>, which is <a target="_blank" rel="nofollow" href="https://www.otexts.org/fpp/6/5">only for additive series without transforming the data</a>. I could use <code>stl()</code> with a multiplicative series if I transform the time series by taking the log. For either function, I need to know whether it’s additive or multiplicative first.
</p>
</blockquote>
<h3>The trend</h3>
<p>The first component to extract is the trend. There are a number of ways you can do this, and some of the simplest ways involve calculating a moving average or median.</p>
<pre><code>sample_ts[,trend := zoo::rollmean(Value, 8, fill=NA, align = &quot;right&quot;)]
</code></pre>
<table>
<thead>
<tr>
<th align="left">TimePeriod</th>
<th align="right">Value</th>
<th align="right">trend</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">2014-03-31</td>
<td align="right">5212</td>
<td align="right">4108.625</td>
</tr>
<tr>
<td align="left">2014-06-30</td>
<td align="right">3774</td>
<td align="right">4121.750</td>
</tr>
<tr>
<td align="left">2014-09-30</td>
<td align="right">3698</td>
<td align="right">4145.500</td>
</tr>
<tr>
<td align="left">2014-12-31</td>
<td align="right">4752</td>
<td align="right">4236.375</td>
</tr>
<tr>
<td align="left">2015-03-31</td>
<td align="right">6154</td>
<td align="right">4376.500</td>
</tr>
<tr>
<td align="left">2015-06-30</td>
<td align="right">4543</td>
<td align="right">4478.875</td>
</tr>
</tbody>
</table>
<blockquote><p>
A moving median is less sensitive to outliers than a moving mean. It doesn’t work well though if you have a time series that includes periods of inactivity. Lots of 0s can result in very weird trends.
</p>
</blockquote>
<h3>The seasonality</h3>
<p>Seasonality will be cyclical patterns that occur in our time series once the data has had trend removed.</p>
<p>Of course, the way to de-trend the data needs to additive or multiplicative depending on what type your time series is. Since we don’t know the type of time series at this point, we’ll do both.</p>
<pre><code>sample_ts[,`:=`( detrended_a = Value - trend,  detrended_m = Value / trend )]
</code></pre>
<table>
<thead>
<tr>
<th align="left">TimePeriod</th>
<th align="right">Value</th>
<th align="right">trend</th>
<th align="right">detrended_a</th>
<th align="right">detrended_m</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">2014-03-31</td>
<td align="right">5212</td>
<td align="right">4108.625</td>
<td align="right">1103.375</td>
<td align="right">1.2685509</td>
</tr>
<tr>
<td align="left">2014-06-30</td>
<td align="right">3774</td>
<td align="right">4121.750</td>
<td align="right">-347.750</td>
<td align="right">0.9156305</td>
</tr>
<tr>
<td align="left">2014-09-30</td>
<td align="right">3698</td>
<td align="right">4145.500</td>
<td align="right">-447.500</td>
<td align="right">0.8920516</td>
</tr>
<tr>
<td align="left">2014-12-31</td>
<td align="right">4752</td>
<td align="right">4236.375</td>
<td align="right">515.625</td>
<td align="right">1.1217137</td>
</tr>
<tr>
<td align="left">2015-03-31</td>
<td align="right">6154</td>
<td align="right">4376.500</td>
<td align="right">1777.500</td>
<td align="right">1.4061465</td>
</tr>
<tr>
<td align="left">2015-06-30</td>
<td align="right">4543</td>
<td align="right">4478.875</td>
<td align="right">64.125</td>
<td align="right">1.0143172</td>
</tr>
</tbody>
</table>
<p>To work out the seasonality we need to work out what the typical de-trended values are over a cycle. Here I will calculate the mean value for the observations in Q1, Q2, Q3, and Q4.</p>
<pre><code>sample_ts[,`:=`(seasonal_a = mean(detrended_a, na.rm = TRUE),
             seasonal_m = mean(detrended_m, na.rm = TRUE)), 
          by=.(quarter(TimePeriod)) ]
</code></pre>
<table>
<thead>
<tr>
<th align="left">TimePeriod</th>
<th align="right">Value</th>
<th align="right">trend</th>
<th align="right">detrended_a</th>
<th align="right">detrended_m</th>
<th align="right">seasonal_a</th>
<th align="right">seasonal_m</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">2014-03-31</td>
<td align="right">5212</td>
<td align="right">4108.625</td>
<td align="right">1103.375</td>
<td align="right">1.2685509</td>
<td align="right">574.1919</td>
<td align="right">1.2924422</td>
</tr>
<tr>
<td align="left">2014-06-30</td>
<td align="right">3774</td>
<td align="right">4121.750</td>
<td align="right">-347.750</td>
<td align="right">0.9156305</td>
<td align="right">-111.2878</td>
<td align="right">1.0036648</td>
</tr>
<tr>
<td align="left">2014-09-30</td>
<td align="right">3698</td>
<td align="right">4145.500</td>
<td align="right">-447.500</td>
<td align="right">0.8920516</td>
<td align="right">-219.8363</td>
<td align="right">0.9488803</td>
</tr>
<tr>
<td align="left">2014-12-31</td>
<td align="right">4752</td>
<td align="right">4236.375</td>
<td align="right">515.625</td>
<td align="right">1.1217137</td>
<td align="right">136.7827</td>
<td align="right">1.1202999</td>
</tr>
<tr>
<td align="left">2015-03-31</td>
<td align="right">6154</td>
<td align="right">4376.500</td>
<td align="right">1777.500</td>
<td align="right">1.4061465</td>
<td align="right">574.1919</td>
<td align="right">1.2924422</td>
</tr>
<tr>
<td align="left">2015-06-30</td>
<td align="right">4543</td>
<td align="right">4478.875</td>
<td align="right">64.125</td>
<td align="right">1.0143172</td>
<td align="right">-111.2878</td>
<td align="right">1.0036648</td>
</tr>
</tbody>
</table>
<blockquote><p>
My actual needs aren’t over long economic periods so I’m not using a better seasonality system for this blog post. There are some much better mechanisms than this.
</p>
</blockquote>
<h3>The remainder</h3>
<p>Now that we have our two components, we can calculate the residual in both situations and see which has the better fit.</p>
<pre><code>sample_ts[,`:=`( residual_a = detrended_a - seasonal_a, 
                 residual_m = detrended_m / seasonal_m )]
</code></pre>
<table>
<thead>
<tr>
<th align="left">TimePeriod</th>
<th align="right">Value</th>
<th align="right">trend</th>
<th align="right">detrended_a</th>
<th align="right">detrended_m</th>
<th align="right">seasonal_a</th>
<th align="right">seasonal_m</th>
<th align="right">residual_a</th>
<th align="right">residual_m</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">2014-03-31</td>
<td align="right">5212</td>
<td align="right">4108.625</td>
<td align="right">1103.375</td>
<td align="right">1.2685509</td>
<td align="right">574.1919</td>
<td align="right">1.2924422</td>
<td align="right">529.1831</td>
<td align="right">0.9815146</td>
</tr>
<tr>
<td align="left">2014-06-30</td>
<td align="right">3774</td>
<td align="right">4121.750</td>
<td align="right">-347.750</td>
<td align="right">0.9156305</td>
<td align="right">-111.2878</td>
<td align="right">1.0036648</td>
<td align="right">-236.4622</td>
<td align="right">0.9122871</td>
</tr>
<tr>
<td align="left">2014-09-30</td>
<td align="right">3698</td>
<td align="right">4145.500</td>
<td align="right">-447.500</td>
<td align="right">0.8920516</td>
<td align="right">-219.8363</td>
<td align="right">0.9488803</td>
<td align="right">-227.6637</td>
<td align="right">0.9401098</td>
</tr>
<tr>
<td align="left">2014-12-31</td>
<td align="right">4752</td>
<td align="right">4236.375</td>
<td align="right">515.625</td>
<td align="right">1.1217137</td>
<td align="right">136.7827</td>
<td align="right">1.1202999</td>
<td align="right">378.8423</td>
<td align="right">1.0012620</td>
</tr>
<tr>
<td align="left">2015-03-31</td>
<td align="right">6154</td>
<td align="right">4376.500</td>
<td align="right">1777.500</td>
<td align="right">1.4061465</td>
<td align="right">574.1919</td>
<td align="right">1.2924422</td>
<td align="right">1203.3081</td>
<td align="right">1.0879763</td>
</tr>
<tr>
<td align="left">2015-06-30</td>
<td align="right">4543</td>
<td align="right">4478.875</td>
<td align="right">64.125</td>
<td align="right">1.0143172</td>
<td align="right">-111.2878</td>
<td align="right">1.0036648</td>
<td align="right">175.4128</td>
<td align="right">1.0106135</td>
</tr>
</tbody>
</table>
<h2>Visualising decomposition</h2>
<p>I’ve done the number crunching, but you could also perform a visual decomposition. ggseas gives us a function <code>ggsdc()</code> which we can use.</p>
<pre><code>ggsdc(sample_ts, aes(x = TimePeriod, y = Value), method = &quot;decompose&quot;, 
      frequency = 4, s.window = 8, type = &quot;additive&quot;)+ geom_line()+
  ggtitle(&quot;Additive&quot;)+ theme_minimal()
</code></pre>
<p>
<a target="_blank" rel="nofollow" href="http://itsalocke.com/is-my-time-series-additive-or-multiplicative/additive/"><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" alt="Additive time series decomposition" src="https://i2.wp.com/itsalocke.com/wp-content/uploads/2017/02/additive-275x300.png?resize=275%2C300" data-recalc-dims="1"></a><br>
<a target="_blank" rel="nofollow" href="http://itsalocke.com/is-my-time-series-additive-or-multiplicative/multiplicative/"><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" alt="Multiplicative time series decomposition" src="https://i0.wp.com/itsalocke.com/wp-content/uploads/2017/02/multiplicative-275x300.png?resize=275%2C300" data-recalc-dims="1"></a><br>
<br>
The different decompositions produce differently distributed residuals. We need to assess these to identify which decomposition is a better fit.</p>
<h2>Assessing fit</h2>
<p>After decomposing our data, we need to compare the residuals. As we’re just trying to classify the time series, we don’t need to do anything particularly sophisticated – a big part of this exercise is to produce a quick function that could be used to perform an initial classification in a batch processing environment so simpler is better.</p>
<p>We’re going to check the whether how much correlation between data points is still encoded within the residuals. This is the <a target="_blank" rel="nofollow" href="https://onlinecourses.science.psu.edu/stat510/node/60">Auto-Correlation Factor (ACF)</a> and it has a function for calculating it. As some of the correlations could be negative we will select the type with the smallest sum of squares of correlation values.</p>
<pre><code>ssacf&lt;- function(x) sum(acf(x, na.action = na.omit)$acf^2)
compare_ssacf&lt;-function(add,mult) ifelse(ssacf(add)&lt; ssacf(mult), &quot;Additive&quot;, &quot;Multiplicative&quot;) 
sample_ts[,.(ts_type = compare_ssacf(residual_a, residual_m ))]
</code></pre>
<table>
<thead>
<tr>
<th align="left">ts_type</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">Multiplicative</td>
</tr>
</tbody>
</table>
<h2>Putting it all together</h2>
<p>This isn’t a fully generalized function (as it doesn’t have configurable lags, medians, seasonality etc) but if I had to apply to run this exercise over multiple time series from this dataset, my overall function and usage would look like:</p>
<pre><code>ssacf&lt;- function(x) sum(acf(x, na.action = na.omit, plot = FALSE)$acf^2)
compare_ssacf&lt;-function(add,mult) ifelse(ssacf(add)&lt; ssacf(mult), 
                                         &quot;Additive&quot;, &quot;Multiplicative&quot;) 
additive_or_multiplicative &lt;- function(dt){
  m&lt;-copy(dt)
  m[,trend := zoo::rollmean(Value, 8, fill=&quot;extend&quot;, align = &quot;right&quot;)]
  m[,`:=`( detrended_a = Value - trend,  detrended_m = Value / trend )]
  m[Value==0,detrended_m:= 0]
  m[,`:=`(seasonal_a = mean(detrended_a, na.rm = TRUE),
                  seasonal_m = mean(detrended_m, na.rm = TRUE)), 
            by=.(quarter(TimePeriod)) ]
  m[is.infinite(seasonal_m),seasonal_m:= 1]
  m[,`:=`( residual_a = detrended_a - seasonal_a, 
                   residual_m = detrended_m / seasonal_m)]
  compare_ssacf(m$residual_a, m$residual_m )
}

# Applying it to all time series in table
sample_ts&lt;-nzdata[ , .(Type=additive_or_multiplicative(.SD)),
                    .(Account, Category)]
</code></pre>
<table>
<thead>
<tr>
<th align="left">Account</th>
<th align="left">Category</th>
<th align="left">Type</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">Current account</td>
<td align="left">Inflow total</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Balance</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Goods; Exports (fob) total</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Services; Exports total</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Primary income; Inflow total</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Secondary income; Inflow total</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Goods balance</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Services balance</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Primary income balance</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Outflow total</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Goods; Imports (fob) total</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Services; Imports total</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Primary income; Outflow total</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Current account</td>
<td align="left">Secondary income; Outflow total</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Capital account</td>
<td align="left">Inflow total</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Capital account</td>
<td align="left">Outflow total</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">NA</td>
<td align="left">Net errors and omissions</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">Foreign inv. in NZ total</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">Balance</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">Foreign inv. in NZ; Direct inv. liabilities</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">Foreign inv. in NZ; Portfolio inv. liabilities</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">Foreign inv. in NZ; Other inv. liabilities</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">NZ inv. abroad total</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">NZ inv. abroad; Direct inv. assets</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">NZ inv. abroad; Portfolio inv. assets</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">NZ inv. abroad; Financial derivative assets</td>
<td align="left">Multiplicative</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">NZ inv. abroad; Other inv. assets</td>
<td align="left">Additive</td>
</tr>
<tr>
<td align="left">Financial account</td>
<td align="left">NZ inv. abroad; Reserve assets</td>
<td align="left">Multiplicative</td>
</tr>
</tbody>
</table>
<h2>Conclusion</h2>
<p>This is a very simple way of quickly assessing whether multiple time series are additive or multiplicative. It gives an effective starting point for conditionally processing batches of time series. Get the <a target="_blank" rel="nofollow" href="https://gist.github.com/stephlocke/682f618e36b07945aa48871e0563b3fa">GIST</a> of the code used throughout this blog to work through it yourself. If you’ve got an easier way of classifying time series, let me know in the comments!</p>
<div class="twitter-share"><a target="_blank" class="twitter-share-button" rel="nofollow" href="https://twitter.com/intent/tweet?url=http%3A%2F%2Fitsalocke.com%2Fis-my-time-series-additive-or-multiplicative%2F&via=stefflocke&%23038;related=stefflocke%3ASteph">Tweet</a></div>
<p>The post <a target="_blank" rel="nofollow" href="http://itsalocke.com/is-my-time-series-additive-or-multiplicative/">Is my time series additive or multiplicative?</a> appeared first on <a target="_blank" rel="nofollow" href="http://itsalocke.com/">Locke Data</a>.</p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/aQ31LcTFkL8">