{"cells": [{"cell_type": "code", "source": ["# Including the required libraries\n", "library(ggplot2) \n", "library(data.table)"], "outputs": [], "execution_count": null, "metadata": {"_kg_hide-input": false, "_uuid": "cb0d81cdf909773e663f19b618dbc77f5105bb0c", "_kg_hide-output": false, "_cell_guid": "f55b8926-d70e-43e3-a2b8-792853b606ae"}}, {"cell_type": "code", "source": ["# Reading Train and Transactions\n", "eda_train_data = fread(\"../input/train.csv\")\n", "eda_transactions_data = fread(\"../input/transactions.csv\")\n", "\n", "head(eda_train_data)\n", "head(eda_transactions_data)\n", "\n", "setkey(eda_train_data, msno)\n", "setkey(eda_transactions_data, msno)"], "outputs": [], "execution_count": null, "metadata": {"_kg_hide-input": false, "_kg_hide-output": false}}, {"cell_type": "code", "source": ["# Left outer join on eda_train_data and eda_transactions_data\n", "merged_dt = merge(eda_train_data, \n", "                  eda_transactions_data[, .(msno, is_auto_renew)], \n", "                  all.x=TRUE)\n", "\n", "# Grouping by msno. \n", "dt_renew_churn = merged_dt[, .( auto_renew_ones = sum(is_auto_renew==1),\n", "                                is_churn=max(is_churn),\n", "                                nof_transcations = .N\n", "                               ), \n", "                             by=msno]\n", "\n", "# Calculating % of transactions with is_auto_renew==1\n", "dt_renew_churn[, percent_renew := (100*auto_renew_ones)/nof_transcations]\n", "\n", "# Binning the percent_renew\n", "breaks = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110)\n", "# 0 : [0,10)\n", "# 1 : [10,20) and so on\n", "dt_renew_churn[, percent_renew_bin:= .bincode(percent_renew, breaks, FALSE, TRUE) - 1 ]\n", "\n", "# Creating pivot table\n", "d = dt_renew_churn[, .(count=.N, churns=sum(is_churn), percent_churns=sum(is_churn)/.N), \n", "                 by=percent_renew_bin][order(percent_renew_bin)]\n", "d\n", "\n", "# Plotting\n", "ggplot(data=d, aes(x=percent_renew_bin, y=percent_churns)) + geom_bar(stat=\"identity\")"], "outputs": [], "execution_count": null, "metadata": {}}, {"cell_type": "markdown", "source": ["As you can see, there are differences in percentage of churns for users with different % of 'is_auto_renew'.\n", "'percent_churns' for each user seems to be a significant variable."], "metadata": {}}], "nbformat": 4, "nbformat_minor": 1, "metadata": {"kernelspec": {"language": "R", "name": "ir", "display_name": "R"}, "language_info": {"name": "R", "file_extension": ".r", "version": "3.4.1", "codemirror_mode": "r", "mimetype": "text/x-r-source", "pygments_lexer": "r"}}}