

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function
library(data.table)
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

#system("ls ../input")

train <- fread('../input/train.csv', 
               select = c('Demanda_uni_equil'),
               colClasses=c(Demanda_uni_equil="numeric"))
train_1 <- fread('../input/train.csv', 
               select = c( 'Agencia_ID','Ruta_SAK', 'Producto_ID', 'Cliente_ID', 'Demanda_uni_equil'))
               
#generate the distances
mahal_val <- mahalanobis(
			 train, 
	         colMeans(train, na.rm = TRUE), 
	         cov(train, use = "pairwise.complete.obs")
	         )


#generate the cutoff values using chisquare
cutoff <- qchisq( 0.999, ncol(train))

#so see num people who are out liers
summary(mahal_val < cutoff)
rm(train)

#generate pure data set
train <- train_1[mahal_val < cutoff, ]
summary(train)
