#========= VAR PADRÃO CHOLESKY

dados_var <- tibble(
  log_despesa = log(as.numeric(despesa_real_tri_sa)),
  log_pib     = log(as.numeric(pib_real_tri_sa)),
  log_receita = log(as.numeric(receita_real_tri_sa))
)


# Estimar o modelo VAR reduzido em nível
# O número de defasagens (p=1) foi mantido do seu código original.
var_red_nivel <- VAR(dados_var, p = 1, type = "const")

#====================== IDENTIFICAÇÃO DOS CHOQUES COM CHOLESKY

# A decomposição de Cholesky é um tipo de identificação recursiva.
# Para usá-la, informamos à função SVAR que a matriz de choques (Bmat)
# é triangular inferior, com elementos a serem estimados.

# 1) Definir as dimensões do modelo
n_vars <- ncol(var_red_nivel$y)

# 2) Montar Bmat (Matriz de Choques)
# Criar uma matriz triangular inferior com zeros na parte superior e NAs na diagonal e parte inferior
Bmat <- matrix(NA, nrow = n_vars, ncol = n_vars)
Bmat[upper.tri(Bmat)] <- 0

# Estimar o SVAR
# Para a decomposição de Cholesky, a matriz Amat é a identidade.
# A função SVAR assume isso como padrão quando Bmat é definida desta forma.
svar_mod_cholesky <- SVAR(var_red_nivel, Bmat = Bmat)
Warning in SVAR(var_red_nivel, Bmat = Bmat): The B-model is just identified. No
test possible.
# Exibir o resumo do modelo.
print(svar_mod_cholesky)

SVAR Estimation Results:
======================== 


Estimated B matrix:
            log_despesa log_pib log_receita
log_despesa    0.076091 0.00000     0.00000
log_pib       -0.003856 0.01843     0.00000
log_receita   -0.021297 0.04744     0.06238
#====================== ANÁLISE DO IMPACTO COM FUNÇÕES DE IMPULSO-RESPOSTA

# Gerar e plotar as IRFs para analisar o impacto dos choques fiscais no PIB.

# 1. Impacto de um choque de DESPESA sobre o PIB
irf_despesa_pib_svar <- irf(svar_mod_cholesky,
                           impulse = "log_despesa",  # Impulso: Choque na Despesa
                           response = "log_pib",     # Resposta: PIB
                           n.ahead = 36,
                           ci = 0.95,
                           boot = TRUE)
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
plot(irf_despesa_pib_svar,
     main = "Resposta do PIB a um Choque de Despesa (SVAR-Cholesky)",
     ylab = "Impacto no PIB (em nível)",
     xlab = "Período (trimestres)")

# 2. Impacto de um choque de RECEITA sobre o PIB
irf_receita_pib_svar <- irf(svar_mod_cholesky,
                           impulse = "log_receita",  # Impulso: Choque na Receita
                           response = "log_pib",     # Resposta: PIB
                           n.ahead = 36,
                           ci = 0.95,
                           boot = TRUE)
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
plot(irf_receita_pib_svar,
     main = "Resposta do PIB a um Choque de Receita (SVAR-Cholesky)",
     ylab = "Impacto no PIB (em nível)",
     xlab = "Período (trimestres)")

#============ 1° DIFERENÇA

# Ordem: Despesa -> PIB -> Receita
dados_var_3_diff <- tibble(
  d_log_despesa = diff(log(as.numeric(despesa_real_tri_sa))),
  d_log_pib     = diff(log(as.numeric(pib_real_tri_sa))),
  d_log_receita = diff(log(as.numeric(receita_real_tri_sa)))
)

# Estimar o modelo VAR reduzido em primeira diferença
# O número de defasagens (p=3) foi mantido do seu código original.
var_red_diff <- VAR(dados_var_3_diff, p = 3, type = "const")

#====================== IDENTIFICAÇÃO DOS CHOQUES COM CHOLESKY

# A decomposição de Cholesky é um tipo de identificação recursiva.
# Para usá-la, informamos à função SVAR que a matriz de choques (Bmat)
# é triangular inferior, com elementos a serem estimados.

# 1) Definir as dimensões do modelo
n_vars <- ncol(var_red_diff$y)

# 2) Montar Bmat (Matriz de Choques)
# Criar uma matriz triangular inferior com zeros na parte superior e NAs na diagonal e parte inferior
Bmat <- matrix(NA, nrow = n_vars, ncol = n_vars)
Bmat[upper.tri(Bmat)] <- 0

# Estimar o SVAR
# Para a decomposição de Cholesky, a matriz Amat é a identidade.
# A função SVAR assume isso como padrão quando Bmat é definida desta forma.
svar_mod_cholesky_diff <- SVAR(var_red_diff, Bmat = Bmat)
Warning in SVAR(var_red_diff, Bmat = Bmat): The B-model is just identified. No
test possible.
# Exibir o resumo do modelo.
print(svar_mod_cholesky_diff)

SVAR Estimation Results:
======================== 


Estimated B matrix:
              d_log_despesa d_log_pib d_log_receita
