library(tidyverse)
I_MAT <- read.csv("Wyoming_2024_SAM_v1.7/Wyoming_ixi_39_naics2_2024.csv")
NAMES <- I_MAT[,1]
LEN  <-  nrow(I_MAT)
I_MAT <- I_MAT[,-1]
I_MAT <- as.matrix(I_MAT,nrow=LEN)
colnames(I_MAT) <- NAMES
rownames(I_MAT) <- NAMES
TOTALS <- colSums(I_MAT )
TOTALS <- ifelse(TOTALS==0,0.000000,TOTALS) %>% as.numeric
#Column division
A_MAT <- t(t(I_MAT)/TOTALS)
ACCOUNT_RANGE <- (LEN-13):LEN
TYPE1_A <- A_MAT[-ACCOUNT_RANGE,-ACCOUNT_RANGE]
MULTIPLIER_OUTPUT_TYPE1 <- solve(diag(LEN-14)-TYPE1_A)
TYPE1_MULTIPLIERS_TOTAL <- colSums(MULTIPLIER_OUTPUT_TYPE1 )

DEMAND_RANGE <- ACCOUNT_RANGE[-1:-6]
TYPE2_A <- A_MAT[-DEMAND_RANGE,-DEMAND_RANGE]
MULTIPLIER_OUTPUT_TYPE2  <-  solve(diag(LEN-8)-TYPE2_A)
MULTIPLIER_OUTPUT_TYPE2
HH_VALUE_ACCOUNTS <- (nrow(MULTIPLIER_OUTPUT_TYPE2  )-5):nrow(MULTIPLIER_OUTPUT_TYPE2  )
TYPE2_MULTIPLIERS_TOTAL <- colSums(MULTIPLIER_OUTPUT_TYPE2[-HH_VALUE_ACCOUNTS ,])
TYPE2_MULTIPLIERS_TOTAL 

###########Value Added

VALUE_ADD_MAT <- colSums(I_MAT[c('EmpComp','PropInc','OthPropInc','TOPI','SUB'),])/colSums(I_MAT)
VALUE_ADD_MAT 
colSums(VALUE_ADD_MAT[colnames(TYPE2_A)]*MULTIPLIER_OUTPUT_TYPE2 )
colSums(VALUE_ADD_MAT[colnames(TYPE1_A)]*MULTIPLIER_OUTPUT_TYPE1 )
colSums(MULTIPLIER_OUTPUT_TYPE1*VALUE_ADD_MAT[colnames(TYPE1_A)] )



VA_TYPE2 <- colSums(solve(diag(nrow(TYPE2_A))-TYPE2_A*VALUE_ADD_MAT[colnames(TYPE2_A)]))
VA_TYPE1 <- colSums(solve(diag(nrow(TYPE1_A))-TYPE1_A*VALUE_ADD_MAT[colnames(TYPE1_A)]))

EmpComp_MAT <- I_MAT[c('EmpComp'),]/colSums(I_MAT)
	EmpComp_TYPE2 <- colSums(solve(diag(nrow(TYPE2_A))-TYPE2_A*EmpComp_MAT[colnames(TYPE2_A)]))
EmpComp_TYPE2 
	EmpComp_TYPE1 <- colSums(solve(diag(nrow(TYPE1_A))-TYPE1_A*EmpComp_MAT[colnames(TYPE1_A)]))
min(EmpComp_TYPE1 )


	
PropInc_MAT <- I_MAT[c('PropInc'),]/colSums(I_MAT)
OthPropInc_MAT <- I_MAT[c('OthPropInc'),]/colSums(I_MAT)
TOPI_MAT <- I_MAT[c('TOPI'),]/colSums(I_MAT)
SUB_MAT <- I_MAT[c('SUB'),]/colSums(I_MAT)




