#Universidade Federal de São Carlos (UFSCar) - Campus Sorocaba 
#Departamento de Economia - DEc-So
######################## ALFA CRÍTICO = 5% = 0.05 PARA TODOS OS TESTES ########################

# Preparo dos pacotes
rm(list=ls())
if (!require("pacman")) install.packages("pacman")
## Carregando pacotes exigidos: pacman
## Warning: package 'pacman' was built under R version 4.2.3
library(pacman)
carregar_pacotes <- function() {
  pacman::p_load(
    readxl, 
    tseries, 
    psych, 
    faraway, 
    zoo, 
    lmtest, 
    stargazer, 
    texreg, 
    broom, 
    knitr, 
    sandwich, 
    forecast, 
    xtable, 
    forecast
  )
}
carregar_pacotes()

# Importação de dados

  # Importação da planilha
    dados <- read_xlsx("~\\TF_Grupo11\\Dados\\dados.xlsx", sheet = "dados", col_names = TRUE)

# Tratamento de dados

  # Definição das variáveis lineares
    inpc <- as.vector(dados$inpc)
    parroz <- as.vector(dados$parroz)
    pfeijao <- as.vector(dados$pfeijao)
    pmilho <- as.vector(dados$pmilho)
    psoja <- as.vector(dados$psoja)

    # Checagem da estacionariedade das variáveis lineares
      adf.test(inpc)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  inpc
## Dickey-Fuller = -0.96988, Lag order = 4, p-value = 0.9394
## alternative hypothesis: stationary
      adf.test(parroz)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  parroz
## Dickey-Fuller = -2.5807, Lag order = 4, p-value = 0.3354
## alternative hypothesis: stationary
      adf.test(pfeijao)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  pfeijao
## Dickey-Fuller = -2.6896, Lag order = 4, p-value = 0.2902
## alternative hypothesis: stationary
      adf.test(pmilho)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  pmilho
## Dickey-Fuller = -2.0341, Lag order = 4, p-value = 0.5624
## alternative hypothesis: stationary
      adf.test(psoja)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  psoja
## Dickey-Fuller = -1.6893, Lag order = 4, p-value = 0.7056
## alternative hypothesis: stationary
      kpss.test(inpc)
## Warning in kpss.test(inpc): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  inpc
## KPSS Level = 2.3635, Truncation lag parameter = 4, p-value = 0.01
      kpss.test(parroz)
## Warning in kpss.test(parroz): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  parroz
## KPSS Level = 1.8001, Truncation lag parameter = 4, p-value = 0.01
      kpss.test(pfeijao)
## Warning in kpss.test(pfeijao): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  pfeijao
## KPSS Level = 1.3238, Truncation lag parameter = 4, p-value = 0.01
      kpss.test(pmilho)
## Warning in kpss.test(pmilho): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  pmilho
## KPSS Level = 1.7567, Truncation lag parameter = 4, p-value = 0.01
      kpss.test(psoja)
## Warning in kpss.test(psoja): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  psoja
## KPSS Level = 1.8107, Truncation lag parameter = 4, p-value = 0.01
    # Estudando graficamente
      plot(inpc)

      plot(parroz)

      plot(pfeijao)

      plot(pmilho)

      plot(psoja)

  # Definição das variáveis não-lineares
    linpc <- as.vector(log(dados$inpc))
    lmilho <- as.vector(log(dados$pmilho))
    lsoja <- as.vector(log(dados$psoja))
    larroz <- as.vector(log(dados$parroz))
    lfeijao <- as.vector(log(dados$pfeijao))

    # Checagem da estacionariedade das variáveis não-lineares
      adf.test(linpc)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  linpc
## Dickey-Fuller = -1.597, Lag order = 4, p-value = 0.7439
## alternative hypothesis: stationary
      adf.test(larroz) 
## 
##  Augmented Dickey-Fuller Test
## 
## data:  larroz
## Dickey-Fuller = -2.6761, Lag order = 4, p-value = 0.2958
## alternative hypothesis: stationary
      adf.test(lfeijao)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lfeijao
## Dickey-Fuller = -2.7633, Lag order = 4, p-value = 0.2596
## alternative hypothesis: stationary
      adf.test(lmilho)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lmilho
## Dickey-Fuller = -2.582, Lag order = 4, p-value = 0.3348
## alternative hypothesis: stationary
      adf.test(lsoja)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lsoja
