12.9 Testes

12.9.1 Testes de independência

The aim of time-series modeling may be thought of as, given an observed series, finding a transformation which when applied to the series yields a residual series whose members are independent and identically distributed. (Kotz et al. 2005, 6297)

Os testes a seguir são chamados de portmanteau.

12.9.1.1 Teste de Box-Pierce

[I]f the fitted model is appropriate and the parameters \(\phi\) are exactly known, then the calculated \(a_t\)’s would be uncorrelated normal deviates, their serial correlations \(r\) would be approximately \(N(0,(1/n)I)\), and thus \(n \sum_{1}^{m} r_{k}^2\) would possess a \(\chi^2\) distribution with \(m\) degrees of freedom. (Box and Pierce 1970, 1517)

\[\left\{ \begin{array}{l} H_0: \text{A série temporal é distribuída de forma independente.}\\ H_1: \text{A série temporal não é distribuída de forma independente, possuindo correlação serial.} \\ \end{array} \right. \]

Considerando \(Q \equiv Q(\hat{r})\), as estimativas \(\hat{\rho}(k)\) e \(n\) suficientemente grande, \[\begin{equation} Q = n \sum_{k=1}^{m} \hat{\rho}(k)^2 \sim \chi_{m-p-q}^2 \tag{12.15} \end{equation}\] onde \(n\) é o número de observações, \(\hat{\rho}(k)\) é a estimativa da autocorrelação de ordem \(k\) conforme Eq. (12.8), \(m\) é o número de lags considerados, \(p\) é a ordem autorregressiva e \(q\) a ordem das médias móveis no modelo ARMA.

12.9.1.2 Teste de Ljung-Box

The observed discrepancies [of “suspiciously low values of \(Q(\hat{r})\)”] could be accounted for by several factors, for instance departures from normality of the autocorrelations. (Ljung and Box 1978, 298)

Considernado as discrepâncias observadas, (Ljung and Box 1978, 298) sugerem uma estatística de teste modificada \[\begin{equation} \tilde{Q} = n(n+2) \sum_{k=1}^{m} \frac{\hat{\rho}(k)^2}{n-k} \sim \chi_{m-p-q}^2 \tag{12.16} \end{equation}\] com os mesmos argumentos de \(Q\).

A função stats::Box.test() calcula a estatística de teste Box–Pierce ou Ljung–Box para examinar a hipótese nula de independência em uma série temporal, baseada em (Box and Pierce 1970), (Ljung and Box 1978) e (Harvey 1993). Note que \(m=\)lag, a função não opera com NA, e traz em sua documentação o detalhe a seguir.

These tests are sometimes applied to the residuals from an ARMA(p, q) fit, in which case the references suggest a better approximation to the null-hypothesis distribution is obtained by setting fitdf = p+q, provided of course that lag > fitdf. ?stats::Box.test()

# Ruído branco gaussiano
set.seed(42); z <- rnorm(100) 
plot(z, type = 'l', main = 'Ruído branco gaussiano N(0,1)')

Box.test(z, lag = 1, type = 'Box')   # Box-Pierce
## 
##  Box-Pierce test
## 
## data:  z
## X-squared = 0.31274, df = 1, p-value = 0.576
Box.test(z, lag = 1, type = 'Ljung') # Ljung-Box
## 
##  Box-Ljung test
## 
## data:  z
## X-squared = 0.32222, df = 1, p-value = 0.5703
# Luteinizing Hormone in Blood Samples
plot(lh)

Box.test(lh, lag = 1, type = 'Box')
## 
##  Box-Pierce test
## 
## data:  lh
## X-squared = 15.899, df = 1, p-value = 6.682e-05
Box.test(lh, lag = 1, type = 'Ljung')
## 
##  Box-Ljung test
## 
## data:  lh
## X-squared = 16.914, df = 1, p-value = 3.912e-05
# Monthly corticosteroid drug subsidy in Australia from 1991 to 2008
plot(h02)

Box.test(h02, lag = 1, type = 'Box')
## 
##  Box-Pierce test
## 
## data:  h02
## X-squared = 116.23, df = 1, p-value < 2.2e-16
Box.test(h02, lag = 1, type = 'Ljung')
## 
##  Box-Ljung test
## 
## data:  h02
## X-squared = 117.95, df = 1, p-value < 2.2e-16

Podem-se testar vários lags.

box_test <- function(x, lag.max = 24, fitdf = 0){
  par(mfrow=c(1,2))
  box_matrix <- matrix(NA, nrow = lag.max, ncol = 2)
  for(m in (fitdf+1):lag.max){
    box_matrix[m,1] <- Box.test(x, lag = m, type = 'Box', fitdf = fitdf)$p.value
    box_matrix[m,2] <- Box.test(x, lag = m, type = 'Ljung', fitdf = fitdf)$p.value
  }
  plot(box_matrix[,1], main = paste0('Box-Pierce, fitdf=', fitdf), ylim = c(0,1))
  abline(h = 0.05, col = 'red', lty = 2)
  plot(box_matrix[,2], main = paste0('Ljung-Box, fitdf=', fitdf), ylim = c(0,1))
  abline(h = 0.05, col = 'red', lty = 2)
}
# Ruído branco gaussiano
set.seed(42); z <- rnorm(100) 
box_test(z)

# Luteinizing Hormone in Blood Samples
box_test(lh)

# Monthly corticosteroid drug subsidy in Australia from 1991 to 2008
box_test(h02)

12.9.2 Estacionariedade e raízes unitárias

12.9.2.1 Teste Aumentado Dickey-Fuller

A função tseries::adf.test() calcula o teste Aumentado de Dickey-Fuller (ADF) para a hipótese nula de que a série tem uma raiz unitária, baseado em (Said and Dickey 1984) e (Banerjee et al. 1993). Valores ausentes não são permitidos, e quando número de defasagens usado é k igual a zero, o teste padrão Dickey-Fuller é computado. O valor padrão de trunc((length(x)-1)^(1/3)) corresponde ao limite superior sugerido na taxa na qual o número de defasagens, \(k\), deve ser feito para crescer com o tamanho da amostra para a configuração geral \(ARMA(p,q)\). Os valores p são interpolados da Tabela 4.2 de (Banerjee et al. 1993, 103). Se a estatística computada estiver fora da tabela de valores críticos, uma mensagem de aviso será gerada.

# Ruído branco gaussiano
tseries::adf.test(z, alternative = 'stationary') # alternative = 'stationary' é o padrão
## 
##  Augmented Dickey-Fuller Test
## 
## data:  z
## Dickey-Fuller = -3.9236, Lag order = 4, p-value = 0.01573
## alternative hypothesis: stationary
tseries::adf.test(z, alternative = 'explosive')
## 
##  Augmented Dickey-Fuller Test
## 
## data:  z
## Dickey-Fuller = -3.9236, Lag order = 4, p-value = 0.9843
## alternative hypothesis: explosive
# Luteinizing Hormone in Blood Samples
tseries::adf.test(lh, alternative = 'stationary')
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lh
## Dickey-Fuller = -3.558, Lag order = 3, p-value = 0.04624
## alternative hypothesis: stationary
tseries::adf.test(lh, alternative = 'explosive')
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lh
## Dickey-Fuller = -3.558, Lag order = 3, p-value = 0.9538
## alternative hypothesis: explosive
# Monthly corticosteroid drug subsidy in Australia from 1991 to 2008
tseries::adf.test(h02, alternative = 'stationary')
## 
##  Augmented Dickey-Fuller Test
## 
## data:  h02
## Dickey-Fuller = -9.5147, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
tseries::adf.test(h02, alternative = 'explosive')
## 
##  Augmented Dickey-Fuller Test
## 
## data:  h02
## Dickey-Fuller = -9.5147, Lag order = 5, p-value = 0.99
## alternative hypothesis: explosive

Podem-se testar vários lags.

adf_test <- function(x, lag.max = 24){
  par(mfrow=c(1,2))
  adf_matrix <- matrix(NA, nrow = lag.max, ncol = 2)
  for(m in 1:lag.max){
    adf_matrix[m,1] <- tseries::adf.test(x, alternative = 'stationary', k = m)$p.value
    adf_matrix[m,2] <- tseries::adf.test(x, alternative = 'explosive', k = m)$p.value
  }
  plot(adf_matrix[,1], main = "alternative = 'stationary'", ylim = c(0,1))
  abline(h = 0.05, col = 'red', lty = 2)
  abline(v = trunc((length(x)-1)^(1/3)), col = 'blue', lty = 2)
  plot(adf_matrix[,2], main = "alternative = 'explosive'", ylim = c(0,1))
  abline(h = 0.05, col = 'red', lty = 2)
  abline(v = trunc((length(x)-1)^(1/3)), col = 'blue', lty = 2)
}
# Ruído branco gaussiano
set.seed(42); z <- rnorm(100) 
adf_test(z)

# Luteinizing Hormone in Blood Samples
adf_test(lh)

# Monthly corticosteroid drug subsidy in Australia from 1991 to 2008
adf_test(h02)

12.9.2.2 Teste de Phillips-Perron

While the Dickey-Fuller approach accounts for the autocorrelation of the first-differences of a series in a parametric fashion by estimating additional nuisance parameters, this new approach deals with this phenomenon in a nonparametric way. (Perron 1988, 297)

A função tseries::pp.test() calcula o teste de Phillips-Perron para a hipótese nula de que a série tem uma raiz unitária, baseada em (Perron 1988) e (Banerjee et al. 1993).

# Ruído branco gaussiano
tseries::pp.test(z)
## 
##  Phillips-Perron Unit Root Test
## 
## data:  z
## Dickey-Fuller Z(alpha) = -94.063, Truncation lag parameter = 3, p-value = 0.01
## alternative hypothesis: stationary
# Luteinizing Hormone in Blood Samples
tseries::pp.test(lh)
## 
##  Phillips-Perron Unit Root Test
## 
## data:  lh
## Dickey-Fuller Z(alpha) = -22.224, Truncation lag parameter = 3, p-value = 0.02442
## alternative hypothesis: stationary
# Monthly corticosteroid drug subsidy in Australia from 1991 to 2008
tseries::pp.test(h02)
## 
##  Phillips-Perron Unit Root Test
## 
## data:  h02
## Dickey-Fuller Z(alpha) = -87.279, Truncation lag parameter = 4, p-value = 0.01
## alternative hypothesis: stationary

12.9.2.3 KPSS

By testing both the unit root hypothesis and the stationarity hypothesis, we can distinguish series that appear to be stationary, series that appear to have a unit root, and series for which the data (or the tests) are not sufficiently informative to be sure whether they are stationary or integrated. (Kwiatkowski et al. 1992, 20)

A função tseries::kpss.test() calcula o teste Kwiatkowski-Phillips-Schmidt-Shin (KPSS) para a hipótese nula de que a série é nível ou tendência estacionária a partir de (Kwiatkowski et al. 1992). A função urca::ur.kpss() implementa o mesmo teste, mas retorna apenas a estatística do teste sem o p-value.

De acordo com (Rob J. Hyndman and Khandakar 2008, 10), o teste KPSS é utilizado para encontrar o argumento \(d\) do modelo ARIMA\((p,d,q)\) para séries não sazonais através da função forecast::auto.arima().

# Ruído branco gaussiano
tseries::kpss.test(z)
## 
##  KPSS Test for Level Stationarity
## 
## data:  z
## KPSS Level = 0.076489, Truncation lag parameter = 4, p-value = 0.1
# Luteinizing Hormone in Blood Samples
tseries::kpss.test(lh)
## 
##  KPSS Test for Level Stationarity
## 
## data:  lh
## KPSS Level = 0.29382, Truncation lag parameter = 3, p-value = 0.1
# Monthly corticosteroid drug subsidy in Australia from 1991 to 2008
tseries::kpss.test(h02)
## 
##  KPSS Test for Level Stationarity
## 
## data:  h02
## KPSS Level = 2.5973, Truncation lag parameter = 4, p-value = 0.01

12.9.2.4 Elliott-Rothenberg-Stock

# Ruído branco gaussiano
urca::ur.ers(z)
## 
## ############################################################### 
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test # 
## ############################################################### 
## 
## The value of the test statistic is: -1.6828
# Luteinizing Hormone in Blood Samples
urca::ur.ers(lh)
## 
## ############################################################### 
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test # 
## ############################################################### 
## 
## The value of the test statistic is: -2.8503
# Monthly corticosteroid drug subsidy in Australia from 1991 to 2008
urca::ur.ers(h02)
## 
## ############################################################### 
## # Elliot, Rothenberg and Stock Unit Root / Cointegration Test # 
## ############################################################### 
## 
## The value of the test statistic is: -2.4305

12.9.2.5 Schmidt-Phillips

# Ruído branco gaussiano
urca::ur.sp(z)
## 
## ################################################### 
## # Schmidt-Phillips Unit Root / Cointegration Test # 
## ################################################### 
## 
## The value of the test statistic is: -7.6232
# Luteinizing Hormone in Blood Samples
urca::ur.sp(lh)
## 
## ################################################### 
## # Schmidt-Phillips Unit Root / Cointegration Test # 
## ################################################### 
## 
## The value of the test statistic is: NaN
# Monthly corticosteroid drug subsidy in Australia from 1991 to 2008
urca::ur.sp(h02)
## 
## ################################################### 
## # Schmidt-Phillips Unit Root / Cointegration Test # 
## ################################################### 
## 
## The value of the test statistic is: NaN

12.9.2.6 Zivot-Andrews

# Ruído branco gaussiano
urca::ur.za(z)
## 
## ################################################ 
## # Zivot-Andrews Unit Root / Cointegration Test # 
## ################################################ 
## 
## The value of the test statistic is: -10.0143
# Luteinizing Hormone in Blood Samples
urca::ur.za(lh)
## 
## ################################################ 
## # Zivot-Andrews Unit Root / Cointegration Test # 
## ################################################ 
## 
## The value of the test statistic is: -4.5332
# Monthly corticosteroid drug subsidy in Australia from 1991 to 2008
urca::ur.za(h02)
## 
## ################################################ 
## # Zivot-Andrews Unit Root / Cointegration Test # 
## ################################################ 
## 
## The value of the test statistic is: -7.4839

12.9.2.7 OCSB

(Canova and Hansen 1995)

12.9.3 Cointegração

Quando \(X_t\) e \(Y_t\) são \(I(1)\) e se há um \(\theta\) tal que \(Y_t−\theta X_t\) é \(I(0)\), \(X_t\) e \(Y_t\) são cointegrados. Em outras palavras, a cointegração de \(X_t\) e \(Y_t\) significa que \(X_t\) e \(Y_t\) têm a mesma tendência estocástica ou uma tendência estocástica comum e que essa tendência pode ser eliminada tomando uma diferença específica da série de modo que a série resultante seja estacionária. (Hanck et al. 2024, 394)

A series with no deterministic component which has a stationary, invertible, ARMA representation after differencing \(d\) times, is said to be integrated of order \(d\), denoted \(x_t \sim I(d)\). (Engle and Granger 1987, 252)

A função aTSA::coint.test() executa testes de Engle-Granger para a hipótese nula de que duas ou mais séries temporais, cada uma das quais é \(I(1)\), não são cointegradas.

set.seed(1); X <- matrix(rnorm(200),100,2)
set.seed(2); y <- 0.3*X[,1] + 1.2*X[,2] + rnorm(100)
aTSA::coint.test(y,X)
## Response: y 
## Input: X 
## Number of inputs: 2 
## Model: y ~ X + 1 
## ------------------------------- 
## Engle-Granger Cointegration Test 
## alternative: cointegrated 
## 
## Type 1: no trend 
##     lag      EG p.value 
##    4.00   -5.30    0.01 
## ----- 
##  Type 2: linear trend 
##     lag      EG p.value 
##   4.000  -0.731   0.100 
## ----- 
##  Type 3: quadratic trend 
##     lag      EG p.value 
##   4.000   0.646   0.100 
## ----------- 
## Note: p.value = 0.01 means p.value <= 0.01 
##     : p.value = 0.10 means p.value >= 0.10
aTSA::coint.test(y,X,d = 1)
## Response: diff(y,1) 
## Input: diff(X,1) 
## Number of inputs: 2 
## Model: y ~ X - 1 
## ------------------------------- 
## Engle-Granger Cointegration Test 
## alternative: cointegrated 
## 
## Type 1: no trend 
##     lag      EG p.value 
##    4.00   -7.40    0.01 
## ----- 
##  Type 2: linear trend 
##     lag      EG p.value 
##   4.000  -0.174   0.100 
## ----- 
##  Type 3: quadratic trend 
##     lag      EG p.value 
##  4.0000  0.0401  0.1000 
## ----------- 
## Note: p.value = 0.01 means p.value <= 0.01 
##     : p.value = 0.10 means p.value >= 0.10

12.9.4 Testes de quebra estrutural

O pacote strucchange (Zeileis et al. 2002), (Zeileis et al. 2003), (Zeileis 2006) apresenta testes/métodos da estrutura de teste de flutuação generalizada, bem como da estrutura de teste F (teste de Chow). Isso inclui métodos para ajustar, plotar e testar processos de flutuação (por exemplo, CUSUM, MOSUM, estimativas recursivas/móveis) e estatísticas F, respectivamente. É possível monitorar dados recebidos on-line usando processos de flutuação. Finalmente, os pontos de interrupção em modelos de regressão com mudanças estruturais podem ser estimados junto com intervalos de confiança.

Ponte browniana

library(strucchange)
library(tstests)
# teste F de Chow
sctest(z ~ 1, type = 'Chow', point = 10) # ruído branco gaussiano
## 
##  Chow test
## 
## data:  z ~ 1
## F = 2.7636, p-value = 0.09963
sctest(h02 ~ 1, type = 'Chow', point = 10)
## 
##  Chow test
## 
## data:  h02 ~ 1
## F = 21.902, p-value = 5.253e-06
# teste de Nyblom-Hansen
sctest(z ~ 1, type = 'Nyblom-Hansen', point = 10)
## 
##  Score-based CUSUM test with mean L2 norm
## 
## data:  z ~ 1
## f(efp) = 0.45443, p-value = 0.2163
sctest(h02 ~ 1, type = 'Nyblom-Hansen', point = 10)
## 
##  Score-based CUSUM test with mean L2 norm
## 
## data:  h02 ~ 1
## f(efp) = 9.5709, p-value = 0.005
# nyblom_test()
sctest(z ~ 1, type = 'Rec-CUSUM', point = 10) # padrão
## 
##  Recursive CUSUM test
## 
## data:  z ~ 1
## S = 0.7817, p-value = 0.1549
sctest(h02 ~ 1, type = 'Rec-CUSUM', point = 10)
## 
##  Recursive CUSUM test
## 
## data:  h02 ~ 1
## S = 3.6987, p-value < 2.2e-16
sctest(z ~ 1, type = 'Rec-MOSUM', point = 10)
## 
##  Recursive MOSUM test
## 
## data:  z ~ 1
## M = 0.92666, p-value = 0.3234
sctest(h02 ~ 1, type = 'Rec-MOSUM', point = 10)
## 
##  Recursive MOSUM test
## 
## data:  h02 ~ 1
## M = 2.9991, p-value = 0.01
## dating breaks
plot(Nile)
bp.nile <- breakpoints(Nile ~ 1)
ci.nile <- confint(bp.nile, breaks = 1)
lines(ci.nile)

12.9.5 Outros testes

12.9.5.1 Testes de Friedman e Nemenyi

A função tsutils::nemenyi() (Kourentzes 2023) realiza comparações múltiplas não paramétricas, entre colunas, usando o teste de Friedman (M. Friedman 1937) e o teste post-hoc de Nemenyi (Nemenyi 1963).

library(tsutils)
x <- matrix( rnorm(50*4,mean=0,sd=1), 50, 4)
x[,2] <- x[,2]+1
x[,3] <- x[,3]+0.7
x[,4] <- x[,4]+0.5
colnames(x) <- c('Method A','Method B','Method C - long name','Method D')
nemenyi(x, conf.level=0.95, plottype='vline')

## Friedman and Nemenyi Tests
## The confidence level is 5%
## Number of observations is 50 and number of methods is 4
## Friedman test p-value: 0.0078 - Ha: Different
## Critical distance: 0.6633

Referências

Banerjee, Anindya, Juan J Dolado, John W Galbraith, and David Hendry. 1993. Co-Integration, Error Correction, and the Econometric Analysis of Non-Stationary Data. Oxford university press.
Box, George EP, and David A Pierce. 1970. “Distribution of Residual Autocorrelations in Autoregressive-Integrated Moving Average Time Series Models.” Journal of the American Statistical Association 65 (332): 1509–26. https://citeseerx.ist.psu.edu/document?repid=rep1&type=pdf&doi=7cd4e19b3eeecf086574969a2cc9d5a4b987275b.
Canova, Fabio, and Bruce E Hansen. 1995. “Are Seasonal Patterns Constant over Time? A Test for Seasonal Stability.” Journal of Business & Economic Statistics 13 (3): 237–52. https://www.jstor.org/stable/pdf/1392184.pdf.
Engle, Robert F, and Clive WJ Granger. 1987. “Co-Integration and Error Correction: Representation, Estimation, and Testing.” Econometrica: Journal of the Econometric Society, 251–76. https://www.jstor.org/stable/1913236.
Friedman, Milton. 1937. “The Use of Ranks to Avoid the Assumption of Normality Implicit in the Analysis of Variance.” Journal of the American Statistical Association 32 (200): 675–701. https://sci2s.ugr.es/keel/pdf/algorithm/articulo/1937-JSTOR-Friedman.pdf.
Hanck, Christoph, Martin Arnold, Alexander Gerber, and Martin Schmelzer. 2024. Introduction to Econometrics with r. Universität Duisburg-Essen. https://www.econometrics-with-r.org/.
———. 1993. Time series models, second edition. Harvester-Wheatsheaf, New York. https://ideas.repec.org/a/eee/intfor/v9y1993i4p582-583.html.
Hyndman, Rob J, and Yeasmin Khandakar. 2008. “Automatic Time Series Forecasting: The Forecast Package for R.” Journal of Statistical Software 27 (3): 1–22. https://doi.org/10.18637/jss.v027.i03.
Kotz, Samuel, Narayanaswamy Balakrishnan, Campbell B Read, Brani Vidakovic, and Norman L Johnson. 2005. Encyclopedia of Statistical Sciences, Volume 1. John Wiley & Sons.
Kourentzes, Nikolaos. 2023. Tsutils: Time Series Exploration, Modelling and Forecasting. https://CRAN.R-project.org/package=tsutils.
Kwiatkowski, Denis, Peter CB Phillips, Peter Schmidt, and Yongcheol Shin. 1992. “Testing the Null Hypothesis of Stationarity Against the Alternative of a Unit Root: How Sure Are We That Economic Time Series Have a Unit Root?” Journal of Econometrics 54 (1-3): 159–78. https://elischolar.library.yale.edu/cgi/viewcontent.cgi?article=2221&context=cowles-discussion-paper-series.
Ljung, Greta M, and George EP Box. 1978. “On a Measure of Lack of Fit in Time Series Models.” Biometrika 65 (2): 297–303. https://larrylisblog.net/WebContents/Financial%20Models/LjungBox.pdf.
Nemenyi, Peter Bjorn. 1963. Distribution-Free Multiple Comparisons. Princeton University.
Perron, Pierre. 1988. “Trends and Random Walks in Macroeconomic Time Series: Further Evidence from a New Approach.” Journal of Economic Dynamics and Control 12 (2-3): 297–332. https://doi.org/10.1016/0165-1889(88)90043-7.
Said, Said E, and David A Dickey. 1984. “Testing for Unit Roots in Autoregressive-Moving Average Models of Unknown Order.” Biometrika 71 (3): 599–607. https://www.larrylisblog.net/WebContents/Financial%20Models/ADFTest.pdf.
Zeileis, Achim. 2006. “Implementing a Class of Structural Change Tests: An Econometric Computing Approach.” Computational Statistics & Data Analysis 50 (11): 2987–3008. https://doi.org/10.1016/j.csda.2005.07.001.
Zeileis, Achim, Christian Kleiber, Walter Krämer, and Kurt Hornik. 2003. “Testing and Dating of Structural Changes in Practice.” Computational Statistics & Data Analysis 44 (1–2): 109–23. https://doi.org/10.1016/S0167-9473(03)00030-6.
Zeileis, Achim, Friedrich Leisch, Kurt Hornik, and Christian Kleiber. 2002. “Strucchange: An r Package for Testing for Structural Change in Linear Regression Models.” Journal of Statistical Software 7 (2): 1–38. https://doi.org/10.18637/jss.v007.i02.