Primeiro, importamos os dados da taxa de juros e venda de veículos
dados <- read_excel("taxa de câmbio.xlsx")
## tibble[,2] [158 x 2] (S3: tbl_df/tbl/data.frame)
## $ data: POSIXct[1:158], format: "1999-01-01" "1999-02-01" ...
## $ txc : num [1:158] 1.98 2.06 1.72 1.66 1.72 1.77 1.79 1.92 1.92 1.95 ...
acf(dados$txc, plot = FALSE)
##
## Autocorrelations of series 'dados$txc', by lag
##
## 0 1 2 3 4 5 6 7 8 9 10 11 12
## 1.000 0.963 0.937 0.893 0.848 0.800 0.754 0.710 0.684 0.655 0.636 0.616 0.597
## 13 14 15 16 17 18 19 20 21
## 0.577 0.560 0.534 0.512 0.477 0.446 0.413 0.379 0.341
pacf(dados$txc, plot = FALSE)
##
## Partial autocorrelations of series 'dados$txc', by lag
##
## 1 2 3 4 5 6 7 8 9 10 11
## 0.963 0.128 -0.238 -0.098 -0.030 0.017 0.019 0.243 -0.003 0.001 -0.031
## 12 13 14 15 16 17 18 19 20 21
## -0.069 -0.024 0.046 -0.060 0.014 -0.128 -0.037 0.011 -0.005 -0.094
arima(dados$txc, order = c(1,0,0))
##
## Call:
## arima(x = dados$txc, order = c(1, 0, 0))
##
## Coefficients:
## ar1 intercept
## 0.9637 2.1183
## s.e. 0.0190 0.2592
##
## sigma^2 estimated as 0.01833: log likelihood = 90.43, aic = -174.86
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.963711 0.018992 50.7434 < 2.2e-16 ***
## intercept 2.118316 0.259187 8.1729 3.01e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
AIC(arima(dados$txc, order = c(1,0,0)))
## [1] -174.8581
BIC(arima(dados$txc, order = c(1,0,0)))
## [1] -165.6703
arima(dados$txc, order = c(3,0,0))
##
## Call:
## arima(x = dados$txc, order = c(3, 0, 0))
##
## Coefficients:
## ar1 ar2 ar3 intercept
## 0.8686 0.3580 -0.2657 2.1398
## s.e. 0.0762 0.1005 0.0780 0.2368
##
## sigma^2 estimated as 0.01675: log likelihood = 97.42, aic = -184.84
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.868623 0.076153 11.4062 < 2.2e-16 ***
## ar2 0.357986 0.100476 3.5629 0.0003668 ***
## ar3 -0.265717 0.078032 -3.4053 0.0006610 ***
## intercept 2.139845 0.236843 9.0349 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
AIC(arima(dados$txc, order = c(3,0,0)))
## [1] -184.8438
BIC(arima(dados$txc, order = c(3,0,0)))
## [1] -169.5308
arima(dados$txc, order = c(8,0,0))
##
## Call:
## arima(x = dados$txc, order = c(8, 0, 0))
##
## Coefficients:
## ar1 ar2 ar3 ar4 ar5 ar6 ar7 ar8
## 0.8282 0.4178 -0.1882 -0.0658 0.0610 -0.1838 -0.2197 0.3230
## s.e. 0.0747 0.1002 0.1057 0.1059 0.1074 0.1069 0.1021 0.0775
## intercept
## 2.0824
## s.e. 0.2949
##
## sigma^2 estimated as 0.01485: log likelihood = 106.44, aic = -192.89
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.828201 0.074742 11.0809 < 2.2e-16 ***
## ar2 0.417813 0.100185 4.1704 3.041e-05 ***
## ar3 -0.188159 0.105678 -1.7805 0.07500 .
## ar4 -0.065845 0.105880 -0.6219 0.53402
## ar5 0.060975 0.107400 0.5677 0.57022
## ar6 -0.183808 0.106885 -1.7197 0.08549 .
## ar7 -0.219728 0.102101 -2.1521 0.03139 *
## ar8 0.323030 0.077526 4.1667 3.090e-05 ***
## intercept 2.082413 0.294850 7.0626 1.634e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
AIC(arima(dados$txc, order = c(8,0,0)))
## [1] -192.8866
BIC(arima(dados$txc, order = c(8,0,0)))
## [1] -162.2606
tseries::adf.test(dados$txc)
##
## Augmented Dickey-Fuller Test
##
## data: dados$txc
## Dickey-Fuller = -2.417, Lag order = 5, p-value = 0.4025
## alternative hypothesis: stationary
## Augmented Dickey-Fuller Test
## alternative: stationary
##
## Type 1: no drift no trend
## lag ADF p.value
## [1,] 0 -0.504 0.499
## [2,] 1 -0.510 0.496
## [3,] 2 -0.417 0.524
## [4,] 3 -0.381 0.534
## [5,] 4 -0.439 0.518
## Type 2: with drift no trend
## lag ADF p.value
## [1,] 0 -1.52 0.514
## [2,] 1 -1.26 0.604
## [3,] 2 -1.79 0.412
## [4,] 3 -1.99 0.332
## [5,] 4 -1.87 0.378
## Type 3: with drift and trend
## lag ADF p.value
## [1,] 0 -1.94 0.600
## [2,] 1 -1.68 0.709
## [3,] 2 -2.35 0.425
## [4,] 3 -2.66 0.298
## [5,] 4 -2.51 0.363
## ----
## Note: in fact, p.value = 0.01 means p.value <= 0.01
pp.test(dados$txc)
## Phillips-Perron Unit Root Test
## alternative: stationary
##
## Type 1: no drift no trend
## lag Z_rho p.value
## 4 -0.406 0.6
## -----
## Type 2: with drift no trend
## lag Z_rho p.value
## 4 -5.7 0.418
## -----
## Type 3: with drift and trend
## lag Z_rho p.value
## 4 -7.55 0.61
## ---------------
## Note: p-value = 0.01 means p.value <= 0.01
txc_diff <- diff(dados$txc)
tseries::adf.test(txc_diff)
## Warning in tseries::adf.test(txc_diff): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: txc_diff
## Dickey-Fuller = -4.9841, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
## Augmented Dickey-Fuller Test
## alternative: stationary
##
## Type 1: no drift no trend
## lag ADF p.value
## [1,] 0 -14.46 0.01
## [2,] 1 -7.35 0.01
## [3,] 2 -5.97 0.01
## [4,] 3 -5.40 0.01
## [5,] 4 -4.94 0.01
## Type 2: with drift no trend
## lag ADF p.value
## [1,] 0 -14.42 0.01
## [2,] 1 -7.33 0.01
## [3,] 2 -5.95 0.01
## [4,] 3 -5.38 0.01
## [5,] 4 -4.93 0.01
## Type 3: with drift and trend
## lag ADF p.value
## [1,] 0 -14.42 0.01
## [2,] 1 -7.38 0.01
## [3,] 2 -6.03 0.01
## [4,] 3 -5.46 0.01
## [5,] 4 -5.00 0.01
## ----
## Note: in fact, p.value = 0.01 means p.value <= 0.01
pp.test(txc_diff)
## Phillips-Perron Unit Root Test
## alternative: stationary
##
## Type 1: no drift no trend
## lag Z_rho p.value
## 4 -211 0.01
## -----
## Type 2: with drift no trend
## lag Z_rho p.value
## 4 -211 0.01
## -----
## Type 3: with drift and trend
## lag Z_rho p.value
## 4 -210 0.01
## ---------------
## Note: p-value = 0.01 means p.value <= 0.01
arima(dados$txc, order = c(1,1,1))
##
## Call:
## arima(x = dados$txc, order = c(1, 1, 1))
##
## Coefficients:
## ar1 ma1
## -0.7465 0.5961
## s.e. 0.2651 0.3253
##
## sigma^2 estimated as 0.0178: log likelihood = 93.44, aic = -180.89
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 -0.74647 0.26510 -2.8158 0.004865 **
## ma1 0.59613 0.32533 1.8324 0.066893 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
AIC(arima(dados$txc, order = c(1,1,1)))
## [1] -180.8858
BIC(arima(dados$txc, order = c(1,1,1)))
## [1] -171.7171
arima(dados$txc, order = c(1,1,0))
##
## Call:
## arima(x = dados$txc, order = c(1, 1, 0))
##
## Coefficients:
## ar1
## -0.1470
## s.e. 0.0788
##
## sigma^2 estimated as 0.0183: log likelihood = 91.29, aic = -178.58
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 -0.147000 0.078781 -1.8659 0.06205 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
AIC(arima(dados$txc, order = c(1,1,0)))
## [1] -178.5827
BIC(arima(dados$txc, order = c(1,1,0)))
## [1] -172.4702
arima(dados$txc, order = c(0,1,1))
##
## Call:
## arima(x = dados$txc, order = c(0, 1, 1))
##
## Coefficients:
## ma1
## -0.0980
## s.e. 0.0642
##
## sigma^2 estimated as 0.01843: log likelihood = 90.71, aic = -177.43
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ma1 -0.097975 0.064210 -1.5258 0.127
AIC(arima(dados$txc, order = c(0,1,1)))
## [1] -177.4265
BIC(arima(dados$txc, order = c(0,1,1)))
## [1] -171.314
arima(dados$txc, order = c(2,1,1))
##
## Call:
## arima(x = dados$txc, order = c(2, 1, 1))
##
## Coefficients:
## ar1 ar2 ma1
## 0.0625 0.2791 -0.1861
## s.e. 0.2329 0.0810 0.2371
##
## sigma^2 estimated as 0.01711: log likelihood = 96.49, aic = -184.97
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.062502 0.232910 0.2684 0.7884288
## ar2 0.279125 0.081033 3.4446 0.0005719 ***
## ma1 -0.186057 0.237113 -0.7847 0.4326423
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
AIC(arima(dados$txc, order = c(2,1,1)))
## [1] -184.9743
BIC(arima(dados$txc, order = c(2,1,1)))
## [1] -172.7494
arima(dados$txc, order = c(2,1,2))
##
## Call:
## arima(x = dados$txc, order = c(2, 1, 2))
##
## Coefficients:
## ar1 ar2 ma1 ma2
## 0.1015 0.1945 -0.2300 0.0979
## s.e. 0.3113 0.1796 0.3188 0.1759
##
## sigma^2 estimated as 0.01708: log likelihood = 96.63, aic = -183.26
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.101484 0.311317 0.3260 0.7444
## ar2 0.194500 0.179621 1.0828 0.2789
## ma1 -0.230048 0.318776 -0.7217 0.4705
## ma2 0.097854 0.175885 0.5563 0.5780
AIC(arima(dados$txc, order = c(2,1,2)))
## [1] -183.265
BIC(arima(dados$txc, order = c(2,1,2)))
## [1] -167.9837
arima(dados$txc, order = c(3,1,3))
##
## Call:
## arima(x = dados$txc, order = c(3, 1, 3))
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2 ma3
## -0.4844 -0.1502 0.4674 0.4171 0.3878 -0.4326
## s.e. 0.2792 0.2795 0.2107 0.2815 0.2526 0.2513
##
## sigma^2 estimated as 0.01645: log likelihood = 99.23, aic = -184.46
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 -0.48443 0.27918 -1.7352 0.08271 .
## ar2 -0.15024 0.27946 -0.5376 0.59085
## ar3 0.46738 0.21066 2.2186 0.02651 *
## ma1 0.41710 0.28154 1.4815 0.13847
## ma2 0.38780 0.25263 1.5350 0.12477
## ma3 -0.43255 0.25128 -1.7214 0.08518 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
AIC(arima(dados$txc, order = c(3,1,3)))
## [1] -184.4587
BIC(arima(dados$txc, order = c(3,1,3)))
## [1] -163.065
txc_estim <- dados %>% dplyr::filter(data < "2010-12-31")
txc_aval <- dados %>% dplyr::filter(data >= "2010-12-31")
ar1_estim <- arima(txc_estim$txc, order = c(1,0,0))
##
## Call:
## arima(x = txc_estim$txc, order = c(1, 0, 0))
##
## Coefficients:
## ar1 intercept
## 0.9626 2.1462
## s.e. 0.0203 0.2687
##
## sigma^2 estimated as 0.01916: log likelihood = 79.13, aic = -152.26
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.962608 0.020322 47.3682 < 2.2e-16 ***
## intercept 2.146243 0.268732 7.9865 1.388e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
newfit <- Arima(c(txc_estim$txc, txc_aval$txc), model = ar1_estim)
previsao_ar1 <- fitted(newfit)[145:158]
## [1] 1.687808 1.687808 1.678182 1.649303 1.591547 1.601173 1.581921 1.581921
## [9] 1.610799 1.861077 1.707060 1.832199 1.889955 1.755190
ar3_estim <- arima(txc_estim$txc, order = c(3,0,0))
##
## Call:
## arima(x = txc_estim$txc, order = c(3, 0, 0))
##
## Coefficients:
## ar1 ar2 ar3 intercept
## 0.8766 0.3645 -0.2830 2.1831
## s.e. 0.0793 0.1053 0.0813 0.2371
##
## sigma^2 estimated as 0.01738: log likelihood = 86.02, aic = -162.03
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.876616 0.079290 11.0558 < 2.2e-16 ***
## ar2 0.364494 0.105292 3.4618 0.0005367 ***
## ar3 -0.283009 0.081279 -3.4819 0.0004978 ***
## intercept 2.183135 0.237081 9.2084 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
newfit <- Arima(c(txc_estim$txc, txc_aval$txc), model = ar3_estim)
previsao_ar3 <- fitted(newfit)[145:158]
## [1] 1.701234 1.677349 1.682733 1.652790 1.592088 1.587475 1.590568 1.580448
## [9] 1.612407 1.851262 1.797281 1.779340 1.924603 1.786955
ar8_estim <- arima(txc_estim$txc, order = c(8,0,0))
##
## Call:
## arima(x = txc_estim$txc, order = c(8, 0, 0))
##
## Coefficients:
## ar1 ar2 ar3 ar4 ar5 ar6 ar7 ar8
## 0.8370 0.4230 -0.2255 -0.0424 0.0883 -0.2113 -0.2327 0.3331
## s.e. 0.0777 0.1048 0.1097 0.1102 0.1111 0.1089 0.1040 0.0787
## intercept
## 2.1409
## s.e. 0.2856
##
## sigma^2 estimated as 0.01522: log likelihood = 95.05, aic = -170.1
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.836990 0.077660 10.7776 < 2.2e-16 ***
## ar2 0.423036 0.104750 4.0385 5.379e-05 ***
## ar3 -0.225467 0.109668 -2.0559 0.03979 *
## ar4 -0.042382 0.110167 -0.3847 0.70046
## ar5 0.088327 0.111148 0.7947 0.42680
## ar6 -0.211291 0.108908 -1.9401 0.05237 .
## ar7 -0.232690 0.103976 -2.2379 0.02523 *
## ar8 0.333079 0.078708 4.2318 2.318e-05 ***
## intercept 2.140932 0.285591 7.4965 6.555e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
newfit <- Arima(c(txc_estim$txc, txc_aval$txc), model = ar8_estim)
previsao_ar8 <- fitted(newfit)[145:158]
## [1] 1.706501 1.676879 1.681285 1.670006 1.575066 1.574483 1.594186 1.568822
## [9] 1.601384 1.849750 1.819147 1.782291 1.923195 1.818834
mean((txc_aval$txc - previsao_ar1)^2)
## [1] 0.009866348
mean((txc_aval$txc - previsao_ar3)^2)
## [1] 0.01042449
mean((txc_aval$txc - previsao_ar8)^2)
## [1] 0.01131308
mae(txc_aval$txc, previsao_ar1)
## [1] 0.07299892
mae(txc_aval$txc, previsao_ar3)
## [1] 0.07482712
mae(txc_aval$txc, previsao_ar8)
## [1] 0.0770292
mape(txc_aval$txc, previsao_ar1)
## [1] 0.04220973
mape(txc_aval$txc, previsao_ar3)
## [1] 0.04329105
mape(txc_aval$txc, previsao_ar8)
## [1] 0.04465084
ar1_full <- arima(dados$txc, order = c(1,0,0))
forc_ar1 <- forecast(ar1_full, 10)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 159 1.724817 1.551315 1.898320 1.459468 1.990167
## 160 1.739097 1.498138 1.980056 1.370582 2.107612
## 161 1.752859 1.462985 2.042733 1.309535 2.196183
## 162 1.766121 1.437271 2.094971 1.263188 2.269053
## 163 1.778902 1.417599 2.140204 1.226338 2.331465
## 164 1.791219 1.402194 2.180243 1.196257 2.386180
## 165 1.803089 1.389980 2.216197 1.171294 2.434883
## 166 1.814528 1.380246 2.248809 1.150352 2.478704
## 167 1.825552 1.372492 2.278613 1.132656 2.518448
## 168 1.836176 1.366347 2.306006 1.117634 2.554718
arima211 <- arima(dados$txc, order = c(2,1,1))
forc_arima211 <- forecast(arima211, 1)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 159 1.681677 1.514039 1.849315 1.425297 1.938057