{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from matplotlib import pyplot\n",
    "\n",
    "from scipy.stats import shapiro\n",
    "from scipy.stats import normaltest\n",
    "from scipy.stats import anderson\n",
    "from scipy.stats import chi2_contingency\n",
    "from scipy.stats import chi2\n",
    "\n",
    "import statsmodels.api as sm\n",
    "from statsmodels.formula.api import ols\n",
    "from statsmodels.stats.multicomp import pairwise_tukeyhsd\n",
    "from statsmodels.graphics.gofplots import qqplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import the data\n",
    "campaigndf = pd.read_csv(\"campaign.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "campaigndf.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "campaigndf.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "campaigndf.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#t-test\n",
    "t_val, p_val = stats.ttest_ind(campaigndf['Logins 4 weeks'], campaigndf['Logins 6 months'])\n",
    "print(\"(Login 4w, Login 6m) => t-value=%s, p-value=%s\" % (str(t_val), str(p_val)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#t-test\n",
    "t_val, p_val = stats.ttest_ind(campaigndf['Sales 4 weeks'], campaigndf['Sales 6 months'])\n",
    "print(\"(Sales 4w, Sales 6m) => t-value=%s, p-value=%s\" % (str(t_val), str(p_val)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Significant difference between avg number of logins/1 month and logins/6 month - indicates healthy visit rates\n",
    "\n",
    "Significant difference between avg sales/1 month and sales/6 month - indicates healthy sales"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#anova\n",
    "model = ols('Age ~ C(Q(\"Gender\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Age - Gender\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('Age ~ C(Q(\"Area\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Age - Area\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('Age ~ C(Q(\"Mobile\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Age - Mobile\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('Age ~ C(Q(\"Response\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Age - Response\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "No significant difference of avg. age in Yes/No responses, different mobile usage patterns, residential areas of gender. Therefore, analying age across these variables is not useful. However, age could be used in ML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "L4 = campaigndf['Logins 4 weeks']\n",
    "model = ols('L4 ~ C(Q(\"Gender\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Logins 4 weeks - Gender\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('L4 ~ C(Q(\"Area\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Logins 4 weeks - Area\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('L4 ~ C(Q(\"Mobile\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Logins 4 weeks - Mobile\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('L4 ~ C(Q(\"Response\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Logins 4 weeks - Response\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The avg number of logins/month is siginificantly different for response. Lets do Tukey (below) and see the actual difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tukey = pairwise_tukeyhsd(endog=campaigndf['Logins 4 weeks'],\n",
    "                          groups=campaigndf['Response'],\n",
    "                          alpha=0.05)\n",
    "\n",
    "#display results\n",
    "print(tukey)\n",
    "\n",
    "campaigndf.pivot_table(columns=['Response'], values='Logins 4 weeks', aggfunc=np.mean).round(2).plot.bar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "L6 = campaigndf['Logins 6 months']\n",
    "model = ols('L6 ~ C(Q(\"Gender\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Logins 6 months - Gender\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('L6 ~ C(Q(\"Area\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Logins 6 months - Area\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('L6 ~ C(Q(\"Mobile\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Logins 6 months - Mobile\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('L6 ~ C(Q(\"Response\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Logins 6 months - Response\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The avg number of logins/6 month is siginificantly different for response and gender. Lets do Tukey (below) and see the actual difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tukey = pairwise_tukeyhsd(endog=campaigndf['Logins 6 months'],\n",
    "                          groups=campaigndf['Response'],\n",
    "                          alpha=0.05)\n",
    "\n",
    "#display results\n",
    "print(tukey)\n",
    "campaigndf.pivot_table(columns=['Response'], values='Logins 6 months', aggfunc=np.mean).round(2).plot.bar()\n",
    "\n",
    "\n",
    "tukey = pairwise_tukeyhsd(endog=campaigndf['Logins 6 months'],\n",
    "                          groups=campaigndf['Gender'],\n",
    "                          alpha=0.05)\n",
    "\n",
    "#display results\n",
    "print(tukey)\n",
    "campaigndf.pivot_table(columns=['Gender'], values='Logins 6 months', aggfunc=np.mean).round(2).plot.bar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "S4 = campaigndf['Sales 4 weeks']\n",
    "model = ols('S4 ~ C(Q(\"Gender\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales 4 weeks - Gender\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('S4 ~ C(Q(\"Area\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales 4 weeks - Area\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('S4 ~ C(Q(\"Mobile\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales 4 weeks - Mobile\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('S4 ~ C(Q(\"Response\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales 4 weeks - Response\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The avg sales/1 month is siginificantly different for response and area. Lets do Tukey (below) and see the actual difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tukey = pairwise_tukeyhsd(endog=campaigndf['Sales 4 weeks'],\n",
    "                          groups=campaigndf['Area'],\n",
    "                          alpha=0.05)\n",
    "\n",
    "#display results\n",
    "print(tukey)\n",
    "campaigndf.pivot_table(columns=['Area'], values='Sales 4 weeks', aggfunc=np.mean).round(2).plot.bar()\n",
    "\n",
    "\n",
    "tukey = pairwise_tukeyhsd(endog=campaigndf['Sales 4 weeks'],\n",
    "                          groups=campaigndf['Response'],\n",
    "                          alpha=0.05)\n",
    "\n",
    "#display results\n",
    "print(tukey)\n",
    "campaigndf.pivot_table(columns=['Response'], values='Sales 4 weeks', aggfunc=np.mean).round(2).plot.bar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "S6 = campaigndf['Sales 6 months']\n",
    "model = ols('S6 ~ C(Q(\"Gender\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales 6 months - Gender\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('S6 ~ C(Q(\"Area\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales 6 months - Area\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('S6 ~ C(Q(\"Mobile\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales 6 months - Mobile\")\n",
    "display(anova_table)\n",
    "\n",
    "model = ols('S6 ~ C(Q(\"Response\"))', data=campaigndf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales 6 months - Response\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The avg sales/6 month is siginificantly different for response. Lets do Tukey (below) and see the actual difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tukey = pairwise_tukeyhsd(endog=campaigndf['Sales 6 months'],\n",
    "                          groups=campaigndf['Response'],\n",
    "                          alpha=0.05)\n",
    "\n",
    "#display results\n",
    "print(tukey)\n",
    "campaigndf.pivot_table(columns=['Response'], values='Sales 6 months', aggfunc=np.mean).round(2).plot.bar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_crosstab = pd.crosstab(campaigndf['Response'], campaigndf['Gender'], \n",
    "margins = False) \n",
    "print(data_crosstab) \n",
    "\n",
    "stat, p, dof, expected = chi2_contingency(data_crosstab)\n",
    "print('dof=%d' % dof)\n",
    "print(expected)\n",
    "\n",
    "# interpret p-value\n",
    "alpha = 0.05\n",
    "print('significance=%.3f, p=%.3f' % (alpha, p))\n",
    "if p <= alpha:\n",
    "    print('Dependent (reject H0)')\n",
    "else:\n",
    "    print('Independent (fail to reject H0)')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_crosstab = pd.crosstab(campaigndf['Response'], campaigndf['Area'], \n",
    "margins = False) \n",
    "print(data_crosstab) \n",
    "\n",
    "stat, p, dof, expected = chi2_contingency(data_crosstab)\n",
    "print('dof=%d' % dof)\n",
    "print(expected)\n",
    "\n",
    "# interpret p-value\n",
    "alpha = 0.05\n",
    "print('significance=%.3f, p=%.3f' % (alpha, p))\n",
    "if p <= alpha:\n",
    "    print('Dependent (reject H0)')\n",
    "else:\n",
    "    print('Independent (fail to reject H0)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_crosstab = pd.crosstab(campaigndf['Response'], campaigndf['Email'], \n",
    "margins = False) \n",
    "print(data_crosstab) \n",
    "\n",
    "stat, p, dof, expected = chi2_contingency(data_crosstab)\n",
    "print('dof=%d' % dof)\n",
    "print(expected)\n",
    "\n",
    "# interpret p-value\n",
    "alpha = 0.05\n",
    "print('significance=%.3f, p=%.3f' % (alpha, p))\n",
    "if p <= alpha:\n",
    "    print('Dependent (reject H0)')\n",
    "else:\n",
    "    print('Independent (fail to reject H0)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_crosstab = pd.crosstab(campaigndf['Response'], campaigndf['Mobile'], \n",
    "margins = False) \n",
    "print(data_crosstab) \n",
    "\n",
    "stat, p, dof, expected = chi2_contingency(data_crosstab)\n",
    "print('dof=%d' % dof)\n",
    "print(expected)\n",
    "\n",
    "# interpret p-value\n",
    "alpha = 0.05\n",
    "print('significance=%.3f, p=%.3f' % (alpha, p))\n",
    "if p <= alpha:\n",
    "    print('Dependent (reject H0)')\n",
    "else:\n",
    "    print('Independent (fail to reject H0)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print (\"\\n Age\")\n",
    "pyplot.hist(campaigndf['Age'])\n",
    "pyplot.show()\n",
    "\n",
    "print (\"\\n L4\")\n",
    "pyplot.hist(campaigndf['Logins 4 weeks'])\n",
    "pyplot.show()\n",
    "\n",
    "print (\"\\n L6\")\n",
    "pyplot.hist(campaigndf['Logins 6 months'])\n",
    "pyplot.show()\n",
    "\n",
    "print (\"\\n S4\")\n",
    "pyplot.hist(campaigndf['Sales 4 weeks'])\n",
    "pyplot.show()\n",
    "\n",
    "print (\"\\n S6\")\n",
    "pyplot.hist(campaigndf['Sales 6 months'])\n",
    "pyplot.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "output = anderson(campaigndf['Sales 6 months'])\n",
    "print('Output: %.4f' % output.statistic)\n",
    "\n",
    "p = 0\n",
    "for i in range(len(output.critical_values)):\n",
    "    sl, cv = output.significance_level[i], output.critical_values[i]\n",
    "    if output.statistic < output.critical_values[i]:\n",
    "        print('%.4f: %.4f, data looks normal (cant reject H0)' % (sl, cv))\n",
    "    else:\n",
    "        print('%.4f: %.4f, data not look normal (reject H0)' % (sl, cv))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "output = anderson(campaigndf['Sales 4 weeks'])\n",
    "print('Output: %.4f' % output.statistic)\n",
    "\n",
    "p = 0\n",
    "for i in range(len(output.critical_values)):\n",
    "    sl, cv = output.significance_level[i], output.critical_values[i]\n",
    "    if output.statistic < output.critical_values[i]:\n",
    "        print('%.4f: %.4f, data looks normal (cant reject H0)' % (sl, cv))\n",
    "    else:\n",
    "        print('%.4f: %.4f, data not look normal (reject H0)' % (sl, cv))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qqplot(campaigndf['Sales 4 weeks'], line='s')\n",
    "pyplot.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qqplot(campaigndf['Sales 6 months'], line='s')\n",
    "pyplot.show()"
   ]
  }
 ],
 "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.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
