



# this should be a separate file
# this should be a separate file
# this should be a separate file
# this should be a separate file
# this should be a separate file
# this should be a separate file




# 
# 
# ## transformation :  take log of revenue and paidunits  (however, first need to account for negative values -- few of these)
# ##  rollup the upgrades to their respecitve parts
# 
# TargetVar = "log.revenue.acc"
# Explanitory = c("log.revenue.anal", "log.paidunits.anal", "log.paidunits.acc", "transid")
# 
# k = 12 ## number of subsamples
# train_test_split = 3/4  ## percent of training set. Usually about 0.80  
# replace = FALSE
# byCols = setdiff(kCols, "date")
# cat("byCols is " , pasteQand(byCols, q=" "), fill=TRUE)
# 
# ## add a row index to ensure consistency
# DT.iTunes.acc_v_anal[, rowindex := seq(.N)]
# 
# ## Create subsamples indices
# inds.list <- {set.seed(7); lapply(seq(k), function(i) DT.iTunes.acc_v_anal[, .inds := seq(.N)  %in% sample(.N, floor(.N*(train_test_split)), replace=replace), by=byCols][, rowindex[.inds]] )}
# DT.iTunes.acc_v_anal[, .inds := NULL] ## cleanup
# setattr(inds.list, "names", sprintf("fold_%02i", seq(inds.list)))
# 
# ## Model for current index
# i <- 1 # for (i in seq(inds.list))
# training <- inds.list[[i]]
# test <- setdiff(seq(nrow(DT.iTunes.acc_v_anal)), training)
# ## confirm we have all of the rows and no duplicates
# stopifnot(!duplicated(c(test, training)))  # no duplicates
# stopifnot(sort(DT.iTunes.acc_v_anal$rowindex) == sort(c(test, training)))  ## identical
# 
# DT.training <- DT.iTunes.acc_v_anal[rowindex %in% training]
# DT.test     <- DT.iTunes.acc_v_anal[rowindex %in% test]
# DT.test <- DT.test[(transid %in% DT.training$transid)]
# mod01 <- lm(revenue.acc ~ 0+revenue.anal+transac_type_abbr+is_DTorDA+more_than_six_months_ago, data=DT.training)
# par(mfrow=c(2, 2)); plot(mod01)
# 
# pred <- predict(mod01, DT.test)
# DT.test[, pred := pred]
# qplot(data=DT.test[(is_DTorDA)], x=revenue.acc, y=pred)  + geom_abline(x=1, y=1, linetype=4, alpha=.5, color="red") + ggtitle("DT and DA")
# qplot(data=DT.test[(!is_DTorDA)], x=revenue.acc, y=pred)  + geom_abline(x=1, y=1, linetype=4, alpha=.5, color="red") + ggtitle("Non - DT or DA")
# 
# -------------------------------
# 
# DT.iTunes.acc_v_anal[revenue.acc < 10000][, sum(revenue.acc), keyby=list(Rev_Sign=sign(revenue.acc), transid)]
# qplot(data=DT.iTunes.acc_v_anal, revenue.acc, fill=music_vs_video_by_transac, geom=c("bar", "freqpoly")) + facet_grid("is_DTorDA~.", scale="free_x") + scale_x_log10()
# dev.new()
# ggplot(iris, aes(Sepal.Width, ..count..)) + 
#   geom_histogram(aes(colour=Species, fill=Species), binwidth=.2) +
#   geom_freqpoly(colour="black", binwidth=.2) +
#   facet_wrap(~Species)
# 
# ggplot(data=DT.iTunes.acc_v_anal[1:20], aes(y=revenue.acc, color=music_vs_video_by_transac, x=transac_type_abbr)) + 
#   geom_bar(width=.2, stat="identity", position=position_jitter()) + coord_flip() + thousands.y()
#   geom_freqpoly(colour="black", binwidth=.2) +
#   facet_wrap(~Species)
# 
# ggplot(data=DT.iTunes.acc_v_anal) + 
# geom_histogram(stat="identity", aes(x=revenue.acc, fill=transac_type_abbr, color=music_vs_video_by_transac))
#  + facet_grid("music_vs_video_by_transac~.")
# 
# # ggScatterPlot(DT.iTunes.acc_v_anal, x="paidunits.acc", y="paidunits.anal", color="transac_type_abbr", facet.formula="transac_type_abbr~.", debug=3)
# DT.iTunes.acc_v_anal
# 
# -----------------
# TODO -- forecast iTunes using basic priors
# 
# 
# (1) Analytics Total --> (2) Activity Total
#                         (2) Activity  Total --> (3) Accounting Total (ie When will it book)
# 
# 
# DT.acc_v_anal.crop
# 
# 
# 
# 





