# 02 Analysis.r

### If 01 was not ran, load from jesus
.g(); setScienceIfNot("EU_PricingTest_2015", load=FALSE)
lib(ggplot, quie=TRUE)

loadIfNotExists("DT.currency")
loadIfNotExists("DT.groupings")
loadIfNotExists("DT.EU_analytics")
loadIfNotExists("DT.EU_analytics.bak")
# loadFromJesus("DT.EU_analytics", over=TRUE)
loadIfNotExists("DT.EU_aggd")
# loadIfNotExists("DT.EU_aggd_reshaped.no_country")
loadIfNotExists("DT.raw")
get_dim_country(refresh=FALSE)
setkeyIfNot(DT.country, "country_code")


ggplot(DT.EU_aggd_reshaped, aes(x=preTest.units, y=Wk1.units, shape=category, color=country_code)) + geom_point(alpha=0.8, position=position_jitter(width=.05, height=.05)) + gg_xyline() + scale_y_log10() + scale_x_log10()

## Interaction plots.  Two of them.
par(mfrow=c(2, 2))
DT.EU_aggd[, interaction.plot(category, TestWk, units)]
DT.EU_aggd[, interaction.plot(TestWk, category, units)]
DT.EU_aggd[, interaction.plot(TestWk, country_code, units)]
DT.EU_aggd[, interaction.plot(country_code, TestWk, units)]

## Basic models for anova
model.revenue__cat_testwk <- lm(log_1p_revenue ~ category * TestWk, data=DT.EU_aggd)
model.units__cat_testwk   <- lm(log_1p_units   ~ category * TestWk, data=DT.EU_aggd)
anova(model.revenue__cat_testwk)
anova(model.units__cat_testwk)

## Assumption Check
par (mfrow=c(1, 2))
qqnorm(model.units__cat_testwk$res)
plot(model.units__cat_testwk$fitted,model.units__cat_testwk$res,xlab="Fitted",ylab="Residuals")

---------------------------------

global_alpha <- 0.2
j <- 0.15

units
revenue
iTunes - DT
iTunes - DA
Spotify - S
Spotify - AS
DT.EU_aggd[store_name == "Spotify"]

P.iTunes_DA <- lapply(names(countries_using), function(ctry)
  ggplot(DT.EU_aggd[(is_iTunesDA & !is.na(TestWk))][country_code %in% ctry], alpha=global_alpha) +
    aes(x=TestWk.dates, y=units, color=daily_avg_album_units_30preTest, group=upc) + 
    geom_point(alpha=global_alpha) + geom_line(alpha=global_alpha) + 
    facet_grid("category ~ TreatmentWk1") + 
    labs(title=paste0("Album Downloads for ", DT.country[.(ctry)]$country_name), y="Log of Units", x="") + 
    legendtop(notitle=TRUE) + scale_y_log10(lim=c(1, 15)) + angledtext()
)

P.iTunes_DT <- lapply(names(countries_using), function(ctry)
  ggplot(DT.EU_aggd[store_name == "iTunes" & transac_type_abbr == "DT" & album_was_purchased_incountry_during_experiment][country_code %in% ctry], alpha=global_alpha) +
    aes(x=TestWk.dates, y=units, color=increased_Wk1, group=upc) + 
    geom_point(alpha=global_alpha) + geom_line(alpha=global_alpha) + facet_grid("category ~ iTunesDA_units_preTest_above5") +
    labs(title=paste0("Track Downloads for ", DT.country[.(ctry)]$country_name), y="Log of Units", x="") + 
    legendtop(notitle=TRUE) + scale_y_log10()
)

printToPDF(P.iTunes_DA, height = 8, width = 8 )
printToPDF(P.iTunes_DT, height = 8, width = 8 )

DT.EU_aggd[store_name == "iTunes" & transac_type_abbr == "DT"][country_code %in% ctry]

---------------------------------
lims <- c(0, 30)
## count the number of units wk1 as a function of units wk 2
  ggplot(DT.EU_aggd_reshaped[(is_iTunesDA)][country_code %in% ctry] [preTest.units < 30]) +
    aes(x=pre_preTest.units+1, y=preTest.units+1, color=TreatmentWk1, group=TreatmentWk1) + 
    geom_point(position=position_jitter(w=j, h=j)) + 
    # geom_line(alpha=global_alpha) + 
    geom_smooth(alpha=global_alpha) + 
    gg_xyline() +
    facet_grid("category ~ iTunesDA_units_preTest_above5") + 
    labs(title=paste0("Album Downloads for ", DT.country[.(ctry)]$country_name), y="Log of Units", x="") + 
    legendtop(notitle=TRUE) +
    ylim(lims) + xlim(lims) +
    log.x(lim=lims+1) + log.y(lim=lims+1) 
