{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# Lecture 17\n", "\n", "Today:\n", "1. Review of hypotesis test\n", "2. Application: A/B Testing\n", " + Example\n", "3. Causality" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# 1. Review of hypotesis test\n", "\n", "A possible rule for rejecting the null hypothesis:\n", "\n", "- establish cutoff for p-value\n", "\n", "- for example, a 5% cutoff: if the observed p-value is 5% or less, then reject the null hypothesis. Otherwise, do not reject it" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# 2. A/B Testing: Comparing Two Samples\n", "\n", "- compare values of sampled individuals in group a with values of sampled individuals in group b\n", "- example: random sample of visiotrs to etsy. comparing A) click rate using design A vs B) click rate using design B" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### Example: smoking behaviors of mothers and its influence on babies weights\n", "\n", "- comparing A) birth weights of babies of mothers who smoked during pregnancy vs. B) birth weights of babies of mothers who didn't smoke. question: could the difference be due to chance alone?\n", "\n", "HYPOTHESES\n", "- Null: In the population, the distributions of the birth weights of babies in two groups are the same\n", "- Alternate: babies of the mothers who smoked weighed less than the babies of the non-smokers\n", "- To test this we have to compute a test statistic (one number) between group A and group B. the test statistic is group b - group a\n", " - the statistic for the null hypothesis would be 0\n", "\n", "SIMULATION\n", "- If the null is true, all rearrangements of the birth weights among the two groups are equally likely.\n", "- Plan:\n", " - shuffle birth weights\n", " - assign some to \"group a\" and the rest to \"group b,\" maintaining sample sizes\n", " - find the difference b/t the averages of two shuffled groups\n", " -repeat" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", "Attaching package: ‘dplyr’\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "The following objects are masked from ‘package:stats’:\n", "\n", " filter, lag\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "The following objects are masked from ‘package:base’:\n", "\n", " intersect, setdiff, setequal, union\n", "\n" ] } ], "source": [ "library('dplyr')\n", "library('ggplot2')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "babyweight <- read.csv(\"babyweight.csv\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 32
  2. \n", "\t
  3. 4
  4. \n", "
\n" ] }, "execution_count": 12, "metadata": { }, "output_type": "execute_result" } ], "source": [ "dim(babyweight)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
XWgtGestSmoke
1 294038 yes
2 313038 no
3 242036 yes
4 245034 no
5 276039 yes
6 244035 yes
\n" ] }, "execution_count": 3, "metadata": { }, "output_type": "execute_result" } ], "source": [ "head(babyweight)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
XWgtGestSmoke
1 294038 yes
3 242036 yes
5 276039 yes
6 244035 yes
8 330142 yes
11 271536 yes
\n" ] }, "execution_count": 9, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "2973.625" ] }, "execution_count": 9, "metadata": { }, "output_type": "execute_result" } ], "source": [ "smokers <- filter( babyweight, Smoke == \"yes\")\n", "head(smoker)\n", "ave_weight_smokers <- mean(smokers$Wgt)\n", "ave_weight_smokers" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "3066.125" ] }, "execution_count": 8, "metadata": { }, "output_type": "execute_result" } ], "source": [ "nonsmokers <- filter( babyweight, Smoke == \"no\")\n", "ave_weight_nonsmokers <- mean(nonsmokers$Wgt)\n", "ave_weight_nonsmokers" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "
SmokeAveWeight
no 3066.125
yes 2973.625
\n" ] }, "execution_count": 10, "metadata": { }, "output_type": "execute_result" } ], "source": [ "babyweight_grouped <- group_by( babyweight, Smoke )\n", "babyweight_summary <- summarize(babyweight_grouped, AveWeight = mean(Wgt))\n", "babyweight_summary" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "92.5" ] }, "execution_count": 11, "metadata": { }, "output_type": "execute_result" } ], "source": [ "# observed statistic\n", "\n", "observed_diff <- ave_weight_nonsmokers - ave_weight_smokers\n", "observed_diff" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 2420
  2. \n", "\t
  3. 3523
  4. \n", "\t
  5. 3459
  6. \n", "\t
  7. 2440
  8. \n", "\t
  9. 2520
  10. \n", "\t
  11. 3200
  12. \n", "\t
  13. 2957
  14. \n", "\t
  15. 3346
  16. \n", "\t
  17. 2928
  18. \n", "\t
  19. 2450
  20. \n", "\t
  21. 2920
  22. \n", "\t
  23. 3446
  24. \n", "\t
  25. 2619
  26. \n", "\t
  27. 3244
  28. \n", "\t
  29. 3226
  30. \n", "\t
  31. 3095
  32. \n", "\t
  33. 2760
  34. \n", "\t
  35. 3175
  36. \n", "\t
  37. 3322
  38. \n", "\t
  39. 2729
  40. \n", "\t
  41. 3500
  42. \n", "\t
  43. 2740
  44. \n", "\t
  45. 2715
  46. \n", "\t
  47. 3530
  48. \n", "\t
  49. 2841
  50. \n", "\t
  51. 3130
  52. \n", "\t
  53. 3410
  54. \n", "\t
  55. 3130
  56. \n", "\t
  57. 2580
  58. \n", "\t
  59. 2940
  60. \n", "\t
  61. 3301
  62. \n", "\t
  63. 3040
  64. \n", "
\n" ] }, "execution_count": 16, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "
    \n", "\t
  1. 2420
  2. \n", "\t
  3. 3459
  4. \n", "
\n" ] }, "execution_count": 16, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "
    \n", "\t
  1. 2420
  2. \n", "\t
  3. 3523
  4. \n", "\t
  5. 3459
  6. \n", "\t
  7. 2440
  8. \n", "\t
  9. 2520
  10. \n", "\t
  11. 3200
  12. \n", "\t
  13. 2957
  14. \n", "\t
  15. 3346
  16. \n", "\t
  17. 2928
  18. \n", "\t
  19. 2450
  20. \n", "\t
  21. 2920
  22. \n", "\t
  23. 3446
  24. \n", "\t
  25. 2619
  26. \n", "\t
  27. 3244
  28. \n", "\t
  29. 3226
  30. \n", "\t
  31. 3095
  32. \n", "
\n" ] }, "execution_count": 16, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "
    \n", "\t
  1. 2760
  2. \n", "\t
  3. 3175
  4. \n", "\t
  5. 3322
  6. \n", "\t
  7. 2729
  8. \n", "\t
  9. 3500
  10. \n", "\t
  11. 2740
  12. \n", "\t
  13. 2715
  14. \n", "\t
  15. 3530
  16. \n", "\t
  17. 2841
  18. \n", "\t
  19. 3130
  20. \n", "\t
  21. 3410
  22. \n", "\t
  23. 3130
  24. \n", "\t
  25. 2580
  26. \n", "\t
  27. 2940
  28. \n", "\t
  29. 3301
  30. \n", "\t
  31. 3040
  32. \n", "
\n" ] }, "execution_count": 16, "metadata": { }, "output_type": "execute_result" } ], "source": [ "# Selecting elements from a list\n", "\n", "shuffled_babies <- sample( babyweight$Wgt, 32, replace = FALSE )\n", "shuffled_babies\n", "shuffled_babies[c(1, 3)] #selecting first and third baby\n", "shuffled_babies[1:16] #first sixteen numbers in the list; telling R what index to select\n", "shuffled_babies[17:32] #last sixteen numbers in the list" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "# simulate\n", "\n", "num_simulations <- 1000\n", "\n", "# set up data frame with 1000 rows, each row being an observation. one column would be the test statistic. test statistic = mean weight of group b - mean weight of group a. two other columns would be average weight group A and average weight of group B.\n", "simulated_data <- data.frame(ave_weight_A = double(num_simulations), \n", " ave_weight_B = double(num_simulations),\n", " statistic = double(num_simulations) )\n", "\n", "\n", "count <- 1\n", "while( count <= num_simulations ) {\n", "\n", " shuffled_babies <- sample( babyweight$Wgt, 32, replace = FALSE )\n", " group_A <- shuffled_babies[1:16]\n", " group_B <- shuffled_babies[17:32]\n", " \n", " #find mean of weight in each group, place in correct data frame, and then find the difference\n", " simulated_data$ave_weight_A[count] <- mean(group_A)\n", " simulated_data$ave_weight_B[count] <- mean(group_B)\n", " simulated_data$statistic[count] <- simulated_data$ave_weight_B[count] - simulated_data$ave_weight_A[count]\n", "\n", "\n", " count <- count + 1\n", "}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
ave_weight_Aave_weight_Bstatistic
3034.6883005.062 -29.625
2990.9383048.812 57.875
2994.2503045.500 51.250
3011.4383028.312 16.875
3077.4382962.312-115.125
3019.2503020.500 1.250
\n" ] }, "execution_count": 20, "metadata": { }, "output_type": "execute_result" } ], "source": [ "head(simulated_data)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n \n \n \n\n\n \n \n \n\n\n\n\n\n\n\n\n\n \n \n \n \n\n\n \n\n\n \n \n \n\n\n \n \n \n\n\n \n \n \n \n \n \n \n \n \n\n\n \n \n \n \n \n\n\n\n" }, "execution_count": 21, "metadata": { "image/svg+xml": { "isolated": true } }, "output_type": "execute_result" } ], "source": [ "ggplot(simulated_data, aes( x = statistic)) + geom_histogram( bins = 10 )" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "0.766" ] }, "execution_count": 23, "metadata": { }, "output_type": "execute_result" } ], "source": [ "# find percentile of observed stat:\n", "sum( simulated_data$statistic <= observed_diff ) / 1000\n", "\n", "# area to the left is 76.6th percentile" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "0.234" ] }, "execution_count": 25, "metadata": { }, "output_type": "execute_result" } ], "source": [ "# p-value\n", "1-sum(simulated_data$statistic <= observed_diff) / 1000\n", "\n", "# area to the right is 23.4th percentile" ] } ], "metadata": { "kernelspec": { "display_name": "R (R-Project)", "language": "r", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.4.4" } }, "nbformat": 4, "nbformat_minor": 0 }