Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # sample series
- x <- AirPassengers
- # to illustrate of a more general case,
- # take a subsample that does not start in the first season
- # and ends in the last season
- x <- window(x, start=c(1949,2), end=c(1959,4))
- # monthly intercepts
- S <- frequency(x)
- monthly.means <- do.call("rbind",
- replicate(ceiling(length(x)/S), diag(S), simplify = FALSE))
- monthly.means <- ts(monthly.means, frequency = S, start = c(start(x)[1], 1))
- monthly.means <- window(monthly.means, start = start(x), end = end(x))
- # column names
- if (S == 12) {
- colnames(monthly.means) <- month.abb
- } else
- colnames(monthly.means) <- paste("season", 1L:S)
- monthly.means[1:15,]
- # Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
- # [1,] 0 1 0 0 0 0 0 0 0 0 0 0
- # [2,] 0 0 1 0 0 0 0 0 0 0 0 0
- # [3,] 0 0 0 1 0 0 0 0 0 0 0 0
- # [4,] 0 0 0 0 1 0 0 0 0 0 0 0
- # [5,] 0 0 0 0 0 1 0 0 0 0 0 0
- # [6,] 0 0 0 0 0 0 1 0 0 0 0 0
- # [7,] 0 0 0 0 0 0 0 1 0 0 0 0
- # [8,] 0 0 0 0 0 0 0 0 1 0 0 0
- # [9,] 0 0 0 0 0 0 0 0 0 1 0 0
- # [10,] 0 0 0 0 0 0 0 0 0 0 1 0
- # [11,] 0 0 0 0 0 0 0 0 0 0 0 1
- # [12,] 1 0 0 0 0 0 0 0 0 0 0 0
- # [13,] 0 1 0 0 0 0 0 0 0 0 0 0
- # [14,] 0 0 1 0 0 0 0 0 0 0 0 0
- # [15,] 0 0 0 1 0 0 0 0 0 0 0 0
- monthly.trends <- monthly.means * seq_along(x) / S
- round(monthly.trends[1:15,], 2)
- # Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
- # [1,] 0 0.08 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
- # [2,] 0 0.00 0.17 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
- # [3,] 0 0.00 0.00 0.25 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
- # [4,] 0 0.00 0.00 0.00 0.33 0.00 0.0 0.00 0.00 0.00 0.00 0.00
- # [5,] 0 0.00 0.00 0.00 0.00 0.42 0.0 0.00 0.00 0.00 0.00 0.00
- # [6,] 0 0.00 0.00 0.00 0.00 0.00 0.5 0.00 0.00 0.00 0.00 0.00
- # [7,] 0 0.00 0.00 0.00 0.00 0.00 0.0 0.58 0.00 0.00 0.00 0.00
- # [8,] 0 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.67 0.00 0.00 0.00
- # [9,] 0 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.75 0.00 0.00
- # [10,] 0 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.83 0.00
- # [11,] 0 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.92
- # [12,] 1 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
- # [13,] 0 1.08 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
- # [14,] 0 0.00 1.17 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
- # [15,] 0 0.00 0.00 1.25 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
- set.seed(123)
- xreg1 <- runif(length(x), 100, 200)
- xreg2 <- rnorm(length(x), mean = mean(x))
- summary(lm(x ~ 0 + monthly.means + monthly.trends + xreg1 + xreg2))
- # Coefficients:
- # Estimate Std. Error t value Pr(>|t|)
- # monthly.meansJan 439.46067 363.55393 1.209 0.230
- # monthly.meansFeb 457.02743 362.72206 1.260 0.211
- # monthly.meansMar 470.05809 365.81017 1.285 0.202
- # monthly.meansApr 462.42569 364.45517 1.269 0.208
- # monthly.meansMay 452.74617 364.39972 1.242 0.217
- # monthly.meansJun 453.95303 364.38891 1.246 0.216
- # monthly.meansJul 462.79607 365.35861 1.267 0.208
- # monthly.meansAug 455.03446 363.19802 1.253 0.213
- # monthly.meansSep 453.29026 363.20374 1.248 0.215
- # monthly.meansOct 440.57396 363.25728 1.213 0.228
- # monthly.meansNov 431.31230 364.77546 1.182 0.240
- # monthly.meansDec 443.82401 363.05542 1.222 0.224
- # monthly.trendsJan 27.92925 1.52426 18.323 <2e-16 ***
- # monthly.trendsFeb 23.58292 1.33391 17.680 <2e-16 ***
- # monthly.trendsMar 27.96668 1.35829 20.590 <2e-16 ***
- # monthly.trendsApr 27.33010 1.33884 20.413 <2e-16 ***
- # monthly.trendsMay 29.04411 1.53939 18.867 <2e-16 ***
- # monthly.trendsJun 35.80080 1.52180 23.525 <2e-16 ***
- # monthly.trendsJul 39.76516 1.59066 24.999 <2e-16 ***
- # monthly.trendsAug 40.53217 1.52845 26.519 <2e-16 ***
- # monthly.trendsSep 32.49961 1.54164 21.081 <2e-16 ***
- # monthly.trendsOct 28.34616 1.52805 18.551 <2e-16 ***
- # monthly.trendsNov 24.20748 1.53510 15.769 <2e-16 ***
- # monthly.trendsDec 26.36839 1.53157 17.217 <2e-16 ***
- # xreg1 0.05708 0.05222 1.093 0.277
- # xreg2 -1.45284 1.45625 -0.998 0.321
- # ---
- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
- #
- # Residual standard error: 13.81 on 97 degrees of freedom
- # Multiple R-squared: 0.9979, Adjusted R-squared: 0.9974
- # F-statistic: 1787 on 26 and 97 DF, p-value: < 2.2e-16
- coef.seasonal.intercepts <- coef(lm(x ~ 0 + monthly.means))
- sample.means <- lapply(split(x, cycle(x)), mean)
- all.equal(coef.seasonal.intercepts,
- as.vector(unlist(sample.means)), check.attributes = FALSE)
- # [1] TRUE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement