Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

148 fix target sda outputs #153

Merged
merged 21 commits into from
Jul 31, 2020
Merged

148 fix target sda outputs #153

merged 21 commits into from
Jul 31, 2020

Conversation

jdhoffa
Copy link
Member

@jdhoffa jdhoffa commented Jul 29, 2020

The original target_sda function had several places in which the output was unexpected. This was a result of basing the function off of a methodology document with errors.

In this PR, I have already:

  • updated the vignettes/target_sda.Rmd to reflect the correct methodology (and reworded the article to make it more clear)

  • temporarily removed regression tests that reflect potentially incorrect output

  • added on LARGE regression test, that reflects the correct output (including a new possibility to output several targets for several scenarios)

  • work on target_sda() until that large test passes

  • refactor that test into smaller more meaningful tests

Relates to #144, #145, #148

jdhoffa added 5 commits July 29, 2020 13:17
ref values are wrong to begin with, so i am commenting these out
while I fix the code.
I will work on the code until this massive expect known_output
test passes. After this, I will refactor the test into more
meaningful chunks.
Comment on lines 181 to 237
test_that("with known input outputs as expected", {
#TODO: Refactor this test into smaller isolated expected output tests
matched <- fake_matched(sector_ald = "cement")

ald <- fake_ald(
sector = "cement",
technology = "cement",
name_company = c(rep("shaanxi auto", 4), "company 2"),
year = c(2020, 2021, 2022, 2025, 2020),
emission_factor = c(0.9, 0.9, 0.8, 0.5, 12)
)


co2_intensity_scenario <- fake_co2_scenario(
scenario = c(rep("b2ds", 2), rep("sds", 2)),
year = rep(c(2020, 2025), 2),
emission_factor = c(0.5, 0.1, 0.5, 0.4)

)

out <- target_sda(matched,
ald,
co2_intensity_scenario) %>%
arrange(.data$year)

out <- split(out, out$emission_factor_metric)

expected_out <- tibble::tribble(
~sector, ~year, ~emission_factor_metric, ~emission_factor_value,
"cement", 2020L, "projected", 0.9,
"cement", 2021L, "projected", 0.9,
"cement", 2022L, "projected", 0.8,
"cement", 2025L, "projected", 0.5,
"cement", 2020L, "target_b2ds", 0.9,
"cement", 2025L, "target_b2ds", 1.29,
"cement", 2020L, "target_sds", 0.9,
"cement", 2025L, "target_sds", 5.16,
"cement", 2020L, "adjusted_scenario_b2ds", 6.45,
"cement", 2025L, "adjusted_scenario_b2ds", 1.29,
"cement", 2020L, "adjusted_scenario_sds", 6.45,
"cement", 2025L, "adjusted_scenario_sds", 5.16,
"cement", 2020L, "corporate_economy", 6.45,
"cement", 2021L, "corporate_economy", 0.9,
"cement", 2022L, "corporate_economy", 0.8,
"cement", 2025L, "corporate_economy", 0.5
)


expect_equal(out$projected$emission_factor_value, c(0.9, 0.9, 0.8, 0.5))
expect_equal(out$corporate_economy$emission_factor_value, c(06.45, 0.9, 0.8, 0.5))
expect_equal(out$adjusted_scenario_b2ds$emission_factor_value, c(6.45, 1.29))
expect_equal(out$adjusted_scenario_sds$emission_factor_value, c(6.45, 5.16))
expect_equal(out$target_b2ds$emission_factor_value, c(0.9, 1.29))
expect_equal(out$target_sds$emission_factor_value, c(0.9, 5.16))


})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test I am aiming to have pass

@jdhoffa jdhoffa marked this pull request as ready for review July 30, 2020 16:10
@jdhoffa
Copy link
Member Author

jdhoffa commented Jul 30, 2020

@maurolepore there are a few TODO's, that I wanted your input on, and still that tibble R CMD CHECK warning, but other than that, this is more or less ready to be reviewed.


out <- split(out, out$emission_factor_metric)

expected_out <- tibble::tribble(
Copy link
Contributor

@maurolepore maurolepore Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why R CMD check complained. We import dplyr::tibble(), not tibble::tibble().

We could import tibble in DESCRIPTION, then @importFrom tibble tibble; but we already depend on dplyr, and it reexports tibble() -- so we use dplyr::tibble(). This is confusing.

Comment on lines 204 to 222
expected_out <- tribble(
~sector, ~year, ~emission_factor_metric, ~emission_factor_value,
"cement", 2020L, "projected", 0.9,
"cement", 2021L, "projected", 0.9,
"cement", 2022L, "projected", 0.8,
"cement", 2025L, "projected", 0.5,
"cement", 2020L, "target_b2ds", 0.9,
"cement", 2025L, "target_b2ds", 1.29,
"cement", 2020L, "target_sds", 0.9,
"cement", 2025L, "target_sds", 5.16,
"cement", 2020L, "adjusted_scenario_b2ds", 6.45,
"cement", 2025L, "adjusted_scenario_b2ds", 1.29,
"cement", 2020L, "adjusted_scenario_sds", 6.45,
"cement", 2025L, "adjusted_scenario_sds", 5.16,
"cement", 2020L, "corporate_economy", 6.45,
"cement", 2021L, "corporate_economy", 0.9,
"cement", 2022L, "corporate_economy", 0.8,
"cement", 2025L, "corporate_economy", 0.5
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this dead code? expected_out seems unused.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup you are correct! Thanks

@jdhoffa
Copy link
Member Author

jdhoffa commented Jul 31, 2020

Just had a look, and all the changes look good to me. Once we sort the regression value issues out after the team meeting, lets merge this :)

@jdhoffa jdhoffa merged commit 41fe44d into RMI-PACTA:master Jul 31, 2020
@jdhoffa jdhoffa deleted the 148-enhance-target_sda-outputs branch August 3, 2020 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants