Tapestry_analysis/Tapestry.r
2026-08-07 12:12:14 -06:00

42 lines
1.8 KiB
R

library(tidyverse)
library(readxl)
GET_MULTIPLIERS <- function(ROOT_FOLDER="States/",YEAR=2024,VERSION='1.5.6'){
SUB_FOLDER <- list.files(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(REGIONS)){
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()
ggplot(ALL,aes(x=Region,y=Total,color=Region)) + geom_boxplot()+theme(legend.position = "none")
STATE_BIND <- cbind(state.name,state.abb) %>% as.tibble %>%rename(Region=state.name,State=state.abb)
ALL$Region <- gsub("_"," ",ALL$Region)
ALL <- ALL %>% inner_join(STATE_BIND)
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)
ggplot(ALL,aes(x=State,y=Total,color=State)) + geom_boxplot()+theme(legend.position = "none")