
THREE PARTS. 

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



## The idea here is to look at which products are best for us. 
## This needs more work. 

## Look at the ratio of revenue by product
formnumb(DT.users[date >= "2014-10-01", list(Spotify=sumn(spotify_net_revenue_usd), Orchard=sumn(orchard_and_osc_revenue_usd)), by=list(product, product_description)][, ratio_O_to_S := Orchard / Spotify][, ratio_O_to_S := as.perc(ratio_O_to_S)][order(Orchard, decreasing=TRUE)][1:20])


## Find the top products for Orchard, then Spotify, then combine
TopTenProducts2014.Orch <- DT.users[year(date) == 2014][ order(orchard_and_osc_revenue_usd, decreasing=TRUE), as.character(unique(product))][1:10]
TopTenProducts2014.Spot <- DT.users[year(date) == 2014][ order(spotify_net_revenue_usd, decreasing=TRUE), as.character(unique(product))][1:10]
TopProducts <- unique(c(TopTenProducts2014.Orch, TopTenProducts2014.Spot))



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


## THIS SECTION CREATES LINE CHARTS BY PRODUCT ###

## On second thought - use just main 4 products
TopProducts <- c("P", "A", "PD", "U")

## which countries to plot
CtryUsing <- c("global", "US", "GB", "FR", "DE", "AU", "SE", "NO", "NL")  #  "BR" "MX", "ES"

minDate.plotting <- as.Date("2013-07-01")

DT.plot <- DT.users[product %in% TopProducts][country %in% CtryUsing][, country := factor(country, levels=CtryUsing)]
DT.plot <- DT.plot[date >= minDate.plotting]

DT.plot[month(date) == 12 & product %in% c("P"), labels_streams := fwp(MarketShare_byStreams)]
DT.plot[month(date) == 12 & product %in% c("P"), labels_revenue := fwp(MarketShare_byRevenue)]


P.MarketShare_byRevenue <- ggLinegraph(DT.plot, x="date", y="MarketShare_byRevenue", color="product", facet_for="country ~ .", title="REVENUE\nRatio of Orchard+RED to all of Spotify, across different territories", dotsize=0.9, legend="top", label="labels_revenue", labelcolor=list("black", NULL), labelalpha=c(1, 0.9), labelsize=3, ylab_append="Highlighted values are for 'Premium' Product")
P.MarketShare_byStreams <- ggLinegraph(DT.plot, x="date", y="MarketShare_byStreams", color="product", facet_for="country ~ .", title="STREAMS\nRatio of Orchard+RED to all of Spotify, across different territories", dotsize=0.9, legend="top", label="labels_streams", labelcolor=list("black", NULL), labelalpha=c(1, 0.9), labelsize=3, ylab_append="Highlighted values are for 'Premium' Product")

P.orchard_spotify_ratios <- list(P.MarketShare_byStreams, P.MarketShare_byRevenue)

printToPDF(P.orchard_spotify_ratios, height=12, width=16, ncol=2)


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

## THIS SECTION CREATES BOXPLOTS ###

## Greatest Decline? 
## Look at 3-month average for Oct-Dec 2013 /2014

ProductsUsing <- c("A", "P", "PD", "U", "KHB", "PD", "TSA", "VFHB")
prodCols <- c("product", "product_description")
ctryCols <- c("region_group", "country")

colsRatio <- c(Revenue="MarketShare_byRevenue", Streams="MarketShare_byStreams")
DT.quarterly <- copy(DT.users)
DT.quarterly[, c("quarter", "year") := list(quarter(date), year(date))]
DT.quarterly[product %ni% ProductsUsing, (prodCols) := list("ZZ", "All Other Products")]

## Aggregate
DT.quarterly <- aggregateDT(DT.quarterly, by=c("year", "quarter", prodCols, ctryCols), exclude=c("revenue_share", "date"))
computeRatioCols_(DT.quarterly)

