{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simulations and Statistical Inference" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Registered S3 methods overwritten by 'ggplot2':\n", " method from \n", " [.quosures rlang\n", " c.quosures rlang\n", " print.quosures rlang\n", "── \u001b[1mAttaching packages\u001b[22m ─────────────────────────────────────── tidyverse 1.2.1 ──\n", "\u001b[32m✔\u001b[39m \u001b[34mggplot2\u001b[39m 3.1.1 \u001b[32m✔\u001b[39m \u001b[34mpurrr \u001b[39m 0.3.2\n", "\u001b[32m✔\u001b[39m \u001b[34mtibble \u001b[39m 2.1.2 \u001b[32m✔\u001b[39m \u001b[34mdplyr \u001b[39m 0.8.1\n", "\u001b[32m✔\u001b[39m \u001b[34mtidyr \u001b[39m 0.8.3 \u001b[32m✔\u001b[39m \u001b[34mstringr\u001b[39m 1.4.0\n", "\u001b[32m✔\u001b[39m \u001b[34mreadr \u001b[39m 1.3.1 \u001b[32m✔\u001b[39m \u001b[34mforcats\u001b[39m 0.4.0\n", "── \u001b[1mConflicts\u001b[22m ────────────────────────────────────────── tidyverse_conflicts() ──\n", "\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mfilter()\u001b[39m masks \u001b[34mstats\u001b[39m::filter()\n", "\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mlag()\u001b[39m masks \u001b[34mstats\u001b[39m::lag()\n" ] } ], "source": [ "library(tidyverse)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "options(repr.plot.width=4, repr.plot.height=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set random number seed for reproducibility" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "set.seed(42)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generating numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the `c` function" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 2
  4. \n", "\t
  5. 3
  6. \n", "\t
  7. 5
  8. \n", "\t
  9. 8
  10. \n", "\t
  11. 13
  12. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 5\n", "\\item 8\n", "\\item 13\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 2\n", "3. 3\n", "4. 5\n", "5. 8\n", "6. 13\n", "\n", "\n" ], "text/plain": [ "[1] 1 2 3 5 8 13" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "c(1,2,3,5,8,13)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the `:` operator" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 2
  4. \n", "\t
  5. 3
  6. \n", "\t
  7. 4
  8. \n", "\t
  9. 5
  10. \n", "\t
  11. 6
  12. \n", "\t
  13. 7
  14. \n", "\t
  15. 8
  16. \n", "\t
  17. 9
  18. \n", "\t
  19. 10
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 4\n", "\\item 5\n", "\\item 6\n", "\\item 7\n", "\\item 8\n", "\\item 9\n", "\\item 10\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 2\n", "3. 3\n", "4. 4\n", "5. 5\n", "6. 6\n", "7. 7\n", "8. 8\n", "9. 9\n", "10. 10\n", "\n", "\n" ], "text/plain": [ " [1] 1 2 3 4 5 6 7 8 9 10" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "1:10" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 10
  2. \n", "\t
  3. 9
  4. \n", "\t
  5. 8
  6. \n", "\t
  7. 7
  8. \n", "\t
  9. 6
  10. \n", "\t
  11. 5
  12. \n", "\t
  13. 4
  14. \n", "\t
  15. 3
  16. \n", "\t
  17. 2
  18. \n", "\t
  19. 1
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 10\n", "\\item 9\n", "\\item 8\n", "\\item 7\n", "\\item 6\n", "\\item 5\n", "\\item 4\n", "\\item 3\n", "\\item 2\n", "\\item 1\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 10\n", "2. 9\n", "3. 8\n", "4. 7\n", "5. 6\n", "6. 5\n", "7. 4\n", "8. 3\n", "9. 2\n", "10. 1\n", "\n", "\n" ], "text/plain": [ " [1] 10 9 8 7 6 5 4 3 2 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "10:1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the `seq` function" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 2
  4. \n", "\t
  5. 3
  6. \n", "\t
  7. 4
  8. \n", "\t
  9. 5
  10. \n", "\t
  11. 6
  12. \n", "\t
  13. 7
  14. \n", "\t
  15. 8
  16. \n", "\t
  17. 9
  18. \n", "\t
  19. 10
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 4\n", "\\item 5\n", "\\item 6\n", "\\item 7\n", "\\item 8\n", "\\item 9\n", "\\item 10\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 2\n", "3. 3\n", "4. 4\n", "5. 5\n", "6. 6\n", "7. 7\n", "8. 8\n", "9. 9\n", "10. 10\n", "\n", "\n" ], "text/plain": [ " [1] 1 2 3 4 5 6 7 8 9 10" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "seq(1, 10)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 3
  4. \n", "\t
  5. 5
  6. \n", "\t
  7. 7
  8. \n", "\t
  9. 9
  10. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 3\n", "\\item 5\n", "\\item 7\n", "\\item 9\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 3\n", "3. 5\n", "4. 7\n", "5. 9\n", "\n", "\n" ], "text/plain": [ "[1] 1 3 5 7 9" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "seq(1, 10, 2)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 10
  2. \n", "\t
  3. 9
  4. \n", "\t
  5. 8
  6. \n", "\t
  7. 7
  8. \n", "\t
  9. 6
  10. \n", "\t
  11. 5
  12. \n", "\t
  13. 4
  14. \n", "\t
  15. 3
  16. \n", "\t
  17. 2
  18. \n", "\t
  19. 1
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 10\n", "\\item 9\n", "\\item 8\n", "\\item 7\n", "\\item 6\n", "\\item 5\n", "\\item 4\n", "\\item 3\n", "\\item 2\n", "\\item 1\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 10\n", "2. 9\n", "3. 8\n", "4. 7\n", "5. 6\n", "6. 5\n", "7. 4\n", "8. 3\n", "9. 2\n", "10. 1\n", "\n", "\n" ], "text/plain": [ " [1] 10 9 8 7 6 5 4 3 2 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "seq(10, 1, -1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 0
  2. \n", "\t
  3. 0.1
  4. \n", "\t
  5. 0.2
  6. \n", "\t
  7. 0.3
  8. \n", "\t
  9. 0.4
  10. \n", "\t
  11. 0.5
  12. \n", "\t
  13. 0.6
  14. \n", "\t
  15. 0.7
  16. \n", "\t
  17. 0.8
  18. \n", "\t
  19. 0.9
  20. \n", "\t
  21. 1
  22. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 0\n", "\\item 0.1\n", "\\item 0.2\n", "\\item 0.3\n", "\\item 0.4\n", "\\item 0.5\n", "\\item 0.6\n", "\\item 0.7\n", "\\item 0.8\n", "\\item 0.9\n", "\\item 1\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 0\n", "2. 0.1\n", "3. 0.2\n", "4. 0.3\n", "5. 0.4\n", "6. 0.5\n", "7. 0.6\n", "8. 0.7\n", "9. 0.8\n", "10. 0.9\n", "11. 1\n", "\n", "\n" ], "text/plain": [ " [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "seq(0, 1, length.out = 11)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using `rep`\n", "\n", "`rep` is useful for generating repeating data patterns." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 3
  2. \n", "\t
  3. 3
  4. \n", "\t
  5. 3
  6. \n", "\t
  7. 3
  8. \n", "\t
  9. 3
  10. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 3\n", "\\item 3\n", "\\item 3\n", "\\item 3\n", "\\item 3\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 3\n", "2. 3\n", "3. 3\n", "4. 3\n", "5. 3\n", "\n", "\n" ], "text/plain": [ "[1] 3 3 3 3 3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rep(3, 5)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 2
  4. \n", "\t
  5. 3
  6. \n", "\t
  7. 1
  8. \n", "\t
  9. 2
  10. \n", "\t
  11. 3
  12. \n", "\t
  13. 1
  14. \n", "\t
  15. 2
  16. \n", "\t
  17. 3
  18. \n", "\t
  19. 1
  20. \n", "\t
  21. 2
  22. \n", "\t
  23. 3
  24. \n", "\t
  25. 1
  26. \n", "\t
  27. 2
  28. \n", "\t
  29. 3
  30. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 2\n", "3. 3\n", "4. 1\n", "5. 2\n", "6. 3\n", "7. 1\n", "8. 2\n", "9. 3\n", "10. 1\n", "11. 2\n", "12. 3\n", "13. 1\n", "14. 2\n", "15. 3\n", "\n", "\n" ], "text/plain": [ " [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rep(1:3, times=5)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 1
  4. \n", "\t
  5. 1
  6. \n", "\t
  7. 1
  8. \n", "\t
  9. 1
  10. \n", "\t
  11. 2
  12. \n", "\t
  13. 2
  14. \n", "\t
  15. 2
  16. \n", "\t
  17. 2
  18. \n", "\t
  19. 2
  20. \n", "\t
  21. 3
  22. \n", "\t
  23. 3
  24. \n", "\t
  25. 3
  26. \n", "\t
  27. 3
  28. \n", "\t
  29. 3
  30. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 2\n", "\\item 2\n", "\\item 2\n", "\\item 2\n", "\\item 2\n", "\\item 3\n", "\\item 3\n", "\\item 3\n", "\\item 3\n", "\\item 3\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 1\n", "3. 1\n", "4. 1\n", "5. 1\n", "6. 2\n", "7. 2\n", "8. 2\n", "9. 2\n", "10. 2\n", "11. 3\n", "12. 3\n", "13. 3\n", "14. 3\n", "15. 3\n", "\n", "\n" ], "text/plain": [ " [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rep(1:3, each=5)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 2
  4. \n", "\t
  5. 3
  6. \n", "\t
  7. 1
  8. \n", "\t
  9. 2
  10. \n", "\t
  11. 3
  12. \n", "\t
  13. 1
  14. \n", "\t
  15. 2
  16. \n", "\t
  17. 3
  18. \n", "\t
  19. 1
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 1\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 2\n", "3. 3\n", "4. 1\n", "5. 2\n", "6. 3\n", "7. 1\n", "8. 2\n", "9. 3\n", "10. 1\n", "\n", "\n" ], "text/plain": [ " [1] 1 2 3 1 2 3 1 2 3 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rep(1:3, length.out=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using `sample`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simulate `n` tooses of a coin" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "n <- 10" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'H'
  2. \n", "\t
  3. 'H'
  4. \n", "\t
  5. 'H'
  6. \n", "\t
  7. 'H'
  8. \n", "\t
  9. 'T'
  10. \n", "\t
  11. 'T'
  12. \n", "\t
  13. 'T'
  14. \n", "\t
  15. 'T'
  16. \n", "\t
  17. 'H'
  18. \n", "\t
  19. 'T'
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'H'\n", "\\item 'H'\n", "\\item 'H'\n", "\\item 'H'\n", "\\item 'T'\n", "\\item 'T'\n", "\\item 'T'\n", "\\item 'T'\n", "\\item 'H'\n", "\\item 'T'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'H'\n", "2. 'H'\n", "3. 'H'\n", "4. 'H'\n", "5. 'T'\n", "6. 'T'\n", "7. 'T'\n", "8. 'T'\n", "9. 'H'\n", "10. 'T'\n", "\n", "\n" ], "text/plain": [ " [1] \"H\" \"H\" \"H\" \"H\" \"T\" \"T\" \"T\" \"T\" \"H\" \"T\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t1 <- sample(c('H', 'T'), n, replace = TRUE)\n", "t1" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "t1\n", "H T \n", "5 5 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "table(t1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simulate `n` tosses of a biased coin" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'T'
  2. \n", "\t
  3. 'H'
  4. \n", "\t
  5. 'H'
  6. \n", "\t
  7. 'T'
  8. \n", "\t
  9. 'T'
  10. \n", "\t
  11. 'H'
  12. \n", "\t
  13. 'H'
  14. \n", "\t
  15. 'T'
  16. \n", "\t
  17. 'T'
  18. \n", "\t
  19. 'T'
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'T'\n", "\\item 'H'\n", "\\item 'H'\n", "\\item 'T'\n", "\\item 'T'\n", "\\item 'H'\n", "\\item 'H'\n", "\\item 'T'\n", "\\item 'T'\n", "\\item 'T'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'T'\n", "2. 'H'\n", "3. 'H'\n", "4. 'T'\n", "5. 'T'\n", "6. 'H'\n", "7. 'H'\n", "8. 'T'\n", "9. 'T'\n", "10. 'T'\n", "\n", "\n" ], "text/plain": [ " [1] \"T\" \"H\" \"H\" \"T\" \"T\" \"H\" \"H\" \"T\" \"T\" \"T\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t2 <- sample(c('H', 'T'), n, replace = TRUE, prob = c(0.3, 0.7))\n", "t2" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "t2\n", "H T \n", "4 6 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "table(t2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simulate `n` rolls of a 6-sided die" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "n <- 100" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 3
  2. \n", "\t
  3. 1
  4. \n", "\t
  5. 1
  6. \n", "\t
  7. 3
  8. \n", "\t
  9. 4
  10. \n", "\t
  11. 5
  12. \n", "\t
  13. 5
  14. \n", "\t
  15. 5
  16. \n", "\t
  17. 4
  18. \n", "\t
  19. 2
  20. \n", "\t
  21. 4
  22. \n", "\t
  23. 3
  24. \n", "\t
  25. 2
  26. \n", "\t
  27. 1
  28. \n", "\t
  29. 2
  30. \n", "\t
  31. 6
  32. \n", "\t
  33. 3
  34. \n", "\t
  35. 6
  36. \n", "\t
  37. 2
  38. \n", "\t
  39. 4
  40. \n", "\t
  41. 4
  42. \n", "\t
  43. 6
  44. \n", "\t
  45. 2
  46. \n", "\t
  47. 5
  48. \n", "\t
  49. 4
  50. \n", "\t
  51. 5
  52. \n", "\t
  53. 4
  54. \n", "\t
  55. 2
  56. \n", "\t
  57. 2
  58. \n", "\t
  59. 3
  60. \n", "\t
  61. 1
  62. \n", "\t
  63. 5
  64. \n", "\t
  65. 2
  66. \n", "\t
  67. 2
  68. \n", "\t
  69. 6
  70. \n", "\t
  71. 6
  72. \n", "\t
  73. 2
  74. \n", "\t
  75. 4
  76. \n", "\t
  77. 3
  78. \n", "\t
  79. 6
  80. \n", "\t
  81. 5
  82. \n", "\t
  83. 2
  84. \n", "\t
  85. 6
  86. \n", "\t
  87. 2
  88. \n", "\t
  89. 2
  90. \n", "\t
  91. 5
  92. \n", "\t
  93. 1
  94. \n", "\t
  95. 1
  96. \n", "\t
  97. 4
  98. \n", "\t
  99. 5
  100. \n", "\t
  101. 2
  102. \n", "\t
  103. 1
  104. \n", "\t
  105. 5
  106. \n", "\t
  107. 4
  108. \n", "\t
  109. 4
  110. \n", "\t
  111. 1
  112. \n", "\t
  113. 3
  114. \n", "\t
  115. 3
  116. \n", "\t
  117. 5
  118. \n", "\t
  119. 5
  120. \n", "\t
  121. 4
  122. \n", "\t
  123. 6
  124. \n", "\t
  125. 5
  126. \n", "\t
  127. 4
  128. \n", "\t
  129. 6
  130. \n", "\t
  131. 2
  132. \n", "\t
  133. 2
  134. \n", "\t
  135. 1
  136. \n", "\t
  137. 2
  138. \n", "\t
  139. 5
  140. \n", "\t
  141. 6
  142. \n", "\t
  143. 5
  144. \n", "\t
  145. 4
  146. \n", "\t
  147. 1
  148. \n", "\t
  149. 4
  150. \n", "\t
  151. 2
  152. \n", "\t
  153. 2
  154. \n", "\t
  155. 3
  156. \n", "\t
  157. 5
  158. \n", "\t
  159. 5
  160. \n", "\t
  161. 6
  162. \n", "\t
  163. 3
  164. \n", "\t
  165. 6
  166. \n", "\t
  167. 4
  168. \n", "\t
  169. 5
  170. \n", "\t
  171. 1
  172. \n", "\t
  173. 4
  174. \n", "\t
  175. 1
  176. \n", "\t
  177. 1
  178. \n", "\t
  179. 5
  180. \n", "\t
  181. 6
  182. \n", "\t
  183. 3
  184. \n", "\t
  185. 1
  186. \n", "\t
  187. 2
  188. \n", "\t
  189. 5
  190. \n", "\t
  191. 6
  192. \n", "\t
  193. 1
  194. \n", "\t
  195. 5
  196. \n", "\t
  197. 6
  198. \n", "\t
  199. 5
  200. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 3\n", "\\item 1\n", "\\item 1\n", "\\item 3\n", "\\item 4\n", "\\item 5\n", "\\item 5\n", "\\item 5\n", "\\item 4\n", "\\item 2\n", "\\item 4\n", "\\item 3\n", "\\item 2\n", "\\item 1\n", "\\item 2\n", "\\item 6\n", "\\item 3\n", "\\item 6\n", "\\item 2\n", "\\item 4\n", "\\item 4\n", "\\item 6\n", "\\item 2\n", "\\item 5\n", "\\item 4\n", "\\item 5\n", "\\item 4\n", "\\item 2\n", "\\item 2\n", "\\item 3\n", "\\item 1\n", "\\item 5\n", "\\item 2\n", "\\item 2\n", "\\item 6\n", "\\item 6\n", "\\item 2\n", "\\item 4\n", "\\item 3\n", "\\item 6\n", "\\item 5\n", "\\item 2\n", "\\item 6\n", "\\item 2\n", "\\item 2\n", "\\item 5\n", "\\item 1\n", "\\item 1\n", "\\item 4\n", "\\item 5\n", "\\item 2\n", "\\item 1\n", "\\item 5\n", "\\item 4\n", "\\item 4\n", "\\item 1\n", "\\item 3\n", "\\item 3\n", "\\item 5\n", "\\item 5\n", "\\item 4\n", "\\item 6\n", "\\item 5\n", "\\item 4\n", "\\item 6\n", "\\item 2\n", "\\item 2\n", "\\item 1\n", "\\item 2\n", "\\item 5\n", "\\item 6\n", "\\item 5\n", "\\item 4\n", "\\item 1\n", "\\item 4\n", "\\item 2\n", "\\item 2\n", "\\item 3\n", "\\item 5\n", "\\item 5\n", "\\item 6\n", "\\item 3\n", "\\item 6\n", "\\item 4\n", "\\item 5\n", "\\item 1\n", "\\item 4\n", "\\item 1\n", "\\item 1\n", "\\item 5\n", "\\item 6\n", "\\item 3\n", "\\item 1\n", "\\item 2\n", "\\item 5\n", "\\item 6\n", "\\item 1\n", "\\item 5\n", "\\item 6\n", "\\item 5\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 3\n", "2. 1\n", "3. 1\n", "4. 3\n", "5. 4\n", "6. 5\n", "7. 5\n", "8. 5\n", "9. 4\n", "10. 2\n", "11. 4\n", "12. 3\n", "13. 2\n", "14. 1\n", "15. 2\n", "16. 6\n", "17. 3\n", "18. 6\n", "19. 2\n", "20. 4\n", "21. 4\n", "22. 6\n", "23. 2\n", "24. 5\n", "25. 4\n", "26. 5\n", "27. 4\n", "28. 2\n", "29. 2\n", "30. 3\n", "31. 1\n", "32. 5\n", "33. 2\n", "34. 2\n", "35. 6\n", "36. 6\n", "37. 2\n", "38. 4\n", "39. 3\n", "40. 6\n", "41. 5\n", "42. 2\n", "43. 6\n", "44. 2\n", "45. 2\n", "46. 5\n", "47. 1\n", "48. 1\n", "49. 4\n", "50. 5\n", "51. 2\n", "52. 1\n", "53. 5\n", "54. 4\n", "55. 4\n", "56. 1\n", "57. 3\n", "58. 3\n", "59. 5\n", "60. 5\n", "61. 4\n", "62. 6\n", "63. 5\n", "64. 4\n", "65. 6\n", "66. 2\n", "67. 2\n", "68. 1\n", "69. 2\n", "70. 5\n", "71. 6\n", "72. 5\n", "73. 4\n", "74. 1\n", "75. 4\n", "76. 2\n", "77. 2\n", "78. 3\n", "79. 5\n", "80. 5\n", "81. 6\n", "82. 3\n", "83. 6\n", "84. 4\n", "85. 5\n", "86. 1\n", "87. 4\n", "88. 1\n", "89. 1\n", "90. 5\n", "91. 6\n", "92. 3\n", "93. 1\n", "94. 2\n", "95. 5\n", "96. 6\n", "97. 1\n", "98. 5\n", "99. 6\n", "100. 5\n", "\n", "\n" ], "text/plain": [ " [1] 3 1 1 3 4 5 5 5 4 2 4 3 2 1 2 6 3 6 2 4 4 6 2 5 4 5 4 2 2 3 1 5 2 2 6 6 2\n", " [38] 4 3 6 5 2 6 2 2 5 1 1 4 5 2 1 5 4 4 1 3 3 5 5 4 6 5 4 6 2 2 1 2 5 6 5 4 1\n", " [75] 4 2 2 3 5 5 6 3 6 4 5 1 4 1 1 5 6 3 1 2 5 6 1 5 6 5" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "d1 <- sample(1:6, n, replace=TRUE)\n", "d1" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "d1\n", " 1 2 3 4 5 6 \n", "15 20 11 17 22 15 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "table(d1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sampling without replacement. For example, if we wanted to assiggn 16 samples to treatment A or B at random such that exactly half had each treatment." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'B'
  2. \n", "\t
  3. 'A'
  4. \n", "\t
  5. 'B'
  6. \n", "\t
  7. 'B'
  8. \n", "\t
  9. 'B'
  10. \n", "\t
  11. 'B'
  12. \n", "\t
  13. 'A'
  14. \n", "\t
  15. 'A'
  16. \n", "\t
  17. 'A'
  18. \n", "\t
  19. 'B'
  20. \n", "\t
  21. 'A'
  22. \n", "\t
  23. 'B'
  24. \n", "\t
  25. 'B'
  26. \n", "\t
  27. 'A'
  28. \n", "\t
  29. 'A'
  30. \n", "\t
  31. 'A'
  32. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'B'\n", "\\item 'A'\n", "\\item 'B'\n", "\\item 'B'\n", "\\item 'B'\n", "\\item 'B'\n", "\\item 'A'\n", "\\item 'A'\n", "\\item 'A'\n", "\\item 'B'\n", "\\item 'A'\n", "\\item 'B'\n", "\\item 'B'\n", "\\item 'A'\n", "\\item 'A'\n", "\\item 'A'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'B'\n", "2. 'A'\n", "3. 'B'\n", "4. 'B'\n", "5. 'B'\n", "6. 'B'\n", "7. 'A'\n", "8. 'A'\n", "9. 'A'\n", "10. 'B'\n", "11. 'A'\n", "12. 'B'\n", "13. 'B'\n", "14. 'A'\n", "15. 'A'\n", "16. 'A'\n", "\n", "\n" ], "text/plain": [ " [1] \"B\" \"A\" \"B\" \"B\" \"B\" \"B\" \"A\" \"A\" \"A\" \"B\" \"A\" \"B\" \"B\" \"A\" \"A\" \"A\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sample(rep(c('A', 'B'), each=8))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Random number generators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Discrete distributions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sampling from a Bernoullli distribution returns TRUE for success and FALSE for failure." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. TRUE
  2. \n", "\t
  3. TRUE
  4. \n", "\t
  5. FALSE
  6. \n", "\t
  7. TRUE
  8. \n", "\t
  9. FALSE
  10. \n", "\t
  11. FALSE
  12. \n", "\t
  13. FALSE
  14. \n", "\t
  15. FALSE
  16. \n", "\t
  17. FALSE
  18. \n", "\t
  19. TRUE
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item TRUE\n", "\\item TRUE\n", "\\item FALSE\n", "\\item TRUE\n", "\\item FALSE\n", "\\item FALSE\n", "\\item FALSE\n", "\\item FALSE\n", "\\item FALSE\n", "\\item TRUE\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. TRUE\n", "2. TRUE\n", "3. FALSE\n", "4. TRUE\n", "5. FALSE\n", "6. FALSE\n", "7. FALSE\n", "8. FALSE\n", "9. FALSE\n", "10. TRUE\n", "\n", "\n" ], "text/plain": [ " [1] TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rbernoulli(n=10, p=0.5)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 0
  2. \n", "\t
  3. 0
  4. \n", "\t
  5. 0
  6. \n", "\t
  7. 0
  8. \n", "\t
  9. 0
  10. \n", "\t
  11. 1
  12. \n", "\t
  13. 1
  14. \n", "\t
  15. 1
  16. \n", "\t
  17. 1
  18. \n", "\t
  19. 1
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 0\n", "2. 0\n", "3. 0\n", "4. 0\n", "5. 0\n", "6. 1\n", "7. 1\n", "8. 1\n", "9. 1\n", "10. 1\n", "\n", "\n" ], "text/plain": [ " [1] 0 0 0 0 0 1 1 1 1 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "as.integer(rbernoulli(n=10, p=0.5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sampling from a Binomial distribution returns the number of **successes** in `size` trials for `n` experiments." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 4
  2. \n", "\t
  3. 2
  4. \n", "\t
  5. 2
  6. \n", "\t
  7. 3
  8. \n", "\t
  9. 3
  10. \n", "\t
  11. 4
  12. \n", "\t
  13. 3
  14. \n", "\t
  15. 1
  16. \n", "\t
  17. 2
  18. \n", "\t
  19. 2
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 4\n", "\\item 2\n", "\\item 2\n", "\\item 3\n", "\\item 3\n", "\\item 4\n", "\\item 3\n", "\\item 1\n", "\\item 2\n", "\\item 2\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 4\n", "2. 2\n", "3. 2\n", "4. 3\n", "5. 3\n", "6. 4\n", "7. 3\n", "8. 1\n", "9. 2\n", "10. 2\n", "\n", "\n" ], "text/plain": [ " [1] 4 2 2 3 3 4 3 1 2 2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rbinom(n=10, p=0.5, size=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sampling from a negative binomial distribution returns the number of **failures** until `size` succcesses are observed for `n` experiemnts." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 4
  2. \n", "\t
  3. 0
  4. \n", "\t
  5. 9
  6. \n", "\t
  7. 4
  8. \n", "\t
  9. 2
  10. \n", "\t
  11. 4
  12. \n", "\t
  13. 6
  14. \n", "\t
  15. 5
  16. \n", "\t
  17. 5
  18. \n", "\t
  19. 4
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 4\n", "\\item 0\n", "\\item 9\n", "\\item 4\n", "\\item 2\n", "\\item 4\n", "\\item 6\n", "\\item 5\n", "\\item 5\n", "\\item 4\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 4\n", "2. 0\n", "3. 9\n", "4. 4\n", "5. 2\n", "6. 4\n", "7. 6\n", "8. 5\n", "9. 5\n", "10. 4\n", "\n", "\n" ], "text/plain": [ " [1] 4 0 9 4 2 4 6 5 5 4" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rnbinom(n=10, size=5, prob=0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sampling from a Poisson distribution returns the number of **successes** in `n` experiments if the average success rate per experiment is `lambda`." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 5
  2. \n", "\t
  3. 2
  4. \n", "\t
  5. 1
  6. \n", "\t
  7. 0
  8. \n", "\t
  9. 2
  10. \n", "\t
  11. 2
  12. \n", "\t
  13. 1
  14. \n", "\t
  15. 2
  16. \n", "\t
  17. 0
  18. \n", "\t
  19. 9
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 5\n", "\\item 2\n", "\\item 1\n", "\\item 0\n", "\\item 2\n", "\\item 2\n", "\\item 1\n", "\\item 2\n", "\\item 0\n", "\\item 9\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 5\n", "2. 2\n", "3. 1\n", "4. 0\n", "5. 2\n", "6. 2\n", "7. 1\n", "8. 2\n", "9. 0\n", "10. 9\n", "\n", "\n" ], "text/plain": [ " [1] 5 2 1 0 2 2 1 2 0 9" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rpois(n=10, lambda = 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note**: We can give different parameters for each experiment in these distributions." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 2
  2. \n", "\t
  3. 0
  4. \n", "\t
  5. 5
  6. \n", "\t
  7. 4
  8. \n", "\t
  9. 4
  10. \n", "\t
  11. 3
  12. \n", "\t
  13. 7
  14. \n", "\t
  15. 4
  16. \n", "\t
  17. 7
  18. \n", "\t
  19. 10
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 2\n", "\\item 0\n", "\\item 5\n", "\\item 4\n", "\\item 4\n", "\\item 3\n", "\\item 7\n", "\\item 4\n", "\\item 7\n", "\\item 10\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 2\n", "2. 0\n", "3. 5\n", "4. 4\n", "5. 4\n", "6. 3\n", "7. 7\n", "8. 4\n", "9. 7\n", "10. 10\n", "\n", "\n" ], "text/plain": [ " [1] 2 0 5 4 4 3 7 4 7 10" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rpois(n=10, lambda=1:10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Continuous distributions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sampling from a standard uniform distribution." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 0.159469854552299
  2. \n", "\t
  3. 0.149578995071352
  4. \n", "\t
  5. 0.499272880377248
  6. \n", "\t
  7. 0.940564878052101
  8. \n", "\t
  9. 0.334231326589361
  10. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 0.159469854552299\n", "\\item 0.149578995071352\n", "\\item 0.499272880377248\n", "\\item 0.940564878052101\n", "\\item 0.334231326589361\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 0.159469854552299\n", "2. 0.149578995071352\n", "3. 0.499272880377248\n", "4. 0.940564878052101\n", "5. 0.334231326589361\n", "\n", "\n" ], "text/plain": [ "[1] 0.1594699 0.1495790 0.4992729 0.9405649 0.3342313" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "runif(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sampling form a uniform distribution, with values between 90 and 100." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 91.8843432958238
  2. \n", "\t
  3. 92.6971617829986
  4. \n", "\t
  5. 95.3074407810345
  6. \n", "\t
  7. 90.2145022852346
  8. \n", "\t
  9. 97.987603074871
  10. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 91.8843432958238\n", "\\item 92.6971617829986\n", "\\item 95.3074407810345\n", "\\item 90.2145022852346\n", "\\item 97.987603074871\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 91.8843432958238\n", "2. 92.6971617829986\n", "3. 95.3074407810345\n", "4. 90.2145022852346\n", "5. 97.987603074871\n", "\n", "\n" ], "text/plain": [ "[1] 91.88434 92.69716 95.30744 90.21450 97.98760" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "runif(5, 90, 100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sampling from a standard normal distribution." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. -1.22474795035999
  2. \n", "\t
  3. 0.179516441117938
  4. \n", "\t
  5. 0.567620594423535
  6. \n", "\t
  7. -0.492877353553475
  8. \n", "\t
  9. 6.28840653511241e-05
  10. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item -1.22474795035999\n", "\\item 0.179516441117938\n", "\\item 0.567620594423535\n", "\\item -0.492877353553475\n", "\\item 6.28840653511241e-05\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. -1.22474795035999\n", "2. 0.179516441117938\n", "3. 0.567620594423535\n", "4. -0.492877353553475\n", "5. 6.28840653511241e-05\n", "\n", "\n" ], "text/plain": [ "[1] -1.224748e+00 1.795164e-01 5.676206e-01 -4.928774e-01 6.288407e-05" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rnorm(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Looping" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 0.06091411\n", "[1] -0.1028669\n", "[1] -0.3117706\n", "[1] 0.09639036\n", "[1] 0.2010454\n", "[1] -0.2384309\n", "[1] 0.1271087\n", "[1] -0.2115652\n", "[1] -0.01946474\n", "[1] -0.04661421\n" ] } ], "source": [ "for (i in 1:10) {\n", " print(mean(rnorm(10)))\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Saving variables generated in a loop" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 0.102869685091412
  2. \n", "\t
  3. 0.0394963601049801
  4. \n", "\t
  5. 0.0587241371393688
  6. \n", "\t
  7. -0.208039597462606
  8. \n", "\t
  9. 0.175088960315561
  10. \n", "\t
  11. -0.0224984001571429
  12. \n", "\t
  13. 0.157365195356717
  14. \n", "\t
  15. -0.11380999366832
  16. \n", "\t
  17. -0.151701911199645
  18. \n", "\t
  19. 0.0806058319328881
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 0.102869685091412\n", "\\item 0.0394963601049801\n", "\\item 0.0587241371393688\n", "\\item -0.208039597462606\n", "\\item 0.175088960315561\n", "\\item -0.0224984001571429\n", "\\item 0.157365195356717\n", "\\item -0.11380999366832\n", "\\item -0.151701911199645\n", "\\item 0.0806058319328881\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 0.102869685091412\n", "2. 0.0394963601049801\n", "3. 0.0587241371393688\n", "4. -0.208039597462606\n", "5. 0.175088960315561\n", "6. -0.0224984001571429\n", "7. 0.157365195356717\n", "8. -0.11380999366832\n", "9. -0.151701911199645\n", "10. 0.0806058319328881\n", "\n", "\n" ], "text/plain": [ " [1] 0.10286969 0.03949636 0.05872414 -0.20803960 0.17508896 -0.02249840\n", " [7] 0.15736520 -0.11380999 -0.15170191 0.08060583" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "n <- 10\n", "vars <- numeric(n)\n", "for (i in 1:n) {\n", " vars[i] <- mean(rnorm(10))\n", "}\n", "vars" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 0.152764106747895
  2. \n", "\t
    1. \n", "\t
    2. 0.988596845178799
    3. \n", "\t
    4. -0.073458334672174
    5. \n", "
    \n", "
  3. \n", "\t
    1. \n", "\t
    2. -1.38702655355974
    3. \n", "\t
    4. -1.30667590437536
    5. \n", "\t
    6. -0.768395325077939
    7. \n", "
    \n", "
  4. \n", "\t
    1. \n", "\t
    2. -0.527108125374458
    3. \n", "\t
    4. -0.0214270650416246
    5. \n", "\t
    6. 0.670498070996282
    7. \n", "\t
    8. -0.434617038572407
    9. \n", "
    \n", "
  5. \n", "\t
    1. \n", "\t
    2. -1.11387978331285
    3. \n", "\t
    4. 0.6071059948785
    5. \n", "\t
    6. 0.275456968739591
    7. \n", "\t
    8. 1.15734706941474
    9. \n", "\t
    10. -1.68248085947826
    11. \n", "
    \n", "
  6. \n", "\t
    1. \n", "\t
    2. 0.0873190888948744
    3. \n", "\t
    4. 1.35336189394267
    5. \n", "\t
    6. 0.724173800683369
    7. \n", "\t
    8. -0.832552825764212
    9. \n", "\t
    10. 0.732528486768414
    11. \n", "\t
    12. -0.871926869997213
    13. \n", "
    \n", "
  7. \n", "\t
    1. \n", "\t
    2. -0.453397511229793
    3. \n", "\t
    4. 1.18753427862908
    5. \n", "\t
    6. -0.290145311790079
    7. \n", "\t
    8. 0.828546144965436
    9. \n", "\t
    10. -0.291227708779169
    11. \n", "\t
    12. -1.57636240474791
    13. \n", "\t
    14. -0.848815696948922
    15. \n", "
    \n", "
  8. \n", "\t
    1. \n", "\t
    2. -1.0885198619572
    3. \n", "\t
    4. -0.484290571117173
    5. \n", "\t
    6. -0.336311209009573
    7. \n", "\t
    8. -0.153357890661644
    9. \n", "\t
    10. -0.243247228574846
    11. \n", "\t
    12. 1.89220204160011
    13. \n", "\t
    14. -1.38599833728542
    15. \n", "\t
    16. -0.41482430070778
    17. \n", "
    \n", "
  9. \n", "\t
    1. \n", "\t
    2. 0.349081528106831
    3. \n", "\t
    4. 1.62844226581541
    5. \n", "\t
    6. 0.0885218957136163
    7. \n", "\t
    8. 1.23915070840056
    9. \n", "\t
    10. -1.64455553575371
    11. \n", "\t
    12. 1.44635652537848
    13. \n", "\t
    14. -0.690560171665183
    15. \n", "\t
    16. -0.276431085453852
    17. \n", "\t
    18. -1.10941875989944
    19. \n", "
    \n", "
  10. \n", "\t
    1. \n", "\t
    2. 0.133869316429452
    3. \n", "\t
    4. 1.78533905173155
    5. \n", "\t
    6. 2.42216335525159
    7. \n", "\t
    8. -1.07682890211841
    9. \n", "\t
    10. 0.485941110403099
    11. \n", "\t
    12. 1.38852173874429
    13. \n", "\t
    14. -0.195656817284544
    15. \n", "\t
    16. -0.218174797706211
    17. \n", "\t
    18. -0.304777954546445
    19. \n", "\t
    20. 0.597832724111443
    21. \n", "
    \n", "
  11. \n", "
\n" ], "text/latex": [ "\\begin{enumerate}\n", "\\item 0.152764106747895\n", "\\item \\begin{enumerate*}\n", "\\item 0.988596845178799\n", "\\item -0.073458334672174\n", "\\end{enumerate*}\n", "\n", "\\item \\begin{enumerate*}\n", "\\item -1.38702655355974\n", "\\item -1.30667590437536\n", "\\item -0.768395325077939\n", "\\end{enumerate*}\n", "\n", "\\item \\begin{enumerate*}\n", "\\item -0.527108125374458\n", "\\item -0.0214270650416246\n", "\\item 0.670498070996282\n", "\\item -0.434617038572407\n", "\\end{enumerate*}\n", "\n", "\\item \\begin{enumerate*}\n", "\\item -1.11387978331285\n", "\\item 0.6071059948785\n", "\\item 0.275456968739591\n", "\\item 1.15734706941474\n", "\\item -1.68248085947826\n", "\\end{enumerate*}\n", "\n", "\\item \\begin{enumerate*}\n", "\\item 0.0873190888948744\n", "\\item 1.35336189394267\n", "\\item 0.724173800683369\n", "\\item -0.832552825764212\n", "\\item 0.732528486768414\n", "\\item -0.871926869997213\n", "\\end{enumerate*}\n", "\n", "\\item \\begin{enumerate*}\n", "\\item -0.453397511229793\n", "\\item 1.18753427862908\n", "\\item -0.290145311790079\n", "\\item 0.828546144965436\n", "\\item -0.291227708779169\n", "\\item -1.57636240474791\n", "\\item -0.848815696948922\n", "\\end{enumerate*}\n", "\n", "\\item \\begin{enumerate*}\n", "\\item -1.0885198619572\n", "\\item -0.484290571117173\n", "\\item -0.336311209009573\n", "\\item -0.153357890661644\n", "\\item -0.243247228574846\n", "\\item 1.89220204160011\n", "\\item -1.38599833728542\n", "\\item -0.41482430070778\n", "\\end{enumerate*}\n", "\n", "\\item \\begin{enumerate*}\n", "\\item 0.349081528106831\n", "\\item 1.62844226581541\n", "\\item 0.0885218957136163\n", "\\item 1.23915070840056\n", "\\item -1.64455553575371\n", "\\item 1.44635652537848\n", "\\item -0.690560171665183\n", "\\item -0.276431085453852\n", "\\item -1.10941875989944\n", "\\end{enumerate*}\n", "\n", "\\item \\begin{enumerate*}\n", "\\item 0.133869316429452\n", "\\item 1.78533905173155\n", "\\item 2.42216335525159\n", "\\item -1.07682890211841\n", "\\item 0.485941110403099\n", "\\item 1.38852173874429\n", "\\item -0.195656817284544\n", "\\item -0.218174797706211\n", "\\item -0.304777954546445\n", "\\item 0.597832724111443\n", "\\end{enumerate*}\n", "\n", "\\end{enumerate}\n" ], "text/markdown": [ "1. 0.152764106747895\n", "2. 1. 0.988596845178799\n", "2. -0.073458334672174\n", "\n", "\n", "\n", "3. 1. -1.38702655355974\n", "2. -1.30667590437536\n", "3. -0.768395325077939\n", "\n", "\n", "\n", "4. 1. -0.527108125374458\n", "2. -0.0214270650416246\n", "3. 0.670498070996282\n", "4. -0.434617038572407\n", "\n", "\n", "\n", "5. 1. -1.11387978331285\n", "2. 0.6071059948785\n", "3. 0.275456968739591\n", "4. 1.15734706941474\n", "5. -1.68248085947826\n", "\n", "\n", "\n", "6. 1. 0.0873190888948744\n", "2. 1.35336189394267\n", "3. 0.724173800683369\n", "4. -0.832552825764212\n", "5. 0.732528486768414\n", "6. -0.871926869997213\n", "\n", "\n", "\n", "7. 1. -0.453397511229793\n", "2. 1.18753427862908\n", "3. -0.290145311790079\n", "4. 0.828546144965436\n", "5. -0.291227708779169\n", "6. -1.57636240474791\n", "7. -0.848815696948922\n", "\n", "\n", "\n", "8. 1. -1.0885198619572\n", "2. -0.484290571117173\n", "3. -0.336311209009573\n", "4. -0.153357890661644\n", "5. -0.243247228574846\n", "6. 1.89220204160011\n", "7. -1.38599833728542\n", "8. -0.41482430070778\n", "\n", "\n", "\n", "9. 1. 0.349081528106831\n", "2. 1.62844226581541\n", "3. 0.0885218957136163\n", "4. 1.23915070840056\n", "5. -1.64455553575371\n", "6. 1.44635652537848\n", "7. -0.690560171665183\n", "8. -0.276431085453852\n", "9. -1.10941875989944\n", "\n", "\n", "\n", "10. 1. 0.133869316429452\n", "2. 1.78533905173155\n", "3. 2.42216335525159\n", "4. -1.07682890211841\n", "5. 0.485941110403099\n", "6. 1.38852173874429\n", "7. -0.195656817284544\n", "8. -0.218174797706211\n", "9. -0.304777954546445\n", "10. 0.597832724111443\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "[[1]]\n", "[1] 0.1527641\n", "\n", "[[2]]\n", "[1] 0.98859685 -0.07345833\n", "\n", "[[3]]\n", "[1] -1.3870266 -1.3066759 -0.7683953\n", "\n", "[[4]]\n", "[1] -0.52710813 -0.02142707 0.67049807 -0.43461704\n", "\n", "[[5]]\n", "[1] -1.113880 0.607106 0.275457 1.157347 -1.682481\n", "\n", "[[6]]\n", "[1] 0.08731909 1.35336189 0.72417380 -0.83255283 0.73252849 -0.87192687\n", "\n", "[[7]]\n", "[1] -0.4533975 1.1875343 -0.2901453 0.8285461 -0.2912277 -1.5763624 -0.8488157\n", "\n", "[[8]]\n", "[1] -1.0885199 -0.4842906 -0.3363112 -0.1533579 -0.2432472 1.8922020 -1.3859983\n", "[8] -0.4148243\n", "\n", "[[9]]\n", "[1] 0.3490815 1.6284423 0.0885219 1.2391507 -1.6445555 1.4463565 -0.6905602\n", "[8] -0.2764311 -1.1094188\n", "\n", "[[10]]\n", " [1] 0.1338693 1.7853391 2.4221634 -1.0768289 0.4859411 1.3885217\n", " [7] -0.1956568 -0.2181748 -0.3047780 0.5978327\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "lapply(1:10,FUN = rnorm)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1.39742941081576
  2. \n", "\t
  3. 0.503903887768466
  4. \n", "\t
  5. -0.117686052712023
  6. \n", "\t
  7. 0.375071580419853
  8. \n", "\t
  9. 0.239310604458899
  10. \n", "\t
  11. 0.100315651769656
  12. \n", "\t
  13. -0.976662058729188
  14. \n", "\t
  15. -0.279974621278527
  16. \n", "\t
  17. 0.203381481503197
  18. \n", "\t
  19. 0.0364462992139071
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate}\n", "\\item 1.39742941081576\n", "\\item 0.503903887768466\n", "\\item -0.117686052712023\n", "\\item 0.375071580419853\n", "\\item 0.239310604458899\n", "\\item 0.100315651769656\n", "\\item -0.976662058729188\n", "\\item -0.279974621278527\n", "\\item 0.203381481503197\n", "\\item 0.0364462992139071\n", "\\end{enumerate}\n" ], "text/markdown": [ "1. 1.39742941081576\n", "2. 0.503903887768466\n", "3. -0.117686052712023\n", "4. 0.375071580419853\n", "5. 0.239310604458899\n", "6. 0.100315651769656\n", "7. -0.976662058729188\n", "8. -0.279974621278527\n", "9. 0.203381481503197\n", "10. 0.0364462992139071\n", "\n", "\n" ], "text/plain": [ "[[1]]\n", "[1] 1.397429\n", "\n", "[[2]]\n", "[1] 0.5039039\n", "\n", "[[3]]\n", "[1] -0.1176861\n", "\n", "[[4]]\n", "[1] 0.3750716\n", "\n", "[[5]]\n", "[1] 0.2393106\n", "\n", "[[6]]\n", "[1] 0.1003157\n", "\n", "[[7]]\n", "[1] -0.9766621\n", "\n", "[[8]]\n", "[1] -0.2799746\n", "\n", "[[9]]\n", "[1] 0.2033815\n", "\n", "[[10]]\n", "[1] 0.0364463\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "lapply(1:10, FUN = function(x) mean(rnorm(x)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using `replicate`\n", "\n", "`replicate` is like `rep` but works for a function (such as a random number generator)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
A matrix: 5 × 3 of type dbl
-0.8673179-0.2785431-0.4034675
0.9506517 0.5461152 0.1046594
-0.5850115-1.3038212-0.3188808
0.3209575-0.2509145 1.6183439
-0.2993960 0.1710074 0.7141886
\n" ], "text/latex": [ "A matrix: 5 × 3 of type dbl\n", "\\begin{tabular}{lll}\n", "\t -0.8673179 & -0.2785431 & -0.4034675\\\\\n", "\t 0.9506517 & 0.5461152 & 0.1046594\\\\\n", "\t -0.5850115 & -1.3038212 & -0.3188808\\\\\n", "\t 0.3209575 & -0.2509145 & 1.6183439\\\\\n", "\t -0.2993960 & 0.1710074 & 0.7141886\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A matrix: 5 × 3 of type dbl\n", "\n", "| -0.8673179 | -0.2785431 | -0.4034675 |\n", "| 0.9506517 | 0.5461152 | 0.1046594 |\n", "| -0.5850115 | -1.3038212 | -0.3188808 |\n", "| 0.3209575 | -0.2509145 | 1.6183439 |\n", "| -0.2993960 | 0.1710074 | 0.7141886 |\n", "\n" ], "text/plain": [ " [,1] [,2] [,3] \n", "[1,] -0.8673179 -0.2785431 -0.4034675\n", "[2,] 0.9506517 0.5461152 0.1046594\n", "[3,] -0.5850115 -1.3038212 -0.3188808\n", "[4,] 0.3209575 -0.2509145 1.6183439\n", "[5,] -0.2993960 0.1710074 0.7141886" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "replicate(3, rnorm(5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`replicate` is quite useful for simulations. For example, suppose we want to know the distribution of the sample mean if we sampled 100 numbers from the standard normal distribution 1,000 times." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "n_expts <- 1000\n", "n <- 100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using for loop" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFoCAMAAAC46dgSAAAC+lBMVEUAAAABAQECAgIDAwME\nBAQFBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUW\nFhYXFxcYGBgZGRkaGhobGxscHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQlJSUmJiYnJyco\nKCgpKSkqKiorKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM0NDQ1NTU2NjY3Nzc4ODg5OTk6\nOjo7Ozs8PDw9PT0+Pj4/Pz9AQEBBQUFCQkJDQ0NERERFRUVGRkZHR0dISEhJSUlKSkpLS0tM\nTExNTU1OTk5PT09QUFBRUVFSUlJTU1NVVVVWVlZXV1dYW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PpGTvxQklUwX4GXFWtPV7I\nHBwomUkrHAkkR1aQVZhYu40ZupfQ2Bf32owYRKILfs544Z6kDf6CX9NcZwJJBSqyqjcP9h/R\nfqoHpty97C4quqx9mFFnraArzX/cINpApgJ1WYWLZZA0gsU3D3VM7/ywfvri/fMDR6hoA5kK\nFGYVLjGd5BEMogOCmQPBzIFg5kAwcyCYORDMHAhmDgQzB4KZA8HMgWDmQDBzIJg5EMwcCGYO\nBDMHgpkDwcyBYOZAMHMgmDkQzBwIZg4EMweCmQPBzIFg5kAwcyCYORDMHAhmDgQzB4KZA8HM\ngWDmQDBzklXwbdnnp7VoOb364C2ZeT+pFaKghV5KRUJUP114VVbh0hrVGTpEsgr+XurUkrUj\naEGPhWu+Tb8yC55G0195ZQrdpzpDh0hWwXfqBs+k03ohdtM0k+DapsP1tcem2hp5MvFIXsF/\n1B470zkhztFo8zs4q91xxbk5SvIKPqY9FuTo6zTSLPhJyv7+hiNKk3OS5BX8lfZYkKuvBwsW\n28dmUMqoAyqzcxAINgmuNgQLUbmzJKVb44eRTkgg2Cu4T4a2csAnWOMBGyP9JyQQ7BVcTIeF\neEQTvCvfmDllFn2oND/HgGCv4BU0bMPMUW2Gi4vfypj10sb7Uwc3fgKVhASCvYIvlrXNuvNU\n50FCfFnWPTP720vOKk7QKZJVcNIAwcyBYOZAMHMgmDkQzBwIZg4EMweCmQPBzIFg5kAwcyCY\nORDMHAhmDgQzB4KZA8HMgWDmQDBzIJg5EMwcCGYOBDMHgpkDwcyBYOZAMHMgmDkQzBwIZs7/\nA6e7+7xSj5WPAAAAAElFTkSuQmCC", "text/plain": [ "Plot with title “Histogram of mus”" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "set.seed(123)\n", "mus <- numeric(n_expts)\n", "for (i in 1:n_expts) {\n", " mus[i] <- mean(rnorm(n))\n", "}\n", "hist(mus)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using replicate" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}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0bMPMUW2Gi4vfypj10sb7Uwc3fgKVhASCvYIvlrXNuvNU\n50FCfFnWPTP720vOKk7QKZJVcNIAwcyBYOZAMHMgmDkQzBwIZg4EMweCmQPBzIFg5kAwcyCY\nORDMHAhmDgQzB4KZA8HMgWDmQDBzIJg5EMwcCGYOBDMHgpkDwcyBYOZAMHMgmDkQzBwIZs7/\nA6e7+7xSj5WPAAAAAElFTkSuQmCC", "text/plain": [ "Plot with title “Histogram of mus”" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "set.seed(123)\n", "mus <- replicate(n_expts, mean(rnorm(n)))\n", "hist(mus)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Making a `data.frame` or `tibble` of simulated data\n", "\n", "Let's simulate the following experiment. \n", "\n", "- There are 10 subjects in Group A and 10 subjects in Group B with random PIDs from 10000-99999\n", "- We measure 5 genes in each subject. The genes have the same distribution for each subject, but different genes have different distribtutions:\n", " - gene1 $\\sim N(10, 1)$\n", " - gene2 $\\sim N(11, 2)$\n", " - gene3 $\\sim N(12, 3)$\n", " - gene4 $\\sim N(13, 4)$\n", " - gene5 $\\sim (N(14, 5)$\n", " \n", "NB: This is for illustration purposes! Our gene counts (even if 'normalized') are not normally distributed!" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A matrix: 3 × 5 of type dbl
1.2649930.94679061.076689-1.5034061.069951
3.8307472.43790422.455073 1.4041363.169101
2.9406224.33744903.873871 2.2644721.632268
\n" ], "text/latex": [ "A matrix: 3 × 5 of type dbl\n", "\\begin{tabular}{lllll}\n", "\t 1.264993 & 0.9467906 & 1.076689 & -1.503406 & 1.069951\\\\\n", "\t 3.830747 & 2.4379042 & 2.455073 & 1.404136 & 3.169101\\\\\n", "\t 2.940622 & 4.3374490 & 3.873871 & 2.264472 & 1.632268\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A matrix: 3 × 5 of type dbl\n", "\n", "| 1.264993 | 0.9467906 | 1.076689 | -1.503406 | 1.069951 |\n", "| 3.830747 | 2.4379042 | 2.455073 | 1.404136 | 3.169101 |\n", "| 2.940622 | 4.3374490 | 3.873871 | 2.264472 | 1.632268 |\n", "\n" ], "text/plain": [ " [,1] [,2] [,3] [,4] [,5] \n", "[1,] 1.264993 0.9467906 1.076689 -1.503406 1.069951\n", "[2,] 3.830747 2.4379042 2.455073 1.404136 3.169101\n", "[3,] 2.940622 4.3374490 3.873871 2.264472 1.632268" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "replicate(5, rnorm(3, 1:5, 1))" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "n <- 10\n", "n_genes <- 5\n", "min_pid <- 10000\n", "max_pid <- 99999\n", "groupings <- c('A', 'B')\n", "n_groups <- length(groupings)\n", "gene_mus <- 10:14\n", "gene_sigmas <- 1:5\n", "pad_width <- 3" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "pids <- sample(min_pid:max_pid, n_groups*n)\n", "groups <- sample(rep(groupings, each=n))\n", "genes <- t(replicate(n_groups*n, rnorm(n_genes, gene_mus, gene_sigmas)))\n", "gene_names <- paste('gene', str_pad(1:n_genes, width = pad_width, pad='0'), sep='')\n", "colnames(genes) <- gene_names" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "df <- tibble(\n", " pid = pids,\n", " grp = groups,\n", " genes\n", ")" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
pidgrpgenes
<int><chr><dbl[,5]>
97320A 9.532829, 11.047354, 14.08499, 14.35483, 22.422266
50148B10.046358, 11.926989, 14.43202, 14.20053, 14.613053
86850A 9.535037, 9.418596, 13.01083, 13.11000, 9.554716
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " pid & grp & genes\\\\\n", " & & \\\\\n", "\\hline\n", "\t 97320 & A & 9.532829, 11.047354, 14.08499, 14.35483, 22.422266\\\\\n", "\t 50148 & B & 10.046358, 11.926989, 14.43202, 14.20053, 14.613053\\\\\n", "\t 86850 & A & 9.535037, 9.418596, 13.01083, 13.11000, 9.554716\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| pid <int> | grp <chr> | genes <dbl[,5]> |\n", "|---|---|---|\n", "| 97320 | A | 9.532829, 11.047354, 14.08499, 14.35483, 22.422266 |\n", "| 50148 | B | 10.046358, 11.926989, 14.43202, 14.20053, 14.613053 |\n", "| 86850 | A | 9.535037, 9.418596, 13.01083, 13.11000, 9.554716 |\n", "\n" ], "text/plain": [ " pid grp genes \n", "1 97320 A 9.532829, 11.047354, 14.08499, 14.35483, 22.422266\n", "2 50148 B 10.046358, 11.926989, 14.43202, 14.20053, 14.613053\n", "3 86850 A 9.535037, 9.418596, 13.01083, 13.11000, 9.554716" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sample_n(df, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Breakdown of simulation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set up simulation configration parameters." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "n <- 10\n", "n_genes <- 5\n", "min_pid <- 10000\n", "max_pid <- 99999\n", "groupings <- c('A', 'B')\n", "n_groups <- length(groupings)\n", "gene_mus <- 10:14\n", "gene_sigmas <- 1:5\n", "pad_width <- 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create unique PIDs for each subject" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "pids <- sample(min_pid:max_pid, n_groups*n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assign a group to each subject at random" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "groups <- sample(rep(groupings, each=n))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make up 5 genes from different distributions for each subject" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "genes <- t(replicate(n_groups*n, rnorm(n_genes, gene_mus, gene_sigmas)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make nice names for each gene" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "gene_names <- paste('gene', str_pad(1:n_genes, width = pad_width, pad='0'), sep='')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assign names to gene columns" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "colnames(genes) <- gene_names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create `tibble` to store simulated data" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "df <- tibble(\n", " pid = pids,\n", " grp = groups,\n", " genes\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peek into `tibble`" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
pidgrpgenes
<int><chr><dbl[,5]>
48611A11.118810, 10.281882, 9.460913, 21.654971, 12.769903
82193B 9.250903, 12.410494, 10.502977, 8.569185, 15.181737
66967A11.680328, 9.572095, 13.354410, 9.423921, 6.070365
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " pid & grp & genes\\\\\n", " & & \\\\\n", "\\hline\n", "\t 48611 & A & 11.118810, 10.281882, 9.460913, 21.654971, 12.769903\\\\\n", "\t 82193 & B & 9.250903, 12.410494, 10.502977, 8.569185, 15.181737\\\\\n", "\t 66967 & A & 11.680328, 9.572095, 13.354410, 9.423921, 6.070365\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| pid <int> | grp <chr> | genes <dbl[,5]> |\n", "|---|---|---|\n", "| 48611 | A | 11.118810, 10.281882, 9.460913, 21.654971, 12.769903 |\n", "| 82193 | B | 9.250903, 12.410494, 10.502977, 8.569185, 15.181737 |\n", "| 66967 | A | 11.680328, 9.572095, 13.354410, 9.423921, 6.070365 |\n", "\n" ], "text/plain": [ " pid grp genes \n", "1 48611 A 11.118810, 10.281882, 9.460913, 21.654971, 12.769903\n", "2 82193 B 9.250903, 12.410494, 10.502977, 8.569185, 15.181737\n", "3 66967 A 11.680328, 9.572095, 13.354410, 9.423921, 6.070365" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sample_n(df, 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }