Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. # sample series
  2. x <- AirPassengers
  3. # to illustrate of a more general case,
  4. # take a subsample that does not start in the first season
  5. # and ends in the last season
  6. x <- window(x, start=c(1949,2), end=c(1959,4))
  7.  
  8. # monthly intercepts
  9. S <- frequency(x)
  10. monthly.means <- do.call("rbind",
  11. replicate(ceiling(length(x)/S), diag(S), simplify = FALSE))
  12.  
  13. monthly.means <- ts(monthly.means, frequency = S, start = c(start(x)[1], 1))
  14. monthly.means <- window(monthly.means, start = start(x), end = end(x))
  15.  
  16. # column names
  17. if (S == 12) {
  18. colnames(monthly.means) <- month.abb
  19. } else
  20. colnames(monthly.means) <- paste("season", 1L:S)
  21.  
  22. monthly.means[1:15,]
  23. # Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  24. # [1,] 0 1 0 0 0 0 0 0 0 0 0 0
  25. # [2,] 0 0 1 0 0 0 0 0 0 0 0 0
  26. # [3,] 0 0 0 1 0 0 0 0 0 0 0 0
  27. # [4,] 0 0 0 0 1 0 0 0 0 0 0 0
  28. # [5,] 0 0 0 0 0 1 0 0 0 0 0 0
  29. # [6,] 0 0 0 0 0 0 1 0 0 0 0 0
  30. # [7,] 0 0 0 0 0 0 0 1 0 0 0 0
  31. # [8,] 0 0 0 0 0 0 0 0 1 0 0 0
  32. # [9,] 0 0 0 0 0 0 0 0 0 1 0 0
  33. # [10,] 0 0 0 0 0 0 0 0 0 0 1 0
  34. # [11,] 0 0 0 0 0 0 0 0 0 0 0 1
  35. # [12,] 1 0 0 0 0 0 0 0 0 0 0 0
  36. # [13,] 0 1 0 0 0 0 0 0 0 0 0 0
  37. # [14,] 0 0 1 0 0 0 0 0 0 0 0 0
  38. # [15,] 0 0 0 1 0 0 0 0 0 0 0 0
  39.  
  40. monthly.trends <- monthly.means * seq_along(x) / S
  41. round(monthly.trends[1:15,], 2)
  42. # Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  43. # [1,] 0 0.08 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
  44. # [2,] 0 0.00 0.17 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
  45. # [3,] 0 0.00 0.00 0.25 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
  46. # [4,] 0 0.00 0.00 0.00 0.33 0.00 0.0 0.00 0.00 0.00 0.00 0.00
  47. # [5,] 0 0.00 0.00 0.00 0.00 0.42 0.0 0.00 0.00 0.00 0.00 0.00
  48. # [6,] 0 0.00 0.00 0.00 0.00 0.00 0.5 0.00 0.00 0.00 0.00 0.00
  49. # [7,] 0 0.00 0.00 0.00 0.00 0.00 0.0 0.58 0.00 0.00 0.00 0.00
  50. # [8,] 0 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.67 0.00 0.00 0.00
  51. # [9,] 0 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.75 0.00 0.00
  52. # [10,] 0 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.83 0.00
  53. # [11,] 0 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.92
  54. # [12,] 1 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
  55. # [13,] 0 1.08 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
  56. # [14,] 0 0.00 1.17 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
  57. # [15,] 0 0.00 0.00 1.25 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
  58.  
  59. set.seed(123)
  60. xreg1 <- runif(length(x), 100, 200)
  61. xreg2 <- rnorm(length(x), mean = mean(x))
  62.  
  63. summary(lm(x ~ 0 + monthly.means + monthly.trends + xreg1 + xreg2))
  64. # Coefficients:
  65. # Estimate Std. Error t value Pr(>|t|)
  66. # monthly.meansJan 439.46067 363.55393 1.209 0.230
  67. # monthly.meansFeb 457.02743 362.72206 1.260 0.211
  68. # monthly.meansMar 470.05809 365.81017 1.285 0.202
  69. # monthly.meansApr 462.42569 364.45517 1.269 0.208
  70. # monthly.meansMay 452.74617 364.39972 1.242 0.217
  71. # monthly.meansJun 453.95303 364.38891 1.246 0.216
  72. # monthly.meansJul 462.79607 365.35861 1.267 0.208
  73. # monthly.meansAug 455.03446 363.19802 1.253 0.213
  74. # monthly.meansSep 453.29026 363.20374 1.248 0.215
  75. # monthly.meansOct 440.57396 363.25728 1.213 0.228
  76. # monthly.meansNov 431.31230 364.77546 1.182 0.240
  77. # monthly.meansDec 443.82401 363.05542 1.222 0.224
  78. # monthly.trendsJan 27.92925 1.52426 18.323 <2e-16 ***
  79. # monthly.trendsFeb 23.58292 1.33391 17.680 <2e-16 ***
  80. # monthly.trendsMar 27.96668 1.35829 20.590 <2e-16 ***
  81. # monthly.trendsApr 27.33010 1.33884 20.413 <2e-16 ***
  82. # monthly.trendsMay 29.04411 1.53939 18.867 <2e-16 ***
  83. # monthly.trendsJun 35.80080 1.52180 23.525 <2e-16 ***
  84. # monthly.trendsJul 39.76516 1.59066 24.999 <2e-16 ***
  85. # monthly.trendsAug 40.53217 1.52845 26.519 <2e-16 ***
  86. # monthly.trendsSep 32.49961 1.54164 21.081 <2e-16 ***
  87. # monthly.trendsOct 28.34616 1.52805 18.551 <2e-16 ***
  88. # monthly.trendsNov 24.20748 1.53510 15.769 <2e-16 ***
  89. # monthly.trendsDec 26.36839 1.53157 17.217 <2e-16 ***
  90. # xreg1 0.05708 0.05222 1.093 0.277
  91. # xreg2 -1.45284 1.45625 -0.998 0.321
  92. # ---
  93. # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  94. #
  95. # Residual standard error: 13.81 on 97 degrees of freedom
  96. # Multiple R-squared: 0.9979, Adjusted R-squared: 0.9974
  97. # F-statistic: 1787 on 26 and 97 DF, p-value: < 2.2e-16
  98.  
  99. coef.seasonal.intercepts <- coef(lm(x ~ 0 + monthly.means))
  100. sample.means <- lapply(split(x, cycle(x)), mean)
  101. all.equal(coef.seasonal.intercepts,
  102. as.vector(unlist(sample.means)), check.attributes = FALSE)
  103. # [1] TRUE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement