Example with beer time series

This example below shows lm() and tslm() functions used to fit the beer time series that has a seasonal pattern and no trend. We note that tslm() creates automatically the indicator variables for modelling the seasonal patterns.

The model fitted corresponds to: \[ \text{beer}_t=\underbrace{\beta_0 + \beta_2 \ \text{Feb}_t+ \cdots+\beta_{12} \ \text{Dec}_t }_{\widehat{\text{beer}}_t}+\epsilon_t \]

require(fma)


Dec = rep(c(0, 0, 0, 0,0,0, 0, 0,0,0, 0,1), length.out=length(beer) )
Feb = rep(c(0,1, 0, 0,0,0, 0, 0,0,0, 0,0), length.out=length(beer) )
Mar = rep(c(0, 0, 1, 0,0,0, 0, 0,0,0, 0,0), length.out=length(beer) )
Apr = rep(c(0, 0, 0, 1,0,0, 0, 0,0,0, 0,0), length.out=length(beer) )
May = rep(c(0, 0, 0, 0,1,0, 0, 0,0,0, 0,0), length.out=length(beer) )
Jun = rep(c(0, 0, 0, 0,0,1, 0, 0,0,0, 0,0), length.out=length(beer) )
Jul = rep(c(0, 0, 0, 0,0,0, 1, 0,0,0, 0,0), length.out=length(beer) )
Aug = rep(c(0, 0, 0, 0,0,0, 0, 1,0,0, 0,0), length.out=length(beer) )
Sep= rep(c(0, 0, 0, 0,0,0, 0, 0,1,0, 0,0), length.out=length(beer) )
Oct= rep(c(0, 0, 0, 0,0,0, 0, 0,0,1, 0,0), length.out=length(beer) )
Nov= rep(c(0, 0, 0, 0,0,0, 0, 0,0,0, 1,0), length.out=length(beer) )


lm.beer<-lm(beer~Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov+Dec)
summary(lm.beer)

Call:
lm(formula = beer ~ Feb + Mar + Apr + May + Jun + Jul + Aug + 
    Sep + Oct + Nov + Dec)

Residuals:
    Min      1Q  Median      3Q     Max 
-16.250  -6.250  -1.650   5.688  22.750 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  147.800      4.334  34.104  < 2e-16 ***
Feb           -9.000      6.129  -1.468  0.14909    
Mar            8.400      6.129   1.371  0.17746    
Apr           -7.600      6.129  -1.240  0.22153    
May           -7.200      6.129  -1.175  0.24640    
Jun          -19.800      6.129  -3.231  0.00234 ** 
Jul          -13.400      6.129  -2.186  0.03415 *  
Aug           -4.000      6.129  -0.653  0.51738    
Sep           -7.300      6.501  -1.123  0.26754    
Oct           19.450      6.501   2.992  0.00453 ** 
Nov           35.950      6.501   5.530 1.65e-06 ***
Dec           38.700      6.501   5.953 3.95e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 9.691 on 44 degrees of freedom
Multiple R-squared:  0.8045,    Adjusted R-squared:  0.7556 
F-statistic: 16.46 on 11 and 44 DF,  p-value: 3.551e-12
tslm.beer<-tslm(beer~ season)

summary(tslm.beer)

Call:
tslm(formula = beer ~ season)

Residuals:
    Min      1Q  Median      3Q     Max 
-16.250  -6.250  -1.650   5.688  22.750 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  147.800      4.334  34.104  < 2e-16 ***
season2       -9.000      6.129  -1.468  0.14909    
season3        8.400      6.129   1.371  0.17746    
season4       -7.600      6.129  -1.240  0.22153    
season5       -7.200      6.129  -1.175  0.24640    
season6      -19.800      6.129  -3.231  0.00234 ** 
season7      -13.400      6.129  -2.186  0.03415 *  
season8       -4.000      6.129  -0.653  0.51738    
season9       -7.300      6.501  -1.123  0.26754    
season10      19.450      6.501   2.992  0.00453 ** 
season11      35.950      6.501   5.530 1.65e-06 ***
season12      38.700      6.501   5.953 3.95e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 9.691 on 44 degrees of freedom
Multiple R-squared:  0.8045,    Adjusted R-squared:  0.7556 
F-statistic: 16.46 on 11 and 44 DF,  p-value: 3.551e-12
plot(forecast(tslm.beer, h=20))

Example with airpass time series

The model fitted corresponds to: \[ \text{airpass}_t=\underbrace{\beta_0 + \beta_1 \ t + \beta_2 \ \text{Feb}_t+ \cdots+\beta_{12} \ \text{Dec}_t }_{\widehat{\text{beer}}_t}+\epsilon_t \]

tslm.airpass<-tslm(airpass~ trend + season)

summary(tslm.airpass)

Call:
tslm(formula = airpass ~ trend + season)

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
plot(forecast(tslm.airpass, h=20))