## Visualize Part History

## Jeff Hebert
## 9/8/2016


library(data.table)

max_rows <- 100000

# 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 = max_rows, select= "Response", showProgress = FALSE, data.table = FALSE)
dt_date <- fread("../input/train_date.csv", nrows = max_rows, showProgress = FALSE, data.table = FALSE)



# Investigate random set of failing and passing parts

set.seed(123)
# Get 10 failing parts
idx1 <- sample(which(response$Response == 1), 10)
# Get 10 passing parts
idx2 <- sample(which(response$Response == 0), 10)

idx <- c(idx1, idx2)
station_list <- unique(substr(setdiff(names(dt_date),"Id"),1,2))


# Visualize the history of multiple parts

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)


plot(dat$timestamp, dat$step_number, main = "Multiple Part History",
     ylab = "Processing Step", xlab = "Timestamp",
     col = factor(dat$station, levels = station_list), pch = "*")
legend("topleft", station_list, pch = 1, col = factor(station_list, levels = station_list))



# Visualize the history of each part separately

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)
    plot(dat$timestamp, main = paste("Part ", dt_date$Id[i], " Response = ", response$Response[i]),
         xlab = "Processing Step", ylab = "Timestamp",
         col = station)
    legend("bottomright", station_list, pch = 1, col = factor(station_list))
  }
}

