library(ggplot2)
library(scales)


aggdBy = c("Gender", "Age")
totalFor = c()
useDate   = FALSE
useUnknownGender = FALSE


## Put all of the tables together into a single list
nms.ALL.DTs.using <- paste0("ALL.DTs.", c("yes", "no"), ".totals.", ifelse(useDate, "yes", "no"), ".dates" )
ALL.DTs <- do.call(c, lapply(nms.ALL.DTs.using, get) )
rm(nms.ALL.DTs.using)


DT.nm  <- makeAggName(aggdBy, date=useDate)
if(length(totalFor))
  DT.nm <- paste( c(DT.nm, paste0("Tots4", add.totals.for) ), collapse="_")

## Confirm DT.nm is in the names of tables
if(DT.nm %ni% names(ALL.DTs)) 
  stop("NO DT has been pulled!")
mainDT <- copy(ALL.DTs[[DT.nm]])


if (!useUnknownGender) {
  if ("Gender" %in% names(mainDT))
    mainDT <- mainDT[Gender != "unknown"]
}

## COMPARE WITH: 
fb_agg_age_gender_label

## AND: 
fb_agg_age_gender_label2 <- ALL.DTs[[ "fb_agg_age_gender_label" ]]
fb_agg_age_gender_label2 <- fb_agg_age_gender_label2 [!mutualTopRows(fb_agg_age_gender_label2, cols=c("CPM", "CTR", "Impressions"), by="Gender")]
# Also, take out outliers early. 

# Plot

graphCPM_CTR <- function(DT, log=FALSE) {
  xl <- "CPM"
  P <- 
  ggplot(DT[Gender!="unknown" & CTR != 0 & CPM != 0], aes(x=CPM, y=CTR/100, color=Gender)) + 
        geom_point(alpha=0.7, size=1.2) + geom_smooth(method="lm", fill=NA, size=1.25) +
        ggtitle("Facebook Spend vs Clicks\nApril to Sept 2013\nBy Gender") + 
        xlab("Dollars Spent Per 1,000 Impressions") + 
        ylab("Percentage of Clicks per Impression")

  if (log)
    P + scale_y_log10() + scale_x_log10()
  else
    P + scale_y_continuous(labels = percent)
}


graphCPM_CTR(fb_agg_age_gender_label2, log=TRUE)


ggsave("FB_Adds_ByGender.pdf")

# analysis
OvAll <- fb[Gender!="unknown", lapply(.SD, sum, na.rm=TRUE)
   , keyby=Gender, .SDcols=c("Spend", "Impressions", "Reach", "Clicks", "uClicks", "Actions", "PeopleActioning")]

addCalcCols_(OvAll, calcBy="Gender", addSplits=FALSE)
OvAll

.a(addCalcCols_)
