This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
License: CC BY-SA 4.0
Sugestão de citação: FIGUEIREDO, Adriano Marcos Rodrigues. Exemplo Séries Temporais: consumo Morettin e Toloi - formatos ts e tsibble. Campo Grande-MS, Brasil: RStudio/Rpubs, 2021. Disponível em https://rpubs.com/amrofi/exemplo_consumo_morettin_toloi.
Os dados vem do livro de Morettin e Toloi, Análise de Séries Temporais (https://www.ime.usp.br/~pam/ST.html), consumo no varejo de São Paulo, mensais de Jan/1984 a Out/1996, em https://www.ime.usp.br/~pam/CONSUMO.XLS.
Inicialmente, trabalharemos com o formato ts
, conforme o
livro de Hyndman (2018), segunda edição. Depois faremos a parte da
decomposição e lags com formato tsibble
, conforme o livro
de Hyndman (2021), terceira edição.
#vou puxar os libraries que tenho costume de usar: destaque para o readxl e tseries
library(readxl) # puxar Excel
library(fpp2) # livro Hyndman formato ts - já carrega ggplot2 e forecast
library(fpp3) # livro Hyndman formato tsibble - já carrega fable, feasts e tidyr e dplyr
#library(forecast) # pacote para forecast em formato ts
#library(fable) # pacote para forecast em formato fable
#library(feasts) # pacote para forecast em formato fable
#library(foreign);library(dynlm);library(car);library(lmtest); library(sandwich);library(tseries);
library(lattice) # pacoe gráfico
#dados <- read_excel("CONSUMO morettin R.xlsx",
# sheet = "dados")
dados <-structure(list(obs = structure(c(441763200, 444441600, 446947200,
449625600, 452217600, 454896000, 457488000, 460166400, 462844800,
465436800, 468115200, 470707200, 473385600, 476064000, 478483200,
481161600, 483753600, 486432000, 489024000, 491702400, 494380800,
496972800, 499651200, 502243200, 504921600, 507600000, 510019200,
512697600, 515289600, 517968000, 520560000, 523238400, 525916800,
528508800, 531187200, 533779200, 536457600, 539136000, 541555200,
544233600, 546825600, 549504000, 552096000, 554774400, 557452800,
560044800, 562723200, 565315200, 567993600, 570672000, 573177600,
575856000, 578448000, 581126400, 583718400, 586396800, 589075200,
591667200, 594345600, 596937600, 599616000, 602294400, 604713600,
607392000, 609984000, 612662400, 615254400, 617932800, 620611200,
623203200, 625881600, 628473600, 631152000, 633830400, 636249600,
638928000, 641520000, 644198400, 646790400, 649468800, 652147200,
654739200, 657417600, 660009600, 662688000, 665366400, 667785600,
670464000, 673056000, 675734400, 678326400, 681004800, 683683200,
686275200, 688953600, 691545600, 694224000, 696902400, 699408000,
702086400, 704678400, 707356800, 709948800, 712627200, 715305600,
717897600, 720576000, 723168000, 725846400, 728524800, 730944000,
733622400, 736214400, 738892800, 741484800, 744163200, 746841600,
749433600, 752112000, 754704000, 757382400, 760060800, 762480000,
765158400, 767750400, 770428800, 773020800, 775699200, 778377600,
780969600, 783648000, 786240000, 788918400, 791596800, 794016000,
796694400, 799286400, 801964800, 804556800, 807235200, 809913600,
812505600, 815184000, 817776000, 820454400, 823132800, 825638400,
828316800, 830908800, 833587200, 836179200, 838857600, 841536000,
844128000), class = c("POSIXct", "POSIXt"), tzone = "UTC"), t = c(1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125,
126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151,
152, 153, 154), consumo = c(114.13, 110.79, 116.46, 111.57, 120.66,
121.15, 121.27, 127.02, 129.04, 133.3, 130.6, 179.39, 120.64,
114.05, 130.6, 118.26, 145.54, 135.13, 153.35, 159.95, 150.01,
164.93, 170.37, 220.96, 134.26, 133.11, 147.84, 164.46, 181.86,
170.44, 186.64, 174.21, 181.62, 194.16, 181.9, 232.01, 140.16,
130.78, 119.04, 120.73, 129.81, 111.04, 122.75, 133.95, 125.41,
132.05, 129.54, 176.37, 110.09, 113.25, 124.03, 110.63, 116.72,
124.63, 124.38, 130.27, 119.87, 115.75, 122.44, 162.43, 105.89,
115.59, 147, 131.7, 131.32, 136.66, 126.43, 134.88, 128.26, 125.32,
124.61, 166.11, 116.25, 96.93, 89.27, 101.87, 125.57, 113.31,
109.39, 127.33, 120.56, 117.73, 113.81, 147.25, 100.15, 95.11,
112.26, 109.39, 114.2, 113.8, 126.47, 128.36, 115.71, 116.09,
99.53, 127.27, 87.08, 85.67, 82.02, 98.2, 96.44, 90.23, 97.15,
95.08, 94, 93, 96.09, 129.21, 75.39, 77.7, 97.34, 84.97, 87.55,
86.64, 90.52, 95.4, 95.2, 95.8, 101.23, 128.49, 85.63, 82.77,
96.55, 81.33, 96.91, 83.76, 90.19, 114.84, 108.4, 106.05, 109.71,
143.86, 99.12, 99.28, 114.75, 106.13, 110.02, 108.07, 112.52,
113.87, 107.84, 112.12, 112.03, 139.37, 92.24, 93.56, 107.37,
102.89, 114.78, 102.88, 118.41, 119.23, 117.36, 122.06)), row.names = c(NA,
-154L), class = c("tbl_df", "tbl", "data.frame"))
attach(dados)
dados2<- matrix(consumo)
dados.ts<- ts(dados2,start=c(1984,1), frequency = 12)
plot(dados.ts,main="Consumo do varejo de Sao Paulo, Morettin e Toloi (2006)",
xlab="Ano",ylab="Indice")
plot(consumo, main="Consumo do varejo de Sao Paulo, Morettin e Toloi (2006)")
# no pacote lattice
xyplot.ts(dados.ts)
#no ggplot
library(ggplot2)
datas <- seq(as.Date(paste(c(start(dados.ts),1), collapse="/")),
by = "month", length.out = length(consumo))
dados.df <- data.frame(date = datas, value = consumo)
ggplot(data=dados.df) + geom_line(aes(date, consumo))
# semelhante grafico sazonal do consumo do Morettin e Toloi:
seasonplot(dados.ts,ylab="indice", xlab="Ano",
main="Seasonal plot: Consumo do varejo de Sao Paulo, Morettin e Toloi (2006)",
year.labels=TRUE, year.labels.left=TRUE, col=1:20, pch=19)
#Seasonal subseries plots: consumo do Morettin e Toloi:
monthplot(consumo,ylab="indice",xlab="Mes",xaxt="n",
main="Seasonal deviation plot: Consumo do varejo de Sao Paulo, Morettin e Toloi (2006)")
axis(1,at=1:12,labels=month.abb,cex=0.8)
consumo.ts<- ts(consumo,start=c(1984,1), frequency = 12)
# fazendo lag de 6 meses
# observar que a função lag do stats faz com objeto ts
# enquanto a do dplyr requer um vetor
cons.l6<-stats::lag(consumo.ts, -6)
plot(consumo.ts, type="o",col = "black",lwd=2,lty=1)
lines(cons.l6,type="o",col = "red",lwd=2,lty=2)
legend("topright",c("Consumo varejo", "Consumo varejo t-6"),lwd=2,lty=1:2,col=c(1,2))
knitr::kable(cbind(consumo.ts,cons.l6))
consumo.ts | cons.l6 |
---|---|
114.13 | NA |
110.79 | NA |
116.46 | NA |
111.57 | NA |
120.66 | NA |
121.15 | NA |
121.27 | 114.13 |
127.02 | 110.79 |
129.04 | 116.46 |
133.30 | 111.57 |
130.60 | 120.66 |
179.39 | 121.15 |
120.64 | 121.27 |
114.05 | 127.02 |
130.60 | 129.04 |
118.26 | 133.30 |
145.54 | 130.60 |
135.13 | 179.39 |
153.35 | 120.64 |
159.95 | 114.05 |
150.01 | 130.60 |
164.93 | 118.26 |
170.37 | 145.54 |
220.96 | 135.13 |
134.26 | 153.35 |
133.11 | 159.95 |
147.84 | 150.01 |
164.46 | 164.93 |
181.86 | 170.37 |
170.44 | 220.96 |
186.64 | 134.26 |
174.21 | 133.11 |
181.62 | 147.84 |
194.16 | 164.46 |
181.90 | 181.86 |
232.01 | 170.44 |
140.16 | 186.64 |
130.78 | 174.21 |
119.04 | 181.62 |
120.73 | 194.16 |
129.81 | 181.90 |
111.04 | 232.01 |
122.75 | 140.16 |
133.95 | 130.78 |
125.41 | 119.04 |
132.05 | 120.73 |
129.54 | 129.81 |
176.37 | 111.04 |
110.09 | 122.75 |
113.25 | 133.95 |
124.03 | 125.41 |
110.63 | 132.05 |
116.72 | 129.54 |
124.63 | 176.37 |
124.38 | 110.09 |
130.27 | 113.25 |
119.87 | 124.03 |
115.75 | 110.63 |
122.44 | 116.72 |
162.43 | 124.63 |
105.89 | 124.38 |
115.59 | 130.27 |
147.00 | 119.87 |
131.70 | 115.75 |
131.32 | 122.44 |
136.66 | 162.43 |
126.43 | 105.89 |
134.88 | 115.59 |
128.26 | 147.00 |
125.32 | 131.70 |
124.61 | 131.32 |
166.11 | 136.66 |
116.25 | 126.43 |
96.93 | 134.88 |
89.27 | 128.26 |
101.87 | 125.32 |
125.57 | 124.61 |
113.31 | 166.11 |
109.39 | 116.25 |
127.33 | 96.93 |
120.56 | 89.27 |
117.73 | 101.87 |
113.81 | 125.57 |
147.25 | 113.31 |
100.15 | 109.39 |
95.11 | 127.33 |
112.26 | 120.56 |
109.39 | 117.73 |
114.20 | 113.81 |
113.80 | 147.25 |
126.47 | 100.15 |
128.36 | 95.11 |
115.71 | 112.26 |
116.09 | 109.39 |
99.53 | 114.20 |
127.27 | 113.80 |
87.08 | 126.47 |
85.67 | 128.36 |
82.02 | 115.71 |
98.20 | 116.09 |
96.44 | 99.53 |
90.23 | 127.27 |
97.15 | 87.08 |
95.08 | 85.67 |
94.00 | 82.02 |
93.00 | 98.20 |
96.09 | 96.44 |
129.21 | 90.23 |
75.39 | 97.15 |
77.70 | 95.08 |
97.34 | 94.00 |
84.97 | 93.00 |
87.55 | 96.09 |
86.64 | 129.21 |
90.52 | 75.39 |
95.40 | 77.70 |
95.20 | 97.34 |
95.80 | 84.97 |
101.23 | 87.55 |
128.49 | 86.64 |
85.63 | 90.52 |
82.77 | 95.40 |
96.55 | 95.20 |
81.33 | 95.80 |
96.91 | 101.23 |
83.76 | 128.49 |
90.19 | 85.63 |
114.84 | 82.77 |
108.40 | 96.55 |
106.05 | 81.33 |
109.71 | 96.91 |
143.86 | 83.76 |
99.12 | 90.19 |
99.28 | 114.84 |
114.75 | 108.40 |
106.13 | 106.05 |
110.02 | 109.71 |
108.07 | 143.86 |
112.52 | 99.12 |
113.87 | 99.28 |
107.84 | 114.75 |
112.12 | 106.13 |
112.03 | 110.02 |
139.37 | 108.07 |
92.24 | 112.52 |
93.56 | 113.87 |
107.37 | 107.84 |
102.89 | 112.12 |
114.78 | 112.03 |
102.88 | 139.37 |
118.41 | 92.24 |
119.23 | 93.56 |
117.36 | 107.37 |
122.06 | 102.89 |
NA | 114.78 |
NA | 102.88 |
NA | 118.41 |
NA | 119.23 |
NA | 117.36 |
NA | 122.06 |
dcons<-diff(consumo.ts,1)
print(dcons)
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct
## 1984 -3.34 5.67 -4.89 9.09 0.49 0.12 5.75 2.02 4.26
## 1985 -58.75 -6.59 16.55 -12.34 27.28 -10.41 18.22 6.60 -9.94 14.92
## 1986 -86.70 -1.15 14.73 16.62 17.40 -11.42 16.20 -12.43 7.41 12.54
## 1987 -91.85 -9.38 -11.74 1.69 9.08 -18.77 11.71 11.20 -8.54 6.64
## 1988 -66.28 3.16 10.78 -13.40 6.09 7.91 -0.25 5.89 -10.40 -4.12
## 1989 -56.54 9.70 31.41 -15.30 -0.38 5.34 -10.23 8.45 -6.62 -2.94
## 1990 -49.86 -19.32 -7.66 12.60 23.70 -12.26 -3.92 17.94 -6.77 -2.83
## 1991 -47.10 -5.04 17.15 -2.87 4.81 -0.40 12.67 1.89 -12.65 0.38
## 1992 -40.19 -1.41 -3.65 16.18 -1.76 -6.21 6.92 -2.07 -1.08 -1.00
## 1993 -53.82 2.31 19.64 -12.37 2.58 -0.91 3.88 4.88 -0.20 0.60
## 1994 -42.86 -2.86 13.78 -15.22 15.58 -13.15 6.43 24.65 -6.44 -2.35
## 1995 -44.74 0.16 15.47 -8.62 3.89 -1.95 4.45 1.35 -6.03 4.28
## 1996 -47.13 1.32 13.81 -4.48 11.89 -11.90 15.53 0.82 -1.87 4.70
## Nov Dec
## 1984 -2.70 48.79
## 1985 5.44 50.59
## 1986 -12.26 50.11
## 1987 -2.51 46.83
## 1988 6.69 39.99
## 1989 -0.71 41.50
## 1990 -3.92 33.44
## 1991 -16.56 27.74
## 1992 3.09 33.12
## 1993 5.43 27.26
## 1994 3.66 34.15
## 1995 -0.09 27.34
## 1996
split.screen(c(1,2))
## [1] 1 2
plot(consumo.ts,type="o",col = "black",lwd=2,lty=1)
legend("topright",c("consumo do varejo SP"),lwd=2,lty=1,col=c(1))
screen(2)
plot(dcons,type="o",col = "red",lwd=2,lty=1)
legend("bottomright",c("dcons"),lwd=2,lty=1,col=c(2))
close.screen(all = TRUE) # exit split-screen mode
cons.components.ad<- decompose(consumo.ts, type=c("additive"))
cons.components.ad
## $x
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct
## 1984 114.13 110.79 116.46 111.57 120.66 121.15 121.27 127.02 129.04 133.30
## 1985 120.64 114.05 130.60 118.26 145.54 135.13 153.35 159.95 150.01 164.93
## 1986 134.26 133.11 147.84 164.46 181.86 170.44 186.64 174.21 181.62 194.16
## 1987 140.16 130.78 119.04 120.73 129.81 111.04 122.75 133.95 125.41 132.05
## 1988 110.09 113.25 124.03 110.63 116.72 124.63 124.38 130.27 119.87 115.75
## 1989 105.89 115.59 147.00 131.70 131.32 136.66 126.43 134.88 128.26 125.32
## 1990 116.25 96.93 89.27 101.87 125.57 113.31 109.39 127.33 120.56 117.73
## 1991 100.15 95.11 112.26 109.39 114.20 113.80 126.47 128.36 115.71 116.09
## 1992 87.08 85.67 82.02 98.20 96.44 90.23 97.15 95.08 94.00 93.00
## 1993 75.39 77.70 97.34 84.97 87.55 86.64 90.52 95.40 95.20 95.80
## 1994 85.63 82.77 96.55 81.33 96.91 83.76 90.19 114.84 108.40 106.05
## 1995 99.12 99.28 114.75 106.13 110.02 108.07 112.52 113.87 107.84 112.12
## 1996 92.24 93.56 107.37 102.89 114.78 102.88 118.41 119.23 117.36 122.06
## Nov Dec
## 1984 130.60 179.39
## 1985 170.37 220.96
## 1986 181.90 232.01
## 1987 129.54 176.37
## 1988 122.44 162.43
## 1989 124.61 166.11
## 1990 113.81 147.25
## 1991 99.53 127.27
## 1992 96.09 129.21
## 1993 101.23 128.49
## 1994 109.71 143.86
## 1995 112.03 139.37
## 1996
##
## $seasonal
## Jan Feb Mar Apr May
## 1984 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1985 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1986 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1987 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1988 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1989 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1990 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1991 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1992 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1993 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1994 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1995 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1996 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## Jun Jul Aug Sep Oct
## 1984 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1985 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1986 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1987 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1988 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1989 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1990 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1991 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1992 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1993 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1994 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1995 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1996 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## Nov Dec
## 1984 2.99663431 41.48548848
## 1985 2.99663431 41.48548848
## 1986 2.99663431 41.48548848
## 1987 2.99663431 41.48548848
## 1988 2.99663431 41.48548848
## 1989 2.99663431 41.48548848
## 1990 2.99663431 41.48548848
## 1991 2.99663431 41.48548848
## 1992 2.99663431 41.48548848
## 1993 2.99663431 41.48548848
## 1994 2.99663431 41.48548848
## 1995 2.99663431 41.48548848
## 1996
##
## $trend
## Jan Feb Mar Apr May Jun Jul
## 1984 NA NA NA NA NA NA 126.55292
## 1985 133.40667 136.11542 138.36125 140.55292 143.52792 146.91708 149.21667
## 1986 164.01542 165.99667 167.90792 170.44292 172.14125 173.08208 173.78833
## 1987 155.84625 151.50667 147.48708 142.55708 137.78750 133.28750 129.71625
## 1988 126.68625 126.60083 126.21667 125.30667 124.33167 123.45500 122.69917
## 1989 128.69375 128.97125 129.51292 130.26125 130.75042 130.99417 131.57917
## 1990 120.02417 118.99958 118.36417 117.72708 116.96083 115.72500 114.26833
## 1991 115.79333 116.54792 116.38875 116.11833 115.45500 114.02750 112.65042
## 1992 103.20083 100.59250 98.30125 96.43458 95.32917 95.26667 94.86042
## 1993 92.56708 92.30417 92.36750 92.53417 92.86500 93.04917 93.44583
## 1994 94.45208 95.24833 96.60833 97.58542 98.36583 99.35958 100.56208
## 1995 110.13208 111.02208 110.95833 111.18792 111.53750 111.44708 110.97333
## 1996 109.53458 110.00333 110.62333 111.43417 NA NA NA
## Aug Sep Oct Nov Dec
## 1984 126.96000 127.68500 128.55292 129.86833 131.48750
## 1985 150.57833 152.09083 154.73417 158.17250 161.15708
## 1986 173.93708 172.64000 169.61792 165.62708 160.98333
## 1987 127.73292 127.21042 126.99750 126.03125 126.05208
## 1988 122.62167 123.67625 125.51125 126.99750 128.10708
## 1989 131.23333 128.05042 124.40208 122.91958 121.70708
## 1990 113.52167 114.40375 115.67500 115.51458 115.06125
## 1991 111.71250 110.05917 108.33292 107.12667 105.40458
## 1992 94.04125 94.34750 94.43458 93.51292 92.99292
## 1993 94.08375 94.26208 94.07750 94.31583 94.58583
## 1994 101.81208 103.25833 105.05000 106.62958 108.18875
## 1995 110.44833 109.90250 109.46000 109.52333 109.50542
## 1996 NA NA NA
##
## $random
## Jan Feb Mar Apr May
## 1984 NA NA NA NA NA
## 1985 2.82614347 -4.08458570 -0.70385653 -12.18927320 2.26676847
## 1986 -14.16260653 -14.90583570 -13.01052320 4.12072680 9.97343513
## 1987 -0.09343987 -2.74583570 -21.38968987 -11.72343987 -7.72281487
## 1988 -1.00343987 4.62999763 4.87072680 -4.57302320 -7.35698153
## 1989 -7.21093987 4.59958097 24.54447680 11.54239347 0.82426847
## 1990 11.81864347 -4.08875237 -22.03677320 -5.75343987 8.86385180
## 1991 -0.05052320 -3.45708570 2.92864347 3.37531013 -1.00031487
## 1992 -0.52802320 3.05833097 -9.22385653 11.86906013 1.36551847
## 1993 -1.58427320 3.37666430 12.02989347 2.53947680 -5.06031487
## 1994 6.77072680 5.50249763 6.99906013 -6.15177320 -1.20114820
## 1995 4.58072680 6.23874763 10.84906013 5.04572680 -1.26281487
## 1996 -1.70177320 1.53749763 3.80406013 1.55947680 NA
## Jun Jul Aug Sep Oct
## 1984 NA -5.37340515 -6.34132181 -0.20104403 0.59767124
## 1985 -6.09705729 4.04284485 2.97034485 -3.63687737 6.04642124
## 1986 3.04794271 12.76117819 -6.12840515 7.42395597 20.39267124
## 1987 -16.55747396 -7.05673848 -0.18423848 -3.35646070 0.90308791
## 1988 6.86502604 1.59034485 1.24701152 -5.36229403 -13.91066209
## 1989 11.35585938 -5.23965515 -2.75465515 -1.34646070 -3.23149542
## 1990 3.27502604 -4.96882181 7.40701152 4.60020597 -2.09441209
## 1991 5.46252604 13.72909485 10.24617819 4.09478930 3.60767124
## 1992 0.65335938 2.19909485 -5.36257181 -1.90354403 -5.58399542
## 1993 -0.71914062 -3.01632181 -5.08507181 -0.61812737 -2.42691209
## 1994 -9.90955729 -10.46257181 6.62659485 3.58562263 -3.14941209
## 1995 2.31294271 1.45617819 -2.97965515 -3.61854403 -1.48941209
## 1996 NA NA NA NA NA
## Nov Dec
## 1984 -2.26496765 6.41701152
## 1985 9.20086569 18.31742819
## 1986 13.27628235 29.54117819
## 1987 0.51211569 8.83242819
## 1988 -7.55413431 -7.16257181
## 1989 -1.30621765 2.91742819
## 1990 -4.70121765 -9.29673848
## 1991 -10.59330098 -19.62007181
## 1992 -0.41955098 -5.26840515
## 1993 3.91753235 -7.58132181
## 1994 0.08378235 -5.81423848
## 1995 -0.48996765 -11.62090515
## 1996
##
## $figure
## [1] -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## [6] -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## [11] 2.99663431 41.48548848
##
## $type
## [1] "additive"
##
## attr(,"class")
## [1] "decomposed.ts"
# pega os valores estimados do componente sazonal
cons.saz.ad<-cons.components.ad$seasonal
print(cons.saz.ad) # Indice Estacional (IE) aditivo
## Jan Feb Mar Apr May
## 1984 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1985 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1986 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1987 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1988 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1989 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1990 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1991 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1992 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1993 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1994 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1995 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## 1996 -15.59281013 -17.98083097 -7.05739347 -10.10364347 -0.25468513
## Jun Jul Aug Sep Oct
## 1984 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1985 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1986 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1987 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1988 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1989 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1990 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1991 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1992 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1993 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1994 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1995 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## 1996 -5.69002604 0.09048848 6.40132181 1.55604403 4.14941209
## Nov Dec
## 1984 2.99663431 41.48548848
## 1985 2.99663431 41.48548848
## 1986 2.99663431 41.48548848
## 1987 2.99663431 41.48548848
## 1988 2.99663431 41.48548848
## 1989 2.99663431 41.48548848
## 1990 2.99663431 41.48548848
## 1991 2.99663431 41.48548848
## 1992 2.99663431 41.48548848
## 1993 2.99663431 41.48548848
## 1994 2.99663431 41.48548848
## 1995 2.99663431 41.48548848
## 1996
plot(cons.components.ad)
plot(cons.saz.ad)
cons.components.mu<- decompose(consumo.ts, type=c("multiplicative"))
cons.components.mu
## $x
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct
## 1984 114.13 110.79 116.46 111.57 120.66 121.15 121.27 127.02 129.04 133.30
## 1985 120.64 114.05 130.60 118.26 145.54 135.13 153.35 159.95 150.01 164.93
## 1986 134.26 133.11 147.84 164.46 181.86 170.44 186.64 174.21 181.62 194.16
## 1987 140.16 130.78 119.04 120.73 129.81 111.04 122.75 133.95 125.41 132.05
## 1988 110.09 113.25 124.03 110.63 116.72 124.63 124.38 130.27 119.87 115.75
## 1989 105.89 115.59 147.00 131.70 131.32 136.66 126.43 134.88 128.26 125.32
## 1990 116.25 96.93 89.27 101.87 125.57 113.31 109.39 127.33 120.56 117.73
## 1991 100.15 95.11 112.26 109.39 114.20 113.80 126.47 128.36 115.71 116.09
## 1992 87.08 85.67 82.02 98.20 96.44 90.23 97.15 95.08 94.00 93.00
## 1993 75.39 77.70 97.34 84.97 87.55 86.64 90.52 95.40 95.20 95.80
## 1994 85.63 82.77 96.55 81.33 96.91 83.76 90.19 114.84 108.40 106.05
## 1995 99.12 99.28 114.75 106.13 110.02 108.07 112.52 113.87 107.84 112.12
## 1996 92.24 93.56 107.37 102.89 114.78 102.88 118.41 119.23 117.36 122.06
## Nov Dec
## 1984 130.60 179.39
## 1985 170.37 220.96
## 1986 181.90 232.01
## 1987 129.54 176.37
## 1988 122.44 162.43
## 1989 124.61 166.11
## 1990 113.81 147.25
## 1991 99.53 127.27
## 1992 96.09 129.21
## 1993 101.23 128.49
## 1994 109.71 143.86
## 1995 112.03 139.37
## 1996
##
## $seasonal
## Jan Feb Mar Apr May Jun Jul
## 1984 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1985 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1986 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1987 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1988 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1989 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1990 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1991 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1992 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1993 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1994 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1995 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1996 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## Aug Sep Oct Nov Dec
## 1984 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1985 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1986 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1987 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1988 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1989 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1990 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1991 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1992 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1993 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1994 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1995 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1996 1.0562029 1.0136767 1.0299569
##
## $trend
## Jan Feb Mar Apr May Jun Jul
## 1984 NA NA NA NA NA NA 126.55292
## 1985 133.40667 136.11542 138.36125 140.55292 143.52792 146.91708 149.21667
## 1986 164.01542 165.99667 167.90792 170.44292 172.14125 173.08208 173.78833
## 1987 155.84625 151.50667 147.48708 142.55708 137.78750 133.28750 129.71625
## 1988 126.68625 126.60083 126.21667 125.30667 124.33167 123.45500 122.69917
## 1989 128.69375 128.97125 129.51292 130.26125 130.75042 130.99417 131.57917
## 1990 120.02417 118.99958 118.36417 117.72708 116.96083 115.72500 114.26833
## 1991 115.79333 116.54792 116.38875 116.11833 115.45500 114.02750 112.65042
## 1992 103.20083 100.59250 98.30125 96.43458 95.32917 95.26667 94.86042
## 1993 92.56708 92.30417 92.36750 92.53417 92.86500 93.04917 93.44583
## 1994 94.45208 95.24833 96.60833 97.58542 98.36583 99.35958 100.56208
## 1995 110.13208 111.02208 110.95833 111.18792 111.53750 111.44708 110.97333
## 1996 109.53458 110.00333 110.62333 111.43417 NA NA NA
## Aug Sep Oct Nov Dec
## 1984 126.96000 127.68500 128.55292 129.86833 131.48750
## 1985 150.57833 152.09083 154.73417 158.17250 161.15708
## 1986 173.93708 172.64000 169.61792 165.62708 160.98333
## 1987 127.73292 127.21042 126.99750 126.03125 126.05208
## 1988 122.62167 123.67625 125.51125 126.99750 128.10708
## 1989 131.23333 128.05042 124.40208 122.91958 121.70708
## 1990 113.52167 114.40375 115.67500 115.51458 115.06125
## 1991 111.71250 110.05917 108.33292 107.12667 105.40458
## 1992 94.04125 94.34750 94.43458 93.51292 92.99292
## 1993 94.08375 94.26208 94.07750 94.31583 94.58583
## 1994 101.81208 103.25833 105.05000 106.62958 108.18875
## 1995 110.44833 109.90250 109.46000 109.52333 109.50542
## 1996 NA NA NA
##
## $random
## Jan Feb Mar Apr May Jun Jul
## 1984 NA NA NA NA NA NA 0.9598431
## 1985 1.0366261 0.9814219 0.9957447 0.9164827 1.0178221 0.9661237 1.0294031
## 1986 0.9383618 0.9392454 0.9288382 1.0510118 1.0604205 1.0343623 1.0757297
## 1987 1.0309464 1.0110610 0.8514482 0.9224709 0.9456365 0.8750710 0.9478643
## 1988 0.9961546 1.0477783 1.0366433 0.9616677 0.9423005 1.0603938 1.0153785
## 1989 0.9432045 1.0497723 1.1973567 1.1012776 1.0081234 1.0958289 0.9624586
## 1990 1.1102803 0.9540707 0.7956180 0.9425322 1.0776341 1.0284764 0.9588944
## 1991 0.9914613 0.9558493 1.0174974 1.0261317 0.9928399 1.0483008 1.1245370
## 1992 0.9672608 0.9975414 0.8801973 1.1091875 1.0154471 0.9948630 1.0258334
## 1993 0.9336101 0.9859786 1.1117098 1.0002067 0.9463025 0.9780459 0.9702947
## 1994 1.0392565 1.0178490 1.0542825 0.9078041 0.9888950 0.8854829 0.8983450
## 1995 1.0317056 1.0474183 1.0909681 1.0396971 0.9900944 1.0185673 1.0156174
## 1996 0.9653313 0.9962129 1.0238952 1.0057292 NA NA NA
## Aug Sep Oct Nov Dec
## 1984 0.9472352 0.9969767 1.0067674 0.9835182 1.0189520
## 1985 1.0057138 0.9730109 1.0348905 1.0534275 1.0240100
## 1986 0.9482733 1.0378218 1.1113964 1.0740979 1.0763803
## 1987 0.9928703 0.9725457 1.0095415 1.0052363 1.0449960
## 1988 1.0058422 0.9561471 0.8954045 0.9429109 0.9469631
## 1989 0.9730968 0.9881225 0.9780784 0.9914580 1.0193419
## 1990 1.0619513 1.0395934 0.9881630 0.9635763 0.9557981
## 1991 1.0878790 1.0371587 1.0404359 0.9086548 0.9017923
## 1992 0.9572457 0.9828743 0.9561650 1.0049608 1.0377344
## 1993 0.9600335 0.9963237 0.9886912 1.0497047 1.0145727
## 1994 1.0679391 1.0356302 0.9801568 1.0062619 0.9931113
## 1995 0.9761191 0.9679944 0.9945087 1.0003920 0.9505471
## 1996 NA NA NA
##
## $figure
## [1] 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## [8] 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
##
## $type
## [1] "multiplicative"
##
## attr(,"class")
## [1] "decomposed.ts"
# pega os valores estimados do componente sazonal
cons.saz.mu<-cons.components.mu$seasonal
print(cons.saz.mu) # Indice Estacional (IE) multiplicativo
## Jan Feb Mar Apr May Jun Jul
## 1984 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1985 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1986 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1987 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1988 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1989 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1990 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1991 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1992 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1993 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1994 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1995 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## 1996 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457
## Aug Sep Oct Nov Dec
## 1984 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1985 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1986 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1987 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1988 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1989 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1990 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1991 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1992 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1993 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1994 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1995 1.0562029 1.0136767 1.0299569 1.0224863 1.3389367
## 1996 1.0562029 1.0136767 1.0299569
plot(cons.components.mu)
plot(cons.saz.mu)
Usaremos o pacote tsibble para converter de ts em tsibble. Outra opção é carregar novamente a série a partir dos dados básicos. Ver também https://rpubs.com/amrofi/decomposicao_time_series.
Primeiro, converterei para tsibble.
library(readxl) # puxar Excel
library(fpp2) # livro Hyndman formato ts - já carrega ggplot2 e forecast
library(fpp3) # livro Hyndman formato tsibble - já carrega fable, feasts e tidyr e dplyr
dados_tsbl <- as_tsibble(dados, key = t, index = obs)
Farei as médias móveis de 12 meses em fim de período e de 3 meses centrada.
dados_MA <- dados_tsbl %>%
mutate(
`3-MA` = slider::slide_dbl(consumo, mean,
.before = 1, .after = 1, .complete = TRUE)
)
dados_MA <- dados_MA %>%
mutate(
`12-MA` = slider::slide_dbl(consumo, mean,
.before = 11, .complete = TRUE)
)
attach(dados_MA)
## The following objects are masked from dados:
##
## consumo, obs, t
dados_MA[3:5] %>%
plot.ts(main="Consumo de Morettin e Toloi, Médias Móveis de 3 e 12 meses",
plot.type = c("single"),
col=c("blue","red","black"),
lty = 1:3)
dados_MA[3:5] %>%
plot.ts(main="Consumo de Morettin e Toloi, Médias Móveis de 3 e 12 meses",
plot.type = c("multiple"),
col=c("blue"),
lty = 1)
Farei essa parte com o formato ts apenas para ilustrar, pois em geral a operação será automatizada.
# calculo manual do IE
# multiplicativo
# farei com a série em formato ts
consumo.ts<-ts(dados_MA$consumo,start = c(1984,1),frequency = 12)
cons12.ma<-forecast::ma(consumo.ts,12)
S<-100*consumo.ts/cons12.ma
print(S)
## Jan Feb Mar Apr May Jun Jul
## 1984 NA NA NA NA NA NA 95.82553
## 1985 90.43026 83.78919 94.39059 84.13913 101.40188 91.97705 102.77002
## 1986 81.85816 80.18836 88.04826 96.48978 105.64580 98.47351 107.39501
## 1987 89.93479 86.31963 80.71215 84.68888 94.21029 83.30864 94.62962
## 1988 86.89972 89.45439 98.26753 88.28740 93.87793 100.95176 101.36988
## 1989 82.28061 89.62463 113.50219 101.10451 100.43563 104.32526 96.08664
## 1990 96.85549 81.45407 75.41978 86.53064 107.36073 97.91316 95.73081
## 1991 86.49030 81.60592 96.45262 94.20562 98.91300 99.80049 112.26767
## 1992 84.37916 85.16540 83.43739 101.83069 101.16526 94.71309 102.41363
## 1993 81.44364 84.17822 105.38339 91.82554 94.27664 93.11206 96.86895
## 1994 90.65973 86.89916 99.93962 83.34237 98.51998 84.29987 89.68589
## 1995 90.00102 89.42365 103.41720 95.45102 98.63947 96.96979 101.39373
## 1996 84.21085 85.05197 97.05909 92.33254 NA NA NA
## Aug Sep Oct Nov Dec
## 1984 100.04726 101.06121 103.69271 100.56339 136.43122
## 1985 106.22378 98.63185 106.58926 107.71152 137.10846
## 1986 100.15691 105.20158 114.46904 109.82503 144.12051
## 1987 104.86725 98.58469 103.97842 102.78403 139.91835
## 1988 106.23734 96.92241 92.22281 96.41135 126.79236
## 1989 102.77877 100.16367 100.73786 101.37522 136.48343
## 1990 112.16361 105.38116 101.77653 98.52436 127.97532
## 1991 114.90209 105.13436 107.16041 92.90871 120.74428
## 1992 101.10457 99.63168 98.48087 102.75586 138.94607
## 1993 101.39902 100.99501 101.83094 107.33087 135.84487
## 1994 112.79604 104.97942 100.95193 102.88889 132.97131
## 1995 103.09798 98.12334 102.43011 102.28871 127.27224
## 1996 NA NA NA
# tapply(S, cycle(S), mean) #fazendo assim eu nao consigo todos os meses
# farei uma janela para 1984,7 a 1996,4
Sw<-window(S, start=c(1984,7),end=c(1996,4))
IEM<-tapply(Sw, cycle(Sw), mean) # IE medio mensal
# agora somo todos os IEM e faço o IE
IEM.total<-sum(IEM)
IE<-12*IEM/IEM.total
IE
## 1 2 3 4 5 6 7 8
## 0.8723518 0.8537530 0.9479397 0.9180656 0.9962633 0.9520214 0.9983457 1.0562029
## 9 10 11 12
## 1.0136767 1.0299569 1.0224863 1.3389367
plot(IE, type = "o")
# pega os valores estimados do componente sazonal
cons.saz.mu<-cons.components.mu$seasonal
knitr::kable(cbind(IE=IE,CONS.SAZ=unique(cons.saz.mu)))
IE | CONS.SAZ |
---|---|
0.8723518 | 0.8723518 |
0.8537530 | 0.8537530 |
0.9479397 | 0.9479397 |
0.9180656 | 0.9180656 |
0.9962633 | 0.9962633 |
0.9520214 | 0.9520214 |
0.9983457 | 0.9983457 |
1.0562029 | 1.0562029 |
1.0136767 | 1.0136767 |
1.0299569 | 1.0299569 |
1.0224863 | 1.0224863 |
1.3389367 | 1.3389367 |
# CONFERE
as_tsibble(consumo.ts) %>%
gg_season( labels = "both") +
labs(y = "Índice",
title = "Seasonal plot: varejo de SP")
## Plot variable not specified, automatically selected `y = value`
decomp.ad<-as_tsibble(consumo.ts) %>%
model(classical_decomposition(value,type="additive")) %>%
components()
decomp.ad
decomp.ad %>%
autoplot()+
labs(title = "Decomposição clássica aditiva do consumo do varejo de SP")
## Warning: Removed 6 rows containing missing values (`geom_line()`).
decomp.mu<-as_tsibble(consumo.ts) %>%
model(classical_decomposition(value,type="mult")) %>%
components()
decomp.mu
decomp.mu %>%
autoplot()+
labs(title = "Decomposição clássica multiplicativa do consumo do varejo de SP")
## Warning: Removed 6 rows containing missing values (`geom_line()`).
HYNDMAN, Rob. (2018). fpp2: Data for “Forecasting: Principles and Practice” (2nd Edition). R package version 2.3. Disponível em: https://CRAN.R-project.org/package=fpp2.
HYNDMAN, R.J., & ATHANASOPOULOS, G. (2018) Forecasting: principles and practice, 2nd edition, OTexts: Melbourne, Australia. Disponível em: https://otexts.com/fpp2/. Accessed on 27 Mar 2023.
HYNDMAN, R.J., & ATHANASOPOULOS, G. (2021) Forecasting: principles and practice, 3rd edition, OTexts: Melbourne, Australia. Disponível em: https://otexts.com/fpp3/.
MORETTIN, Pedro A.; TOLOI, Clélia M.C. Análise de Séries Temporais. São paulo: Editora Edgard Blücher, 2004. https://www.ime.usp.br/~pam/st/