{"cells":[{"metadata":{"_execution_state":"idle","_uuid":"d87ee7f52ef3cdc72c973ede882f5a973a8ff26d","_cell_guid":"a07b17a7-4215-4a31-8263-89cbda180862"},"cell_type":"markdown","source":"# Reading data\n\nThe starting point is to create a simple model using only the training sample provided:"},{"metadata":{"trusted":false,"_uuid":"5a94da321915f57cb9852c5f3280a780febe1c1c"},"cell_type":"code","source":"train <- read.csv(\"../input/train_sample.csv\")\nhead(train)\nstr(train)\ntrain$is_attributed <- as.factor(train$is_attributed)\nlevels(train$is_attributed) <- c(\"zeros\", \"ones\")\nmean(train$is_attributed == \"ones\")\nsum(train$is_attributed == \"ones\")","execution_count":12,"outputs":[]},{"metadata":{"_uuid":"47f2de5fe597aaf7630011ed807f45be5e46cfb0"},"cell_type":"markdown","source":"Then reading the test set:"},{"metadata":{"trusted":false,"_uuid":"624a49fe28acf64d4a7952964abdf80829417ea7"},"cell_type":"code","source":"test <- read.csv(\"../input/test.csv\")\nhead(test)\nstr(test)","execution_count":3,"outputs":[]},{"metadata":{"_uuid":"3a4c8d09d8fb88869df5bd17aa9b04c87c1cf3a3"},"cell_type":"markdown","source":"Since there is no **attributed_time** in the test set, the feature can not be used in the model (it can actually provide informations or help building features). Comparing the range of time for the train and the test sample:"},{"metadata":{"trusted":false,"_uuid":"33fa062fe240d1e0dd9da28f44735cb6ef1758b1"},"cell_type":"code","source":"train$click_time <- as.POSIXct(as.character(train$click_time))\ntest$click_time <- as.POSIXct(as.character(test$click_time))\n\nprint(range(train$click_time))\nprint(range(test$click_time))","execution_count":7,"outputs":[]},{"metadata":{"_uuid":"dc9469f88cc55d474e987c9325d3228ef29ee56a"},"cell_type":"markdown","source":"# A simple model - **Rpart**\n\nExcluding the **attributed_time** and optimizing against the area under the curve using the **caret** package:"},{"metadata":{"_uuid":"f20f3a09de663a7907ec68d67dfe50d63e35a685"},"cell_type":"markdown","source":""},{"metadata":{"trusted":false,"_uuid":"5871b9b118bc8c8fce940f7ed154858d92769c31"},"cell_type":"code","source":"library(caret)\nlibrary(dplyr)\nlibrary(parallel)\nlibrary(doParallel)\n\ncores <- detectCores()\nregisterDoParallel(cores = cores)\n\nctrl <- trainControl(method = \"cv\", \n                     number = 20, \n                     selectionFunction = \"best\",\n                     allowParallel = TRUE,\n                     summaryFunction = twoClassSummary,\n                     classProbs = TRUE\n                    ) \nfit.rpart <- train(is_attributed ~ ., \n                   data = train %>% select(-click_time, -attributed_time), \n                   method = \"rpart\", \n                   trControl = ctrl,\n                   metric = \"ROC\")\nregisterDoSEQ()\nprint(fit.rpart)\nplot(fit.rpart)\nplot(varImp(fit.rpart))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"98890beb3899c1746b83155cb5566cfbd0d514a9"},"cell_type":"markdown","source":"# Fine tuning the model\n\nExploring a larger set of values for the complexity parameter **cp**"},{"metadata":{"trusted":false,"_uuid":"52d64b3798c54240f973561738446696b0d29ce7"},"cell_type":"code","source":"cores <- detectCores()\nregisterDoParallel(cores = cores)\n\nctrl <- trainControl(method = \"cv\", \n                     number = 20, \n                     selectionFunction = \"best\",\n                     allowParallel = TRUE,\n                     summaryFunction = twoClassSummary,\n                     classProbs = TRUE\n                    ) \ngrid <- expand.grid(cp = 10^seq(-10, -2, by = 1))\nfit.rpart <- train(is_attributed ~ ., \n                   data = train %>% select(-click_time, -attributed_time), \n                   method = \"rpart\", \n                   trControl = ctrl,\n                   metric = \"ROC\",\n                   tuneGrid = grid)\nregisterDoSEQ()\nprint(fit.rpart)\nplot(fit.rpart)\nplot(varImp(fit.rpart))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a3c7217fa3bd84cb0e48558a8ef7f7408bcdceb8"},"cell_type":"markdown","source":"Finally:"},{"metadata":{"trusted":false,"_uuid":"b66ef826ff4827a38b5af80d370557121b87d08c"},"cell_type":"code","source":"cores <- detectCores()\nregisterDoParallel(cores = cores)\n\nctrl <- trainControl(method = \"cv\", \n                     number = 20, \n                     selectionFunction = \"best\",\n                     allowParallel = TRUE,\n                     summaryFunction = twoClassSummary,\n                     classProbs = TRUE\n                    ) \ngrid <- expand.grid(cp = c(0.0015, 0.0020, 0.0025, 0.0030, 0.0035))\nfit.rpart <- train(is_attributed ~ ., \n                   data = train %>% select(-click_time, -attributed_time), \n                   method = \"rpart\", \n                   trControl = ctrl,\n                   metric = \"ROC\",\n                   tuneGrid = grid)\nregisterDoSEQ()\nprint(fit.rpart)\nplot(fit.rpart)\nplot(varImp(fit.rpart))","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":1}