{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### <center>Stats 5600 - Final Project</center>\n# <center>Customer Revenue Prediction</center>\n#### Sujan Barama\n#### Thejas Kiran","metadata":{}},{"cell_type":"markdown","source":"### Importing libraries and dataset","metadata":{}},{"cell_type":"code","source":"# install.packages(\"reshape2\")\n# install.packages('ggplot2')\n# install.packages('strignr')\n# install.packages('jsonlite')\n# install.packages('dplyr')\n# install.packages('KernSmooth')\n# install.packages('rpart')","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:09:07.623798Z","iopub.execute_input":"2022-12-09T01:09:07.626944Z","iopub.status.idle":"2022-12-09T01:09:07.733214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(dplyr)\nlibrary(jsonlite)\nlibrary(stringr)\nlibrary(ggplot2)\nlibrary(reshape2)\nlibrary(tidyverse)\nlibrary(Hmisc)\nlibrary(caTools)\nlibrary(Metrics)\nlibrary(MASS)\nlibrary(xgboost)\nlibrary(caret)\nlibrary(rpart)\nlibrary(rpart.plot)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:09:07.762484Z","iopub.execute_input":"2022-12-09T01:09:07.795983Z","iopub.status.idle":"2022-12-09T01:09:12.200730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data <- read.csv('../input/ga-customer-revenue-prediction/train_v2.csv', nrows = 10, header = TRUE)\n\n# # nrow(data)\n\n# # head(data, 1)\n\n# # The below code was run as the system did not have enough processing power\n\n# # column_names <- as.vector(t(read.csv(\"../input/ga-customer-revenue-prediction/train_v2.csv\", header=FALSE, colClasses='character', nrows=1)))\n\n# # data <- read.csv(\"../input/ga-customer-revenue-prediction/train_v2.csv\", header=FALSE, col.names=column_names, skip=1800000, nrows = 100000)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-09T01:09:12.203162Z","iopub.execute_input":"2022-12-09T01:09:12.204971Z","iopub.status.idle":"2022-12-09T01:09:12.215415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Manipulating some of the dataset columns to get it in tabular form","metadata":{}},{"cell_type":"code","source":"# device_info <- lapply(data$device, fromJSON)\n# # device_info[1]\n# print('1a')\n\n# browser <- c()\n# os <- c()\n# device_category <- c()\n\n# for(i in device_info) {\n#     browser <- append(browser, i$browser)\n#     os <- append(os, i$operatingSystem)\n#     device_category <- append(device_category, i$deviceCategory)\n# }\n\n# data['device_browser'] <- browser\n# data['device_os'] <- os\n# data['device_category'] <- device_category\n# print('1b')\n# geo_network_info <- lapply(data$geoNetwork, fromJSON)\n# # geo_network_info[1]\n# print('2a')\n\n# continent <- c()\n# sub_continent <- c()\n# country <- c()\n# network_domain <- c()\n\n# for(i in geo_network_info) {\n#     continent <- append(continent, i$continent)\n#     sub_continent <- append(sub_continent, i$subContinent)\n#     network_domain <- append(network_domain, i$networkDomain)\n#     country <- append(country, i$country)\n# }\n\n# data['geo_info_continent'] <- continent\n# data['geo_info_sub_continent'] <- sub_continent\n# data['geo_info_network_domain'] <- network_domain\n# data['geo_info_country'] <- country\n# print('2b')\n\n# totals_info <- lapply(data$totals, fromJSON)\n# # totals_info[1]\n# print('3a')\n\n# visits <- c()\n# hits <- c()\n# page_views <- c()\n# bounces <- c()\n# total_transaction_revenue <- c()\n# new_visits <- c()\n# session_quality_dim <- c()\n# transaction_revenue <- c()\n\n# count <- 0\n# for(i in totals_info) {\n#     if(length(i$visits))\n#         visits <- append(visits, i$visits)\n#     else\n#         visits <- append(visits, 0) \n#     if(length(i$hits))\n#         hits <- append(hits, i$hits)\n#     else\n#         hits <- append(hits, 0)\n#     if(length(i$page_views))\n#         page_views <- append(page_views, i$pageViews)\n#     else\n#         page_views <- append(page_views, 0)\n#     if(length(i$bounces))\n#         bounces <- append(bounces, i$bounces)\n#     else\n#         bounces <- append(bounces, 0)\n#     if(length(i$totalTransactionRevenue))\n#         total_transaction_revenue <- append(total_transaction_revenue, i$totalTransactionRevenue)\n#     else\n#         total_transaction_revenue <- append(total_transaction_revenue, 0)\n#     if(length(i$sessionQualityDim))\n#         session_quality_dim <- append(session_quality_dim, i$sessionQualityDim)\n#     else\n#         session_quality_dim <- append(session_quality_dim, 0)\n#     if(length(i$newVisits))\n#         new_visits <- append(new_visits, i$newVisits)\n#     else\n#         new_visits <- append(new_visits, 0)\n#     if(length(i$transactionRevenue))\n#         transaction_revenue <- append(transaction_revenue, i$transactionRevenue)\n#     else\n#         transaction_revenue <- append(transaction_revenue, 0)    \n# }\n\n# data['total_visits'] <- visits\n# data['total_hits'] <- hits\n# data['total_page_views'] <- page_views\n# data['total_total_transaction_revenue'] <- total_transaction_revenue\n# data['total_bounces'] <- bounces\n# data['total_new_visits'] <- new_visits\n# data['total_session_quality_dim'] <- session_quality_dim\n# data['total_transaction_revenue'] <- transaction_revenue\n# print('3b')\n# traffic_info <- lapply(data$trafficSource, fromJSON)\n# # traffic_info[1]\n# print('4a')\n\n# campaign <- c()\n# source <- c()\n# medium <- c()\n# keyword <- c()\n\n# for(i in traffic_info) {\n#     campaign <- append(campaign, i$campaign)\n#     source <- append(source, i$source)\n#     medium <- append(medium, i$medium)\n#     if(length(i$keyword))\n#         keyword <- append(keyword, i$keyword)\n#     else\n#         keyword <- append(keyword, '(not provided)')\n# }\n\n# data['traffic_campaign'] <- campaign\n# data['traffic_source'] <- source\n# data['traffic_medium'] <- medium\n# data['traffic_keyword'] <- keyword\n# print('4b')\n# # # gsub is used to replace characters and str_sub is used for splitting\n# custom_info <- lapply(str_sub(gsub(\"'\", '\"', data$customDimensions, 2, -2)), fromJSON) #gsub(\"\", \"\", data$hits)\n# # custom_info[1]\n# print('5a')\n\n# index <- c()\n# value <- c()\n\n# for(i in custom_info) {\n#     if(length(i$index))\n#         index <- append(index, i$index)\n#     else\n#         index <- append(index, 0)\n#     if(length(i$value))\n#         value <- append(value, i$value)    \n#     else\n#         value <- append(value, 0)\n# }\n\n# data['custom_index'] <- index\n# data['custom_value'] <- value\n# print('5b')","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:09:12.217874Z","iopub.execute_input":"2022-12-09T01:09:12.219228Z","iopub.status.idle":"2022-12-09T01:09:12.233843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Removing all columns from which data has been extracted\n# data <- data %>% select(-c('customDimensions', 'device', 'geoNetwork', 'totals', 'trafficSource'))\n\n# write.csv(data, './data-18.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:09:12.237376Z","iopub.execute_input":"2022-12-09T01:09:12.239017Z","iopub.status.idle":"2022-12-09T01:09:12.250519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Understanding the dataset and cleaning it","metadata":{}},{"cell_type":"code","source":"data <- read.csv('../input/ga-customer-value-mod-data/final_data.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:13:54.733040Z","iopub.execute_input":"2022-12-09T01:13:54.734509Z","iopub.status.idle":"2022-12-09T01:14:15.039738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length(data)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:15.043475Z","iopub.execute_input":"2022-12-09T01:14:15.045102Z","iopub.status.idle":"2022-12-09T01:14:15.061633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str(data)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:15.063980Z","iopub.execute_input":"2022-12-09T01:14:15.065281Z","iopub.status.idle":"2022-12-09T01:14:15.106147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting the data types of some columns in the dataframe\ndata$total_visits <- as.numeric(data$total_visits)\ndata$total_hits <- as.numeric(data$total_hits)\ndata$total_total_transaction_revenue <- as.numeric(data$total_total_transaction_revenue)\ndata$total_bounces <- as.numeric(data$total_bounces)\ndata$total_new_visits <- as.numeric(data$total_new_visits)\ndata$total_session_quality_dim <- as.numeric(data$total_session_quality_dim)\ndata$total_transaction_revenue <- as.numeric(data$total_transaction_revenue)\ndata$custom_index <- as.numeric(data$custom_index)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:15.108657Z","iopub.execute_input":"2022-12-09T01:14:15.110106Z","iopub.status.idle":"2022-12-09T01:14:15.153052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We remove the columns `total_visits, socialEngagementType, and total_page_vaiews` as they have just one value for all the datapoints in the dataset. We also remove the columns `visitId and fullVisitorId` as they are columns that depict the ID for the user.","metadata":{}},{"cell_type":"code","source":"data <- data %>% dplyr::select(-c('total_visits', 'socialEngagementType', 'total_page_views', 'visitId', 'fullVisitorId'))\nhead(data, 4)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:15.155521Z","iopub.execute_input":"2022-12-09T01:14:15.156939Z","iopub.status.idle":"2022-12-09T01:14:15.192944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_mat <- round(cor(data[sapply(data, is.numeric)]), 2)\nmelted_corr_mat <- melt(corr_mat) \noptions(repr.plot.width = 15, repr.plot.height = 8)\nggplot(data = melted_corr_mat, aes(x = Var1, y = Var2, fill=value)) + geom_tile() + ggtitle('Correlation Heatmap')","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:15.195409Z","iopub.execute_input":"2022-12-09T01:14:15.196812Z","iopub.status.idle":"2022-12-09T01:14:16.156712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Based on the above correlation matrix, we remove all the columns that are highly correlated with each other. There is no attribute that is directly correlated to our target `total_transaction_revenue` other than `totals_total_transaction_revenue` which is just a cummulative number of the target. Hence, we remove this also from our dataset.","metadata":{}},{"cell_type":"code","source":"data <- data %>% dplyr::select(-c('visitStartTime', 'total_total_transaction_revenue'))","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:16.159152Z","iopub.execute_input":"2022-12-09T01:14:16.160618Z","iopub.status.idle":"2022-12-09T01:14:16.175922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(data[sapply(data, is.numeric)])","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:16.178259Z","iopub.execute_input":"2022-12-09T01:14:16.179638Z","iopub.status.idle":"2022-12-09T01:14:17.606621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Removing all the rows that have numerical outliers in them","metadata":{}},{"cell_type":"code","source":"data <- data[data$visitNumber <= 420, ]","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:17.609941Z","iopub.execute_input":"2022-12-09T01:14:17.611579Z","iopub.status.idle":"2022-12-09T01:14:18.053498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Dealing with categorical variables","metadata":{}},{"cell_type":"code","source":"print('Number of unique instances from each column')\nfor(i in colnames(data[!sapply(data, is.numeric)]))\n    print(paste(i, length(table(data[i]))))","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:18.057095Z","iopub.execute_input":"2022-12-09T01:14:18.058576Z","iopub.status.idle":"2022-12-09T01:14:19.742068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Removing some of the columns that have a lot of categorical values","metadata":{}},{"cell_type":"code","source":"data <- data %>% dplyr::select(-c('geo_info_network_domain', 'traffic_keyword', 'traffic_source'))","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:19.745536Z","iopub.execute_input":"2022-12-09T01:14:19.747540Z","iopub.status.idle":"2022-12-09T01:14:19.765107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(gather(data[!sapply(data, is.numeric)], cols, value), aes(x = value)) + \n       geom_histogram(binwidth = 20, stat = 'count') + facet_grid(. ~ cols, scale = 'free') +\n       theme(axis.title.x=element_blank(),\n        axis.text.x=element_blank(),\n        axis.ticks.x=element_blank())","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:19.768626Z","iopub.execute_input":"2022-12-09T01:14:19.770170Z","iopub.status.idle":"2022-12-09T01:14:46.717997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Nearly 1.6 million data points in the column traffic_campaign is `not set`. We can remove this column.","metadata":{}},{"cell_type":"code","source":"data <- data[data$device_os != '(not set)', ]","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:46.720239Z","iopub.execute_input":"2022-12-09T01:14:46.721536Z","iopub.status.idle":"2022-12-09T01:14:47.074441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Removing all the rows that have the below mentioned OS","metadata":{}},{"cell_type":"code","source":"table(data$device_os)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:47.076736Z","iopub.execute_input":"2022-12-09T01:14:47.077975Z","iopub.status.idle":"2022-12-09T01:14:47.175789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data <- subset(data, (!device_os %in% c('FreeBSD', 'Firefox OS', 'Nintendo 3DS', 'Nintendo WiiU', 'Nokia', 'Playstation Vita', 'OpenBSD', 'NTT DoCoMo', 'SunOS', 'SymbianOS')))","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:47.178260Z","iopub.execute_input":"2022-12-09T01:14:47.179655Z","iopub.status.idle":"2022-12-09T01:14:47.608615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data <- data %>% dplyr::select(-c('traffic_campaign', 'geo_info_country'))\nhead(data, 4)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:47.611231Z","iopub.execute_input":"2022-12-09T01:14:47.612635Z","iopub.status.idle":"2022-12-09T01:14:47.642216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['category'] = 0\ndata[as.vector(data$total_transaction_revenue) > 0, ]['category'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:47.644954Z","iopub.execute_input":"2022-12-09T01:14:47.646315Z","iopub.status.idle":"2022-12-09T01:14:47.916743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length(data$category)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:47.919790Z","iopub.execute_input":"2022-12-09T01:14:47.921124Z","iopub.status.idle":"2022-12-09T01:14:47.936992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length(data)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:47.939234Z","iopub.execute_input":"2022-12-09T01:14:47.940516Z","iopub.status.idle":"2022-12-09T01:14:47.953956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Visualization","metadata":{}},{"cell_type":"code","source":"ggplot(data = aggregate(total_transaction_revenue ~ channelGrouping, data, sum)) + \n    geom_bar(aes(x = channelGrouping, y = total_transaction_revenue), stat = 'identity', fill = 'blue') + \n    ggtitle('Total transaction revenue for each channel grouping')\n\nggplot(data = aggregate(total_transaction_revenue ~ channelGrouping, data, mean)) + \n    geom_bar(aes(x = channelGrouping, y = total_transaction_revenue), stat = 'identity', fill = 'blue') + \n    ggtitle('Average transaction revenue for each channel grouping')","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:18:43.300934Z","iopub.execute_input":"2022-12-09T01:18:43.302738Z","iopub.status.idle":"2022-12-09T01:18:59.235289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(data = aggregate(total_transaction_revenue ~ device_os, data, sum)) + \n    geom_bar(aes(x = device_os, y = total_transaction_revenue), stat = 'identity', fill = 'blue') + \n    ggtitle('Total transaction revenue for each device OS')\n\nggplot(data = aggregate(total_transaction_revenue ~ device_os, data, mean)) + \n    geom_bar(aes(x = device_os, y = total_transaction_revenue), stat = 'identity', fill = 'blue') + \n    ggtitle('Average transaction revenue for each device OS')","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:18:59.237422Z","iopub.execute_input":"2022-12-09T01:18:59.238821Z","iopub.status.idle":"2022-12-09T01:19:15.162280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(data = aggregate(total_transaction_revenue ~ geo_info_continent, data, sum)) + \n    geom_bar(aes(x = geo_info_continent, y = total_transaction_revenue), stat = 'identity', fill = 'blue') + \n    ggtitle('Total transaction revenue for each continent')\n\nggplot(data = aggregate(total_transaction_revenue ~ geo_info_continent, data, mean)) + \n    geom_bar(aes(x = geo_info_continent, y = total_transaction_revenue), stat = 'identity', fill = 'blue') + \n    ggtitle('Average transaction revenue for each continent')","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:19:26.971407Z","iopub.execute_input":"2022-12-09T01:19:26.973152Z","iopub.status.idle":"2022-12-09T01:19:42.809813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data %>% group_by(device_os, device_category) %>% summarise(sales = sum(total_transaction_revenue), .groups = 'drop')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for(i in colnames(data[!sapply(data, is.numeric)]))\n    print(paste(i, length(table(data[i]))))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We are going to remove the columns geo_info_sub_continent and device_browser as they still have a lot of categories.","metadata":{}},{"cell_type":"code","source":"rownames(data) <- seq(1, length(data$date), 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:47.956171Z","iopub.execute_input":"2022-12-09T01:14:47.957452Z","iopub.status.idle":"2022-12-09T01:14:50.444257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data <- data %>% dplyr::select(-c('device_browser', 'geo_info_sub_continent', 'custom_value', 'custom_index'))","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:50.447876Z","iopub.execute_input":"2022-12-09T01:14:50.449425Z","iopub.status.idle":"2022-12-09T01:14:50.467574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(paste('Our final dataset size: ', length(data$date)))","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:50.471417Z","iopub.execute_input":"2022-12-09T01:14:50.473076Z","iopub.status.idle":"2022-12-09T01:14:50.488857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(data, aes(x = factor(category))) + geom_bar()","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:50.492394Z","iopub.execute_input":"2022-12-09T01:14:50.494087Z","iopub.status.idle":"2022-12-09T01:14:53.850438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(data, aes(x = total_transaction_revenue)) + geom_histogram(bins = 10)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:14:53.854065Z","iopub.execute_input":"2022-12-09T01:14:53.855509Z","iopub.status.idle":"2022-12-09T01:14:55.259896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data[!sapply(data, is.numeric)]\ndata$total_transaction_revenue <- sapply(data$total_transaction_revenue, log)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:15:03.933178Z","iopub.execute_input":"2022-12-09T01:15:03.934930Z","iopub.status.idle":"2022-12-09T01:15:04.766555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data$total_transaction_revenue[data$total_transaction_revenue < 0] <- 0\n","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:15:04.770220Z","iopub.execute_input":"2022-12-09T01:15:04.771803Z","iopub.status.idle":"2022-12-09T01:15:04.802214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ggplot(data, aes(x = total_transaction_revenue)) + geom_histogram(bins = 10)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:15:09.877322Z","iopub.execute_input":"2022-12-09T01:15:09.878781Z","iopub.status.idle":"2022-12-09T01:15:10.799534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Splitting data for modeling","metadata":{}},{"cell_type":"code","source":"set.seed(121399)\nsample <- sample.split(data$date, SplitRatio = 0.8)\ntrain  <- subset(data, sample == TRUE)\ntest   <- subset(data, sample == FALSE)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:47:09.640091Z","iopub.execute_input":"2022-12-08T23:47:09.641417Z","iopub.status.idle":"2022-12-08T23:47:17.702207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(paste('Length of training data: ', length(train$date)))\nprint(paste('Length of testing data: ', length(test$date)))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:47:17.705960Z","iopub.execute_input":"2022-12-08T23:47:17.708057Z","iopub.status.idle":"2022-12-08T23:47:17.730851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data modeling for predicting values\n\n#### Regression task","metadata":{}},{"cell_type":"markdown","source":"##### Backward elimination model for all the features in the dataset","metadata":{}},{"cell_type":"code","source":"full_model <- lm(total_transaction_revenue ~ . - category, data = train)\nsmall_model <- stepAIC(full_model, direction = c(\"backward\"), trace = FALSE)\nsmall_model_predictions <- predict(small_model, test)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:47:17.734292Z","iopub.execute_input":"2022-12-08T23:47:17.735975Z","iopub.status.idle":"2022-12-08T23:47:50.933267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"small_model_predictions[small_model_predictions < 0] <- 0\nprint(paste('Root Mean Squared Error of Step Model: ', rmse(test$total_transaction_revenue, small_model_predictions)))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:47:50.937669Z","iopub.execute_input":"2022-12-08T23:47:50.940384Z","iopub.status.idle":"2022-12-08T23:47:50.975750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### XGBoost model","metadata":{}},{"cell_type":"code","source":"train_x = data.matrix(train[, c(-11, -13)])\ntrain_y = train[, 11]\ntest_x = data.matrix(test[, c(-11, -13)])\ntest_y = test[, 11]\n\nxgb_train = xgb.DMatrix(data = train_x, label = train_y)\nxgb_test = xgb.DMatrix(data = test_x, label = test_y)\n\nwatchlist = list(train = xgb_train, test = xgb_test)\nxgb_model = xgb.train(data = xgb_train, watchlist = watchlist, nrounds = 30, eta = 0.05) # 0.05, 100 - 33.09 -> 0.05, 70 - 33.38\n\nxgb_regression_predictions <- predict(xgb_model, xgb_test)","metadata":{"execution":{"iopub.status.busy":"2022-12-09T01:41:27.180348Z","iopub.execute_input":"2022-12-09T01:41:27.182117Z","iopub.status.idle":"2022-12-09T01:41:27.205827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(paste('Root Mean Squared Error of Step Model: ', rmse(test$total_transaction_revenue, xgb_regression_predictions)))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:48:25.736053Z","iopub.execute_input":"2022-12-08T23:48:25.738158Z","iopub.status.idle":"2022-12-08T23:48:25.758429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"var_imp = xgb.importance(\n            feature_names = setdiff(names(xgb_train),\n            c(\"Item_Identifier\", \"Item_Outlet_Sales\")),\n            model = xgb_model)\n \n# Importance plot\nxgb.plot.importance(var_imp)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Decision Tree model","metadata":{}},{"cell_type":"code","source":"dt_regression_model <- rpart(total_transaction_revenue ~ ., data = train[, c(-13)])","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:48:30.101305Z","iopub.execute_input":"2022-12-08T23:48:30.102895Z","iopub.status.idle":"2022-12-08T23:48:56.853519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt_regression_predictions <- predict(dt_regression_model, test[, c(-13)])\ndt_regression_predictions[dt_regression_predictions < 0] <- 0","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:48:56.855823Z","iopub.execute_input":"2022-12-08T23:48:56.857158Z","iopub.status.idle":"2022-12-08T23:48:57.523797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(paste('Root Mean Squared Error of Decision Tree: ', rmse(test$total_transaction_revenue, dt_regression_predictions)))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:48:57.526296Z","iopub.execute_input":"2022-12-08T23:48:57.527660Z","iopub.status.idle":"2022-12-08T23:48:57.543829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rpart.plot(dt_regression_model)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T20:05:07.569921Z","iopub.execute_input":"2022-12-08T20:05:07.570947Z","iopub.status.idle":"2022-12-08T20:05:08.723499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Classification task","metadata":{}},{"cell_type":"markdown","source":"Logistic Regression with Backward Elimination Linear Regression parameters","metadata":{}},{"cell_type":"code","source":"logistic_model <- glm(formula = category ~ channelGrouping + date + \n    visitNumber + device_os + device_category + geo_info_continent + \n    total_hits + total_bounces + total_new_visits + total_session_quality_dim + \n    traffic_medium, data = train, family = 'binomial')","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:48:57.546124Z","iopub.execute_input":"2022-12-08T23:48:57.547413Z","iopub.status.idle":"2022-12-08T23:50:00.552946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logistic_predictions <- predict(logistic_model, test, type = 'response')\nsummary(logistic_model)\nlogistic_predictions = as.numeric(logistic_predictions)\n\nlogistic_predictions[logistic_predictions < 0.1] <- 0\nlogistic_predictions[logistic_predictions >= 0.1] <- 1\n\nconfusionMatrix(as.factor(logistic_predictions), as.factor(test$category))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:50:22.279291Z","iopub.execute_input":"2022-12-08T23:50:22.282030Z","iopub.status.idle":"2022-12-08T23:50:24.066309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"XGBoost for classification","metadata":{}},{"cell_type":"code","source":"train_x = data.matrix(train[, c(-11, -13)])\ntrain_y = train[, 13]\ntest_x = data.matrix(test[, c(-11, -13)])\ntest_y = test[, 13]\n\nxgb_train = xgb.DMatrix(data = train_x, label = train_y)\nxgb_test = xgb.DMatrix(data = test_x, label = test_y)\n\nwatchlist = list(train = xgb_train, test = xgb_test)\nxgb_classification_model = xgboost(data = xgb_train, max.depth = 3, nrounds = 50)\n\nxgb_classificatoin_predictions = predict(xgb_classification_model, xgb_test)\n\nxgb_classificatoin_predictions[xgb_classificatoin_predictions < 0.1] <- 0\nxgb_classificatoin_predictions[xgb_classificatoin_predictions >= 0.1] <- 1\n\nconfusionMatrix(as.factor(xgb_classificatoin_predictions), as.factor(test$category))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:53:30.582308Z","iopub.execute_input":"2022-12-08T23:53:30.583993Z","iopub.status.idle":"2022-12-08T23:53:31.053063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"var_imp = xgb.importance(\n            feature_names = setdiff(names(xgb_train),\n            c(\"Item_Identifier\", \"Item_Outlet_Sales\")),\n            model = xgb_classification_model)\n \n# Importance plot\nxgb.plot.importance(var_imp)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Decision tree model for classification","metadata":{}},{"cell_type":"code","source":"dt_classification_model <- rpart(category ~ ., data = train[, c(-11)])\n\nrpart.plot(dt_classification_model)","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:50:40.578675Z","iopub.execute_input":"2022-12-08T23:50:40.580540Z","iopub.status.idle":"2022-12-08T23:51:06.910161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndt_classification_predictions <- predict(dt_classification_model, test[, c(-11)])\ndt_classification_predictions[dt_classification_predictions < 0.05] <- 0\ndt_classification_predictions[dt_classification_predictions >= 0.05] <- 1\n\nconfusionMatrix(as.factor(dt_classification_predictions), as.factor(test$category))","metadata":{"execution":{"iopub.status.busy":"2022-12-08T23:51:06.914051Z","iopub.execute_input":"2022-12-08T23:51:06.916400Z","iopub.status.idle":"2022-12-08T23:51:08.190980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}