library(tidyverse)
library(readxl)


GET_MULTIPLIERS <- function(ROOT_FOLDER="States/",YEAR=2024){
ROOT_FOLDER="States/";YEAR=2024
	SUB_FOLDER <- list.files(ROOT_FOLDER)
	ENTRIES <- (strsplit(SUB_FOLDER,paste0("_",YEAR)) %>% unlist)
REGIONS <- ENTRIES[seq(1,length(ENTRIES),by=2)]
ALL_FILES <- list.files(paste0(ROOT_FOLDER,SUB_FOLDER),full.names=TRUE)
LEONTIEF <- ALL_FILES[grep("leontief",ALL_FILES)]
for(CURRENT in 1:length(LEONTIEF)){
REGION <- REGIONS[CURRENT]	
FILE <- LEONTIEF[CURRENT]
TYPE1 <- read_xlsx(FILE,'I-Ainv_type1')
TYPE2 <- read_xlsx(FILE,'I-Ainv_type2')
NROW1 <- nrow(TYPE1)
NROW2 <- nrow(TYPE2)
NAMES <- strsplit(colnames(TYPE1[NROW1,2:NROW1]),"_") %>% unlist
NAICS <- NAMES[seq(1,976,by=2)]
Industry_Name <- NAMES[seq(2,976,by=2)]
Indirect <- as.numeric(TYPE1[NROW1,2:NROW1])
Induced <- as.numeric(TYPE2[NROW2,2:(NROW2-6)])

CRES <- cbind(NAICS,Industry_Name,Indirect,Induced) %>% as_tibble %>% mutate(Indirect=as.numeric(Indirect),Induced=as.numeric(Induced)) %>% mutate(Region=REGION,Total=Induced,Induced=Induced-Indirect,Indirect=Indirect-1) %>% select(Region,everything())
if(CURRENT==1){RES <- CRES}else{RES <- rbind(RES,CRES)}
}
return(RES %>% as_tibble)
}
ALL <- GET_MULTIPLIERS() 
ALL$Region <- gsub("_"," ",ALL$Region)
STATE_BIND <- cbind(state.name,state.abb) %>% as_tibble %>% rename(Region=state.name,State=state.abb)
ALL <-  STATE_BIND %>% inner_join(ALL)
STATE_BIND <- ALL %>% group_by(State) %>% summarize(Total=median(Total)) %>% mutate(Rank=rank(-Total))
ORD <- STATE_BIND %>% arrange(Rank) %>% pull(State)
ALL$State <- factor(ALL$State,level=ORD)
ALL_ORIG <- ALL
ALL <- ALL %>% filter(Total<5)
REST <- ALL %>% filter(State!='WY')
WY <- ALL %>% filter(State=='WY')
PLOT <- ggplot(ALL,aes(x=State,y=Total)) + geom_jitter(width = 0.5,size=0.1,aes(color=State)) + geom_boxplot(fill=NA,outlier.shape=NA)+theme_bw()+theme(legend.position = "none",aes(color=State) ) +ylab("Type II Regional Multipliers")
ggsave("Multipliers.png", plot = PLOT, width = 10, height = 7.5)
library(janitor)
IMPLAN <- read_csv("States/Wyoming_2024_SAM_v1.5.6/IMPLAN_multipliers.csv") %>% clean_names() %>% select(-x1)
hist(IMPLAN %>%  pull(type_sam_multiplier)+1,breaks=40)
IMPLAN <- IMPLAN %>%  pull(type_sam_multiplier)+1
DIST <- rbind(cbind("IMPLAN",IMPLAN),cbind("Tapestry",WY  %>% pull(Total))) %>% as_tibble %>% rename(Source=V1,"Multiplier"=IMPLAN) %>% mutate(Multiplier=as.numeric(Multiplier))
Histogram <- ggplot(DIST, aes(x = Multiplier,group=Source,fill=Source)) +  geom_histogram(position = 'identity',alpha=0.5,bins=60)+theme_bw()+xlab("Type II Multiplier")+scale_x_continuous(breaks=0.25*1:100)+theme(legend.position = "top")
ggsave("Histogram.png", plot = Histogram, width = 10, height = 7.5)



Density_Plot <- ggplot(DIST, aes(x = Multiplier,y=after_stat(density),group=Source,fill=Source)) +  geom_density( linewidth = 1,alpha=0.75)+theme_bw()+xlab("Type II Multiplier")+scale_x_continuous(breaks=0.25*1:100)+theme(legend.position = "top")
ggsave("Density.png", plot = Density_Plot, width = 10, height = 7.5)

Density_Plot 


ggplot(DIST, aes(x = Multiplier)) +  geom_histogram(stat_bin(),fill = "#3a86d4", alpha = 0.7)
##############Import analsysi
GET_IMPORT <- function(ROOT_FOLDER="States/",YEAR=2024){
#ROOT_FOLDER="States/";YEAR=2024
	SUB_FOLDER <- list.files(ROOT_FOLDER)
	ENTRIES <- (strsplit(SUB_FOLDER,paste0("_",YEAR)) %>% unlist)
REGIONS <- ENTRIES[seq(1,length(ENTRIES),by=2)]
ALL_FILES <- list.files(paste0(ROOT_FOLDER,SUB_FOLDER),full.names=TRUE)
IXC <- ALL_FILES[grep("ixc",ALL_FILES)]
IXC <- IXC[grep("csv",IXC)]
for(CURRENT in 1:length(IXC)){
#	CURRENT=51
REGION <- REGIONS[CURRENT]	
FILE <- read_csv(IXC[CURRENT])
NAMES <- strsplit(t(FILE[1:nrow(FILE),1]),"_") %>% unlist
	NAICS <- c(NAMES[seq(1,1952,by=2)],NAMES[1953:1966])
	Industry_Name <-c( NAMES[seq(2,1952,by=2)],NAMES[1953:1966])


LIST <- c("ROW","RUSA","FED")
IMPORT <- FILE %>% select(LIST) %>% as.matrix %>% t %>%rowSums() 
LOCAL <- FILE %>% select(-c(1,LIST))  %>% as.matrix %>% t %>% rowSums()
NAICS
cbind(NAICS,Industry_Name,IMPORT,LOCAL) %>% as_tibble %>% mutate(Import=as.numeric(IMPORT),Local_Purchase=as.numeric(LOCAL),Total=Import+Local_Purchase) %>% select(-IMPORT,-LOCAL) %>% tail



CRES <- cbind(REGION,IMPORT,LOCAL) 
if(CURRENT==1){RES <- CRES}else{RES <- rbind(RES,CRES)}
}
return(RES %>% as_tibble %>% mutate(IMPORT_PERCENT=as.numeric(IMPORT_PERCENT)))
}
IMPORT <- GET_IMPORT()
IMPORT 
IMPORT <- IMPORT %>% mutate(IMPORT_PERCENT=as.numeric(IMPORT_PERCENT),RANK=rank(-IMPORT_PERCENT)) %>% arrange(RANK)

IMPORT  %>% print(n=30)
IXC <-  read_csv("States/Wyoming_2024_SAM_v1.5.6/Wyoming_Update_ixi_502_2024.csv")
IXC <-  read_csv("States/North_Dakota_2024_SAM_v1.5.6/North_Dakota_ixi_502_2024.csv")

LIST <- c("ROW","RUSA","FED")
EXPORT <- sum(IXC  %>% select(LIST) )
LOCAL <- sum(IXC  %>% select(-c(1,LIST)) )
EXPORT/LOCAL

