# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages
# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats
# For example, here's several helpful packages to load in 

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function
library(data.table)
library(rpart)

# 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")

# Any results you write to the current directory are saved as output.

train=fread('../input/train.csv',select = c("Semana","Demanda_uni_equil","Canal_ID","Producto_ID"))
test=fread('../input/test.csv',select = c("Semana","Demanda_uni_equil","Canal_ID","Producto_ID"))

train=train[Semana<4,]
train<-subset(train, train$Demanda_uni_equil<=12)

fit <- rpart(Demanda_uni_equil~Canal_ID +Producto_ID,method="anova", data=train)

#pfit<- prune(fit, cp=0.01160389)

#prediction <- predict(pfit, newdata = test)
#my_solution <- data.frame(id = test$id, Demanda_uni_equil = prediction$x)

#prediction
#write.csv(my_solution, file = "my_solution.csv",row.names=FALSE)

plot(fit, uniform=TRUE)
text(fit, use.n=TRUE, all=TRUE, cex=1)



