{"cells":[{"metadata":{"_uuid":"4a9094ddd740f86c38d892842f67dc07d1a9f23c"},"cell_type":"markdown","source":"## GBM - Google Analytics Customer Revenue Prediction\n* Note: this is just a starting point, there's a lot of work to be done.*"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"2ed662be3aa5d066387243fcf310e812a1cbec95"},"cell_type":"markdown","source":"### Dependencies"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":false,"_kg_hide-output":true},"cell_type":"code","source":"# Libraries\nlibrary(readr, quietly=TRUE)\nlibrary(jsonlite, quietly=TRUE)\nlibrary(tidyverse, quietly=TRUE)\n\nlibrary(lubridate, quietly=TRUE)\n\nlibrary(pROC, quietly=TRUE)\nlibrary(microbenchmark, quietly=TRUE)\nlibrary(gbm, quietly=TRUE)\nlibrary(xgboost, quietly=TRUE)\nlibrary(lightgbm, quietly=TRUE)\nlibrary(magrittr, quietly=TRUE)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"36892733d801ade05ec57a731ea4054a9d23fb9b"},"cell_type":"markdown","source":"### Auxiliar functions"},{"metadata":{"_uuid":"fcac3f636f8ea4c14ac51791269189a372490b8f"},"cell_type":"markdown","source":"Function to load and convert files borrowed from this [kernel](https://www.kaggle.com/mrlong/r-flatten-json-columns-to-make-single-data-frame). And to convert infromation from this [kernel](https://www.kaggle.com/erikbruin/google-analytics-eda-with-screenshots-of-the-app)"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-input":false,"_kg_hide-output":true},"cell_type":"code","source":"train <- read_csv(\"../input/train.csv\")\ntest <- read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"42961d5e360501d9a04c9ee92acd731e403bd873","_kg_hide-input":true},"cell_type":"code","source":"# Reference: https://www.kaggle.com/erikbruin/google-analytics-eda-with-screenshots-of-the-app\n# Reference: https://www.kaggle.com/mrlong/r-flatten-json-columns-to-make-single-data-frame\n#JSON columns are \"device\", \"geoNetwork\", \"totals\", \"trafficSource\"\n\ntr_device <- paste(\"[\", paste(train$device, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\ntr_geoNetwork <- paste(\"[\", paste(train$geoNetwork, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\ntr_totals <- paste(\"[\", paste(train$totals, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\ntr_trafficSource <- paste(\"[\", paste(train$trafficSource, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\n\nte_device <- paste(\"[\", paste(test$device, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\nte_geoNetwork <- paste(\"[\", paste(test$geoNetwork, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\nte_totals <- paste(\"[\", paste(test$totals, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\nte_trafficSource <- paste(\"[\", paste(test$trafficSource, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\n\n#Combine to make the full training and test sets\ntrain <- train %>%\n    cbind(tr_device, tr_geoNetwork, tr_totals, tr_trafficSource) %>%\n    select(-device, -geoNetwork, -totals, -trafficSource)\n    \ntest <- test %>%\n    cbind(te_device, te_geoNetwork, te_totals, te_trafficSource) %>%\n    select(-device, -geoNetwork, -totals, -trafficSource)\n\n#Remove temporary tr_ and te_ sets\nrm(tr_device)\nrm(tr_geoNetwork)\nrm(tr_totals)\nrm(tr_trafficSource)\nrm(te_device)\nrm(te_geoNetwork)\nrm(te_totals)\nrm(te_trafficSource)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"42cb6943a55299e625c1938db375c3054e5e5ff6"},"cell_type":"code","source":"# Reference: https://www.kaggle.com/erikbruin/google-analytics-eda-with-screenshots-of-the-app\n#already converting some character variables into factors (the obvious ones)\nfactorVars <- c(\"channelGrouping\", \"socialEngagementType\", \"browser\", \"operatingSystem\", \"deviceCategory\", \"continent\", \"subContinent\", \"country\", \"source\", \"medium\", \"keyword\", \"referralPath\", \"campaign\")\ntrain[, factorVars] <- lapply(train[, factorVars], as.factor)\ntest[, factorVars] <- lapply(test[, factorVars], as.factor)\n\n#also converting the data variable to the date format\ntrain$date <- ymd(train$date)\ntest$date <- ymd(test$date)\n\n#converting character variables into numeric\nnumVars <- c(\"visits\", \"hits\", \"bounces\", \"pageviews\", \"newVisits\")\ntrain[, numVars] <- lapply(train[, numVars], as.numeric)\ntrain$transactionRevenue <- as.numeric(train$transactionRevenue)\ntest[, numVars] <- lapply(test[, numVars], as.numeric)\n\n#converting visit start times to POSIXct\ntrain$visitStartTime <- as.POSIXct(train$visitStartTime, tz=\"UTC\", origin='1970-01-01')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d825460cc50cc6ef4d7a0a681083cd1839cb0e1"},"cell_type":"code","source":"features <- c('hits', 'fullVisitorIdNum', 'transactionRevenue')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5303e446ea91b8886929e309d2c220a66cf98d21"},"cell_type":"code","source":"# removing NaN's for now\ntrain$fullVisitorIdNum <- as.numeric(train$fullVisitorId)\ntest$fullVisitorIdNum <- as.numeric(test$fullVisitorId)\n\ntrain_few_cols <- select(train, features)\ntrain_few_cols %<>% mutate(transactionRevenue = as.numeric(transactionRevenue))\ntrain_few_cols$transactionRevenue[is.na(train_few_cols$transactionRevenue)] <- 0\n\ntest_few_cols <- select(test, c('hits', 'fullVisitorIdNum'))\n#test_few_cols <- test_few_cols[complete.cases(test_few_cols),]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d9c4fc24c19cd2bc600a46157f58b35178a80b7"},"cell_type":"code","source":"# split train into train and validation\n#applying log to revenue\ntrain_few_cols$transactionRevenue <- log(train_few_cols$transactionRevenue)\ntrain_few_cols$transactionRevenue[train_few_cols$transactionRevenue < 0] <- 0\ntrain.validation.split <- sample(2\n\t, nrow(train_few_cols)\n\t, replace = TRUE\n\t, prob = c(0.7, 0.3))\ntrain2 = train_few_cols[train.validation.split == 1,]\nvalidation = train_few_cols[train.validation.split == 2,]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6fd56fabf1c9da63631d0ef17c2606c176ec142d"},"cell_type":"markdown","source":"### Training"},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"13d4868177803a9de58066242c9daf8cf3f1eafe"},"cell_type":"code","source":"# Get the time to train the GBM model\nsystem.time(\n\tgbm.model <- gbm(transactionRevenue ~ .\n\t\t, distribution = \"gaussian\"\n\t\t, data = train2\n\t\t, n.trees = 700 # number of trees\n\t\t, interaction.depth = 3\n\t\t, n.minobsinnode = 100 # minimum number of obs needed in each node\n\t\t, shrinkage = 0.01 # learning rate\n\t\t, bag.fraction = 0.5 # subsampling\n\t\t, train.fraction = 0.5\n\t\t, cv.folds = 10      # do 10-fold cross-validation\n\t\t, verbose = FALSE  # don't print progress\n\t\t)\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4a3162314a1a2212c6e9e226bfc96f26cca93a9"},"cell_type":"code","source":"# Determine best iteration based on test data\nbest.iter = gbm.perf(gbm.model, method = \"test\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4ec85ce2e903281b4b43b3b2d3d15328d8e0b34"},"cell_type":"code","source":"# Get feature importance\ngbm.feature.imp = summary(gbm.model, n.trees = best.iter)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a53cd4124f47cafb5c3003112762067264c5552f"},"cell_type":"code","source":"# Plot and calculate AUC on test data\ngbm.test = predict(gbm.model, newdata = validation, n.trees = best.iter)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fed876ba48b6f9d4d069f2606e4f2d86f2174b62"},"cell_type":"code","source":"RMSE = function(m, o){\n  sqrt(mean((m - o)^2))\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"177475fa5fed98e2a89702aed6076de24b547fc4"},"cell_type":"code","source":"# RSME\nprint(RMSE(validation$transactionRevenue, gbm.test))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"301b8f71e50c6ce7a267da4611a04db57ad230ea"},"cell_type":"markdown","source":"### Submission"},{"metadata":{"trusted":true,"_uuid":"8f8a7a86f44b0011528e70c495d29e02275b1a1a"},"cell_type":"code","source":"Submission_pred = predict(gbm.model, newdata = test_few_cols, n.trees = best.iter)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39b924599d1d108c21fb1e50d9c22b686fbb9cec"},"cell_type":"code","source":"test_unique <- unique(test[c(\"fullVisitorId\", \"fullVisitorIdNum\")])\ntest_sub <- inner_join(test_few_cols, test_unique, by = c(\"fullVisitorIdNum\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"491b70833e13ad3998a213941c3ad78bff3545e4"},"cell_type":"code","source":"my_submission <- data_frame('fullVisitorId' = test_sub$fullVisitorId, 'PredictedLogRevenue' = Submission_pred)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ebbda45efb159f9f804a6136fbc2150bde8607f5"},"cell_type":"code","source":"# group by id and sum revenue\nmy_submission <- my_submission %>% \n  group_by(fullVisitorId) %>% \n  summarise(PredictedLogRevenue = sum(PredictedLogRevenue))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"108a8e5c303f10af6da005fa2a7bfd1422a88fe3"},"cell_type":"code","source":"my_submission <- arrange(my_submission,fullVisitorId)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9c70d7cea1901e0b8e995f648cd77b9500feedb4"},"cell_type":"code","source":"write_csv(my_submission, '1th_gbm_submission.csv')","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}