d_log_despesa      0.080285   0.00000       0.00000
d_log_pib         -0.003837   0.01760       0.00000
d_log_receita     -0.023744   0.04739       0.06432
#====================== ANÁLISE DO IMPACTO COM FUNÇÕES DE IMPULSO-RESPOSTA

# Gerar e plotar as IRFs para analisar o impacto dos choques fiscais no PIB.

# 1. Impacto de um choque de DESPESA sobre o PIB
irf_despesa_pib_svar_diff <- irf(svar_mod_cholesky_diff,
                           impulse = "d_log_despesa",  # Impulso: Choque na Despesa
                           response = "d_log_pib",     # Resposta: PIB
                           n.ahead = 36,
                           ci = 0.95,
                           boot = TRUE)
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
plot(irf_despesa_pib_svar_diff,
     main = "Resposta do PIB a um Choque de Despesa (SVAR-Cholesky)",
     ylab = "Impacto no PIB (1ª Diferença)",
     xlab = "Período (trimestres)")

# 2. Impacto de um choque de RECEITA sobre o PIB
irf_receita_pib_svar_diff <- irf(svar_mod_cholesky_diff,
                           impulse = "d_log_receita",  # Impulso: Choque na Receita
                           response = "d_log_pib",     # Resposta: PIB
                           n.ahead = 36,
                           ci = 0.95,
                           boot = TRUE)
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
Warning in SVAR(x = varboot, Bmat = Bmat): The B-model is just identified. No
test possible.
plot(irf_receita_pib_svar_diff,
     main = "Resposta do PIB a um Choque de Receita (SVAR-Cholesky)",
     ylab = "Impacto no PIB (1ª Diferença)",
     xlab = "Período (trimestres)")

#===== SVEC

# Use os dados em nível, pois o VECM lida com a co-integração
dados_vec <- tibble(
  log_despesa = log(as.numeric(despesa_real_tri_sa)),
  log_pib     = log(as.numeric(pib_real_tri_sa)),
  log_receita = log(as.numeric(receita_real_tri_sa))
)

#====================== TESTE DE CO-INTEGRAÇÃO (JOHANSEN)

# O teste de Johansen determina o número de relações de co-integração (r).
# Use o número de defasagens (K) apropriado para a sua série
jo_test <- ca.jo(dados_vec, type = "trace", K = 2, ecdet = "const")

#====================== ESTIMAÇÃO DO SVEC COM CHOLESKY

# 1) Definir as dimensões
n_vars <- ncol(dados_vec)
r <- 2 # Usando o rank de co-integração que você encontrou

# 2) Montar a matriz SR (Short-Run)
# Para a decomposição de Cholesky, B é triangular inferior
SR_mat <- matrix(NA, nrow = n_vars, ncol = n_vars)
SR_mat[upper.tri(SR_mat)] <- 0

# 3) Montar a matriz LR (Long-Run)
# A função SVEC exige uma matriz 3x3 de NAs, mesmo sem restrições de longo prazo
LR_mat <- matrix(NA, nrow = n_vars, ncol = n_vars)

# Estimar o SVEC
svec_mod <- SVEC(jo_test, r = r, LR = LR_mat, SR = SR_mat, max.iter = 1000)
Warning in SVEC(jo_test, r = r, LR = LR_mat, SR = SR_mat, max.iter = 1000): The
SVEC is just identified. No test possible.
print(svec_mod)

SVEC Estimation Results:
======================== 


Estimated contemporaneous impact matrix:
            log_despesa log_pib log_receita
log_despesa    0.071764 0.00000     0.00000
log_pib       -0.003894 0.01965     0.00000
log_receita   -0.021987 0.05506     0.06002

Estimated long run impact matrix:
            log_despesa log_pib log_receita
log_despesa   -0.002572 0.03942     0.01352
log_pib       -0.002069 0.03170     0.01087
log_receita   -0.002185 0.03349     0.01148
#====================== ANÁLISE DO IMPACTO COM FUNÇÕES DE IMPULSO-RESPOSTA

# Gerar e plotar as IRFs para analisar o impacto dos choques fiscais no PIB.

# 1. Impacto de um choque de DESPESA sobre o PIB
irf_despesa_pib_svec <- irf(svec_mod,
                            impulse = "log_despesa",
                            response = "log_pib",
                            n.ahead = 36,
                            ci = 0.95,
                            boot = TRUE)

plot(irf_despesa_pib_svec,
     main = "Resposta do PIB a um Choque de Despesa (SVEC-Cholesky)",
     ylab = "Impacto no PIB (em nível)",
     xlab = "Período (trimestres)")

# 2. Impacto de um choque de RECEITA sobre o PIB
irf_receita_pib_svec <- irf(svec_mod,
                            impulse = "log_receita",
                            response = "log_pib",
                            n.ahead = 36,
                            ci = 0.95,
                            boot = TRUE)

plot(irf_receita_pib_svec,
     main = "Resposta do PIB a um Choque de Receita (SVEC-Cholesky)",
     ylab = "Impacto no PIB (em nível)",
     xlab = "Período (trimestres)")