Prediction based only on pattern of missing values.  Not a high quality prediction,
but reminds us the pattern of missingness can be informative.

```{r kdddat, tidy=FALSE}
#load some libraries
# devtools::install_github("WinVector/WVPlots")
library('WVPlots')
library('parallel')
library('randomForest')
library('plotly')
library('ggplot2')

# load the data as in the book
# change this path to match your directory structure
dir = '~/Documents/work/PracticalDataScienceWithR/zmPDSwR/KDD2009/' 

d = read.table(paste(dir,'orange_small_train.data.gz',sep=''),
                header=T,sep='\t',na.strings=c('NA',''), 
               stringsAsFactors=FALSE)
churn = read.table(paste(dir,'orange_small_train_churn.labels.txt',sep=''),
                    header=F,sep='\t')
d$churn = churn$V1
appetency = read.table(paste(dir,'orange_small_train_appetency.labels.txt',sep=''),
                        header=F,sep='\t')
d$appetency = appetency$V1
upselling = read.table(paste(dir,'orange_small_train_upselling.labels.txt',sep=''),
                        header=F,sep='\t')
d$upselling = upselling$V1
set.seed(729375)
d$rgroup = runif(dim(d)[[1]])
dTrainM = subset(d,rgroup<=0.5)  # set for building models
dTrainC = subset(d,(rgroup>0.5) & (rgroup<=0.9)) # set for impact coding
dTest = subset(d,rgroup>0.9) # set for evaluation
rm(list=c('d','churn','appetency','upselling','dir'))
outcomes = c('churn','appetency','upselling')
vars = setdiff(colnames(dTrainM),
                c(outcomes,'rgroup'))
yName = 'churn'
yTarget = 1
```

```{r kddprep, tidy=FALSE}
# build data treatments
set.seed(239525)

# convert all variables just to isNA indicators
for(vi in vars) {
  dTrainC[[vi]] <- is.na(dTrainC[[vi]])
  dTest[[vi]] <- is.na(dTest[[vi]])
}
# and drop out constant columns
varMoves <- vapply(vars,
                function(vi) {
                 length(unique(dTrainC[[vi]]))>1
                }, logical(1))
vars <- vars[varMoves]
```

```{r kddrf, tidy=FALSE}
model <- randomForest(x=dTrainC[,vars,drop=FALSE],
                      y=as.factor(as.character(dTrainC[[yName]])))
print(model)
dTest$pred <- predict(model,newdata=dTest[,vars,drop=FALSE],
                type='prob')[,as.character(yTarget),drop=TRUE]
dTest[[yName]] = dTest[[yName]]==yTarget

ti = 'RF prediction on test'
print(DoubleDensityPlot(dTest, 'pred', yName, 
                               title=ti))
print(ROCPlot(dTest, 'pred', yName, yTarget,
                     title=ti))

plotlyROC <- function(predictions, target, title) {
  rocFrame <- WVPlots::graphROC(predictions, target)
  plot_ly(rocFrame$pointGraph, x = ~FalsePositiveRate, y = ~TruePositiveRate, 
        type='scatter', mode='lines+markers', hoverinfo= 'text', 
        text= ~ paste('threshold:', model, 
                      '</br>FalsePositiveRate:', FalsePositiveRate,
                      '</br>TruePositiveRate:', TruePositiveRate)) %>%
    layout(title= title)
}

plotlyROC(dTest$pred, dTest[[yName]]==yTarget, title)
```
