rm(list=ls())
install.packages("readxl", repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'readxl' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'readxl'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\Thomas\AppData\Local\R\win-library\4.2\00LOCK\readxl\libs\x64\readxl.dll
## to C:\Users\Thomas\AppData\Local\R\win-library\4.2\readxl\libs\x64\readxl.dll:
## Permission denied
## Warning: restored 'readxl'
##
## The downloaded binary packages are in
## C:\Users\Thomas\AppData\Local\Temp\RtmpacPDMy\downloaded_packages
install.packages("readr",repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'readr' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Thomas\AppData\Local\Temp\RtmpacPDMy\downloaded_packages
install.packages("dplyr", repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'dplyr' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'dplyr'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\Thomas\AppData\Local\R\win-library\4.2\00LOCK\dplyr\libs\x64\dplyr.dll
## to C:\Users\Thomas\AppData\Local\R\win-library\4.2\dplyr\libs\x64\dplyr.dll:
## Permission denied
## Warning: restored 'dplyr'
##
## The downloaded binary packages are in
## C:\Users\Thomas\AppData\Local\Temp\RtmpacPDMy\downloaded_packages
install.packages('tidyverse', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'tidyverse' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Thomas\AppData\Local\Temp\RtmpacPDMy\downloaded_packages
install.packages('BETS', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'BETS' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Thomas\AppData\Local\Temp\RtmpacPDMy\downloaded_packages
install.packages('urca', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'urca' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'urca'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\Thomas\AppData\Local\R\win-library\4.2\00LOCK\urca\libs\x64\urca.dll
## to C:\Users\Thomas\AppData\Local\R\win-library\4.2\urca\libs\x64\urca.dll:
## Permission denied
## Warning: restored 'urca'
##
## The downloaded binary packages are in
## C:\Users\Thomas\AppData\Local\Temp\RtmpacPDMy\downloaded_packages
install.packages('TSA', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'TSA' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Thomas\AppData\Local\Temp\RtmpacPDMy\downloaded_packages
install.packages('lmtest', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'lmtest' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'lmtest'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\Thomas\AppData\Local\R\win-library\4.2\00LOCK\lmtest\libs\x64\lmtest.dll
## to C:\Users\Thomas\AppData\Local\R\win-library\4.2\lmtest\libs\x64\lmtest.dll:
## Permission denied
## Warning: restored 'lmtest'
##
## The downloaded binary packages are in
## C:\Users\Thomas\AppData\Local\Temp\RtmpacPDMy\downloaded_packages
install.packages('aTSA', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'aTSA' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Thomas\AppData\Local\Temp\RtmpacPDMy\downloaded_packages
install.packages('forecast', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'forecast' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'forecast'
## Warning in file.copy(savedcopy, lib, recursive = TRUE):
## problem copying C:\Users\Thomas\AppData\Local\R\win-
## library\4.2\00LOCK\forecast\libs\x64\forecast.dll to C:
## \Users\Thomas\AppData\Local\R\win-library\4.2\forecast\libs\x64\forecast.dll:
## Permission denied
## Warning: restored 'forecast'
##
## The downloaded binary packages are in
## C:\Users\Thomas\AppData\Local\Temp\RtmpacPDMy\downloaded_packages
library(readxl)
## Warning: package 'readxl' was built under R version 4.2.2
library(readr)
## Warning: package 'readr' was built under R version 4.2.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.2
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.2
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ stringr 1.4.1
## ✔ tidyr 1.2.1 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(BETS)
## Warning: package 'BETS' was built under R version 4.2.2
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
##
## Attaching package: 'BETS'
##
## The following object is masked from 'package:stats':
##
## predict
library(urca)
## Warning: package 'urca' was built under R version 4.2.2
library(TSA)
## Warning: package 'TSA' was built under R version 4.2.2
## Registered S3 methods overwritten by 'TSA':
## method from
## fitted.Arima forecast
## plot.Arima forecast
##
## Attaching package: 'TSA'
##
## The following object is masked from 'package:readr':
##
## spec
##
## The following objects are masked from 'package:stats':
##
## acf, arima
##
## The following object is masked from 'package:utils':
##
## tar
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.2.2
## Carregando pacotes exigidos: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(aTSA)
##
## Attaching package: 'aTSA'
##
## The following object is masked from 'package:graphics':
##
## identify
library(forecast)
## Warning: package 'forecast' was built under R version 4.2.2
##
## Attaching package: 'forecast'
##
## The following object is masked from 'package:aTSA':
##
## forecast
df_projeto <- read_excel("C:/Users/Thomas/Desktop/df projeto.xlsx")
## New names:
## • `` -> `...1`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
## • `` -> `...11`
## • `` -> `...12`
## • `` -> `...13`
## • `` -> `...14`
dfpo1 <- select(df_projeto,3)
dfpo1 <- dfpo1 %>% slice(-1)
tsdf <- ts(dfpo1, start=c(2012, 03), end=c(2019, 12), frequency=12)
plot.ts(tsdf,col=2,main="População ocupada",ylab="Ocupação",xlab="Mês")
monthplot(tsdf)
### Vejo que março é um mês onde a ocupação tem queda relevante,
portanto há sazonalidade. PArece tambem que do primeiro para o segundo
trimestre do ano a ocupação é menor, isso dialoga com o comercio no
Brasil que é mais forte no final do ano devido aos feriados.
plot(decompose(tsdf))
adf.test(tsdf)
## Augmented Dickey-Fuller Test
## alternative: stationary
##
## Type 1: no drift no trend
## lag ADF p.value
## [1,] 0 1.041 0.918
## [2,] 1 0.126 0.679
## [3,] 2 0.222 0.706
## [4,] 3 0.556 0.801
## Type 2: with drift no trend
## lag ADF p.value
## [1,] 0 -0.976 0.704
## [2,] 1 -1.792 0.410
## [3,] 2 -1.651 0.464
## [4,] 3 -1.274 0.600
## Type 3: with drift and trend
## lag ADF p.value
## [1,] 0 -1.22 0.900
## [2,] 1 -2.13 0.518
## [3,] 2 -1.98 0.580
## [4,] 3 -1.56 0.756
## ----
## Note: in fact, p.value = 0.01 means p.value <= 0.01
acf(tsdf, lag.max = 24)
pacf(tsdf, lag.max = 24)
adf.trend <- ur.df(tsdf, type = c('trend'), lags = 12)
adf.trend@teststat
## tau3 phi2 phi3
## statistic -3.271578 3.730011 5.360429
adf.trend@cval
## 1pct 5pct 10pct
## tau3 -4.04 -3.45 -3.15
## phi2 6.50 4.88 4.16
## phi3 8.73 6.49 5.47
adf.drift <- ur.df(tsdf, type = c('drift'), lags = 12)
adf.drift@teststat
## tau2 phi1
## statistic -3.029467 4.821761
adf.drift@cval
## 1pct 5pct 10pct
## tau2 -3.51 -2.89 -2.58
## phi1 6.70 4.71 3.86
ts.plot(diff(tsdf))
ts.plot(diff(diff(tsdf)))
ts.plot(diff(diff(diff(tsdf))))
ts.plot(diff(diff(diff(diff(tsdf)))))
ts.plot(diff(log(tsdf)))
acf(diff(log(tsdf)), lag.max = 36)
adf.trend.tsdf <- ur.df(diff(log(tsdf)), type = "trend" , lag = 12, selectlags = "AIC")
adf.trend.tsdf@teststat
## tau3 phi2 phi3
## statistic -5.604845 10.55467 15.80068
adf.trend.tsdf@cval
## 1pct 5pct 10pct
## tau3 -4.04 -3.45 -3.15
## phi2 6.50 4.88 4.16
## phi3 8.73 6.49 5.47
acf(diff(log(tsdf)), lag.max = 36)
pacf(diff(log(tsdf)), lag.max = 36)
forecast::auto.arima(log(tsdf))
## Series: log(tsdf)
## ARIMA(2,1,0)(2,0,0)[12]
##
## Coefficients:
## ar1 ar2 sar1 sar2
## 0.6492 -0.1723 0.2816 0.1986
## s.e. 0.1064 0.1060 0.1048 0.1077
##
## sigma^2 = 0.01202: log likelihood = 74.2
## AIC=-138.4 AICc=-137.71 BIC=-125.74
fit1.sarima <- Arima(log(tsdf), order = c(1,1,0), seasonal = c(1,0,0))
fit1.sarima
## Series: log(tsdf)
## ARIMA(1,1,0)(1,0,0)[12]
##
## Coefficients:
## ar1 sar1
## 0.5291 0.4005
## s.e. 0.0872 0.0963
##
## sigma^2 = 0.01245: log likelihood = 71.77
## AIC=-137.54 AICc=-137.27 BIC=-129.94
BETS::t_test(fit1.sarima, alpha = 0.05)
## Coeffs Std.Errors t Crit.Values Rej.H0
## ar1 0.5291498 0.08721198 6.067398 1.986377 TRUE
## sar1 0.4005210 0.09632992 4.157805 1.986377 TRUE
fit2.sarima <- Arima(log(tsdf), order = c(2,1,0), seasonal = c(2,0,0))
fit2.sarima
## Series: log(tsdf)
## ARIMA(2,1,0)(2,0,0)[12]
##
## Coefficients:
## ar1 ar2 sar1 sar2
## 0.6492 -0.1723 0.2816 0.1986
## s.e. 0.1064 0.1060 0.1048 0.1077
##
## sigma^2 = 0.01202: log likelihood = 74.2
## AIC=-138.4 AICc=-137.71 BIC=-125.74
BETS::t_test(fit1.sarima, alpha = 0.05)
## Coeffs Std.Errors t Crit.Values Rej.H0
## ar1 0.5291498 0.08721198 6.067398 1.986377 TRUE
## sar1 0.4005210 0.09632992 4.157805 1.986377 TRUE
plot(fit1.sarima$residuals)
plot(fit2.sarima$residuals)
plot(forecast(object = fit1.sarima, h=12, level=0.95))
tsdf2 <- ts(dfpo1, start=c(2012, 03), end=c(2022, 08), frequency=12)
plot(tsdf2)