{
"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
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 5
\n",
"\t- 8
\n",
"\t- 13
\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
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 4
\n",
"\t- 5
\n",
"\t- 6
\n",
"\t- 7
\n",
"\t- 8
\n",
"\t- 9
\n",
"\t- 10
\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- 10
\n",
"\t- 9
\n",
"\t- 8
\n",
"\t- 7
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 3
\n",
"\t- 2
\n",
"\t- 1
\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
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 4
\n",
"\t- 5
\n",
"\t- 6
\n",
"\t- 7
\n",
"\t- 8
\n",
"\t- 9
\n",
"\t- 10
\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
\n",
"\t- 3
\n",
"\t- 5
\n",
"\t- 7
\n",
"\t- 9
\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- 10
\n",
"\t- 9
\n",
"\t- 8
\n",
"\t- 7
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 3
\n",
"\t- 2
\n",
"\t- 1
\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- 0
\n",
"\t- 0.1
\n",
"\t- 0.2
\n",
"\t- 0.3
\n",
"\t- 0.4
\n",
"\t- 0.5
\n",
"\t- 0.6
\n",
"\t- 0.7
\n",
"\t- 0.8
\n",
"\t- 0.9
\n",
"\t- 1
\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- 3
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 3
\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
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 3
\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
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 3
\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
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 1
\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- 'H'
\n",
"\t- 'H'
\n",
"\t- 'H'
\n",
"\t- 'H'
\n",
"\t- 'T'
\n",
"\t- 'T'
\n",
"\t- 'T'
\n",
"\t- 'T'
\n",
"\t- 'H'
\n",
"\t- 'T'
\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- 'T'
\n",
"\t- 'H'
\n",
"\t- 'H'
\n",
"\t- 'T'
\n",
"\t- 'T'
\n",
"\t- 'H'
\n",
"\t- 'H'
\n",
"\t- 'T'
\n",
"\t- 'T'
\n",
"\t- 'T'
\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- 3
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 3
\n",
"\t- 4
\n",
"\t- 5
\n",
"\t- 5
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 2
\n",
"\t- 4
\n",
"\t- 3
\n",
"\t- 2
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 6
\n",
"\t- 3
\n",
"\t- 6
\n",
"\t- 2
\n",
"\t- 4
\n",
"\t- 4
\n",
"\t- 6
\n",
"\t- 2
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 5
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 6
\n",
"\t- 6
\n",
"\t- 2
\n",
"\t- 4
\n",
"\t- 3
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 2
\n",
"\t- 6
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 5
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 4
\n",
"\t- 5
\n",
"\t- 2
\n",
"\t- 1
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 4
\n",
"\t- 1
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 5
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 6
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 5
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 1
\n",
"\t- 4
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 5
\n",
"\t- 5
\n",
"\t- 6
\n",
"\t- 3
\n",
"\t- 6
\n",
"\t- 4
\n",
"\t- 5
\n",
"\t- 1
\n",
"\t- 4
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 5
\n",
"\t- 6
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 5
\n",
"\t- 6
\n",
"\t- 1
\n",
"\t- 5
\n",
"\t- 6
\n",
"\t- 5
\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- 'B'
\n",
"\t- 'A'
\n",
"\t- 'B'
\n",
"\t- 'B'
\n",
"\t- 'B'
\n",
"\t- 'B'
\n",
"\t- 'A'
\n",
"\t- 'A'
\n",
"\t- 'A'
\n",
"\t- 'B'
\n",
"\t- 'A'
\n",
"\t- 'B'
\n",
"\t- 'B'
\n",
"\t- 'A'
\n",
"\t- 'A'
\n",
"\t- 'A'
\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- TRUE
\n",
"\t- TRUE
\n",
"\t- FALSE
\n",
"\t- TRUE
\n",
"\t- FALSE
\n",
"\t- FALSE
\n",
"\t- FALSE
\n",
"\t- FALSE
\n",
"\t- FALSE
\n",
"\t- TRUE
\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- 0
\n",
"\t- 0
\n",
"\t- 0
\n",
"\t- 0
\n",
"\t- 0
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 1
\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- 4
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 4
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 2
\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- 4
\n",
"\t- 0
\n",
"\t- 9
\n",
"\t- 4
\n",
"\t- 2
\n",
"\t- 4
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 5
\n",
"\t- 4
\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- 5
\n",
"\t- 2
\n",
"\t- 1
\n",
"\t- 0
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 0
\n",
"\t- 9
\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- 2
\n",
"\t- 0
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 4
\n",
"\t- 3
\n",
"\t- 7
\n",
"\t- 4
\n",
"\t- 7
\n",
"\t- 10
\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- 0.159469854552299
\n",
"\t- 0.149578995071352
\n",
"\t- 0.499272880377248
\n",
"\t- 0.940564878052101
\n",
"\t- 0.334231326589361
\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- 91.8843432958238
\n",
"\t- 92.6971617829986
\n",
"\t- 95.3074407810345
\n",
"\t- 90.2145022852346
\n",
"\t- 97.987603074871
\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.22474795035999
\n",
"\t- 0.179516441117938
\n",
"\t- 0.567620594423535
\n",
"\t- -0.492877353553475
\n",
"\t- 6.28840653511241e-05
\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- 0.102869685091412
\n",
"\t- 0.0394963601049801
\n",
"\t- 0.0587241371393688
\n",
"\t- -0.208039597462606
\n",
"\t- 0.175088960315561
\n",
"\t- -0.0224984001571429
\n",
"\t- 0.157365195356717
\n",
"\t- -0.11380999366832
\n",
"\t- -0.151701911199645
\n",
"\t- 0.0806058319328881
\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- 0.152764106747895
\n",
"\t\n",
"\t- 0.988596845178799
\n",
"\t- -0.073458334672174
\n",
"
\n",
" \n",
"\t\n",
"\t- -1.38702655355974
\n",
"\t- -1.30667590437536
\n",
"\t- -0.768395325077939
\n",
"
\n",
" \n",
"\t\n",
"\t- -0.527108125374458
\n",
"\t- -0.0214270650416246
\n",
"\t- 0.670498070996282
\n",
"\t- -0.434617038572407
\n",
"
\n",
" \n",
"\t\n",
"\t- -1.11387978331285
\n",
"\t- 0.6071059948785
\n",
"\t- 0.275456968739591
\n",
"\t- 1.15734706941474
\n",
"\t- -1.68248085947826
\n",
"
\n",
" \n",
"\t\n",
"\t- 0.0873190888948744
\n",
"\t- 1.35336189394267
\n",
"\t- 0.724173800683369
\n",
"\t- -0.832552825764212
\n",
"\t- 0.732528486768414
\n",
"\t- -0.871926869997213
\n",
"
\n",
" \n",
"\t\n",
"\t- -0.453397511229793
\n",
"\t- 1.18753427862908
\n",
"\t- -0.290145311790079
\n",
"\t- 0.828546144965436
\n",
"\t- -0.291227708779169
\n",
"\t- -1.57636240474791
\n",
"\t- -0.848815696948922
\n",
"
\n",
" \n",
"\t\n",
"\t- -1.0885198619572
\n",
"\t- -0.484290571117173
\n",
"\t- -0.336311209009573
\n",
"\t- -0.153357890661644
\n",
"\t- -0.243247228574846
\n",
"\t- 1.89220204160011
\n",
"\t- -1.38599833728542
\n",
"\t- -0.41482430070778
\n",
"
\n",
" \n",
"\t\n",
"\t- 0.349081528106831
\n",
"\t- 1.62844226581541
\n",
"\t- 0.0885218957136163
\n",
"\t- 1.23915070840056
\n",
"\t- -1.64455553575371
\n",
"\t- 1.44635652537848
\n",
"\t- -0.690560171665183
\n",
"\t- -0.276431085453852
\n",
"\t- -1.10941875989944
\n",
"
\n",
" \n",
"\t\n",
"\t- 0.133869316429452
\n",
"\t- 1.78533905173155
\n",
"\t- 2.42216335525159
\n",
"\t- -1.07682890211841
\n",
"\t- 0.485941110403099
\n",
"\t- 1.38852173874429
\n",
"\t- -0.195656817284544
\n",
"\t- -0.218174797706211
\n",
"\t- -0.304777954546445
\n",
"\t- 0.597832724111443
\n",
"
\n",
" \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.39742941081576
\n",
"\t- 0.503903887768466
\n",
"\t- -0.117686052712023
\n",
"\t- 0.375071580419853
\n",
"\t- 0.239310604458899
\n",
"\t- 0.100315651769656
\n",
"\t- -0.976662058729188
\n",
"\t- -0.279974621278527
\n",
"\t- 0.203381481503197
\n",
"\t- 0.0364462992139071
\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",
"A matrix: 5 × 3 of type dbl\n",
"\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",
"\n",
"
\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\nTExNTU1OTk5PT09QUFBRUVFSUlJTU1NVVVVWVlZXV1dYWFhZWVlaWlpbW1tcXFxdXV1eXl5f\nX19gYGBhYWFiYmJjY2NkZGRlZWVmZmZnZ2doaGhpaWlqampra2tsbGxtbW1ubm5vb29wcHBx\ncXFycnJzc3N0dHR1dXV2dnZ3d3d4eHh5eXl6enp7e3t8fHx9fX1+fn5/f3+AgICBgYGCgoKD\ng4OEhISFhYWGhoaHh4eIiIiJiYmKioqLi4uMjIyNjY2Ojo6Pj4+QkJCRkZGSkpKTk5OUlJSV\nlZWWlpaXl5eYmJiZmZmampqbm5ucnJydnZ2enp6fn5+goKChoaGioqKjo6OkpKSlpaWmpqan\np6eoqKipqamqqqqrq6usrKytra2urq6vr6+wsLCxsbGysrKzs7O0tLS1tbW2tra3t7e4uLi5\nubm6urq7u7u9vb2+vr6/v7/AwMDBwcHCwsLDw8PExMTFxcXGxsbHx8fIyMjJycnKysrLy8vM\nzMzNzc3Ozs7Pz8/Q0NDR0dHS0tLT09PU1NTV1dXW1tb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3OS5BX8lfZYkKuvBwsW\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": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFoCAMAAAC46dgSAAAC+lBMVEUAAAABAQECAgIDAwME\nBAQFBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUW\nFhYXFxcYGBgZGRkaGhobGxscHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQlJSUmJiYnJyco\nKCgpKSkqKiorKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM0NDQ1NTU2NjY3Nzc4ODg5OTk6\nOjo7Ozs8PDw9PT0+Pj4/Pz9AQEBBQUFCQkJDQ0NERERFRUVGRkZHR0dISEhJSUlKSkpLS0tM\nTExNTU1OTk5PT09QUFBRUVFSUlJTU1NVVVVWVlZXV1dYWFhZWVlaWlpbW1tcXFxdXV1eXl5f\nX19gYGBhYWFiYmJjY2NkZGRlZWV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owYRKILfs544Z6kDf6CX9NcZwJJBSqyqjcP9h/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 <- 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",
"A matrix: 3 × 5 of type dbl\n",
"\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",
"\n",
"
\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",
"A tibble: 3 × 3\n",
"\n",
"\t| pid | grp | genes |
\n",
"\t| <int> | <chr> | <dbl[,5]> |
\n",
"\n",
"\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",
"\n",
"
\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",
"A tibble: 3 × 3\n",
"\n",
"\t| pid | grp | genes |
\n",
"\t| <int> | <chr> | <dbl[,5]> |
\n",
"\n",
"\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",
"\n",
"
\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
}