tests/testthat/test-write_scenario.R

# ensure we can write a scenario and then reread it back in and autopopulate the results ---------------------------------


# Create and manipulate scenarios ----------------
path <- system.file("testdata", "tnz2", package = "mm.reoptimise")
scenario_list <- create_scenario_list(path)
myscenario <- scenario_list[[1]]
scenario_list <- mm.reoptimise::add_scenario(scenario_list, myscenario, "new")

selected_scenario <- scenario_list[[2]]

selected_scenario <- mm.reoptimise::set_scenario(scenario = selected_scenario,
                                                 period = "Annual",
                                                 budget = 7300000,
                                                 kpi1 = "Revenue")
selected_scenario <- mm.reoptimise::run_optimization(selected_scenario)

scenario_list <- mm.reoptimise::add_scenario(scenario_list,
                                               selected_scenario,
                                               user_name = "7m revenue")

# Identify scenario "new" is ready to be written
check <- mm.reoptimise::check_scenario_result_present(scenario_list)
mm.reoptimise::summarise_scenarios(scenario_list)$name[check]

# Write scenarios --------------------------
path <- system.file("testdata", "tnz2", "export", package = "mm.reoptimise")
scenario_name <- "7m revenue"
mm.reoptimise::write_scenario(scenario_list, scenario_name, path)

# Read the scenario back in
# The scenario has been set - i.e. budget, curves_filtererd, constraints table are populated
# And the optimisation has been ran - curves_full exists and optim_spend etc. are populated
prev_scenario <- mm.reoptimise::create_scenario(path)

# # test function
test_that("we can read scenario back in- exactly as per other scenarios",{

      expect_equal(prev_scenario$curves_full$response_at_optim,
                   selected_scenario$curves_full$response_at_optim)
      expect_equal(prev_scenario$curves_full$optim_spend,
                   selected_scenario$curves_full$optim_spend)


})

# Make a new scenarios list for an example ---------------------------
new_scenario_list <- list(scenario_list[[1]])

new_scenario_list <- mm.reoptimise::add_scenario(new_scenario_list,
                                                 prev_scenario,
                                                 "7m revenue")

df <- mm.reoptimise::create_all_scenarios_table(new_scenario_list)

test_that("we can chart and view scenario like the others", {

      expect_equal(mm.reoptimise::summarise_scenarios(new_scenario_list) %>% nrow(), 2)
      expect_equal(df %>% nrow(), 84)

})

mm.reoptimise::chart_mediaplan(df, "7m revenue", "alloc_unit")


# Optim spend - chart ------------------------
#
# df1 <- prev_scenario$curves_full %>%
#       dplyr::group_by(channel_name) %>%
#       dplyr::summarise(prev = sum(optim_spend))
#
# df2 <- selected_scenario$curves_full %>%
#       dplyr::group_by(channel_name) %>%
#       dplyr::summarise(selected = sum(optim_spend))
#
# df <- df1 %>%
#       dplyr::left_join(df2, by = "channel_name") %>%
#       tidyr::pivot_longer(cols = -(channel_name),
#                           names_to = "comparison",
#                           values_to = "value")
#
# ggplot2::ggplot(data = df,
#                 ggplot2::aes(x = channel_name,
#                     y = value,
#                     fill = comparison)) + ggplot2::geom_bar(stat = "identity",
#                                                              position = "dodge")
#
# df_diff <- df1 %>%
#       dplyr::left_join(df2, by = "channel_name") %>%
#       dplyr::mutate(diff = prev-selected)
#
#
cath-parkinson/mm.reoptimise documentation built on May 12, 2022, 3:34 p.m.