1
0
This repository has been archived on 2025-03-06. You can view files and clone it, but cannot push or open issues or pull requests.
ResearchMethods/wk8/wk8.ipynb

420 lines
14 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Week 8: Unrest via Sparse Credit Card Data"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using matplotlib backend: TkAgg\n",
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"source": [
"%pylab\n",
"%matplotlib inline\n",
"import pandas as pd\n",
"from pandas import ExcelWriter\n",
"from pandas import ExcelFile\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from scipy import stats"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We start the analysis by importing the data. Note, we are only importing the data provided in Table 1 of the Appendix of Dong et al.<sup>1</sup> as we are only analysing data from the references days and Day 62.\n",
"\n",
"**\\*\\* Double Check this with Murray \\*\\***\n",
"\n",
"TO DO:\n",
"\\*\\* Need to test the statistical significance of the data"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# Contains data about the number of customers during the recorded time period\n",
"cust_data = pd.read_excel('edited_Dong_etal_2018_data.xlsx', sheetname='# of Customers')\n",
"# Contains data about the customers' median spending amounts during the recorded time period\n",
"spend_data = pd.read_excel('edited_Dong_etal_2018_data.xlsx', sheetname='Median spending amount')\n",
"# Contains data about the number of transactions during the recorded time period\n",
"trans_data = pd.read_excel('edited_Dong_etal_2018_data.xlsx', sheetname='# of transactions')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <style type=\"text/css\" >\n",
" \n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow0_col0 {\n",
" \n",
" background-color: #fc7f00;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow0_col1 {\n",
" \n",
" background-color: #e4ff7a;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow0_col2 {\n",
" \n",
" background-color: #e4ff7a;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow0_col3 {\n",
" \n",
" background-color: #e4ff7a;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow1_col0 {\n",
" \n",
" background-color: #f9ed2f;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow1_col1 {\n",
" \n",
" background-color: #fc7f00;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow1_col2 {\n",
" \n",
" background-color: #fc8000;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow1_col3 {\n",
" \n",
" background-color: #fd8c00;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow2_col0 {\n",
" \n",
" background-color: #f9ed2f;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow2_col1 {\n",
" \n",
" background-color: #fc8000;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow2_col2 {\n",
" \n",
" background-color: #fc7f00;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow2_col3 {\n",
" \n",
" background-color: #fd8b00;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow3_col0 {\n",
" \n",
" background-color: #e4ff7a;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow3_col1 {\n",
" \n",
" background-color: #fe9000;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow3_col2 {\n",
" \n",
" background-color: #fd8f00;\n",
" \n",
" }\n",
" \n",
" #T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow3_col3 {\n",
" \n",
" background-color: #fc7f00;\n",
" \n",
" }\n",
" \n",
" </style>\n",
"\n",
" <table id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01b\" None>\n",
" \n",
"\n",
" <thead>\n",
" \n",
" <tr>\n",
" \n",
" \n",
" <th class=\"blank level0\" >\n",
" \n",
" \n",
" \n",
" \n",
" <th class=\"col_heading level0 col0\" colspan=1>\n",
" Distance from event center (km)\n",
" \n",
" \n",
" \n",
" <th class=\"col_heading level0 col1\" colspan=1>\n",
" Min of ref. Days\n",
" \n",
" \n",
" \n",
" <th class=\"col_heading level0 col2\" colspan=1>\n",
" Max of ref. Days\n",
" \n",
" \n",
" \n",
" <th class=\"col_heading level0 col3\" colspan=1>\n",
" Decrease on Day 62\n",
" \n",
" \n",
" </tr>\n",
" \n",
" </thead>\n",
" <tbody>\n",
" \n",
" <tr>\n",
" \n",
" \n",
" <th id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01b\"\n",
" class=\"row_heading level0 row0\" rowspan=1>\n",
" Distance from event center (km)\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow0_col0\"\n",
" class=\"data row0 col0\" >\n",
" 1\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow0_col1\"\n",
" class=\"data row0 col1\" >\n",
" -0.104681\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow0_col2\"\n",
" class=\"data row0 col2\" >\n",
" -0.101477\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow0_col3\"\n",
" class=\"data row0 col3\" >\n",
" -0.375261\n",
" \n",
" \n",
" </tr>\n",
" \n",
" <tr>\n",
" \n",
" \n",
" <th id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01b\"\n",
" class=\"row_heading level0 row1\" rowspan=1>\n",
" Min of ref. Days\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow1_col0\"\n",
" class=\"data row1 col0\" >\n",
" -0.104681\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow1_col1\"\n",
" class=\"data row1 col1\" >\n",
" 1\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow1_col2\"\n",
" class=\"data row1 col2\" >\n",
" 0.990716\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow1_col3\"\n",
" class=\"data row1 col3\" >\n",
" 0.855398\n",
" \n",
" \n",
" </tr>\n",
" \n",
" <tr>\n",
" \n",
" \n",
" <th id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01b\"\n",
" class=\"row_heading level0 row2\" rowspan=1>\n",
" Max of ref. Days\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow2_col0\"\n",
" class=\"data row2 col0\" >\n",
" -0.101477\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow2_col1\"\n",
" class=\"data row2 col1\" >\n",
" 0.990716\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow2_col2\"\n",
" class=\"data row2 col2\" >\n",
" 1\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow2_col3\"\n",
" class=\"data row2 col3\" >\n",
" 0.869914\n",
" \n",
" \n",
" </tr>\n",
" \n",
" <tr>\n",
" \n",
" \n",
" <th id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01b\"\n",
" class=\"row_heading level0 row3\" rowspan=1>\n",
" Decrease on Day 62\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow3_col0\"\n",
" class=\"data row3 col0\" >\n",
" -0.375261\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow3_col1\"\n",
" class=\"data row3 col1\" >\n",
" 0.855398\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow3_col2\"\n",
" class=\"data row3 col2\" >\n",
" 0.869914\n",
" \n",
" \n",
" \n",
" <td id=\"T_14d8b9dc_4dcc_11e8_bb8d_8086f2cce01brow3_col3\"\n",
" class=\"data row3 col3\" >\n",
" 1\n",
" \n",
" \n",
" </tr>\n",
" \n",
" </tbody>\n",
" </table>\n",
" "
],
"text/plain": [
"<pandas.formats.style.Styler at 0x7f4b09f5a630>"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cust_data.corr().style.background_gradient(cmap='Wistia')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now to plot some data..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### References\n",
"1. Dong, X., Meyer, J., Shmueli, E., Bozkaya, B., & Pentland, A. (2018). Methods for quantifying effects of social unrest using credit card transaction data. EPJ Data Science, 7(1), 8. https://doi.org/10.1140/epjds/s13688-018-0136-x"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}