Description Usage Arguments Value Examples
View source: R/counterfactual_f.R
For a given variable and scenario,it simulates a counterfactual mix, and it estimates the differeence between the actual mix and the counterfactual mix.
1 2 3 4 5 6 7 | counterfactual_mix(
data_input,
col_names,
group_names,
baseline_period,
counterfactual_period
)
|
data_input |
baseline and counterfactuals are provided in the data |
group_names |
The period when the counterfactual scenario is calculated. |
baseline_period |
The period where the baseline mix is assumed |
counterfactual_period |
the period where the counterfactuals are simulated |
col_name |
Model used to construct senarios |
Return Counterfactual mix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | ## Calculate counterfactual scenarios under different scenarios
data("sales_data")
sales_model1 <- lm(total ~
wday +
month,
data = sales_data %>%
filter(date < as.Date("2018-07-01"))
)
start_campaign <- as.Date("2018-07-01")
end_campaign <- as.Date("2018-07-25")
campaign_period <- start_campaign + seq(0, end_campaign - start_campaign)
baseline_period <- min(chosen_period) + seq(-14, 0, 1)
cf_scenario1 <- sales_data %>%
mutate(adj_med_spend = min(adj_med_spend)) %>%
estim_counterfactual(sales_model1, chosen_period)
cf <- data.frame("date" = chosen_period, "counterfactual" = cf_scenario1)
#
sales_data <- sales_data %>%
left_join(cf, on = "date")
delta_shops <- sales_data %>%
mutate(predictions = counterfactual) %>%
counterfactual_mix(
col_names = c("x", "y"),
group_names = c("Shop x", "Shop y"),
baseline_period = baseline_period,
counterfactual_period = campaign_period
)
delta_shops %>%
filter(date %in% campaign_period) %>%
group_by(group) %>%
summarise(
actual = sum(actual),
counterfactual = sum(counterfactual),
delta = sum(delta),
delta_days = sum(delta) / length(campaign_period),
delta_perc = 100 * sum(delta) / sum(counterfactual)
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.