## Dickey-Fuller = -1.8521, Lag order = 4, p-value = 0.638
## alternative hypothesis: stationary
      kpss.test(linpc)
## Warning in kpss.test(linpc): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  linpc
## KPSS Level = 2.3777, Truncation lag parameter = 4, p-value = 0.01
      kpss.test(larroz)
## Warning in kpss.test(larroz): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  larroz
## KPSS Level = 1.9522, Truncation lag parameter = 4, p-value = 0.01
      kpss.test(lfeijao)
## Warning in kpss.test(lfeijao): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  lfeijao
## KPSS Level = 1.3004, Truncation lag parameter = 4, p-value = 0.01
      kpss.test(lmilho)
## Warning in kpss.test(lmilho): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  lmilho
## KPSS Level = 1.8856, Truncation lag parameter = 4, p-value = 0.01
      kpss.test(lsoja)
## Warning in kpss.test(lsoja): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  lsoja
## KPSS Level = 1.8941, Truncation lag parameter = 4, p-value = 0.01
    # Estudando graficamente
      plot(linpc)

      plot(larroz)

      plot(lfeijao)

      plot(lmilho)

      plot(lsoja)

# Regressões:

  # LIN-LOG
    linlog <- lm(inpc ~ lmilho + lsoja + larroz + lfeijao)
    summary(linlog) # Modelo enviesado --> Variáveis não-estacionárias 
## 
## Call:
## lm(formula = inpc ~ lmilho + lsoja + larroz + lfeijao)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -861.46 -235.35  -35.19  296.99  710.17 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -5298.5      769.4  -6.887 3.21e-10 ***
## lmilho        1309.9      193.1   6.784 5.35e-10 ***
## lsoja          219.1      251.3   0.872 0.385104    
## larroz        1105.1      303.2   3.645 0.000403 ***
## lfeijao       -861.2      178.6  -4.821 4.42e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 337.6 on 115 degrees of freedom
## Multiple R-squared:  0.8349, Adjusted R-squared:  0.8291 
## F-statistic: 145.3 on 4 and 115 DF,  p-value: < 2.2e-16
  # LOG-LOG
    loglog <- lm(linpc ~ lmilho + lsoja + larroz + lfeijao)
    summary(loglog) # Modelo enviesado --> Variáveis não-estacionárias 
## 
## Call:
## lm(formula = linpc ~ lmilho + lsoja + larroz + lfeijao)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.20063 -0.05661 -0.00374  0.05595  0.15524 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.583346   0.165055  39.886  < 2e-16 ***
## lmilho       0.256289   0.041424   6.187 9.69e-09 ***
## lsoja        0.008808   0.053915   0.163    0.871    
## larroz       0.266158   0.065041   4.092 7.97e-05 ***
## lfeijao     -0.179278   0.038326  -4.678 7.97e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.07242 on 115 degrees of freedom
## Multiple R-squared:  0.8055, Adjusted R-squared:  0.7987 
## F-statistic: 119.1 on 4 and 115 DF,  p-value: < 2.2e-16
# Teste de normalidade dos resíduos

  # LIN-LOG
    jarque.bera.test(linlog$residuals)
