Skip to content

Instantly share code, notes, and snippets.

@baogorek
Created September 11, 2021 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baogorek/a5f10698c269aba86c89b8e8a4e7d7fe to your computer and use it in GitHub Desktop.
Save baogorek/a5f10698c269aba86c89b8e8a4e7d7fe to your computer and use it in GitHub Desktop.
Environmentally Extended Input Output example with eeior and matrix math
# See Wiki @ https://github.com/USEPA/useeior/Wiki for install instructions
library(useeior)
seeAvailableModels()
# explanation of model names can be found at
# https://github.com/USEPA/USEEIO/blob/master/VersioningScheme.md
# LCI: direct perspective life cycle inventory result
# LCIA: life cycle impact assessment
# Build USEEIO v2.0-GHG
model <- buildModel("USEEIOv2.0s-GHG")
# Use the package function to calcuate CO2 emmissions
result <- calculateEEIOModel(model, perspective = "DIRECT", demand = "Production")
co2_by_sector <- result$LCI_d[, 1]
# Use the model matrices to calculate GHGs
y <- model$DemandVectors$vectors[["2012_US_Production_Complete"]] # Amt avail for consumption
x <- model$L %*% y # Total Output needed so that society can consume amount y
B <- diag(model$B[1, ]) # CO2 multiplier matrix
co2_by_sector2 <- B %*% x # 1rst row is CO2
# Ensure they are effectively the same
cor(co2_by_sector, co2_by_sector2)
summary(co2_by_sector)
summary(co2_by_sector2)
@baogorek
Copy link
Author

History:

write.csv(y_df, "C:\devl\data\y.csv")
write.csv(y_df, "C:\\devl\\data\\y.csv")
y_ce <- read.csv("C:\\devl\\data\\y_ce.csv")
y_ce
y_ce$y_ce
y_ce$y_ce %>% sum
x_ce <- model$L %*% y_ce
x_ce <- model$L %*% y_ce$y_ce
x_ce
co2_by_sector_ce <- B %*% x_ce
co2_by_sector_ce
hh_co2 <- co2_by_sector_ce %>% sum()
hh_co2
co2_by_sector %>% sum()
hhs_in_us <- 122.8 * 1E6
hh_co2
hh_co2 * hhs_in_us
ratio <- co2_by_sector / y
ratio
hh_co2_ratio <- ratio * y_ce$y_ce
hh_co2_ratio
hh_co2_ratio %>% sum()
hh_co2_ratio_sum <- hh_co2_ratio %>% sum()
hh_co2_ratio_sum %>% hhs_in_us
hh_co2_ratio_sum * hhs_in_us
history()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment