{"cells":[{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":false},"cell_type":"code","source":"#Install packages\ninstall.packages(c(\"h2o\",\"caret\",\"lme4\",\"ggalluvial\",\"xgboost\",\"jsonlite\",\"lubridate\",\"knitr\",\"Rmisc\",\"scales\",\"countrycode\",\"highcharter\",\"glmnet\",\"keras\",\"forecast\",\"zoo\",\"magrittr\",\"tidyverse\"))\n\nlibrary(data.table)\nlibrary(h2o)\nlibrary(caret)\nlibrary(lme4)\nlibrary(ggalluvial)\nlibrary(xgboost)\nlibrary(jsonlite)\nlibrary(lubridate)\nlibrary(knitr)\nlibrary(Rmisc)\nlibrary(scales)\nlibrary(countrycode)\nlibrary(highcharter)\nlibrary(glmnet)\nlibrary(keras)\nlibrary(forecast)\nlibrary(zoo)\nlibrary(magrittr)\nlibrary(tidyverse)\nlibrary(ggplot2)\nlibrary(gridExtra)\nlibrary(readr)\nlibrary(dplyr)\nlibrary(tidyr)\nlibrary(purrr)\nlibrary(ggExtra)\n\n#Load data\ntr <- read_csv('../input/train.csv')\nsubm <- read_csv('../input/sample_submission.csv')\ntr <- setDT(tr)\n\n#Convert JSON columns(JSON columns are \"device\", \"geoNetwork\", \"totals\", \"trafficSource\")\ntr_device <- paste(\"[\", paste(tr$device, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\ntr_geoNetwork <- paste(\"[\", paste(tr$geoNetwork, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\ntr_totals <- paste(\"[\", paste(tr$totals, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\ntr_trafficSource <- paste(\"[\", paste(tr$trafficSource, collapse = \",\"), \"]\") %>% fromJSON(flatten = T)\n\n#Combine to make the full data sets\ntr <- tr %>%\n    cbind(tr_device, tr_geoNetwork, tr_totals, tr_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)\n\n#Identify missing values and convert to NA\nna_vals <- c('unknown.unknown', '(not set)', 'not available in demo dataset', '(not provided)', '(none)', '<NA>')\n\nfor(col in names(tr)) {\n  \n  set(tr, i=which(tr[[col]] %in% na_vals), j=col, value=NA)\n  \n}\n\n#Plot missing value\ntr %>% summarise_all(funs(sum(is.na(.))/n()*100)) %>% \ngather(key=\"feature\", value=\"missing_pct\") %>% \n  ggplot(aes(x=reorder(feature,-missing_pct),y=missing_pct)) +\n  geom_bar(stat=\"identity\", fill=\"steelblue\")+\n  labs(y = \"Missing %\", x = \"Features\") +\n  coord_flip() +\n  theme_minimal()\n  \n#Drop columns with 100% missing values\nmissing_value_pct<-sapply(tr,function(col)sum(is.na(col))/length(col))\nmissing_del<-names(missing_value_pct[missing_value_pct==1])\ntr<-select(tr, -any_of(missing_del))\n\n#Correct data types       \ntr %<>%\n  mutate(date = ymd(date),\n         fullVisitorId = as.character(fullVisitorId),\n         sessionId = as.character(sessionId),\n         visitId = as.character(visitId),\n         visitStartTime = as_datetime(visitStartTime),\n         browser = factor(browser),\n         operatingSystem = factor(operatingSystem),\n         deviceCategory = factor(deviceCategory),\n         continent = factor(continent),\n         subContinent = factor(subContinent),\n         country = factor(country),\n         region = factor(region),\n         metro = factor (metro),\n         city = factor(city),\n         networkDomain = factor(networkDomain),\n         campaign = factor(campaign),\n         source = factor(source),\n         medium = factor(medium),\n         hits = as.integer(hits),\n         pageviews = as.integer(pageviews),\n         transactionRevenue = as.numeric(transactionRevenue))\n\n#Identify constant columns\nfea_uniq_values <- sapply(tr, n_distinct,na.rm=TRUE)\nfea_del <- names(fea_uniq_values[fea_uniq_values == 1])\n\n#Remove constant columns\ntr<-select(tr, -any_of(fea_del))\n\n#Describe numeric input columns\nsummary(tr[,c(\"visitNumber\",\"hits\",\"pageviews\")])\n\n#Describe non-numeric input columns\ntr <- setDT(tr)\np1 <- tr[, .(n = .N), by=channelGrouping] %>%\n  ggplot(aes(x=reorder(channelGrouping, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='Channel Grouping',\n       y='Visits (000s)',\n       title='Visits by channelGrouping')\n\np2 <- top_n(tr[, .(n = .N), by=browser],10,n) %>%\n  ggplot(aes(x=reorder(browser, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='browser',\n       y='Visits (000s)',\n       title='Top 10 Visits by browser')    \n\np3 <- na.omit(top_n(tr[, .(n = .N), by=operatingSystem],11,n))%>%\n  ggplot(aes(x=reorder(operatingSystem, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 1), hjust=0.5))+\n  coord_flip() + \n  labs(x='operatingSystem',\n       y='Visits (000s)',\n       title='Top 10 Visits by operatingSystem')           \n\np4 <- tr[, .(n = .N), by=isMobile] %>%\n  ggplot(aes(x=reorder(isMobile, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='isMobile',\n       y='Visits (000s)',\n       title='Visits by isMobile')  \n\np5 <- tr[, .(n = .N), by=deviceCategory] %>%\n  ggplot(aes(x=reorder(deviceCategory, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='deviceCategory',\n       y='Visits (000s)',\n       title='Visits by deviceCategory')  \n\np6 <- na.omit(tr[, .(n = .N), by=continent]) %>%\n  ggplot(aes(x=reorder(continent, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='continent',\n       y='Visits (000s)',\n       title='Visits by continent')  \n\np7 <- top_n(tr[, .(n = .N), by=subContinent],10,n) %>%\n  ggplot(aes(x=reorder(subContinent, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='subContinent',\n       y='Visits (000s)',\n       title='Top 10 Visits by subContinent')        \n\np8 <- top_n(tr[, .(n = .N), by=country],10,n) %>%\n  ggplot(aes(x=reorder(country, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='country',\n       y='Visits (000s)',\n       title='Top 10 Visits by country')  \n\np9 <- na.omit(top_n(tr[, .(n = .N), by=region],11,n)) %>%\n  ggplot(aes(x=reorder(region, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='region',\n       y='Visits (000s)',\n       title='Top 10 Visits by region')  \n\np10 <- na.omit(top_n(tr[, .(n = .N), by=metro],11,n)) %>%\n  ggplot(aes(x=reorder(metro, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='metro',\n       y='Visits (000s)',\n       title='Top 10 Visits by metro')  \n       \np11 <- na.omit(top_n(tr[, .(n = .N), by=city],11,n)) %>%\n  ggplot(aes(x=reorder(city, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n   geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='city',\n       y='Visits (000s)',\n       title='Top 10 Visits by city')  \n\np12 <- na.omit(top_n(tr[, .(n = .N), by=networkDomain],11,n)) %>%\n  ggplot(aes(x=reorder(networkDomain, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='networkDomain',\n       y='Visits (000s)',\n       title='Top 10 Visits by networkDomain')\n\np13 <- na.omit(top_n(tr[, .(n = .N), by=campaign],11,n)) %>%\n  ggplot(aes(x=reorder(campaign, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 3), hjust=0.2))+\n  coord_flip() + \n  labs(x='campaign',\n       y='Visits (000s)',\n       title='Visits by campaign')\n\np14 <- na.omit(top_n(tr[, .(n = .N), by=source],10,n)) %>%\n  ggplot(aes(x=reorder(source, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='source',\n       y='Visits (000s)',\n       title='Top 10 Visits by source')\n\np15 <- na.omit(top_n(tr[, .(n = .N), by=medium],11,n)) %>%\n  ggplot(aes(x=reorder(medium, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='medium',\n       y='Visits (000s)',\n       title='Visits by medium')\n\np16 <- na.omit(top_n(tr[, .(n = .N), by=keyword],11,n)) %>%\n  ggplot(aes(x=reorder(keyword, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 1), hjust=0.5))+\n  coord_flip() + \n  labs(x='keyword',\n       y='Visits (000s)',\n       title='Top 10 Visits by keyword')\n\np17 <- na.omit(top_n(tr[, .(n = .N), by=referralPath],11,n)) %>%\n  ggplot(aes(x=reorder(referralPath, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 0), hjust=0.5))+\n  coord_flip() + \n  labs(x='referralPath',\n       y='Visits (000s)',\n       title='Top 10 Visits by referralPath')\n\np18 <- na.omit(top_n(tr[, .(n = .N), by=adContent],11,n)) %>%\n  ggplot(aes(x=reorder(adContent, n), y=n)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n,digits = 1), hjust=0))+\n  coord_flip() + \n  labs(x='adContent',\n       y='Visits',\n       title='Top 10 Visits by adContent')\n\np19 <- na.omit(top_n(tr[, .(n = .N), by=adwordsClickInfo.page],10,n)) %>%\n  ggplot(aes(x=reorder(adwordsClickInfo.page, n), y=n)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n,digits = 3), hjust=0))+\n  coord_flip() + \n  labs(x='adwordsClickInfo.page',\n       y='Visits',\n       title='Visits by adwordsClickInfo.page')\n\np20 <- na.omit(top_n(tr[, .(n = .N), by=adwordsClickInfo.slot],10,n)) %>%\n  ggplot(aes(x=reorder(adwordsClickInfo.slot, n), y=n)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n,digits =1 ), hjust=0))+\n  coord_flip() + \n  labs(x='adwordsClickInfo.slot',\n       y='Visits',\n       title='Visits by adwordsClickInfo.slot')\n\np21 <- na.omit(top_n(tr[, .(n = .N), by=adwordsClickInfo.gclId],11,n)) %>%\n  ggplot(aes(x=reorder(adwordsClickInfo.gclId, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n/1000,digits = 2), hjust=0))+\n  coord_flip() + \n  labs(x='adwordsClickInfo.gclId',\n       y='Visits (000s)',\n       title='Top 10 Visits by adwordsClickInfo.gclId')    \n\np22 <- na.omit(top_n(tr[, .(n = .N), by=adwordsClickInfo.adNetworkType],11,n)) %>%\n  ggplot(aes(x=reorder(adwordsClickInfo.adNetworkType, n), y=n)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  geom_text(aes(label=round(n,digits = 0), hjust=0))+\n  coord_flip() + \n  labs(x='adwordsClickInfo.adNetworkType',\n       y='Visits',\n       title='Visits by adwordsClickInfo.adNetworkType')         \n\ngrid.arrange(p1,p2,p3,p4,p5,nrow=2)\ngrid.arrange(p6,p7,p8,p9,p10,p11,nrow=2)\ngrid.arrange(p12,p13,p14,p15,p16,p17,nrow=2)\ngrid.arrange(p18,p19,nrow=2)\ngrid.arrange(p20,p22,nrow=2)\n\n##tr %>% \n##ggplot(aes(x=visitNumber, y=..density..)) + \n##geom_histogram(fill='steelblue', na.rm=TRUE, bins=50) + \n##geom_density(aes(x=visitNumber), fill='orange', color='orange', alpha=0.3, na.rm=TRUE) + \n##labs(\n##title = 'Distribution of transaction revenue',\n##x = 'Natural log of transaction revenue'\n##)\n\n##Data Exploration##\n\n#Target variable time range\ntime_range <- range(tr$date)\nprint(time_range)\n\n#Target Variable (transactionRevenue) descriptive statistics\ny <- tr$transactionRevenue/1e6\nsummary(y)\n\n#Percentage of visits with non-zero transaction revenue\ny[is.na(y)] <- 0\nlength(y[y!=0])\nnon_zero_rev <- (length(y[y!=0])/length(y))\nsprintf(\"%1.2f%%\", non_zero_rev*100)\n(sum(y))\n(sum(y))/length(y[y!=0])\n\n#Distribution of revenue<1000USD\noptions(repr.plot.height=4)\ntr %>% filter(transactionRevenue/1e6 >0 & transactionRevenue/1e6 <1000) %>%\nggplot(aes(x=transactionRevenue/1e6)) +\n        geom_histogram(fill=\"blue\", binwidth=10) +\n        scale_x_continuous(breaks= seq(0, 1000, by=100), labels = comma)\n\n#Distribution of log revenue      \ntr %>% \n  ggplot(aes(x=log(transactionRevenue/1e6), y=..density..)) + \n  geom_histogram(fill='steelblue', na.rm=TRUE, bins=40) + \n  geom_density(aes(x=log(transactionRevenue/1e6)), fill='orange', color='orange', alpha=0.3, na.rm=TRUE) + \n  labs(\n    title = 'Distribution of transaction revenue',\n    x = 'Natural log of transaction revenue'\n  )\n  \n#bivariate analysis (scatter plot)\npairs(tr[,1:31], pch = 19, lower.panel = NULL)\n\n#Daily visit and revenue\nsetDT(tr)\ntr[, transactionRevenue := transactionRevenue / 1e+06]\n\ng1 <- tr[, .(n = .N), by=date] %>%\n  ggplot(aes(x=date, y=n)) + \n  geom_line(color='steelblue') +\n  geom_smooth(color='orange') + \n  labs(\n    x='',\n    y='Visits (000s)',\n    title='Daily visits'\n  )\n\ng2 <- tr[, .(revenue = sum(transactionRevenue, na.rm=TRUE)), by=date] %>%\n  ggplot(aes(x=date, y=revenue)) + \n  geom_line(color='steelblue') +\n  geom_smooth(color='orange') + \n  labs(\n    x='',\n    y='Revenue (unit dollars)',\n    title='Daily transaction revenue'\n  )\n\ngrid.arrange(g1, g2, nrow=2)\n\n#Transacion and Revenue by [hour of the day]\ng1 <-\n  tr[, .(visitHour = hour(visitStartTime))][\n    , .(visits = .N), by = visitHour] %>%\n  ggplot(aes(x = visitHour, y = visits / 1000)) +\n  geom_line(color = 'steelblue', size = 1) +\n  geom_point(color = 'steelblue', size = 2) +\n  labs(\n  x = 'Hour of day',\n  y = 'Visits (000s)',\n  title = 'Aggregate visits by hour of day (UTC)',\n  subtitle = 'August 1, 2016 to August 1, 2017'\n  \n  )\n\ng2 <-\n  tr[, .(transactionRevenue, visitHour = hour(visitStartTime))][\n    , .(revenue = sum(transactionRevenue, na.rm =\n  T)), by = visitHour] %>%\n  ggplot(aes(x = visitHour, y = revenue / 1000)) +\n  geom_line(color = 'steelblue', size = 1) +\n  geom_point(color = 'steelblue', size = 2) +\n  labs(\n  x = 'Hour of day',\n  y = 'Transaction revenue (000s)',\n  title = 'Aggregate revenue by hour of day (UTC)',\n  subtitle = 'August 1, 2016 to August 1, 2017'\n\n  )\n\ngrid.arrange(g1, g2, nrow = 2)\n\n##Devise Features##\n\n#Visit, total revenue, mean revenue by [channel]\ng1 <- tr[, .(n = .N), by=channelGrouping] %>%\n  ggplot(aes(x=reorder(channelGrouping, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  coord_flip() + \n  labs(x='Channel Grouping',\n       y='Visits (000s)',\n       title='Visits by channel grouping')\n\ng2 <- tr[, .(revenue = sum(transactionRevenue, na.rm=TRUE)), by=channelGrouping] %>%\n  ggplot(aes(x=reorder(channelGrouping, revenue), y=revenue/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  coord_flip() + \n  labs(x='',\n       y='Revenue (dollars, 000s)',\n       title='Total revenue by channel grouping')\n\ng3 <- tr[, .(meanRevenue = mean(transactionRevenue, na.rm=TRUE)), by=channelGrouping] %>%\n  ggplot(aes(x=reorder(channelGrouping, meanRevenue), y=meanRevenue)) + \n  geom_bar(stat='identity', fill='steelblue') + \n  coord_flip() + \n  labs(x='', \n       y='Revenue (dollars)',\n       title='Mean revenue by channel grouping')\n\ngrid.arrange(g1,g2, g3, ncol = 3)\n\n#Visit, total revenue, mean revenue by [isMobile]\ntr %>%\n  ggplot(aes(x=log(transactionRevenue), y=..density.., fill=isMobile)) +\n  geom_density(alpha=0.5) + \n  scale_fill_manual(values = c('steelblue', 'orange')) + \n  labs(title='Distribution of log revenue by mobile and non-mobile devices')\n\ng1 <- tr[, .(n = .N), by=isMobile] %>%\n  ggplot(aes(x=reorder(isMobile, n), y=n/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  coord_flip() + \n  labs(x='isMobile',\n       y='Visits (000s)',\n       title='Visits by mobile and non-mobile devices')\n\ng2 <- tr[, .(revenue = sum(transactionRevenue, na.rm=TRUE)), by=isMobile] %>%\n  ggplot(aes(x=reorder(isMobile, revenue), y=revenue/1000)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  coord_flip() + \n  labs(x='',\n       y='Revenue (dollars, 000s)',\n       title='Total revenue by mobile and non-mobile devices')\n\ng3 <- tr[, .(meanRevenue = mean(transactionRevenue, na.rm=TRUE)), by=isMobile] %>%\n  ggplot(aes(x=reorder(isMobile, meanRevenue), y=meanRevenue)) + \n  geom_bar(stat='identity', fill='steelblue') + \n  coord_flip() + \n  labs(x='', \n       y='Revenue (dollars)',\n       title='Mean revenue by mobile and non-mobile devices')\n\ngrid.arrange(g1,g2, g3, ncol = 3)\n\n#Visit, total revenue, mean revenue by [operatingSystem]\ng1 <- tr[, .(n=.N/1000), by=operatingSystem][\n  n > 0.001\n] %>%\n  ggplot(aes(x=reorder(operatingSystem, -n), y=n)) + \n  geom_bar(stat='identity', fill='steelblue') +\n  labs(x='Operating System', \n       y='# of visits in data set (000s)',\n       title='Distribution of visits by device operating system') + \n  theme(axis.text.x = element_text(angle = 90, hjust = 1))\n\ng2 <- tr[, .(revenue = sum(transactionRevenue, na.rm=TRUE)), by=operatingSystem][\n  revenue > 0, \n] %>%\n  ggplot(aes(x=reorder(operatingSystem, -revenue), y=revenue)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  labs(x='Operating System',\n       y='Revenue (unit dollars)',\n       title='Distribution of revenue by device operating system') +\n  theme(axis.text.x = element_text(angle = 90, hjust = 1))\n\ngrid.arrange(g1, g2, nrow=2)\n\n#Visit, total revenue, mean revenue by [browser]\ng1 <- tr[, .(n=.N/1000), by=browser][\n  1:10\n] %>%\n  ggplot(aes(x=reorder(browser, -n), y=n)) + \n  geom_bar(stat='identity', fill='steelblue') +\n  labs(x='Browser', \n       y='# of visits in data set (000s)',\n       title='Distribution of visits by browser (Top 10 browsers)') + \n  theme(axis.text.x = element_text(angle = 90, hjust = 1))\n\ng2 <- tr[, .(revenue = sum(transactionRevenue, na.rm=TRUE)/1000), by=browser][\n  1:10\n] %>%\n  ggplot(aes(x=reorder(browser, -revenue), y=revenue)) +\n  geom_bar(stat='identity', fill='steelblue') +\n  labs(x='Browser',\n       y='Revenue (dollars, 000s)',\n       title='Distribution of revenue by browser (top 10 browsers)') +\n  theme(axis.text.x = element_text(angle = 90, hjust = 1))\n\ngrid.arrange(g1, g2, nrow=2)\n\n##Traffic Source##\ng1 <- tr[, .(visits = .N), by = medium][\n  !is.na(medium)] %>%\n  ggplot(aes(x=reorder(medium, visits), y=visits / 1000)) + \n  geom_bar(stat='identity', fill='steelblue') + \n  coord_flip() + \n  labs(\n    x='Medium',\n    y='Visits (000s)',\n    title='Distribution of visits by medium')\n\ng2 <- tr[, .(revenue = sum(transactionRevenue, na.rm=TRUE)), by = medium][\n  !is.na(medium)] %>%\n  ggplot(aes(x=reorder(medium, revenue), y=revenue / 1000)) + \n  geom_bar(stat='identity', fill='steelblue') + \n  coord_flip() + \n  labs(\n    x='',\n    y='Transaction revenue (dollars, 000s)',\n    title='Distribution of revenue by medium')\n    \ng3 <- tr[, .(meanRevenue = mean(transactionRevenue, na.rm=TRUE)), by=medium][\n  !is.na(medium)] %>%\n  ggplot(aes(x=reorder(medium, meanRevenue), y=meanRevenue)) + \n  geom_bar(stat='identity', fill='steelblue') + \n  coord_flip() + \n  labs(x='', \n       y='Revenue (dollars)',\n       title='Mean revenue by medium')\n\n\ngrid.arrange(g1, g2,g3, ncol=3)\n\n##Geographic Features##\n#Total Revenue by Continent\ntr[, .(revenue = sum(transactionRevenue, na.rm=TRUE)/1000), by = continent][\n  !is.na(continent),\n] %>%\n  ggplot(aes(x=reorder(continent, revenue), y=revenue)) + \n  geom_bar(stat='identity', fill='steelblue') + \n  coord_flip() + \n  labs(x='', y='Revenue (dollars, 000s)', title='Total transaction revenue by continent')\n  \n#Visit, total revenue, mean revenue by [country]\ntr %>% \n  bind_cols(as_tibble(y)) %>% \n  mutate(country = factor(country) %>% fct_lump(prop=0.023)) %>% \n  group_by(country) %>% \n  summarize(visits = n(), mean_revenue = mean(value), total_revenue = sum(value)) %>% \n  ungroup() %>% \n  mutate(country = reorder(country, -visits)) %>% \n  data.table::melt(id.vars = c(\"country\")) %>% \n  ggplot(aes(country, value, fill = variable)) +\n  geom_bar(stat = \"identity\") +\n  scale_y_continuous(labels = scales::comma) +\n  facet_wrap(~ variable, scales = \"free\") + \n  theme_minimal() +\n  labs(x = \"country\", y = \"\") +\n  theme(axis.text.x = element_text(angle = 45, hjust = 1),\n        legend.position=\"none\") \n\n#Group by country and calculate total transaction revenue (log)\nby_country <- tr[, .(n = .N, revenue = log(sum(transactionRevenue, na.rm=TRUE))), by = country]\nby_country$iso3 <- countrycode(by_country$country, origin='country.name', destination='iso3c')\nby_country[, rev_per_visit := revenue / n]\n\n#Create the highcharter map of revenue by country\nhighchart() %>%\n    hc_add_series_map(worldgeojson, by_country, value = 'revenue', joinBy = 'iso3') %>%\n    hc_title(text = 'Total transaction revenue by country (natural log)') %>%\n    hc_subtitle(text = \"August 2016 to August 2017\") %>%\n    hc_tooltip(useHTML = TRUE, headerFormat = \"\",\n        pointFormat = \"{point.country}: ${point.revenue:.0f}\")\n        \n","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":4}