## 
##  Jarque Bera Test
## 
## data:  linlog$residuals
## X-squared = 2.5161, df = 2, p-value = 0.2842
    shapiro.test(linlog$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  linlog$residuals
## W = 0.98398, p-value = 0.1662
  # LOG-LOG
    jarque.bera.test(loglog$residuals)
## 
##  Jarque Bera Test
## 
## data:  loglog$residuals
## X-squared = 2.0386, df = 2, p-value = 0.3608
    shapiro.test(loglog$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  loglog$residuals
## W = 0.98702, p-value = 0.3091
# Multicolinearidade

    # LIN-LOG
      vi <- cbind(parroz, pfeijao, pmilho, psoja)
      cor(as.matrix(vi))
##            parroz   pfeijao    pmilho     psoja
## parroz  1.0000000 0.8533685 0.8663793 0.9086823
## pfeijao 0.8533685 1.0000000 0.8382652 0.8506290
## pmilho  0.8663793 0.8382652 1.0000000 0.9214893
## psoja   0.9086823 0.8506290 0.9214893 1.0000000
    # LOG-LOG
      vi2 <- cbind(larroz, lfeijao, lmilho, lsoja)
      cor(as.matrix(vi2))
##            larroz   lfeijao    lmilho     lsoja
## larroz  1.0000000 0.8382806 0.9026071 0.9073520
## lfeijao 0.8382806 1.0000000 0.8433941 0.8154014
## lmilho  0.9026071 0.8433941 1.0000000 0.9234753
## lsoja   0.9073520 0.8154014 0.9234753 1.0000000
  # Fator de inflação de variância - FIVj e TOLj

    # LIN-LOG  
      fiv <- vif(vi)
      FIV <- as.vector(fiv)

      TOL <- as.vector(1 / fiv)
      fiv_tol <- round(cbind.data.frame(FIV, TOL), 4)
      fiv_tol
##       FIV    TOL
## 1  6.6819 0.1497
## 2  4.3898 0.2278
## 3  7.1949 0.1390
## 4 10.0018 0.1000
    # LOG-LOG  
      fiv2 <- vif(vi2)
      FIV2 <- as.vector(fiv2)

      TOL2 <- as.vector(1 / fiv2)
      fiv_tol2 <- round(cbind.data.frame(FIV2, TOL2), 4)
      fiv_tol2
##     FIV2   TOL2
## 1 7.3551 0.1360
## 2 3.9005 0.2564
## 3 8.8519 0.1130
## 4 8.4995 0.1177
# Autocorrelação

    # Teste Dubin-Watson (DW)

      # LIN-LOG
        dwtest(linlog) # Modelo enviesado --> Autocorrelação
## 
##  Durbin-Watson test
## 
## data:  linlog
## DW = 0.13487, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
      # LOG-LOG
        dwtest(loglog) # Modelo enviesado --> Autocorrelação
## 
##  Durbin-Watson test
## 
## data:  loglog
## DW = 0.12875, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
    # Teste Breusch-Godfrey (BG)

      # LIN-LOG
        bgtest(linlog, order = 1, type = c("Chisq")) # Modelo enviesado --> Autocorrelação
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  linlog
## LM test = 99.689, df = 1, p-value < 2.2e-16
        bgtest(linlog, order = 3, type = c("Chisq")) # Modelo enviesado --> Autocorrelação
## 
##  Breusch-Godfrey test for serial correlation of order up to 3
## 
## data:  linlog
## LM test = 100.27, df = 3, p-value < 2.2e-16
        bgtest(linlog, order = 6, type = c("Chisq")) # Modelo enviesado --> Autocorrelação
## 
##  Breusch-Godfrey test for serial correlation of order up to 6
## 
## data:  linlog
## LM test = 100.51, df = 6, p-value < 2.2e-16
      # LOG-LOG
        bgtest(loglog, order = 1, type = c("Chisq")) # Modelo enviesado --> Autocorrelação
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  loglog
## LM test = 99.141, df = 1, p-value < 2.2e-16
        bgtest(loglog, order = 3, type = c("Chisq")) # Modelo enviesado --> Autocorrelação
## 
##  Breusch-Godfrey test for serial correlation of order up to 3
## 
## data:  loglog
## LM test = 99.694, df = 3, p-value < 2.2e-16
        bgtest(loglog, order = 6, type = c("Chisq")) # Modelo enviesado --> Autocorrelação
## 
##  Breusch-Godfrey test for serial correlation of order up to 6
## 
## data:  loglog
## LM test = 100.08, df = 6, p-value < 2.2e-16
  # Ajustando autocorrelação

    # LIN-LOG
        
      # Aplicando diferenciação
        inpc_d <- diff(inpc)
        larroz_d <- diff(larroz)
        lfeijao_d <- diff(lfeijao)
        lmilho_d <- diff(lmilho)
        lsoja_d <- diff(lsoja)
        
        linlog_2 <- lm(inpc[-1] ~ inpc_d + larroz_d + lfeijao_d + lmilho_d + lsoja_d)
        summary(linlog_2) # Ajuste ruim, mas questão da estacionariedade corrigida
## 
## Call:
## lm(formula = inpc[-1] ~ inpc_d + larroz_d + lfeijao_d + lmilho_d + 
##     lsoja_d)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1290.69  -692.61    65.35   441.76  1918.11 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 4912.179    108.760  45.165   <2e-16 ***
## inpc_d         7.012      3.239   2.165   0.0325 *  
## larroz_d     938.926   1504.858   0.624   0.5339    
## lfeijao_d   -100.836    828.974  -0.122   0.9034    
## lmilho_d    -234.383   1454.270  -0.161   0.8722    
## lsoja_d      785.783   1221.558   0.643   0.5214    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 808.3 on 113 degrees of freedom
## Multiple R-squared:  0.04814,    Adjusted R-squared:  0.006024 
## F-statistic: 1.143 on 5 and 113 DF,  p-value: 0.3419
                            # Adição de variáveis talvez melhore o modelo
        
        # Rechecagem da estacionariedade das variáveis lineares
          adf.test(inpc_d)
## Warning in adf.test(inpc_d): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  inpc_d
## Dickey-Fuller = -4.0602, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
          adf.test(larroz_d)
## Warning in adf.test(larroz_d): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  larroz_d
## Dickey-Fuller = -4.4259, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
          adf.test(lfeijao_d)
## Warning in adf.test(lfeijao_d): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lfeijao_d
## Dickey-Fuller = -4.3661, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
          adf.test(lmilho_d)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lmilho_d
## Dickey-Fuller = -3.6597, Lag order = 4, p-value = 0.0309
## alternative hypothesis: stationary
          adf.test(lsoja_d)
## Warning in adf.test(lsoja_d): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lsoja_d
## Dickey-Fuller = -4.6323, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
          kpss.test(inpc_d)
## Warning in kpss.test(inpc_d): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  inpc_d
## KPSS Level = 0.26578, Truncation lag parameter = 4, p-value = 0.1
          kpss.test(larroz_d)
## Warning in kpss.test(larroz_d): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  larroz_d
## KPSS Level = 0.098779, Truncation lag parameter = 4, p-value = 0.1
          kpss.test(lfeijao_d)
## Warning in kpss.test(lfeijao_d): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  lfeijao_d
## KPSS Level = 0.056224, Truncation lag parameter = 4, p-value = 0.1
          kpss.test(lmilho_d)
## Warning in kpss.test(lmilho_d): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  lmilho_d
## KPSS Level = 0.12889, Truncation lag parameter = 4, p-value = 0.1
          kpss.test(lsoja_d)
## Warning in kpss.test(lsoja_d): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  lsoja_d
## KPSS Level = 0.12128, Truncation lag parameter = 4, p-value = 0.1
          # Variáveis estacionárias --> Teórica correção da fonte de autocorrelação

    # LOG-LOG
        
      # Aplicando diferenciação
        linpc_d <- diff(linpc)
        larroz_d <- diff(larroz)
        lfeijao_d <- diff(lfeijao)
        lmilho_d <- diff(lmilho)
        lsoja_d <- diff(lsoja)
        
        loglog_2 <- lm(linpc[-1] ~ linpc_d + larroz_d + lfeijao_d + lmilho_d + lsoja_d)
        summary(loglog_2)  # Ajuste ruim, mas questão da estacionariedade corrigida
## 
## Call:
## lm(formula = linpc[-1] ~ linpc_d + larroz_d + lfeijao_d + lmilho_d + 
##     lsoja_d)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.277357 -0.120192  0.006906  0.075785  0.299998 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.522045   0.022958 371.194   <2e-16 ***
## linpc_d     -0.341902   3.616094  -0.095    0.925    
## larroz_d     0.164507   0.302586   0.544    0.588    
## lfeijao_d    0.004115   0.166745   0.025    0.980    
## lmilho_d     0.169519   0.293802   0.577    0.565    
## lsoja_d      0.086052   0.246143   0.350    0.727    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1624 on 113 degrees of freedom
## Multiple R-squared:  0.01108,    Adjusted R-squared:  -0.03268 
## F-statistic: 0.2531 on 5 and 113 DF,  p-value: 0.9375
                            # Exclusão/adição de variáveis talvez melhore o problema --> R ajustado indica penalização por variável não-significativa
  
        # Rechecagem da estacionariedade das variáveis lineares
          adf.test(linpc_d)
## Warning in adf.test(linpc_d): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  linpc_d
## Dickey-Fuller = -4.0592, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
          adf.test(larroz_d)
## Warning in adf.test(larroz_d): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  larroz_d
## Dickey-Fuller = -4.4259, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
          adf.test(lfeijao_d)
## Warning in adf.test(lfeijao_d): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lfeijao_d
## Dickey-Fuller = -4.3661, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
          adf.test(lmilho_d)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lmilho_d
## Dickey-Fuller = -3.6597, Lag order = 4, p-value = 0.0309
## alternative hypothesis: stationary
          adf.test(lsoja_d)
## Warning in adf.test(lsoja_d): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  lsoja_d
## Dickey-Fuller = -4.6323, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
          kpss.test(linpc_d)
## Warning in kpss.test(linpc_d): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  linpc_d
## KPSS Level = 0.15415, Truncation lag parameter = 4, p-value = 0.1
          kpss.test(larroz_d)
## Warning in kpss.test(larroz_d): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  larroz_d
## KPSS Level = 0.098779, Truncation lag parameter = 4, p-value = 0.1
          kpss.test(lfeijao_d)
## Warning in kpss.test(lfeijao_d): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  lfeijao_d
## KPSS Level = 0.056224, Truncation lag parameter = 4, p-value = 0.1
          kpss.test(lmilho_d)
## Warning in kpss.test(lmilho_d): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  lmilho_d
## KPSS Level = 0.12889, Truncation lag parameter = 4, p-value = 0.1
          kpss.test(lsoja_d)
## Warning in kpss.test(lsoja_d): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  lsoja_d
## KPSS Level = 0.12128, Truncation lag parameter = 4, p-value = 0.1
          # Variáveis estacionárias --> Teórica correção da fonte de autocorrelação
        
    # Repetindo o teste Dubin-Watson (DW)

      # LIN-LOG
        dwtest(linlog_2) # Autocorrelação persistente --> Não tem origem única na estacionariedade
## 
##  Durbin-Watson test
## 
## data:  linlog_2
## DW = 0.039475, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
      # LOG-LOG
        dwtest(loglog_2) # Autocorrelação persistente --> Não tem origem única na estacionariedade
## 
##  Durbin-Watson test
## 
## data:  loglog_2
## DW = 0.013671, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
    # Repetindo o Teste Breusch-Godfrey (BG)

      # LIN-LOG
        bgtest(linlog_2, order = 1, type = c("Chisq")) # Autocorrelação persistente --> Não tem origem única na estacionariedade
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  linlog_2
## LM test = 113.06, df = 1, p-value < 2.2e-16
        bgtest(linlog_2, order = 3, type = c("Chisq")) # Autocorrelação persistente --> Não tem origem única na estacionariedade
## 
##  Breusch-Godfrey test for serial correlation of order up to 3
## 
## data:  linlog_2
## LM test = 113.19, df = 3, p-value < 2.2e-16
        bgtest(linlog_2, order = 6, type = c("Chisq")) # Autocorrelação persistente --> Não tem origem única na estacionariedade
## 
##  Breusch-Godfrey test for serial correlation of order up to 6
## 
## data:  linlog_2
## LM test = 113.29, df = 6, p-value < 2.2e-16
      # LOG-LOG
        bgtest(loglog_2, order = 1, type = c("Chisq")) # Autocorrelação persistente --> Não tem origem única na estacionariedade
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  loglog_2
## LM test = 115.18, df = 1, p-value < 2.2e-16
        bgtest(loglog_2, order = 3, type = c("Chisq")) # Autocorrelação persistente --> Não tem origem única na estacionariedade
## 
##  Breusch-Godfrey test for serial correlation of order up to 3
## 
## data:  loglog_2
## LM test = 115.2, df = 3, p-value < 2.2e-16
        bgtest(loglog_2, order = 6, type = c("Chisq")) # Autocorrelação persistente --> Não tem origem única na estacionariedade
## 
##  Breusch-Godfrey test for serial correlation of order up to 6
## 
## data:  loglog_2
## LM test = 115.25, df = 6, p-value < 2.2e-16
# Heterocedasticidade

  # Teste Breusch-Pagan (BP)

    # Modelos LIN-LOG

      # LIN-LOG
        bptest(linlog)
## 
##  studentized Breusch-Pagan test
## 
## data:  linlog
## BP = 13.297, df = 4, p-value = 0.009913
      # LIN-LOG AJUSTADO
        bptest(linlog_2)
## 
##  studentized Breusch-Pagan test
## 
## data:  linlog_2
## BP = 4.056, df = 5, p-value = 0.5414
        # Correção da heterocedastidade por meio do ajuste
        
    # Modelos LOG-LOG

      # LOG-LOG
        bptest(loglog)
## 
##  studentized Breusch-Pagan test
## 
## data:  loglog
## BP = 14.345, df = 4, p-value = 0.006272
      # LOG-LOG AJUSTADO
        bptest(loglog_2)
## 
##  studentized Breusch-Pagan test
## 
## data:  loglog_2
## BP = 10.065, df = 5, p-value = 0.07341
        # Correção da heterocedastidade por meio do ajuste

  # Teste de White

    # Modelos LIN-LOG

      # LIN-LOG
        res1 <- (linlog$residuals^2)
        arroz_2_n <- larroz^2
        feijao_2_n <- lfeijao^2
        soja_2_n <- lsoja^2
        milho_2_n <- lmilho^2

        resq_linlog <- lm(res1 ~ larroz + lfeijao + lsoja + lmilho +
                          arroz_2_n + feijao_2_n + soja_2_n + milho_2_n +
                          larroz * lfeijao + larroz * lsoja + larroz * lmilho +
                          lfeijao * lsoja + lfeijao * lmilho +
                          lsoja * lmilho
        )
        greslinlog <- glance(resq_linlog)
        Rsq <- greslinlog$r.squared
        N <- nobs(resq_linlog)
        S <- greslinlog$df
        nR2 <- N * Rsq
        chisq_vc <- qchisq(0.95, df = (S - 1))

        nR2
## [1] 35.4055
        chisq_vc
## [1] 22.36203
      # LIN-LOG AJUSTADO
        res1_2 <- (linlog_2$residuals^2)
        arroz_2_d <- larroz_d^2
        feijao_2_d <- lfeijao_d^2
        soja_2_d <- lsoja_d^2
        milho_2_d <- lmilho_d^2

        resq_linlog_2 <- lm(res1_2 ~ larroz_d + lfeijao_d + lsoja_d + lmilho_d +
                            arroz_2_d + feijao_2_d + soja_2_d + milho_2_d +
                            larroz_d * lfeijao_d + larroz_d * lsoja_d + larroz_d * lmilho_d +
                            lfeijao_d * lsoja_d + lfeijao_d * lmilho_d +
                            lsoja_d * lmilho_d
        )
        greslinlog_2 <- glance(resq_linlog_2)
        Rsq_2 <- greslinlog_2$r.squared
        N2 <- nobs(resq_linlog_2)
        S2 <- greslinlog_2$df
        nR2_2 <- N2 * Rsq_2
        chisq_vc_2 <- qchisq(0.95, df = (S - 1))

        nR2_2
## [1] 13.97686
        chisq_vc_2
## [1] 22.36203
    # Modelos LOG-LOG

      # LOG-LOG
        res3 <- (loglog$residuals^2)
        larroz_2 <- larroz^2
        lfeijao_2 <- lfeijao^2
        lsoja_2 <- lsoja^2
        lmilho_2 <- lmilho^2

        resq_loglog_3 <- lm(res3 ~ larroz + lfeijao + lsoja + lmilho +
                            larroz_2 + lfeijao_2 + lsoja_2 + lmilho_2 +
                            larroz * lfeijao + larroz * lsoja + larroz * lmilho +
                            lfeijao * lsoja + lfeijao * lmilho +
                            lsoja * lmilho
        )
        gresloglog_3 <- glance(resq_loglog_3)
        Rsq2_3 <- gresloglog_3$r.squared
        N3 <- nobs(resq_loglog_3)
        S3 <- gresloglog_3$df
        nR2_3 <- N3 * Rsq2_3
        chisq_vc_3 <- qchisq(0.95, df = (S - 1))

        nR2_3
## [1] 35.21795
        chisq_vc_3
## [1] 22.36203
      # LOG-LOG AJUSTADO
        res4 <- (loglog_2$residuals^2)
        larroz_2_d <- larroz_d^2
        lfeijao_2_d <- lfeijao_d^2
        lsoja_2_d <- lsoja_d^2
        lmilho_2_d <- lmilho_d^2

        resq_loglog_4 <- lm(res4 ~ larroz_d + lfeijao_d + lsoja_d + lmilho_d +
                            larroz_2_d + lfeijao_2_d + lsoja_2_d + lmilho_2_d +
                            larroz_d * lfeijao_d + larroz_d * lsoja_d + larroz_d * lmilho_d +
                            lfeijao_d * lsoja_d + lfeijao_d * lmilho_d +
                            lsoja_d * lmilho_d
        )
        gresloglog_4 <- glance(resq_loglog_4)
        Rsq2_4 <- gresloglog_4$r.squared
        N4 <- nobs(resq_loglog_4)
        S4 <- gresloglog_4$df
        nR2_4 <- N4 * Rsq2_4
        chisq_vc_4 <- qchisq(0.95, df = (S - 1))

        nR2_4
## [1] 20.38747
        chisq_vc_4
## [1] 22.36203
        warnings() # Avisos limitam-se a p-valores menores que o informado