## Visualize Part History

## Help Sought from : 
### Jeff Hebert



library(data.table)

max_rows <- 10000

# Load just the data we need for time based visualization
cat("Reading just the data we need \n")
response <- fread("../input/train_numeric.csv", nrows = 10000, select= "Response")
dt_date <- fread("../input/train_date.csv", nrows = 10000)



set.seed(123)
idx1 <- sample(which(response$Response == 1), 8)
idx2 <- sample(which(response$Response == 0), 8)

idx <- c(idx1, idx2)
station_list <- unique(substr(setdiff(names(dt_date),"Id"),1,2))



library(dplyr)

items <- 0
for(i in idx){
  if(items == 0){
    dat <- data.frame(station = names(dt_date[2:ncol(dt_date)]), timestamp = as.numeric(dt_date[i,2:ncol(dt_date)]), response = response$Response[i], stringsAsFactors = FALSE)
    dat <- dat[!is.na(dat$timestamp),]
    dat$step_number <- 1:nrow(dat)
  } else {
    new_dat <- data.frame(station = names(dt_date[2:ncol(dt_date)]), timestamp = as.numeric(dt_date[i,2:ncol(dt_date)]), response = response$Response[i], stringsAsFactors = FALSE)
    new_dat <- new_dat[!is.na(new_dat$timestamp),]
    new_dat$step_number <- 1:nrow(new_dat)
    dat <- bind_rows(dat, new_dat)
  }
  items <- items + 1
}

dat$station <- factor(substr(dat$station,1,2), levels = station_list)

par(mfrow=c(4,4));

for(i in idx){
  if(sum(!is.na(dt_date[i,])) > 100){
    #dat <- dt_date[i,2:ncol(dt_date)]
    dat <- data.frame(station = names(dt_date[2:ncol(dt_date)]), timestamp = as.numeric(dt_date[i,2:ncol(dt_date)]), stringsAsFactors = FALSE)
    dat <- dat[!is.na(dat$timestamp),]
    station <- factor(substr(dat$station,1,2), levels = station_list)
    # cat(station);
    plot(dat$timestamp, main = paste("Item ", dt_date$Id[i], "  ", response$Response[i]),
         xlab = "Progress", ylab = "Time",
         col = station)
    legend("topleft", station_list, pch = 2, col = factor(station_list))
  }
}






