{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 42,
   "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": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read Sales.Dirty.xls\n",
    "salesdf = pd.read_excel('Sales.xls')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Row ID                           int64\n",
       "Order ID                         int64\n",
       "Order Date              datetime64[ns]\n",
       "Order Priority                  object\n",
       "Order Quantity                   int64\n",
       "Sales                          float64\n",
       "Discount                       float64\n",
       "Ship Mode                       object\n",
       "Profit                         float64\n",
       "Unit Price                     float64\n",
       "Shipping Cost                  float64\n",
       "Customer Name                   object\n",
       "Province                        object\n",
       "Region                          object\n",
       "Customer Segment                object\n",
       "Product Category                object\n",
       "Product Sub-Category            object\n",
       "Product Name                    object\n",
       "Product Container               object\n",
       "Product Base Margin            float64\n",
       "Ship Date               datetime64[ns]\n",
       "dtype: object"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "salesdf.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8399, 21)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "salesdf.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Row ID</th>\n",
       "      <th>Order ID</th>\n",
       "      <th>Order Date</th>\n",
       "      <th>Order Priority</th>\n",
       "      <th>Order Quantity</th>\n",
       "      <th>Sales</th>\n",
       "      <th>Discount</th>\n",
       "      <th>Ship Mode</th>\n",
       "      <th>Profit</th>\n",
       "      <th>Unit Price</th>\n",
       "      <th>...</th>\n",
       "      <th>Customer Name</th>\n",
       "      <th>Province</th>\n",
       "      <th>Region</th>\n",
       "      <th>Customer Segment</th>\n",
       "      <th>Product Category</th>\n",
       "      <th>Product Sub-Category</th>\n",
       "      <th>Product Name</th>\n",
       "      <th>Product Container</th>\n",
       "      <th>Product Base Margin</th>\n",
       "      <th>Ship Date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2010-10-13</td>\n",
       "      <td>Low</td>\n",
       "      <td>6</td>\n",
       "      <td>261.5400</td>\n",
       "      <td>0.04</td>\n",
       "      <td>Regular Air</td>\n",
       "      <td>-213.2500</td>\n",
       "      <td>38.94</td>\n",
       "      <td>...</td>\n",
       "      <td>Muhammed MacIntyre</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Small Business</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Storage &amp; Organization</td>\n",
       "      <td>Eldon Base for stackable storage shelf, platinum</td>\n",
       "      <td>Large Box</td>\n",
       "      <td>0.80</td>\n",
       "      <td>2010-10-20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>49</td>\n",
       "      <td>293</td>\n",
       "      <td>2012-10-01</td>\n",
       "      <td>High</td>\n",
       "      <td>49</td>\n",
       "      <td>10123.0200</td>\n",
       "      <td>0.07</td>\n",
       "      <td>Delivery Truck</td>\n",
       "      <td>457.8100</td>\n",
       "      <td>208.16</td>\n",
       "      <td>...</td>\n",
       "      <td>Barry French</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Appliances</td>\n",
       "      <td>1.7 Cubic Foot Compact \"Cube\" Office Refrigera...</td>\n",
       "      <td>Jumbo Drum</td>\n",
       "      <td>0.58</td>\n",
       "      <td>2012-10-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>50</td>\n",
       "      <td>293</td>\n",
       "      <td>2012-10-01</td>\n",
       "      <td>High</td>\n",
       "      <td>27</td>\n",
       "      <td>244.5700</td>\n",
       "      <td>0.01</td>\n",
       "      <td>Regular Air</td>\n",
       "      <td>46.7075</td>\n",
       "      <td>8.69</td>\n",
       "      <td>...</td>\n",
       "      <td>Barry French</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Binders and Binder Accessories</td>\n",
       "      <td>Cardinal Slant-D® Ring Binder, Heavy Gauge Vinyl</td>\n",
       "      <td>Small Box</td>\n",
       "      <td>0.39</td>\n",
       "      <td>2012-10-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>80</td>\n",
       "      <td>483</td>\n",
       "      <td>2011-07-10</td>\n",
       "      <td>High</td>\n",
       "      <td>30</td>\n",
       "      <td>4965.7595</td>\n",
       "      <td>0.08</td>\n",
       "      <td>Regular Air</td>\n",
       "      <td>1198.9710</td>\n",
       "      <td>195.99</td>\n",
       "      <td>...</td>\n",
       "      <td>Clay Rozendal</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>Technology</td>\n",
       "      <td>Telephones and Communication</td>\n",
       "      <td>R380</td>\n",
       "      <td>Small Box</td>\n",
       "      <td>0.58</td>\n",
       "      <td>2011-07-12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>85</td>\n",
       "      <td>515</td>\n",
       "      <td>2010-08-28</td>\n",
       "      <td>Not Specified</td>\n",
       "      <td>19</td>\n",
       "      <td>394.2700</td>\n",
       "      <td>0.08</td>\n",
       "      <td>Regular Air</td>\n",
       "      <td>30.9400</td>\n",
       "      <td>21.78</td>\n",
       "      <td>...</td>\n",
       "      <td>Carlos Soltero</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Appliances</td>\n",
       "      <td>Holmes HEPA Air Purifier</td>\n",
       "      <td>Medium Box</td>\n",
       "      <td>0.50</td>\n",
       "      <td>2010-08-30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>86</td>\n",
       "      <td>515</td>\n",
       "      <td>2010-08-28</td>\n",
       "      <td>Not Specified</td>\n",
       "      <td>21</td>\n",
       "      <td>146.6900</td>\n",
       "      <td>0.05</td>\n",
       "      <td>Regular Air</td>\n",
       "      <td>4.4300</td>\n",
       "      <td>6.64</td>\n",
       "      <td>...</td>\n",
       "      <td>Carlos Soltero</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Consumer</td>\n",
       "      <td>Furniture</td>\n",
       "      <td>Office Furnishings</td>\n",
       "      <td>G.E. Longer-Life Indoor Recessed Floodlight Bulbs</td>\n",
       "      <td>Small Pack</td>\n",
       "      <td>0.37</td>\n",
       "      <td>2010-08-30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>97</td>\n",
       "      <td>613</td>\n",
       "      <td>2011-06-17</td>\n",
       "      <td>High</td>\n",
       "      <td>12</td>\n",
       "      <td>93.5400</td>\n",
       "      <td>0.03</td>\n",
       "      <td>Regular Air</td>\n",
       "      <td>-54.0385</td>\n",
       "      <td>7.30</td>\n",
       "      <td>...</td>\n",
       "      <td>Carl Jackson</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Binders and Binder Accessories</td>\n",
       "      <td>Angle-D Binders with Locking Rings, Label Holders</td>\n",
       "      <td>Small Box</td>\n",
       "      <td>0.38</td>\n",
       "      <td>2011-06-17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>98</td>\n",
       "      <td>613</td>\n",
       "      <td>2011-06-17</td>\n",
       "      <td>High</td>\n",
       "      <td>22</td>\n",
       "      <td>905.0800</td>\n",
       "      <td>0.09</td>\n",
       "      <td>Regular Air</td>\n",
       "      <td>127.7000</td>\n",
       "      <td>42.76</td>\n",
       "      <td>...</td>\n",
       "      <td>Carl Jackson</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Storage &amp; Organization</td>\n",
       "      <td>SAFCO Mobile Desk Side File, Wire Frame</td>\n",
       "      <td>Small Box</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2011-06-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>103</td>\n",
       "      <td>643</td>\n",
       "      <td>2011-03-24</td>\n",
       "      <td>High</td>\n",
       "      <td>21</td>\n",
       "      <td>2781.8200</td>\n",
       "      <td>0.07</td>\n",
       "      <td>Express Air</td>\n",
       "      <td>-695.2600</td>\n",
       "      <td>138.14</td>\n",
       "      <td>...</td>\n",
       "      <td>Monica Federle</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Corporate</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Storage &amp; Organization</td>\n",
       "      <td>SAFCO Commercial Wire Shelving, Black</td>\n",
       "      <td>Large Box</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2011-03-25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>107</td>\n",
       "      <td>678</td>\n",
       "      <td>2010-02-26</td>\n",
       "      <td>Low</td>\n",
       "      <td>44</td>\n",
       "      <td>228.4100</td>\n",
       "      <td>0.07</td>\n",
       "      <td>Regular Air</td>\n",
       "      <td>-226.3600</td>\n",
       "      <td>4.98</td>\n",
       "      <td>...</td>\n",
       "      <td>Dorothy Badders</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Nunavut</td>\n",
       "      <td>Home Office</td>\n",
       "      <td>Office Supplies</td>\n",
       "      <td>Paper</td>\n",
       "      <td>Xerox 198</td>\n",
       "      <td>Small Box</td>\n",
       "      <td>0.38</td>\n",
       "      <td>2010-02-26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Row ID  Order ID Order Date Order Priority  Order Quantity       Sales  \\\n",
       "0       1         3 2010-10-13            Low               6    261.5400   \n",
       "1      49       293 2012-10-01           High              49  10123.0200   \n",
       "2      50       293 2012-10-01           High              27    244.5700   \n",
       "3      80       483 2011-07-10           High              30   4965.7595   \n",
       "4      85       515 2010-08-28  Not Specified              19    394.2700   \n",
       "5      86       515 2010-08-28  Not Specified              21    146.6900   \n",
       "6      97       613 2011-06-17           High              12     93.5400   \n",
       "7      98       613 2011-06-17           High              22    905.0800   \n",
       "8     103       643 2011-03-24           High              21   2781.8200   \n",
       "9     107       678 2010-02-26            Low              44    228.4100   \n",
       "\n",
       "   Discount       Ship Mode     Profit  Unit Price  ...       Customer Name  \\\n",
       "0      0.04     Regular Air  -213.2500       38.94  ...  Muhammed MacIntyre   \n",
       "1      0.07  Delivery Truck   457.8100      208.16  ...        Barry French   \n",
       "2      0.01     Regular Air    46.7075        8.69  ...        Barry French   \n",
       "3      0.08     Regular Air  1198.9710      195.99  ...       Clay Rozendal   \n",
       "4      0.08     Regular Air    30.9400       21.78  ...      Carlos Soltero   \n",
       "5      0.05     Regular Air     4.4300        6.64  ...      Carlos Soltero   \n",
       "6      0.03     Regular Air   -54.0385        7.30  ...        Carl Jackson   \n",
       "7      0.09     Regular Air   127.7000       42.76  ...        Carl Jackson   \n",
       "8      0.07     Express Air  -695.2600      138.14  ...      Monica Federle   \n",
       "9      0.07     Regular Air  -226.3600        4.98  ...     Dorothy Badders   \n",
       "\n",
       "  Province   Region Customer Segment Product Category  \\\n",
       "0  Nunavut  Nunavut   Small Business  Office Supplies   \n",
       "1  Nunavut  Nunavut         Consumer  Office Supplies   \n",
       "2  Nunavut  Nunavut         Consumer  Office Supplies   \n",
       "3  Nunavut  Nunavut        Corporate       Technology   \n",
       "4  Nunavut  Nunavut         Consumer  Office Supplies   \n",
       "5  Nunavut  Nunavut         Consumer        Furniture   \n",
       "6  Nunavut  Nunavut        Corporate  Office Supplies   \n",
       "7  Nunavut  Nunavut        Corporate  Office Supplies   \n",
       "8  Nunavut  Nunavut        Corporate  Office Supplies   \n",
       "9  Nunavut  Nunavut      Home Office  Office Supplies   \n",
       "\n",
       "             Product Sub-Category  \\\n",
       "0          Storage & Organization   \n",
       "1                      Appliances   \n",
       "2  Binders and Binder Accessories   \n",
       "3    Telephones and Communication   \n",
       "4                      Appliances   \n",
       "5              Office Furnishings   \n",
       "6  Binders and Binder Accessories   \n",
       "7          Storage & Organization   \n",
       "8          Storage & Organization   \n",
       "9                           Paper   \n",
       "\n",
       "                                        Product Name Product Container  \\\n",
       "0   Eldon Base for stackable storage shelf, platinum         Large Box   \n",
       "1  1.7 Cubic Foot Compact \"Cube\" Office Refrigera...        Jumbo Drum   \n",
       "2   Cardinal Slant-D® Ring Binder, Heavy Gauge Vinyl         Small Box   \n",
       "3                                               R380         Small Box   \n",
       "4                           Holmes HEPA Air Purifier        Medium Box   \n",
       "5  G.E. Longer-Life Indoor Recessed Floodlight Bulbs        Small Pack   \n",
       "6  Angle-D Binders with Locking Rings, Label Holders         Small Box   \n",
       "7            SAFCO Mobile Desk Side File, Wire Frame         Small Box   \n",
       "8              SAFCO Commercial Wire Shelving, Black         Large Box   \n",
       "9                                          Xerox 198         Small Box   \n",
       "\n",
       "  Product Base Margin  Ship Date  \n",
       "0                0.80 2010-10-20  \n",
       "1                0.58 2012-10-02  \n",
       "2                0.39 2012-10-03  \n",
       "3                0.58 2011-07-12  \n",
       "4                0.50 2010-08-30  \n",
       "5                0.37 2010-08-30  \n",
       "6                0.38 2011-06-17  \n",
       "7                 NaN 2011-06-18  \n",
       "8                 NaN 2011-03-25  \n",
       "9                0.38 2010-02-26  \n",
       "\n",
       "[10 rows x 21 columns]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "salesdf.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Anova Tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Anova => Sales - Order Priority\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>df</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(Q(\"Order Priority\"))</th>\n",
       "      <td>7.261269e+07</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.412692</td>\n",
       "      <td>0.226894</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>1.078634e+11</td>\n",
       "      <td>8394.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              sum_sq      df         F    PR(>F)\n",
       "C(Q(\"Order Priority\"))  7.261269e+07     4.0  1.412692  0.226894\n",
       "Residual                1.078634e+11  8394.0       NaN       NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "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": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Anova => Sales - Ship Mode\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>df</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(Q(\"Ship Mode\"))</th>\n",
       "      <td>1.773769e+10</td>\n",
       "      <td>2.0</td>\n",
       "      <td>825.545281</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>9.019834e+10</td>\n",
       "      <td>8396.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         sum_sq      df           F  PR(>F)\n",
       "C(Q(\"Ship Mode\"))  1.773769e+10     2.0  825.545281     0.0\n",
       "Residual           9.019834e+10  8396.0         NaN     NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "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": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Anova => Sales - Region\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>df</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(Q(\"Region\"))</th>\n",
       "      <td>1.028260e+08</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.143053</td>\n",
       "      <td>0.332599</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>1.078332e+11</td>\n",
       "      <td>8391.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      sum_sq      df         F    PR(>F)\n",
       "C(Q(\"Region\"))  1.028260e+08     7.0  1.143053  0.332599\n",
       "Residual        1.078332e+11  8391.0       NaN       NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "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": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Anova => Sales - Province\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>df</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(Q(\"Province\"))</th>\n",
       "      <td>2.081083e+08</td>\n",
       "      <td>12.0</td>\n",
       "      <td>1.350003</td>\n",
       "      <td>0.182509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>1.077279e+11</td>\n",
       "      <td>8386.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        sum_sq      df         F    PR(>F)\n",
       "C(Q(\"Province\"))  2.081083e+08    12.0  1.350003  0.182509\n",
       "Residual          1.077279e+11  8386.0       NaN       NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "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": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Anova => Sales - Customer Segment\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>df</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(Q(\"Customer Segment\"))</th>\n",
       "      <td>2.238326e+07</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.580425</td>\n",
       "      <td>0.627813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>1.079136e+11</td>\n",
       "      <td>8395.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                sum_sq      df         F    PR(>F)\n",
       "C(Q(\"Customer Segment\"))  2.238326e+07     3.0  0.580425  0.627813\n",
       "Residual                  1.079136e+11  8395.0       NaN       NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "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": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Anova => Sales - Product Category\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>df</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(Q(\"Product Category\"))</th>\n",
       "      <td>9.464206e+09</td>\n",
       "      <td>2.0</td>\n",
       "      <td>403.473146</td>\n",
       "      <td>4.908931e-168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>9.847182e+10</td>\n",
       "      <td>8396.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                sum_sq      df           F         PR(>F)\n",
       "C(Q(\"Product Category\"))  9.464206e+09     2.0  403.473146  4.908931e-168\n",
       "Residual                  9.847182e+10  8396.0         NaN            NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "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": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Anova => Sales - Product Sub-Category\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>df</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(Q(\"Product Sub-Category\"))</th>\n",
       "      <td>3.410615e+10</td>\n",
       "      <td>16.0</td>\n",
       "      <td>242.00718</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>7.382988e+10</td>\n",
       "      <td>8382.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                    sum_sq      df          F  PR(>F)\n",
       "C(Q(\"Product Sub-Category\"))  3.410615e+10    16.0  242.00718     0.0\n",
       "Residual                      7.382988e+10  8382.0        NaN     NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "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": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Anova => Sales - Product Container\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>df</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(Q(\"Product Container\"))</th>\n",
       "      <td>2.530821e+10</td>\n",
       "      <td>6.0</td>\n",
       "      <td>428.399995</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>8.262781e+10</td>\n",
       "      <td>8392.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 sum_sq      df           F  PR(>F)\n",
       "C(Q(\"Product Container\"))  2.530821e+10     6.0  428.399995     0.0\n",
       "Residual                   8.262781e+10  8392.0         NaN     NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "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": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          Multiple Comparison of Means - Tukey HSD, FWER=0.05           \n",
      "========================================================================\n",
      "    group1        group2    meandiff  p-adj   lower      upper    reject\n",
      "------------------------------------------------------------------------\n",
      "Delivery Truck Express Air -4226.9291 0.001 -4560.9416 -3892.9166   True\n",
      "Delivery Truck Regular Air -4234.6538 0.001 -4481.4865 -3987.8211   True\n",
      "   Express Air Regular Air    -7.7247   0.9  -271.2925   255.8431  False\n",
      "------------------------------------------------------------------------\n"
     ]
    }
   ],
   "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": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Multiple Comparison of Means - Tukey HSD, FWER=0.05              \n",
      "==============================================================================\n",
      "     group1          group2      meandiff  p-adj    lower      upper    reject\n",
      "------------------------------------------------------------------------------\n",
      "      Furniture Office Supplies -2189.7746  0.001 -2416.4063 -1963.1429   True\n",
      "      Furniture      Technology  -105.8818 0.6014  -367.7811   156.0175  False\n",
      "Office Supplies      Technology  2083.8928  0.001   1871.316  2296.4696   True\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "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": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                              Multiple Comparison of Means - Tukey HSD, FWER=0.05                              \n",
      "===============================================================================================================\n",
      "            group1                         group2               meandiff  p-adj     lower       upper    reject\n",
      "---------------------------------------------------------------------------------------------------------------\n",
      "                    Appliances Binders and Binder Accessories   -580.1508 0.0701   -1178.601     18.2995  False\n",
      "                    Appliances                      Bookcases   2654.5191  0.001   1759.6783   3549.3599   True\n",
      "                    Appliances             Chairs & Chairmats   2866.2062  0.001     2147.84   3584.5725   True\n",
      "                    Appliances           Computer Peripherals   -648.1689 0.0286  -1266.2368    -30.1011   True\n",
      "                    Appliances                Copiers and Fax  11294.5214  0.001  10088.3976  12500.6452   True\n",
      "                    Appliances                      Envelopes   -990.4713 0.0034  -1809.9158   -171.0268   True\n",
      "                    Appliances                         Labels  -1562.7846  0.001  -2343.1626   -782.4066   True\n",
      "                    Appliances             Office Furnishings   -812.2313  0.001  -1426.0006   -198.4621   True\n",
      "                    Appliances                Office Machines   4737.1659  0.001   3991.6706   5482.6612   True\n",
      "                    Appliances                          Paper  -1333.6859  0.001  -1907.2577   -760.1141   True\n",
      "                    Appliances            Pens & Art Supplies  -1434.1447  0.001  -2074.0461   -794.2434   True\n",
      "                    Appliances                   Rubber Bands  -1614.3013  0.001  -2526.3885    -702.214   True\n",
      "                    Appliances  Scissors, Rulers and Trimmers  -1135.6628 0.0078  -2123.1133   -148.2123   True\n",
      "                    Appliances         Storage & Organization    261.9042    0.9   -398.4081    922.2165  False\n",
      "                    Appliances                         Tables   3553.9629  0.001     2822.55   4285.3758   True\n",
      "                    Appliances   Telephones and Communication     441.516 0.4763   -160.4128   1043.4449  False\n",
      "Binders and Binder Accessories                      Bookcases   3234.6699  0.001   2414.2794   4055.0603   True\n",
      "Binders and Binder Accessories             Chairs & Chairmats    3446.357  0.001   2823.1792   4069.5347   True\n",
      "Binders and Binder Accessories           Computer Peripherals    -68.0182    0.9   -572.3082    436.2718  False\n",
      "Binders and Binder Accessories                Copiers and Fax  11874.6722  0.001  10722.7021  13026.6423   True\n",
      "Binders and Binder Accessories                      Envelopes   -410.3206 0.8759  -1147.7416    327.1005  False\n",
      "Binders and Binder Accessories                         Labels   -982.6338  0.001  -1676.3847    -288.883   True\n",
      "Binders and Binder Accessories             Office Furnishings   -232.0806    0.9   -731.0928    266.9316  False\n",
      "Binders and Binder Accessories                Office Machines   5317.3166  0.001   4663.0509   5971.5824   True\n",
      "Binders and Binder Accessories                          Paper   -753.5351  0.001  -1202.1832    -304.887   True\n",
      "Binders and Binder Accessories            Pens & Art Supplies    -853.994  0.001   -1384.818   -323.1699   True\n",
      "Binders and Binder Accessories                   Rubber Bands  -1034.1505 0.0024  -1873.3189   -194.9821   True\n",
      "Binders and Binder Accessories  Scissors, Rulers and Trimmers   -555.5121 0.7699  -1476.0331    365.0089  False\n",
      "Binders and Binder Accessories         Storage & Organization     842.055  0.001    286.7958   1397.3141   True\n",
      "Binders and Binder Accessories                         Tables   4134.1137  0.001   3495.9403   4772.2871   True\n",
      "Binders and Binder Accessories   Telephones and Communication   1021.6668  0.001    537.2921   1506.0415   True\n",
      "                     Bookcases             Chairs & Chairmats    211.6871    0.9   -699.8763   1123.2505  False\n",
      "                     Bookcases           Computer Peripherals   -3302.688  0.001  -4137.4968  -2467.8793   True\n",
      "                     Bookcases                Copiers and Fax   8640.0023  0.001    7309.727   9970.2776   True\n",
      "                     Bookcases                      Envelopes  -3644.9904  0.001  -4638.1607  -2651.8202   True\n",
      "                     Bookcases                         Labels  -4217.3037  0.001  -5178.4944   -3256.113   True\n",
      "                     Bookcases             Office Furnishings  -3466.7504  0.001  -4298.3817  -2635.1192   True\n",
      "                     Bookcases                Office Machines   2082.6468  0.001   1149.5546   3015.7389   True\n",
      "                     Bookcases                          Paper   -3988.205  0.001  -4790.6278  -3185.7822   True\n",
      "                     Bookcases            Pens & Art Supplies  -4088.6638  0.001   -4939.764  -3237.5637   True\n",
      "                     Bookcases                   Rubber Bands  -4268.8204  0.001  -5339.7083  -3197.9325   True\n",
      "                     Bookcases  Scissors, Rulers and Trimmers  -3790.1819  0.001  -4925.9443  -2654.4195   True\n",
      "                     Bookcases         Storage & Organization  -2392.6149  0.001  -3259.1656  -1526.0642   True\n",
      "                     Bookcases                         Tables    899.4438 0.0655    -22.4361   1821.3237  False\n",
      "                     Bookcases   Telephones and Communication  -2213.0031  0.001  -3035.9345  -1390.0716   True\n",
      "            Chairs & Chairmats           Computer Peripherals  -3514.3751  0.001  -4156.4154  -2872.3349   True\n",
      "            Chairs & Chairmats                Copiers and Fax   8428.3152  0.001   7209.7331   9646.8974   True\n",
      "            Chairs & Chairmats                      Envelopes  -3856.6775  0.001  -4694.3512  -3019.0039   True\n",
      "            Chairs & Chairmats                         Labels  -4428.9908  0.001  -5228.4891  -3629.4925   True\n",
      "            Chairs & Chairmats             Office Furnishings  -3678.4375  0.001  -4316.3408  -3040.5343   True\n",
      "            Chairs & Chairmats                Office Machines   1870.9597  0.001   1105.4723   2636.4471   True\n",
      "            Chairs & Chairmats                          Paper  -4199.8921  0.001  -4799.2187  -3600.5654   True\n",
      "            Chairs & Chairmats            Pens & Art Supplies  -4300.3509  0.001  -4963.4358  -3637.2661   True\n",
      "            Chairs & Chairmats                   Rubber Bands  -4480.5075  0.001  -5409.0067  -3552.0082   True\n",
      "            Chairs & Chairmats  Scissors, Rulers and Trimmers   -4001.869  0.001  -5004.4987  -2999.2394   True\n",
      "            Chairs & Chairmats         Storage & Organization   -2604.302  0.001  -3287.1051  -1921.4989   True\n",
      "            Chairs & Chairmats                         Tables    687.7567 0.1209    -64.0229   1439.5364  False\n",
      "            Chairs & Chairmats   Telephones and Communication  -2424.6902  0.001  -3051.2093  -1798.1711   True\n",
      "          Computer Peripherals                Copiers and Fax  11942.6904  0.001   10780.408  13104.9727   True\n",
      "          Computer Peripherals                      Envelopes   -342.3024    0.9  -1095.7312    411.1264  False\n",
      "          Computer Peripherals                         Labels   -914.6157 0.0011  -1625.3585   -203.8728   True\n",
      "          Computer Peripherals             Office Furnishings   -164.0624    0.9     -686.44    358.3152  False\n",
      "          Computer Peripherals                Office Machines   5385.3348  0.001   4713.0783   6057.5913   True\n",
      "          Computer Peripherals                          Paper   -685.5169  0.001   -1160.017   -211.0169   True\n",
      "          Computer Peripherals            Pens & Art Supplies   -785.9758  0.001  -1338.8224   -233.1292   True\n",
      "          Computer Peripherals                   Rubber Bands   -966.1323 0.0099  -1819.4018   -112.8629   True\n",
      "          Computer Peripherals  Scissors, Rulers and Trimmers   -487.4939    0.9  -1420.8877       445.9  False\n",
      "          Computer Peripherals         Storage & Organization    910.0731  0.001    333.7243   1486.4219   True\n",
      "          Computer Peripherals                         Tables   4202.1319  0.001   3545.5266   4858.7371   True\n",
      "          Computer Peripherals   Telephones and Communication    1089.685  0.001    581.2717   1598.0982   True\n",
      "               Copiers and Fax                      Envelopes -12284.9928  0.001 -13565.7666 -11004.2189   True\n",
      "               Copiers and Fax                         Labels  -12857.306  0.001 -14113.4438 -11601.1682   True\n",
      "               Copiers and Fax             Office Furnishings -12106.7528  0.001 -13266.7549 -10946.7506   True\n",
      "               Copiers and Fax                Office Machines  -6557.3555  0.001   -7792.125  -5322.5861   True\n",
      "               Copiers and Fax                          Paper -12628.2073  0.001 -13767.4513 -11488.9633   True\n",
      "               Copiers and Fax            Pens & Art Supplies -12728.6662  0.001 -13902.7045 -11554.6278   True\n",
      "               Copiers and Fax                   Rubber Bands -12908.8227  0.001 -14250.7599 -11566.8855   True\n",
      "               Copiers and Fax  Scissors, Rulers and Trimmers -12430.1842  0.001 -13824.4406 -11035.9279   True\n",
      "               Copiers and Fax         Storage & Organization -11032.6172  0.001  -12217.904  -9847.3305   True\n",
      "               Copiers and Fax                         Tables  -7740.5585  0.001   -8966.877    -6514.24   True\n",
      "               Copiers and Fax   Telephones and Communication -10853.0054  0.001 -12006.7864  -9699.2243   True\n",
      "                     Envelopes                         Labels   -572.3132 0.6831  -1463.7373    319.1108  False\n",
      "                     Envelopes             Office Furnishings      178.24    0.9   -571.6665    928.1465  False\n",
      "                     Envelopes                Office Machines   5727.6372  0.001   4866.5854    6588.689   True\n",
      "                     Envelopes                          Paper   -343.2145    0.9  -1060.5929    374.1638  False\n",
      "                     Envelopes            Pens & Art Supplies   -443.6734 0.8338  -1215.1141    327.7673  False\n",
      "                     Envelopes                   Rubber Bands   -623.8299 0.7363  -1632.5668     384.907  False\n",
      "                     Envelopes  Scissors, Rulers and Trimmers   -145.1915    0.9  -1222.5519    932.1689  False\n",
      "                     Envelopes         Storage & Organization   1252.3755  0.001    463.9218   2040.8293   True\n",
      "                     Envelopes                         Tables   4544.4343  0.001   3695.5457   5393.3229   True\n",
      "                     Envelopes   Telephones and Communication   1431.9874  0.001    691.7405   2172.2342   True\n",
      "                        Labels             Office Furnishings    750.5532 0.0244     43.5453   1457.5612   True\n",
      "                        Labels                Office Machines   6299.9505  0.001   5475.9901   7123.9109   True\n",
      "                        Labels                          Paper    229.0987    0.9    -443.309    901.5064  False\n",
      "                        Labels            Pens & Art Supplies    128.6398    0.9   -601.1692    858.4489  False\n",
      "                        Labels                   Rubber Bands    -51.5167    0.9  -1028.7836    925.7502  False\n",
      "                        Labels  Scissors, Rulers and Trimmers    427.1218    0.9   -620.8314    1475.075  False\n",
      "                        Labels         Storage & Organization   1824.6888  0.001   1076.9189   2572.4587   True\n",
      "                        Labels                         Tables   5116.7475  0.001   4305.5063   5927.9887   True\n",
      "                        Labels   Telephones and Communication   2004.3006  0.001   1307.5468   2701.0545   True\n",
      "            Office Furnishings                Office Machines   5549.3972  0.001   4881.0907   6217.7038   True\n",
      "            Office Furnishings                          Paper   -521.4545  0.013   -990.3416    -52.5675   True\n",
      "            Office Furnishings            Pens & Art Supplies   -621.9134 0.0096    -1169.95    -73.8768   True\n",
      "            Office Furnishings                   Rubber Bands   -802.0699 0.0905  -1652.2308      48.091  False\n",
      "            Office Furnishings  Scissors, Rulers and Trimmers   -323.4315    0.9  -1253.9845    607.1215  False\n",
      "            Office Furnishings         Storage & Organization   1074.1355  0.001    502.3989   1645.8721   True\n",
      "            Office Furnishings                         Tables   4366.1943  0.001   3713.6337   5018.7548   True\n",
      "            Office Furnishings   Telephones and Communication   1253.7474  0.001    750.5686   1756.9261   True\n",
      "               Office Machines                          Paper  -6070.8518  0.001  -6702.4415  -5439.2621   True\n",
      "               Office Machines            Pens & Art Supplies  -6171.3106  0.001  -6863.6939  -5478.9274   True\n",
      "               Office Machines                   Rubber Bands  -6351.4672  0.001  -7301.1113   -5401.823   True\n",
      "               Office Machines  Scissors, Rulers and Trimmers  -5872.8287  0.001   -6895.071  -4850.5864   True\n",
      "               Office Machines         Storage & Organization  -4475.2617  0.001  -5186.5515  -3763.9719   True\n",
      "               Office Machines                         Tables   -1183.203  0.001   -1960.947    -405.459   True\n",
      "               Office Machines   Telephones and Communication  -4295.6499  0.001  -4953.0989  -3638.2008   True\n",
      "                         Paper            Pens & Art Supplies   -100.4589    0.9   -603.0681    402.1504  False\n",
      "                         Paper                   Rubber Bands   -280.6154    0.9  -1102.2269    540.9961  False\n",
      "                         Paper  Scissors, Rulers and Trimmers    198.0231    0.9   -706.5215   1102.5676  False\n",
      "                         Paper         Storage & Organization   1595.5901  0.001   1067.2392   2123.9409   True\n",
      "                         Paper                         Tables   4887.6488  0.001   4272.7446    5502.553   True\n",
      "                         Paper   Telephones and Communication   1775.2019  0.001   1321.9241   2228.4797   True\n",
      "           Pens & Art Supplies                   Rubber Bands   -180.1565    0.9  -1049.3714    689.0584  False\n",
      "           Pens & Art Supplies  Scissors, Rulers and Trimmers    298.4819    0.9   -649.5106   1246.4745  False\n",
      "           Pens & Art Supplies         Storage & Organization   1696.0489  0.001   1096.3459    2295.752   True\n",
      "           Pens & Art Supplies                         Tables   4988.1077  0.001   4310.9103    5665.305   True\n",
      "           Pens & Art Supplies   Telephones and Communication   1875.6608  0.001    1340.918   2410.4035   True\n",
      "                  Rubber Bands  Scissors, Rulers and Trimmers    478.6385    0.9   -670.7611    1628.038  False\n",
      "                  Rubber Bands         Storage & Organization   1876.2055  0.001    991.8565   2760.5544   True\n",
      "                  Rubber Bands                         Tables   5168.2642  0.001   4229.6345   6106.8939   True\n",
      "                  Rubber Bands   Telephones and Communication   2055.8173  0.001   1214.1646     2897.47   True\n",
      " Scissors, Rulers and Trimmers         Storage & Organization    1397.567  0.001    435.6791    2359.455   True\n",
      " Scissors, Rulers and Trimmers                         Tables   4689.6257  0.001   3677.6075    5701.644   True\n",
      " Scissors, Rulers and Trimmers   Telephones and Communication   1577.1789  0.001    654.3926   2499.9652   True\n",
      "        Storage & Organization                         Tables   3292.0587  0.001   2595.5425   3988.5749   True\n",
      "        Storage & Organization   Telephones and Communication    179.6118    0.9   -379.3948    738.6185  False\n",
      "                        Tables   Telephones and Communication  -3112.4469  0.001  -3753.8835  -2471.0103   True\n",
      "---------------------------------------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "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": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        Multiple Comparison of Means - Tukey HSD, FWER=0.05         \n",
      "====================================================================\n",
      "  group1     group2    meandiff  p-adj    lower      upper    reject\n",
      "--------------------------------------------------------------------\n",
      " Jumbo Box Jumbo Drum    155.259    0.9   -390.796   701.3141  False\n",
      " Jumbo Box  Large Box  -197.4478    0.9  -807.2489   412.3534  False\n",
      " Jumbo Box Medium Box -3404.6195  0.001 -4033.0358 -2776.2031   True\n",
      " Jumbo Box  Small Box -4132.5012  0.001  -4557.532 -3707.4704   True\n",
      " Jumbo Box Small Pack -4731.2225  0.001 -5231.7436 -4230.7015   True\n",
      " Jumbo Box   Wrap Bag -5059.4642  0.001 -5543.4726 -4575.4558   True\n",
      "Jumbo Drum  Large Box  -352.7068 0.5629  -942.7299   237.3162  False\n",
      "Jumbo Drum Medium Box -3559.8785  0.001 -4169.1213 -2950.6357   True\n",
      "Jumbo Drum  Small Box -4287.7603  0.001 -4683.8927 -3891.6278   True\n",
      "Jumbo Drum Small Pack -4886.4816  0.001 -5362.7074 -4410.2557   True\n",
      "Jumbo Drum   Wrap Bag -5214.7232  0.001 -5673.5629 -4755.8836   True\n",
      " Large Box Medium Box -3207.1717  0.001 -3874.1485 -2540.1949   True\n",
      " Large Box  Small Box -3935.0534  0.001  -4415.264 -3454.8428   True\n",
      " Large Box Small Pack -4533.7747  0.001 -5081.9283 -3985.6212   True\n",
      " Large Box   Wrap Bag -4862.0164  0.001 -5395.1347 -4328.8981   True\n",
      "Medium Box  Small Box  -727.8817  0.001 -1231.5203  -224.2431   True\n",
      "Medium Box Small Pack  -1326.603  0.001  -1895.393  -757.8131   True\n",
      "Medium Box   Wrap Bag -1654.8447  0.001 -2209.1594   -1100.53   True\n",
      " Small Box Small Pack  -598.7213  0.001  -929.2755  -268.1671   True\n",
      " Small Box   Wrap Bag   -926.963  0.001 -1231.9365  -621.9894   True\n",
      "Small Pack   Wrap Bag  -328.2416  0.199  -731.8243    75.3411  False\n",
      "--------------------------------------------------------------------\n"
     ]
    }
   ],
   "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": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Product Category  Furniture  Office Supplies  Technology\n",
      "Customer Segment                                        \n",
      "Consumer                358              865         426\n",
      "Corporate               633             1680         763\n",
      "Home Office             406             1145         481\n",
      "Small Business          327              920         395\n"
     ]
    }
   ],
   "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dof=6\n",
      "[[ 338.47791404  905.09465413  405.42743184]\n",
      " [ 631.38754614 1688.33908799  756.27336588]\n",
      " [ 417.09346351 1115.31372782  499.59280867]\n",
      " [ 337.04107632  901.25253006  403.70639362]]\n",
      "significance=0.050, p=0.349\n",
      "Independent (fail to reject H0)\n"
     ]
    }
   ],
   "source": [
    "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": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Product Category   Furniture  Office Supplies  Technology\n",
      "Product Container                                        \n",
      "Jumbo Box                488                0          44\n",
      "Jumbo Drum               374               75         175\n",
      "Large Box                164              160          82\n",
      "Medium Box               119               89         158\n",
      "Small Box                249             3059        1039\n",
      "Small Pack               210              252         494\n",
      "Wrap Bag                 120              975          73\n",
      "dof=12\n",
      "[[ 109.19966663  292.00142874  130.79890463]\n",
      " [ 128.0838195   342.49791642  153.41826408]\n",
      " [  83.33658769  222.84319562   99.82021669]\n",
      " [  75.12608644  200.88820098   89.98571258]\n",
      " [ 892.27622336 2385.95904274 1068.7647339 ]\n",
      " [ 196.23097988  524.72437195  235.04464817]\n",
      " [ 239.7466365   641.08584355  287.16751994]]\n",
      "significance=0.050, p=0.000\n",
      "Dependent (reject H0)\n"
     ]
    }
   ],
   "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": [
    "## t-tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "salesdf.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Row ID', 'Order ID', 'Order Quantity', 'Sales', 'Discount', 'Profit', 'Unit Price', 'Shipping Cost', 'Product Base Margin']\n"
     ]
    }
   ],
   "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Sales,Profit) => t-value=nan, p-value=nan\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\tmahmood\\Anaconda3\\lib\\site-packages\\scipy\\stats\\_distn_infrastructure.py:1932: RuntimeWarning: invalid value encountered in less_equal\n",
      "  cond2 = cond0 & (x <= _a)\n"
     ]
    }
   ],
   "source": [
    "t_val, p_val = stats.ttest_ind(salesdf[num_columns[3]], salesdf[num_columns[8]])\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
}