dev.new()
  ## count the number of units wk1 as a function of units wk 2
  ggplot(DT.EU_aggd_reshaped[(is_iTunesDA & !is.na(TestWk))][country_code %in% ctry] [preTest.units < 30]) +
    aes(x=preTest.units+1, y=Wk1.units+1, color=TreatmentWk1, group=TreatmentWk1) + 
    geom_point(position=position_jitter(w=j, h=j)) + 
    # geom_line(alpha=global_alpha) + 
    geom_smooth(alpha=global_alpha) + 
    gg_xyline() +
    facet_grid("category ~ iTunesDA_units_preTest_above5") + 
    labs(title=paste0("Album Downloads for ", DT.country[.(ctry)]$country_name), y="Log of Units", x="") + 
    legendtop(notitle=TRUE) +
    ylim(lims) + xlim(lims) +
    log.x(lim=lims+1) + log.y(lim=lims+1) 



---------------------------------

cols <- setdiff(names(DT.EU_aggd_reshaped.no_country), key(DT.EU_aggd_reshaped.no_country))
DT.EU_aggd_reshaped.no_country[, (cols) := lapply(.SD, function(x) log(x + 1)), .SDcols=cols]

model.ancova <- lm(Wk1.units ~ preTest.units + category - 1, data=DT.EU_aggd_reshaped.no_country)
anova(model.ancova)
## Assumption Check
par (mfrow=c(1, 2))
qqnorm(model.ancova$res)
plot(model.ancova$fitted,model.ancova$res,xlab="Fitted",ylab="Residuals")

---------------------------------


interaction.plot(DT.EU_aggd[TestWk=="preTest"]$)

DT.EU_aggd[(units != (units %/% 1))]

ggScatterPlot( DT.EU_analytics[store_name == "iTunes"][transac_type_abbr %in% c("DA")] [!is.na(TestWk)]
  , x = )

## Poisson Check
if (FALSE)
{
  DT.EU_aggd[, list(revenue.mean = round(mean(revenue)), revenue.sd = round(sd(revenue)), units.mean = round(mean(units)), units.sd = round(sd(units))), keyby=list(store_name, transac_type_abbr, country_code)]
  exploreDispersion(DT.EU_aggd)
}

warning("Rememebr to test revenue and units", call.=FALSE)

## This week vs last week, overall
P.EU_pricingtest.total_units_across_all_groups <- 
{
  title <- "Comparing total units across all groups for the same collection of UPCs before and during the experiment"
  ggBoxplotWithDots(
    # DT = DT.EU_aggd[transac_type_abbr %in% c("DT", "DA")][]
    DT = DT.EU_analytics[store_name == "iTunes"][transac_type_abbr %in% c("DA")] [!is.na(TestWk)]
    # DT = DT.EU_analytics[store_name == "iTunes"] [!is.na(TestWk)] [ upc %in% DT.EU_analytics[units > 10, upc]]
    , x = "TestWk"
    , y = "units"
    , dots_on = TRUE
    , colDotgroup = "TestWk"
    , dots_color = "black"
    , dots_alpha = 0.4
    , dots_size = 0.8
    , notch = TRUE
    , fill = "TestWk.dates"
    , yscale = "log"
    , legend="top"
    , legend.color=FALSE
    , legend_title_on = FALSE
    , title = chopLine(title, width=55)
    )
}




P.EU_pricingtest_units <- 
{
  ggBoxplotWithDots(
    # DT = DT.EU_aggd[transac_type_abbr %in% c("DT", "DA")][]
    DT = DT.EU_aggd[store_name == "iTunes"][transac_type_abbr %in% c("DA")]
    , x = "TestWk"
    , y = "units"
    , dots_on = TRUE
    , colDotgroup = "TestWk"
    , dots_color = "black"
    , dots_alpha = 0.4
    , dots_size = 0.8
    , notch = TRUE
    , fill = "TestWk.dates"
    , facet_formula=c("country_code ~ TreatmentWk1")
    , yscale = "log"
    , legend="top"
    , legend.color=TRUE
    )
}
printToPDF(P.EU_pricingtest_units, height=12)


{
  ggBoxplotWithDots(
    # DT = DT.EU_aggd[transac_type_abbr %in% c("DT", "DA")][]
    DT = DT.EU_aggd[store_name == "iTunes"]
    , x = "TestWk"
    , y = "revenue"
    , colDotgroup = "TestWk"
    , notch = TRUE
    , fill = "TestWk"
    , facet_formula=c("TreatmentWk1 ~ country_code")
    ) + scale_y_log10() + coord_flip()
  # + ylim(0, 3)
}


DT.EU_aggd[, units.logit := car::logit(units)]
ggplot(DT.EU_aggd[transac_type_abbr == "DA"]) + geom_density(aes(x=units.logit))
ggplot(DT.EU_aggd[transac_type_abbr == "DA"]) + geom_density(aes(x=units))
qplot(data=DT.EU_aggd, aes(x=units))