{"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\nlibrary(ggplot2) # Data visualization\nlibrary(readr) # CSV file I/O, e.g. the read_csv function\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.\n\nlibrary(data.table)\n\n# Input data files are available in the \"../input/\" directory.\n\n# Read in only required columns and force to numeric to ensure that subsequent \n# aggregation when calculating medians works\ntrain <- fread('../input/train.csv', \n               select = c('Agencia_ID', 'Cliente_ID', 'Producto_ID', 'Demanda_uni_equil'),\n               colClasses=c(Agencia_ID=\"numeric\", Cliente_ID=\"numeric\",Producto_ID=\"numeric\",Demanda_uni_equil=\"numeric\"))\n\n# set a table key to enable fast aggregations\nsetkey(train, Producto_ID, Agencia_ID, Cliente_ID)\n\n#calculate the overall median\nmedian <- train[, median(Demanda_uni_equil)]\n\n#calculate the product overall mean; call it M2\nmean_Prod <- train[, exp(mean(log(Demanda_uni_equil+1)))*0.57941, by = .(Producto_ID, Agencia_ID)]\nsetnames(mean_Prod,\"V1\",\"M2\")\n\n\n#calculate the agent and product  mean; call it M3\nmean_Agent_Prod <- train[, exp(mean(log(Demanda_uni_equil+1)))-0.91,by = .(Producto_ID,Agencia_ID,Cliente_ID)]\nsetnames(mean_Agent_Prod,\"V1\",\"M3\")\n\n###################################################################################################\n#  \n# That's the 'modeling' done now need to apply scoring to test set\n# \n###################################################################################################\n\n# Read in Test data \n# Read in only required columns and force to numeric\n\ntest <- fread('../input/test.csv', \n               select = c('id','Agencia_ID', 'Producto_ID', 'Cliente_ID'),\n               colClasses=c(Agencia_ID=\"numeric\",Producto_ID=\"numeric\", Cliente_ID=\"numeric\"))\n\n\n\nsubmit <- merge(test, mean_Agent_Prod, by = c(\"Producto_ID\",\"Agencia_ID\",\"Cliente_ID\"), all.x = TRUE)\n\n# add column M2 that contains mean by Product\nsubmit <- merge(submit, mean_Prod, by = c(\"Producto_ID\", \"Agencia_ID\"), all.x = TRUE)\n\n# Now create Predictions column; intially set to be M3 which contains mean by product and agent\nsubmit$Pred <- round(submit$M3,4)\n\n# where mean by product and agent is null use mean by product (M2)\nsubmit[is.na(M3)]$Pred <- round(submit[is.na(M3)]$M2)\n\n# where median by product is null use overall median\nsubmit[is.na(Pred)]$Pred <- median\n\n# now relabel columns ready for creating submission\nsetnames(submit,\"Pred\",\"Demanda_uni_equil\")\n\n# Write out submission file.\n# Any results you write to the current directory are saved as output.\nwrite.csv(submit[,.(id,Demanda_uni_equil)],\"submit_med_by_Agency_Client.csv\", row.names = FALSE)"
 }
],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"}}, "nbformat": 4, "nbformat_minor": 0}