Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Pesquisa
Views: 261
1
2
# https://cran.r-project.org/web/views/TimeSeries.html
3
# https://www.datacamp.com/tracks/time-series-with-r
4
# https://www.datascience.com/blog/introduction-to-forecasting-with-arima-in-r-learn-data-science-tutorials
5
# https://www.otexts.org/fpp/8/7
6
7
# install.packages('robust')
8
#install.packages('TSPred')
9
10
library(TSPred)
11
library(hexbin)
12
library(robust)
13
library(imputeTS)
14
library(tseries)
15
library(forecast)
16
library(fUnitRoots)
17
library(portes)
18
library(nortest)
19
library(tsoutliers)
20
library(boot)
21
22
# T <- read.xls("Dados das estacoes com 2018.xlsx", sheet = 1, header = TRUE, stringsAsFactors = FALSE)
23
T <- read.csv("Dados das estacoes com 2018.csv")
24
25
head(T)
26
27
sapply(T, class) # Verificando os tipos
28
29
T_INMET <- ts(T$T_INMET,start=c(1961,1),f=12)
30
plot(T_INMET)
31
32
T_ICEA <- ts(T$T_ICEA,start=c(1961,1),f=12)
33
plot(T_ICEA)
34
35
mod1 = lmRob(T_INMET ~ T_ICEA, data = T)
36
summary(mod1)
37
38
bin <- hexbin(T_INMET, T_ICEA, xbins=30)
39
plot(bin)
40
41
aux <- T[is.na(T$T_INMET)&!is.na(T$T_ICEA),]
42
head(aux)
43
44
aux <- predict(mod1, newdata=T[is.na(T$T_INMET)&!is.na(T$T_ICEA),]) # Estima INMET com base no ICEA
45
46
head(aux)
47
48
# T[is.na(T$T_INMET)&!is.na(T$T_ICEA),] <- aux
49
50
T[is.na(T$T_INMET)&!is.na(T$T_ICEA),'T_INMET'] <- aux
51
52
T_INMET <- ts(T$T_INMET,start=c(1961,1),f=12)
53
plot(T_INMET)
54
55
# As falhas que sobraram foram preenchidas pelo estimador de Kalman.
56
T_INMET <- na.kalman(T_INMET, model = "auto.arima")
57
plot(T_INMET)
58
59
UR_INMET <- ts(T$UR_INMET,start=c(1961,1),f=12)
60
plot(UR_INMET)
61
62
UR_ICEA <- ts(T$UR_ICEA,start=c(1961,1),f=12)
63
plot(UR_ICEA)
64
65
mod2 = lmRob(UR_INMET ~ UR_ICEA, data = T)
66
summary(mod2)
67
68
bin <- hexbin(UR_INMET, UR_ICEA, xbins=30)
69
plot(bin)
70
71
aux <- T[is.na(T$UR_INMET)&!is.na(T$UR_ICEA),]
72
head(aux)
73
74
aux <- predict(mod2, newdata=T[is.na(T$UR_INMET)&!is.na(T$UR_ICEA),]) # Estima INMET com base no ICEA
75
76
head(aux)
77
78
T[is.na(T$UR_INMET)&!is.na(T$UR_ICEA),'UR_INMET'] <- aux
79
80
UR_INMET <- ts(T$UR_INMET,start=c(1961,1),f=12)
81
plot(UR_INMET)
82
83
# As falhas que sobraram foram preenchidas pelo estimador de Kalman.
84
UR_INMET <- na.kalman(UR_INMET, model = "auto.arima")
85
plot(UR_INMET)
86
87
install.packages('weathermetrics')
88
89
library(weathermetrics)
90
91
it <- heat.index(t=T_INMET, rh=UR_INMET, temperature.metric="celsius", output.metric="celsius", round=2)
92
it <- ts(it,start=c(1961,1),f=12)
93
plot(it)
94
95
# Decomposição da série temporal (aditiva, possivelmente baseada na transformáda rápida de Fourier, e apenas para fins de análise preliminar)
96
plot(decompose(it))
97
98
urkpssTest(it, type = c("tau"), lags = c("short"),use.lag = NULL, doplot = TRUE)
99
100
adf.test(it, alternative="stationary")
101
102
acf(it)
103
pacf(it)
104
105
ndiffs(it) # Valor de d para fazer a série estacionária.
106
107
# Calculando as correlações para uma diferença de ordem 1
108
acf(diff(it, differences=1))
109
pacf(diff(it, differences=1))
110
111
ajuste_0 <- Arima(it, c(1, 1, 2)) # Sem sazonalidade
112
ajuste_0
113
114
ajuste_1 <- Arima(it, c(1, 1, 2), seasonal=list(order = c(1, 1, 2), period = 12)) # Com sazonalidade
115
ajuste_1
116
117
# Automatizando
118
ajuste_auto <- auto.arima(it, stepwise=FALSE, approximation=FALSE, trace=TRUE)
119
ajuste_auto
120
121
acf(ajuste_auto$residuals)
122
pacf(ajuste_auto$residuals)
123
124
qqnorm(ajuste_auto$residuals)
125
qqline(ajuste_auto$residuals)
126
lillie.test(ajuste_auto$residuals)
127
128
l <- BoxCox.lambda(it)
129
l
130
131
ajuste_auto_bc <- auto.arima(it, stepwise=FALSE, approximation=FALSE, trace=TRUE, lambda=l)
132
ajuste_auto_bc
133
134
qqnorm(ajuste_auto_bc$residuals)
135
qqline(ajuste_auto_bc$residuals)
136
lillie.test(ajuste_auto_bc$residuals)
137
hist(ajuste_auto_bc$residuals)
138
139
# A função não possui o parâmetro lambda
140
# outliers <- tso(BoxCox(it,l),tsmethod = "auto.arima", args.tsmethod = list(lambda=l), types = c("AO","LS","TC"))
141
142
outliers <- tso(BoxCox(it,l),tsmethod = "arima", args.tsmethod = list(order=c(1, 0, 1), seasonal=list(order=c(0, 1, 1), period=12)), types = c("AO","LS","TC"), maxit.iloop=50)
143
144
outliers
145
146
it[outliers$outliers$ind] <- NA
147
148
# Substituindo os outliers por estimativas...
149
it <- na.kalman(it, model = "auto.arima")
150
151
l <- BoxCox.lambda(it)
152
l
153
154
outliers2 <- tso(BoxCox(it,l),tsmethod = "arima", args.tsmethod = list(order=c(1, 0, 2), seasonal=list(order=c(0, 1, 1), period=12)), types = c("AO","LS","TC"), maxit.iloop=50)
155
outliers2
156
157
it[outliers2$outliers$ind] <- NA
158
it <- na.kalman(it, model = "auto.arima")
159
l <- BoxCox.lambda(it)
160
l
161
162
outliers3 <- tso(BoxCox(it,l),tsmethod = "arima", args.tsmethod = list(order=c(1, 0, 2), seasonal=list(order=c(0, 1, 1), period=12)), types = c("AO","LS","TC"), maxit.iloop=50)
163
outliers3
164
165
# Reajustando
166
ajuste_auto_bc_outliers <- auto.arima(it, stepwise=FALSE, approximation=FALSE, trace=TRUE, lambda=l)
167
ajuste_auto_bc_outliers
168
169
qqnorm(ajuste_auto_bc_outliers$residuals)
170
qqline(ajuste_auto_bc_outliers$residuals)
171
lillie.test(ajuste_auto_bc_outliers$residuals)
172
hist(ajuste_auto_bc_outliers$residuals)
173
174
LjungBox(ajuste_auto_bc_outliers, lag=c(5, 10, 20, 25, 39, 35, 40, 45, 50), season=12) # Testa se existe autocorrelação entre os dados considerando a sazonalidade
175
176
previsoes_auto_bc_outliers <- forecast(ajuste_auto_bc_outliers, h=3)
177
previsoes_auto_bc_outliers
178
accuracy(previsoes_auto_bc_outliers)
179
180
mod2 = lmRob(ajuste_auto_bc_outliers$x ~ ajuste_auto_bc_outliers$fitted, data = T)
181
summary(mod2)
182
183
options(repr.plot.width=8, repr.plot.height=8)
184
bin <- hexbin(ajuste_auto_bc_outliers$fitted, ajuste_auto_bc_outliers$x, xbins=30)
185
plot(bin)
186
187
options(repr.plot.width=16, repr.plot.height=8)
188
ts.plot(ajuste_auto_bc_outliers$fitted,ajuste_auto_bc_outliers$x, lty=c(2,1), gpars = list(col = c("blue", "black")))
189
190
plotarimapred(it, ajuste_auto_bc_outliers, xlim=c(2010, 2021))
191
192
AIC_Boot <- function(ts) {
193
aux <- Arima(ts, lambda=l, order=c(1, 0, 0), seasonal=list(order=c(0, 1, 1), period=12))
194
return(aux$aic)
195
}
196
197
aux <- Arima(it, lambda=l, order=c(1, 0, 0), seasonal=list(order=c(0, 1, 1), period=12))
198
199
AIC_Boot(it)
200
201
# Bootstrap estacionário com bloco de 20
202
boot_ajuste_auto_bc_outliers <- tsboot(it, AIC_Boot, R = 1000, l = 20, sim = "geom")
203
204
boot_ajuste_auto_bc_outliers
205
206
boot.ci(boot_ajuste_auto_bc_outliers)
207
208