{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages\n# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats\n# For example, here's several helpful packages to load in \n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nsystem(\"ls ../input\")\n\n# Any results you write to the current directory are saved as output.\nsuppressPackageStartupMessages({\nrequire(\"data.table\");\nrequire(\"ggplot2\");\nrequire(\"dplyr\");\nrequire(\"tidyr\");\n})"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "train <- fread(\"../input/train.csv\",header=TRUE,colClasses = c(rep(\"integer\",7),rep(c(\"numeric\",\"integer\"),2)))\nsetkeyv(train,c(\"Agencia_ID\",\"Canal_ID\",\"Cliente_ID\",\"Producto_ID\"))"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "Find out how many observations have adjusted demand equal to the difference between sales and returns."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "sample_products <- sample(unique(train[,Producto_ID]),10)\nsample_train <- train[Producto_ID %in% sample_products]"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "sample_train[,Match:=(Venta_uni_hoy-Dev_uni_proxima)==Demanda_uni_equil]"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "sample_train[!(Match)]"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "sample_train %>% ggplot(aes(x=Demanda_uni_equil))+geom_histogram(binwidth=2)+facet_wrap(~Producto_ID)+xlim(c(0,100))"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "sample_train[,.(avg_demand=mean(Demanda_uni_equil)),by=.(Semana,Producto_ID)]"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "sample_train[,Ruta_SAK] %>% unique() %>% length()"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "sample_train %>% ggplot(aes(x=Semana,y=Demanda_uni_equil)) + geom_point(aes(color=factor(Canal_ID))) + facet_wrap(~Producto_ID,scale=\"free_y\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "sample_train %>% ggplot(aes(x=Semana,y=Demanda_uni_equil)) + stat_summary(geom=\"line\",fun.y=\"mean\",aes(group=Canal_ID,color=factor(Canal_ID))) + facet_wrap(~Producto_ID,scale=\"free_y\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 }
],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"}}, "nbformat": 4, "nbformat_minor": 0}