{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# T Test\n",
    "\n",
    "An illustratio of how a T Test can be performed and summarized with statsample.\n",
    "\n",
    "#### References\n",
    "\n",
    "http://en.wikipedia.org/wiki/Student%27s_t-test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "if(window['d3'] === undefined ||\n",
       "   window['Nyaplot'] === undefined){\n",
       "    var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n",
       "\n",
       "\n",
       "\n",
       "    var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n",
       "\n",
       "    require.config({paths: path, shim:shim});\n",
       "\n",
       "\n",
       "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n",
       "\n",
       "\tvar script = d3.select(\"head\")\n",
       "\t    .append(\"script\")\n",
       "\t    .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n",
       "\t    .attr(\"async\", true);\n",
       "\n",
       "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n",
       "\n",
       "\n",
       "\t    var event = document.createEvent(\"HTMLEvents\");\n",
       "\t    event.initEvent(\"load_nyaplot\",false,false);\n",
       "\t    window.dispatchEvent(event);\n",
       "\t    console.log('Finished loading Nyaplotjs');\n",
       "\n",
       "\t};\n",
       "\n",
       "\n",
       "});});\n",
       "}\n"
      ],
      "text/plain": [
       "\"if(window['d3'] === undefined ||\\n   window['Nyaplot'] === undefined){\\n    var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n    var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n    require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t    .append(\\\"script\\\")\\n\\t    .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t    .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t    var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t    event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t    window.dispatchEvent(event);\\n\\t    console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Analysis 2016-03-26 10:23:17 +0000\n",
      "= Statsample::Test::T\n",
      "  == One Sample T Test\n",
      "    Sample mean: -0.4698 | Sample sd: 1.1807 | se : 0.3734\n",
      "    Population mean: 50.0000\n",
      "    t(9) = -135.1683, p=0.0000 (both tails)\n",
      "    CI(95%): -51.3144 - -49.6251\n",
      "  == Two Sample T Test\n",
      "    Mean and standard deviation\n",
      "+----------+---------+--------+----+\n",
      "| Variable |  mean   |   sd   | n  |\n",
      "+----------+---------+--------+----+\n",
      "|          | -0.4698 | 1.1807 | 10 |\n",
      "|          | 1.8296  | 0.8102 | 10 |\n",
      "+----------+---------+--------+----+\n",
      "\n",
      "    Levene test for equality of variances : F(1, 18) = 1.4575 , p = 0.2430\n",
      "    T statistics\n",
      "+--------------------+---------+---------+----------------+\n",
      "|        Type        |    t    |   df    | p (both tails) |\n",
      "+--------------------+---------+---------+----------------+\n",
      "| Equal variance     | -5.0777 | 18      | 0.0001         |\n",
      "| Non equal variance | -5.0777 | 15.9373 | 0.0001         |\n",
      "+--------------------+---------+---------+----------------+\n",
      "\n",
      "    Effect size\n",
      "+-------+---------+\n",
      "| x1-x2 | -2.2994 |\n",
      "| d     | -2.4293 |\n",
      "+-------+---------+\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "require 'statsample'\n",
    "\n",
    "Statsample::Analysis.store(Statsample::Test::T) do\n",
    "  a   = rnorm(10)\n",
    "  t_1 = Statsample::Test.t_one_sample(a,{:u=>50})\n",
    "  \n",
    "  summary t_1\n",
    "  \n",
    "  b   = rnorm(10,2)\n",
    "  t_2 = Statsample::Test.t_two_samples_independent(a,b)\n",
    "  \n",
    "  summary t_2\n",
    "end\n",
    "\n",
    "Statsample::Analysis.run_batch"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.2.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.2.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}