DT.yearly <- aggregateDT(DT.quarterly, by=c("year", "product", "product_description", "region_group"), exclude=c("revenue_share", "date", "country", "quarter"))
computeRatioCols_(DT.yearly)
DT.yearly[order(orchard_and_osc_revenue_usd, decreasing=TRUE)][, .SD[1:25], by=year, .SD=c("product", "product_description", "region_group", "orchard_and_osc_revenue_usd", "active_users", "MarketShare_byRevenue")]

DT.quarterly[product %ni% c("P", "A", )]

tmp_DT.yearly_ratios <- DT.users[product %in% c("P", "A") & month(date) >= 10, lapply(.SD, meann), .SDcols=colsRatio, keyby=list(year=year(date), country, product_description)]
DT.diff_in_ratios <- 
    tmp_DT.yearly_ratios[, lapply(.SD, diff), keyby=list(country, product_description), .SDcols=colsRatio
          ][!is.na(MarketShare_byStreams) & !is.na(MarketShare_byRevenue)][abs(MarketShare_byRevenue) < .15 & abs(MarketShare_byStreams) < .15]

addColsFrom_(DT.diff_in_ratios, DT.country, colsToBring="region_group", joinCols.rec=c("country"), joinCols.g=c("country_code"), nms="region_group")
DT.diff_in_ratios[region_group %ni% c("North America", "Scandinavia", "Western Europe", "GSA", "Latin America"), region_group := "All Others"]

setInfo(DT.diff_in_ratios, "Modified from DT.users.\nCompares 3-month averages for A & P, during Oct-Dec 2013 & 2014")

P.change_in_ratios <- lapply(names(colsRatio), function(col) {
  y <- sprintf("marketshare_by%s", col)
  title <- subtext(sprintf("%s Q4 2014 to Q4 2013", col), "Year-on-Year percentage points gained or lossed", line3="in the ratio comparing Orchard+RED to Spotify ")
  ggplot(data=DT.diff_in_ratios) + aes_string(x="product_description", y=y) + geom_boxplot(outlier.size=FALSE) + 
    labs(x="Product", y=paste("Change in Ratio of ", col), title=title) + gg_hline(0, size=.7, color="red", alpha=0.5) + 
  percent.y(limits = c(-0.11, 0.15)) + 
  geom_point(data=DT.diff_in_ratios[region_group == "All Others"], color="#444444", position=position_jitter(width = .025), alpha=0.8) +
  geom_point(data=DT.diff_in_ratios[region_group != "All Others"], aes(color=region_group), position=position_jitter(width = .025), alpha=0.8) + 
  if (col == "Revenue") nolegend()
})

f.name <- "Exploring change in Orchard to Spotify ratio for Streams and Revenue"
printToPDF(P.change_in_ratios, ncol=2, f.name=f.name)



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

if (FALSE) 
{

  ## This goes in ratio by products
  ## Find the top products for Orchard, then Spotify, then combine
  TopTenProducts2014.Orch <- DT.users[year(date) == 2014][ order(orchard_and_osc_revenue_usd, decreasing=TRUE), as.character(unique(product))][1:10]
  TopTenProducts2014.Spot <- DT.users[year(date) == 2014][ order(spotify_net_revenue_usd, decreasing=TRUE), as.character(unique(product))][1:10]
  TopProducts <- unique(c(TopTenProducts2014.Orch, TopTenProducts2014.Spot))


  colsRatio <- c(Revenue="MarketShare_byRevenue", Streams="MarketShare_byStreams")
  col <- names(colsRatio)[[1]]

  y <- sprintf("marketshare_by%s", col)
  ylab <- paste("Change in Market Share of ", col)
  title2 <- subtext(sprintf("%s Q4 2014 to Q4 2013", col), "Year-on-Year percentage points gained or lossed", line3="in the ratio comparing Orchard+RED to Spotify ")
  ylims = c(-0.11, 0.15)
  yscale = "percent"

  ggBoxplotWithDots(DT=DT.users[product %in% TopProducts][spotify_net_revenue_usd > 2000][1:100], x="product_description", y=y, colDotgroup="region_group", title=title2, ylims=ylims, values.otherDotgroup="All Others")
}

