Skip to content

Commit

Permalink
use new ISS prep functions (#213)
Browse files Browse the repository at this point in the history
- towards #94 
- depends on
https://github.com/RMI-PACTA/pacta.data.preparation/pull/361

---------

Co-authored-by: CJ Yetman - RMI <[email protected]>
  • Loading branch information
cjyetman and CJ Yetman - RMI authored Apr 5, 2024
1 parent aefea30 commit 5b3c9bc
Showing 1 changed file with 20 additions and 73 deletions.
93 changes: 20 additions & 73 deletions run_pacta_data_preparation.R
Original file line number Diff line number Diff line change
Expand Up @@ -472,89 +472,36 @@ logger::log_info("Fund data prepared.")
iss_company_emissions <-
readRDS(factset_iss_emissions_data_path) %>%
pacta.data.preparation::prepare_iss_company_emissions()
factset_financial_data <- readRDS(factset_financial_data_path)
factset_entity_info <- readRDS(factset_entity_info_path)
factset_entity_financing_data <- readRDS(factset_entity_financing_data_path)

logger::log_info("Formatting and saving file: \"iss_entity_emission_intensities.rds\".")

factset_financial_data <-
readRDS(factset_financial_data_path) %>%
select(factset_entity_id, issue_type, adj_price, adj_shares_outstanding) %>%
filter(!is.na(factset_entity_id) & !is.na(adj_shares_outstanding)) %>%
filter(issue_type %in% c("EQ", "PF", "CP")) %>%
summarize(
mkt_val = sum(adj_price * adj_shares_outstanding, na.rm = TRUE),
.by = "factset_entity_id"
)

factset_entity_info <-
readRDS(factset_entity_info_path) %>%
select(factset_entity_id, iso_country, sector_code, factset_sector_desc) %>%
left_join(factset_financial_data, by = "factset_entity_id")

rm(factset_financial_data)
invisible(gc())

iss_entity_emission_intensities <-
readRDS(factset_entity_financing_data_path) %>%
left_join(factset_entity_info, by = "factset_entity_id") %>%
filter(countrycode::countrycode(iso_country, "iso2c", "iso4217c") == currency) %>%
left_join(currencies, by = "currency") %>%
mutate(
ff_mkt_val = if_else(!is.na(mkt_val), mkt_val, NA_real_),
ff_debt = if_else(!is.na(ff_debt), ff_debt * exchange_rate, NA_real_),
currency = "USD"
) %>%
summarise(
ff_mkt_val = mean(ff_mkt_val, na.rm = TRUE),
ff_debt = mean(ff_debt, na.rm = TRUE),
.by = "factset_entity_id"
) %>%
inner_join(iss_company_emissions, by = "factset_entity_id") %>%
transmute(
factset_entity_id = factset_entity_id,
emission_intensity_per_mkt_val = if_else(
ff_mkt_val == 0,
NA_real_,
icc_total_emissions / ff_mkt_val
),
emission_intensity_per_debt = if_else(
ff_debt == 0,
NA_real_,
icc_total_emissions / ff_debt
),
ff_mkt_val,
ff_debt,
units = paste0(icc_total_emissions_units, " / ", "$ USD")
)

saveRDS(
select(iss_entity_emission_intensities, -c("ff_mkt_val", "ff_debt")),
file.path(config[["data_prep_outputs_path"]], "iss_entity_emission_intensities.rds")
)

pacta.data.preparation::prepare_iss_entity_emission_intensities(
iss_company_emissions = iss_company_emissions,
factset_financial_data = factset_financial_data,
factset_entity_info = factset_entity_info,
factset_entity_financing_data = factset_entity_financing_data,
currencies = currencies
) %>%
saveRDS(file.path(config[["data_prep_outputs_path"]], "iss_entity_emission_intensities.rds"))

logger::log_info("Formatting and saving file: \"iss_average_sector_emission_intensities.rds\".")

iss_entity_emission_intensities %>%
inner_join(factset_entity_info, by = "factset_entity_id") %>%
summarise(
emission_intensity_per_mkt_val = weighted.mean(
emission_intensity_per_mkt_val,
ff_mkt_val,
na.rm = TRUE
),
emission_intensity_per_debt = weighted.mean(
emission_intensity_per_debt,
ff_debt,
na.rm = TRUE
),
.by = c("sector_code", "factset_sector_desc", "units")
) %>%
pacta.data.preparation::prepare_iss_average_sector_emission_intensities(
iss_company_emissions = iss_company_emissions,
factset_financial_data = factset_financial_data,
factset_entity_info = factset_entity_info,
factset_entity_financing_data = factset_entity_financing_data,
currencies = currencies
) %>%
saveRDS(file.path(config[["data_prep_outputs_path"]], "iss_average_sector_emission_intensities.rds"))


rm(iss_company_emissions)
rm(iss_entity_emission_intensities)
rm(factset_financial_data)
rm(factset_entity_info)
rm(factset_entity_financing_data)
invisible(gc())

logger::log_info("Emissions data prepared.")
Expand Down

0 comments on commit 5b3c9bc

Please sign in to comment.