facebookincubator / GeoLift

GeoLift is an end-to-end geo-experimental methodology based on Synthetic Control Methods used to measure the true incremental effect (Lift) of ad campaign.
https://facebookincubator.github.io/GeoLift/
MIT License
175 stars 54 forks source link

cumulative_value.plot() correction #164

Closed JussanN closed 1 year ago

JussanN commented 1 year ago

This a pull request to correct the cumulative_value.plot(). Below you can find how to test it.

######### START TESTING ###########

data(GeoLift_Test)

head(GeoLift_Test)

GeoTestData_Test <- GeoDataRead(data = GeoLift_Test,
                                date_id = "date",
                                location_id = "location",
                                Y_id = "Y",
                                X = c(),
                                format = "yyyy-mm-dd",
                                summary = TRUE)

head(GeoTestData_Test)

treatment_locations = c("chicago", "portland")
treatment_start_period = 91
treatment_end_period = 105

###### Using default parameters ######

#Cumulative lift dataframe used by the Cumulative plot function
cumulative_lift_df <- cumulative_lift(
  data = GeoTestData_Test,
  treatment_locations = treatment_locations,
  treatment_start_period = treatment_start_period,
  treatment_end_period = treatment_end_period,
  location_id = "location",
  time_id = "time",
  Y_id = "Y"
)

GeoTest <- GeoLift(Y_id = "Y",
                   data = GeoTestData_Test,
                   locations = treatment_locations,
                   treatment_start_time = treatment_start_period,
                   treatment_end_time = treatment_end_period,
                   ConfidenceIntervals = TRUE
) 

summary(GeoTest)

head(cumulative_lift_df)

print("Att")
GeoTest$inference$ATT
cumulative_lift_df[treatment_end_period, 'att']

print("Att confidence interval")
GeoTest$inference$Lower.Conf.Int
GeoTest$inference$Upper.Conf.Int
cumulative_lift_df[treatment_end_period, 'att_lb']
cumulative_lift_df[treatment_end_period, 'att_ub']

print("Aggregated")
GeoTest$incremental
cumulative_lift_df[treatment_end_period, 'incremental']

print("Aggregated confidence interval")
GeoTest$lower_bound
GeoTest$upper_bound
cumulative_lift_df[treatment_end_period, 'incremental_lb']
cumulative_lift_df[treatment_end_period, 'incremental_ub']

#Cumulative lift plot
cumulative_value.plot(data = GeoTestData_Test,
                      treatment_locations = treatment_locations,
                      treatment_start_period = treatment_start_period,
                      treatment_end_period = treatment_end_period,
                      location_id = "location",
                      time_id = "time",
                      Y_id = "Y",
                      treatment_end_date = NULL,
                      frequency = "daily",
                      plot_start_date = NULL)

###### Using model = "best" and stat_test = "Positive" ######

#Cumulative lift dataframe used by the Cumulative plot function
cumulative_lift_df_best_pos <- cumulative_lift(
  data = GeoTestData_Test,
  treatment_locations = treatment_locations,
  treatment_start_period = treatment_start_period,
  treatment_end_period = treatment_end_period,
  location_id = "location",
  time_id = "time",
  Y_id = "Y",
  model = "best",
  stat_test = "Positive"
)

GeoTest_best_pos <- GeoLift(Y_id = "Y",
                        data = GeoTestData_Test,
                        locations = treatment_locations,
                        treatment_start_time = treatment_start_period,
                        treatment_end_time = treatment_end_period,
                        ConfidenceIntervals = TRUE,
                        model = "best",
                        stat_test = "positive"
) 

summary(GeoTest_best_pos)

print("Att")
GeoTest_best_pos$inference$ATT
cumulative_lift_df_best_pos[treatment_end_period, 'att']

print("Att confidence interval")
GeoTest_best_pos$inference$Lower.Conf.Int
GeoTest_best_pos$inference$Upper.Conf.Int
cumulative_lift_df_best_pos[treatment_end_period, 'att_lb']
cumulative_lift_df_best_pos[treatment_end_period, 'att_ub']

print("Aggregated")
GeoTest_best_pos$incremental
cumulative_lift_df_best_pos[treatment_end_period, 'incremental']

print("Aggregated confidence interval")
GeoTest_best_pos$lower_bound
GeoTest_best_pos$upper_bound
cumulative_lift_df_best_pos[treatment_end_period, 'incremental_lb']
cumulative_lift_df_best_pos[treatment_end_period, 'incremental_ub']

#Cumulative lift plot
cumulative_value.plot(data = GeoTestData_Test,
                      treatment_locations = treatment_locations,
                      treatment_start_period = treatment_start_period,
                      treatment_end_period = treatment_end_period,
                      location_id = "location",
                      time_id = "time",
                      Y_id = "Y",
                      model = "best",
                      stat_test = "Positive",
                      treatment_end_date = NULL,
                      frequency = "daily",
                      plot_start_date = NULL)

#att <- GeoTest$summary$average_att$Estimate
#att_lb <- GeoTest$lower_bound
#att_ub <- GeoTest$upper_bound

att <- GeoTest$inference$ATT
att_lb <- GeoTest$inference$Lower.Conf.Int
att_ub <- GeoTest$inference$Upper.Conf.Int

max_test_period <- 105
treatment_start_period <- 91
treatment_locations = c("chicago", "portland")
length(treatment_locations)
max_test_period - treatment_start_period

incremental_factor <- length(treatment_locations) * (max_test_period - treatment_start_period + 1)

att*incremental_factor
att_lb*incremental_factor
att_ub*incremental_factor

max_test_period <- treatment_start_period + 1

summary(GeoTest)

######### END TESTING #########