{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import the libraries\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "%matplotlib inline\n",
    "from scipy import stats\n",
    "import statsmodels.api as sm\n",
    "from statsmodels.formula.api import ols\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read Sales.Dirty.xls\n",
    "salesdf = pd.read_excel('Sales.xls')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "salesdf.dtypespes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "salesdf.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Anova Tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ols('Sales ~ C(Q(\"Order Priority\"))', data=salesdf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales - Order Priority\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ols('Sales ~ C(Q(\"Ship Mode\"))', data=salesdf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales - Ship Mode\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ols('Sales ~ C(Q(\"Region\"))', data=salesdf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales - Region\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ols('Sales ~ C(Q(\"Province\"))', data=salesdf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales - Province\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ols('Sales ~ C(Q(\"Customer Segment\"))', data=salesdf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales - Customer Segment\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ols('Sales ~ C(Q(\"Product Category\"))', data=salesdf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales - Product Category\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ols('Sales ~ C(Q(\"Product Sub-Category\"))', data=salesdf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales - Product Sub-Category\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ols('Sales ~ C(Q(\"Product Container\"))', data=salesdf).fit()\n",
    "anova_table = sm.stats.anova_lm(model, typ=2)\n",
    "print (\"\\nAnova => Sales - Product Container\")\n",
    "display(anova_table)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tukey Tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from statsmodels.stats.multicomp import pairwise_tukeyhsd\n",
    "\n",
    "tukey = pairwise_tukeyhsd(endog=salesdf['Sales'],\n",
    "                          groups=salesdf['Ship Mode'],\n",
    "                          alpha=0.05)\n",
    "\n",
    "#display results\n",
    "print(tukey)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tukey = pairwise_tukeyhsd(endog=salesdf['Sales'],\n",
    "                          groups=salesdf['Product Category'],\n",
    "                          alpha=0.05)\n",
    "\n",
    "#display results\n",
    "print(tukey)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tukey = pairwise_tukeyhsd(endog=salesdf['Sales'],\n",
    "                          groups=salesdf['Product Sub-Category'],\n",
    "                          alpha=0.05)\n",
    "\n",
    "#display results\n",
    "print(tukey)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tukey = pairwise_tukeyhsd(endog=salesdf['Sales'],\n",
    "                          groups=salesdf['Product Container'],\n",
    "                          alpha=0.05)\n",
    "\n",
    "#display results\n",
    "print(tukey)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chi-Squared Tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import chi2_contingency\n",
    "from scipy.stats import chi2\n",
    "\n",
    "data_crosstab = pd.crosstab(salesdf['Customer Segment'], salesdf['Product Category'], \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(salesdf['Product Container'], salesdf['Product Category'], \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": "markdown",
   "metadata": {},
   "source": [
    "## normality tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "salesdf.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "types_map = salesdf.dtypes.to_dict()\n",
    "num_columns = []\n",
    "for k,v in types_map.items():\n",
    "    if np.issubdtype(np.int64, v) or np.issubdtype(np.float64, v):\n",
    "        num_columns.append(k)\n",
    "\n",
    "print(num_columns)\n",
    "\n",
    "\n",
    "t_val, p_val = stats.ttest_ind(salesdf[num_columns[3]], salesdf[num_columns[5]])\n",
    "print(\"(Sales,Profit) => t-value=%s, p-value=%s\" % (str(t_val), str(p_val)))"
   ]
  }
 ],
 "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
}
