mettle
mettle

Reputation: 61

Getting trend and seasonal models from STL/decompose

Once I've done an STL or decomposition on time series data, how do I extract the models for each component? For example, how do I get the slope and intercept for the trend, the period for the seasonal data, and so on? I can provide sample data if needed, but this is a generic question.

Upvotes: 3

Views: 2691

Answers (1)

RHertel
RHertel

Reputation: 23788

As a partial answer to your question, the trend can be extracted quite easily if it is linear. Here's an example:

library(forecast)
plot(decompose(AirPassengers))

enter image description here

In the case of a linear trend we can use the tslm() function to extract the intercept and the slope

tslm(AirPassengers ~ trend)

Call:
lm(formula = formula, data = "AirPassengers", na.action = na.exclude)

Coefficients:
(Intercept)        trend  
     87.653        2.657  

To obtain a fit including the seasons, this could be extended like

fit <- tslm(AirPassengers ~ trend + season)
> summary(fit)

Call:
lm(formula = formula, data = "AirPassengers", na.action = na.exclude)

Residuals:
    Min      1Q  Median      3Q     Max 
-42.121 -18.564  -3.268  15.189  95.085 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  63.50794    8.38856   7.571 5.88e-12 ***
trend         2.66033    0.05297  50.225  < 2e-16 ***
season2      -9.41033   10.74941  -0.875 0.382944    
season3      23.09601   10.74980   2.149 0.033513 *  
season4      17.35235   10.75046   1.614 0.108911    
season5      19.44202   10.75137   1.808 0.072849 .  
season6      56.61502   10.75254   5.265 5.58e-07 ***
season7      93.62136   10.75398   8.706 1.17e-14 ***
season8      90.71103   10.75567   8.434 5.32e-14 ***
season9      39.38403   10.75763   3.661 0.000363 ***
season10      0.89037   10.75985   0.083 0.934177    
season11    -35.51996   10.76232  -3.300 0.001244 ** 
season12     -9.18029   10.76506  -0.853 0.395335    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 26.33 on 131 degrees of freedom
Multiple R-squared:  0.9559,    Adjusted R-squared:  0.9518 
F-statistic: 236.5 on 12 and 131 DF,  p-value: < 2.2e-16

If I interpret this result correctly, there is a monthly average increase of 2.66 passengers, and there are on average 9.4 passengers less in the second month than in the first month, etc.

Upvotes: 4

Related Questions