library(BVAR)
library(tidyverse)

source("Scripts/Functions.r")
#source("Scripts/Load_Wyoming_Web_Data.r")
DATA_TO_GATHER <- list()
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c("WYPOP","WY_POP",TRUE)

DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c("WYNQGSP","WY_GDP",TRUE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <- c("MEHOINUSWYA646N","WY_MED_INCOME",TRUE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <- c("BUSAPPWNSAWY","WY_BUISNESS_APPLICATIONS",FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <- c('ACTLISCOUWY','WY_HOUSES_FOR_SALE',FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <- c('WYRVAC','WY_RENTAL_VACANCY_RATE',FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <- c('WYBPPRIVSA','WY_PRIVATE_HOUSING',FALSE)
#New Private Housing Units Authorized by Building Permits for Wyoming
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('B03002006E056023','LN_FIVE_YEAR_POP',FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('GDPALL56023','LN_GDP',TRUE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('WYLINC3POP','LN_POP',FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('LAUCN560230000000005','LN_EMPLOYMENT',FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('BPPRIV056023','LN_PRIVE_HOUSING',FALSE)

DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('ENU5602320510','LN_NUM_ESTABLISHMENTS',FALSE)

DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('GDPALL56041','UINTA_GDP',TRUE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('WYUINT1POP','UINTA_POP',FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('WYSUBL5POP','SUBLETTE_POP',FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('WYSWEE7POP','SWEETWATER_POP',FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('WYTETO9POP','TETON_POP',FALSE)
##Idaho Counties
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('IDBEAR7POP','BEAR_LAKE_POP',FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('IDCARI9POP','CARIBOU_POP',FALSE)

DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('IDBONN0POP','BONNEVILLE_POP',FALSE)
###US Population
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('POPTOTUSA647NWDB','US_POP',FALSE)
DATA_TO_GATHER[[length(DATA_TO_GATHER)+1]] <-  c('CE16OV','US_EMP',FALSE)




for(x in 1:length(DATA_TO_GATHER)){
  CURRENT <- DATA_TO_GATHER[[x]]
  
  if(CURRENT[3]){C_DATA <- CPI_ADJUST(FRED_GET(CURRENT[1],CURRENT[2]))}else{C_DATA <- FRED_GET(CURRENT[1],CURRENT[2])}
  if(x==1){RES <- C_DATA}else{RES <- RES %>% full_join(C_DATA)}
  rm(CURRENT,C_DATA)
}

DATA <- RES %>% mutate(US_POP=US_POP-WY_POP,WY_POP=WY_POP-LN_POP-UINTA_POP-SUBLETTE_POP-SWEETWATER_POP-TETON_POP)
colnames(DATA)
TS_DATA_ORIG <- DATA %>% select(YEAR,LN_POP,LN_EMPLOYMENT,US_EMP,US_POP,WY_POP,UINTA_POP,SUBLETTE_POP,SWEETWATER_POP,TETON_POP,BEAR_LAKE_POP,CARIBOU_POP,BONNEVILLE_POP) %>%
  filter(!is.na(LN_POP),!is.na(LN_EMPLOYMENT)) %>%
  arrange(YEAR) %>% select(-YEAR)
TS_DATA <- log(ts(TS_DATA_ORIG,start=c(1970),end=c(2024),frequency=1))

MOD <- bvar(TS_DATA,lags=2, n_draw=15000)
opt_irf <- bv_irf(horizon = 25, identification = TRUE)

plot(irf(MOD,opt_irf,conf_bands = c(0.05, 0.1,0.15)),area=TRUE,vars_impulse = c("LN_EMP"),vars_response = c("WY_POP","LN_POP","UINTA_POP","SUBLETTE_POP","SWEETWATER_POP"))
plot(irf(MOD,opt_irf,conf_bands = c(0.05, 0.1,0.15)),area=TRUE,vars_impulse = c("LN_EMP"),vars_response = c("TETON_POP","BEAR_LAKE_POP","CARIBOU_POP","BONNEVILLE_POP"))

plot(irf(MOD,opt_irf,conf_bands = c(0.05, 0.1,0.15)),area=TRUE,vars_impulse = c("LN_POP"),vars_response = c("WY_POP","LN_POP","UINTA_POP","SUBLETTE_POP","SWEETWATER_POP"))
plot(irf(MOD,opt_irf,conf_bands = c(0.05, 0.1,0.15)),area=TRUE,vars_impulse = c("LN_POP"),vars_response = c("TETON_POP","BEAR_LAKE_POP","CARIBOU_POP","BONNEVILLE_POP"))

DATA2 <- RES %>% mutate(US_POP=US_POP-WY_POP,WY_POP=WY_POP-LN_POP-UINTA_POP-SUBLETTE_POP-SWEETWATER_POP-TETON_POP)
TS_DATA2 <- DATA2 %>% select(YEAR,LN_POP,US_POP,WY_POP,UINTA_POP,SUBLETTE_POP,SWEETWATER_POP,TETON_POP,BEAR_LAKE_POP,CARIBOU_POP,BONNEVILLE_POP) %>%
  dplyr::filter(!is.na(LN_POP)) %>%
  arrange(YEAR) %>% select(-YEAR) %>% ts %>% log
MOD2 <- bvar(TS_DATA2,lags=5, n_draw=15000)
plot(irf(MOD2,opt_irf,conf_bands = c(0.05, 0.1,0.15)),area=TRUE,vars_response = c("LN_POP"))
?plot.bvar_irf
plot(predict(MOD,horizon=25,conf_bands = c(0.05, 0.1,0.15)),area=TRUE,vars=c("LN_POP"))
exp(3.5)-exp(3)
acf(resid(MOD))
