{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simulations and Statistical Inference"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──\n",
"✔ ggplot2 2.2.1 ✔ purrr 0.2.5\n",
"✔ tibble 1.4.2 ✔ dplyr 0.7.5\n",
"✔ tidyr 0.8.1 ✔ stringr 1.3.1\n",
"✔ readr 1.1.1 ✔ forcats 0.3.0\n",
"── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──\n",
"✖ dplyr::filter() masks stats::filter()\n",
"✖ dplyr::lag() masks stats::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 reproucibility"
]
},
{
"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- 'T'
\n",
"\t- 'T'
\n",
"\t- 'H'
\n",
"\t- 'T'
\n",
"\t- 'T'
\n",
"\t- 'T'
\n",
"\t- 'T'
\n",
"\t- 'H'
\n",
"\t- 'T'
\n",
"\t- 'T'
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 'T'\n",
"\\item 'T'\n",
"\\item 'H'\n",
"\\item 'T'\n",
"\\item 'T'\n",
"\\item 'T'\n",
"\\item 'T'\n",
"\\item 'H'\n",
"\\item 'T'\n",
"\\item 'T'\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 'T'\n",
"2. 'T'\n",
"3. 'H'\n",
"4. 'T'\n",
"5. 'T'\n",
"6. 'T'\n",
"7. 'T'\n",
"8. 'H'\n",
"9. 'T'\n",
"10. 'T'\n",
"\n",
"\n"
],
"text/plain": [
" [1] \"T\" \"T\" \"H\" \"T\" \"T\" \"T\" \"T\" \"H\" \"T\" \"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",
"2 8 "
]
},
"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- 6
\n",
"\t- 1
\n",
"\t- 6
\n",
"\t- 6
\n",
"\t- 1
\n",
"\t- 4
\n",
"\t- 3
\n",
"\t- 6
\n",
"\t- 3
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 5
\n",
"\t- 3
\n",
"\t- 5
\n",
"\t- 1
\n",
"\t- 5
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 6
\n",
"\t- 4
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 6
\n",
"\t- 3
\n",
"\t- 6
\n",
"\t- 6
\n",
"\t- 4
\n",
"\t- 6
\n",
"\t- 4
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 5
\n",
"\t- 1
\n",
"\t- 5
\n",
"\t- 5
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 4
\n",
"\t- 5
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 6
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 5
\n",
"\t- 5
\n",
"\t- 2
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 2
\n",
"\t- 5
\n",
"\t- 1
\n",
"\t- 3
\n",
"\t- 4
\n",
"\t- 1
\n",
"\t- 4
\n",
"\t- 1
\n",
"\t- 3
\n",
"\t- 4
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 2
\n",
"\t- 1
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 5
\n",
"\t- 1
\n",
"\t- 2
\n",
"\t- 6
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 2
\n",
"\t- 4
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 4
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 6
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 5
\n",
"\t- 4
\n",
"\t- 1
\n",
"\t- 4
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 3
\n",
"\t- 4
\n",
"\t- 4
\n",
"\t- 1
\n",
"\t- 3
\n",
"\t- 4
\n",
"\t- 5
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 6\n",
"\\item 1\n",
"\\item 6\n",
"\\item 6\n",
"\\item 1\n",
"\\item 4\n",
"\\item 3\n",
"\\item 6\n",
"\\item 3\n",
"\\item 6\n",
"\\item 5\n",
"\\item 5\n",
"\\item 3\n",
"\\item 5\n",
"\\item 1\n",
"\\item 5\n",
"\\item 1\n",
"\\item 2\n",
"\\item 6\n",
"\\item 4\n",
"\\item 3\n",
"\\item 3\n",
"\\item 1\n",
"\\item 6\n",
"\\item 3\n",
"\\item 6\n",
"\\item 6\n",
"\\item 4\n",
"\\item 6\n",
"\\item 4\n",
"\\item 3\n",
"\\item 3\n",
"\\item 3\n",
"\\item 5\n",
"\\item 1\n",
"\\item 5\n",
"\\item 5\n",
"\\item 2\n",
"\\item 2\n",
"\\item 4\n",
"\\item 5\n",
"\\item 6\n",
"\\item 5\n",
"\\item 4\n",
"\\item 6\n",
"\\item 2\n",
"\\item 2\n",
"\\item 5\n",
"\\item 5\n",
"\\item 2\n",
"\\item 1\n",
"\\item 1\n",
"\\item 2\n",
"\\item 3\n",
"\\item 2\n",
"\\item 5\n",
"\\item 1\n",
"\\item 3\n",
"\\item 4\n",
"\\item 1\n",
"\\item 4\n",
"\\item 1\n",
"\\item 3\n",
"\\item 4\n",
"\\item 5\n",
"\\item 4\n",
"\\item 2\n",
"\\item 1\n",
"\\item 1\n",
"\\item 2\n",
"\\item 5\n",
"\\item 1\n",
"\\item 2\n",
"\\item 6\n",
"\\item 6\n",
"\\item 5\n",
"\\item 2\n",
"\\item 4\n",
"\\item 5\n",
"\\item 4\n",
"\\item 4\n",
"\\item 2\n",
"\\item 2\n",
"\\item 3\n",
"\\item 6\n",
"\\item 6\n",
"\\item 5\n",
"\\item 5\n",
"\\item 4\n",
"\\item 1\n",
"\\item 4\n",
"\\item 6\n",
"\\item 5\n",
"\\item 3\n",
"\\item 4\n",
"\\item 4\n",
"\\item 1\n",
"\\item 3\n",
"\\item 4\n",
"\\item 5\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 6\n",
"2. 1\n",
"3. 6\n",
"4. 6\n",
"5. 1\n",
"6. 4\n",
"7. 3\n",
"8. 6\n",
"9. 3\n",
"10. 6\n",
"11. 5\n",
"12. 5\n",
"13. 3\n",
"14. 5\n",
"15. 1\n",
"16. 5\n",
"17. 1\n",
"18. 2\n",
"19. 6\n",
"20. 4\n",
"21. 3\n",
"22. 3\n",
"23. 1\n",
"24. 6\n",
"25. 3\n",
"26. 6\n",
"27. 6\n",
"28. 4\n",
"29. 6\n",
"30. 4\n",
"31. 3\n",
"32. 3\n",
"33. 3\n",
"34. 5\n",
"35. 1\n",
"36. 5\n",
"37. 5\n",
"38. 2\n",
"39. 2\n",
"40. 4\n",
"41. 5\n",
"42. 6\n",
"43. 5\n",
"44. 4\n",
"45. 6\n",
"46. 2\n",
"47. 2\n",
"48. 5\n",
"49. 5\n",
"50. 2\n",
"51. 1\n",
"52. 1\n",
"53. 2\n",
"54. 3\n",
"55. 2\n",
"56. 5\n",
"57. 1\n",
"58. 3\n",
"59. 4\n",
"60. 1\n",
"61. 4\n",
"62. 1\n",
"63. 3\n",
"64. 4\n",
"65. 5\n",
"66. 4\n",
"67. 2\n",
"68. 1\n",
"69. 1\n",
"70. 2\n",
"71. 5\n",
"72. 1\n",
"73. 2\n",
"74. 6\n",
"75. 6\n",
"76. 5\n",
"77. 2\n",
"78. 4\n",
"79. 5\n",
"80. 4\n",
"81. 4\n",
"82. 2\n",
"83. 2\n",
"84. 3\n",
"85. 6\n",
"86. 6\n",
"87. 5\n",
"88. 5\n",
"89. 4\n",
"90. 1\n",
"91. 4\n",
"92. 6\n",
"93. 5\n",
"94. 3\n",
"95. 4\n",
"96. 4\n",
"97. 1\n",
"98. 3\n",
"99. 4\n",
"100. 5\n",
"\n",
"\n"
],
"text/plain": [
" [1] 6 1 6 6 1 4 3 6 3 6 5 5 3 5 1 5 1 2 6 4 3 3 1 6 3 6 6 4 6 4 3 3 3 5 1 5 5\n",
" [38] 2 2 4 5 6 5 4 6 2 2 5 5 2 1 1 2 3 2 5 1 3 4 1 4 1 3 4 5 4 2 1 1 2 5 1 2 6\n",
" [75] 6 5 2 4 5 4 4 2 2 3 6 6 5 5 4 1 4 6 5 3 4 4 1 3 4 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",
"16 14 15 18 20 17 "
]
},
"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- 'A'
\n",
"\t- 'A'
\n",
"\t- 'B'
\n",
"\t- 'A'
\n",
"\t- 'B'
\n",
"\t- 'A'
\n",
"\t- 'B'
\n",
"\t- 'B'
\n",
"\t- 'A'
\n",
"\t- 'B'
\n",
"\t- 'B'
\n",
"\t- 'A'
\n",
"\t- 'A'
\n",
"\t- 'B'
\n",
"\t- 'B'
\n",
"\t- 'A'
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 'A'\n",
"\\item 'A'\n",
"\\item 'B'\n",
"\\item 'A'\n",
"\\item 'B'\n",
"\\item 'A'\n",
"\\item 'B'\n",
"\\item 'B'\n",
"\\item 'A'\n",
"\\item 'B'\n",
"\\item 'B'\n",
"\\item 'A'\n",
"\\item 'A'\n",
"\\item 'B'\n",
"\\item 'B'\n",
"\\item 'A'\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 'A'\n",
"2. 'A'\n",
"3. 'B'\n",
"4. 'A'\n",
"5. 'B'\n",
"6. 'A'\n",
"7. 'B'\n",
"8. 'B'\n",
"9. 'A'\n",
"10. 'B'\n",
"11. 'B'\n",
"12. 'A'\n",
"13. 'A'\n",
"14. 'B'\n",
"15. 'B'\n",
"16. 'A'\n",
"\n",
"\n"
],
"text/plain": [
" [1] \"A\" \"A\" \"B\" \"A\" \"B\" \"A\" \"B\" \"B\" \"A\" \"B\" \"B\" \"A\" \"A\" \"B\" \"B\" \"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": [
"### Disscrete distributionns"
]
},
{
"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- FALSE
\n",
"\t- TRUE
\n",
"\t- TRUE
\n",
"\t- FALSE
\n",
"\t- FALSE
\n",
"\t- FALSE
\n",
"\t- TRUE
\n",
"\t- TRUE
\n",
"\t- TRUE
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item TRUE\n",
"\\item FALSE\n",
"\\item TRUE\n",
"\\item TRUE\n",
"\\item FALSE\n",
"\\item FALSE\n",
"\\item FALSE\n",
"\\item TRUE\n",
"\\item TRUE\n",
"\\item TRUE\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. TRUE\n",
"2. FALSE\n",
"3. TRUE\n",
"4. TRUE\n",
"5. FALSE\n",
"6. FALSE\n",
"7. FALSE\n",
"8. TRUE\n",
"9. TRUE\n",
"10. TRUE\n",
"\n",
"\n"
],
"text/plain": [
" [1] TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE TRUE 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- 1
\n",
"\t- 0
\n",
"\t- 0
\n",
"\t- 1
\n",
"\t- 0
\n",
"\t- 1
\n",
"\t- 0
\n",
"\t- 1
\n",
"\t- 1
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 0\n",
"\\item 1\n",
"\\item 0\n",
"\\item 0\n",
"\\item 1\n",
"\\item 0\n",
"\\item 1\n",
"\\item 0\n",
"\\item 1\n",
"\\item 1\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 0\n",
"2. 1\n",
"3. 0\n",
"4. 0\n",
"5. 1\n",
"6. 0\n",
"7. 1\n",
"8. 0\n",
"9. 1\n",
"10. 1\n",
"\n",
"\n"
],
"text/plain": [
" [1] 0 1 0 0 1 0 1 0 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- 2
\n",
"\t- 0
\n",
"\t- 1
\n",
"\t- 3
\n",
"\t- 4
\n",
"\t- 3
\n",
"\t- 3
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 1
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 2\n",
"\\item 0\n",
"\\item 1\n",
"\\item 3\n",
"\\item 4\n",
"\\item 3\n",
"\\item 3\n",
"\\item 2\n",
"\\item 3\n",
"\\item 1\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 2\n",
"2. 0\n",
"3. 1\n",
"4. 3\n",
"5. 4\n",
"6. 3\n",
"7. 3\n",
"8. 2\n",
"9. 3\n",
"10. 1\n",
"\n",
"\n"
],
"text/plain": [
" [1] 2 0 1 3 4 3 3 2 3 1"
]
},
"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- 2
\n",
"\t- 5
\n",
"\t- 6
\n",
"\t- 7
\n",
"\t- 6
\n",
"\t- 6
\n",
"\t- 5
\n",
"\t- 1
\n",
"\t- 0
\n",
"\t- 9
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 2\n",
"\\item 5\n",
"\\item 6\n",
"\\item 7\n",
"\\item 6\n",
"\\item 6\n",
"\\item 5\n",
"\\item 1\n",
"\\item 0\n",
"\\item 9\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 2\n",
"2. 5\n",
"3. 6\n",
"4. 7\n",
"5. 6\n",
"6. 6\n",
"7. 5\n",
"8. 1\n",
"9. 0\n",
"10. 9\n",
"\n",
"\n"
],
"text/plain": [
" [1] 2 5 6 7 6 6 5 1 0 9"
]
},
"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- 3
\n",
"\t- 5
\n",
"\t- 3
\n",
"\t- 1
\n",
"\t- 3
\n",
"\t- 7
\n",
"\t- 3
\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 3
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 3\n",
"\\item 5\n",
"\\item 3\n",
"\\item 1\n",
"\\item 3\n",
"\\item 7\n",
"\\item 3\n",
"\\item 2\n",
"\\item 2\n",
"\\item 3\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 3\n",
"2. 5\n",
"3. 3\n",
"4. 1\n",
"5. 3\n",
"6. 7\n",
"7. 3\n",
"8. 2\n",
"9. 2\n",
"10. 3\n",
"\n",
"\n"
],
"text/plain": [
" [1] 3 5 3 1 3 7 3 2 2 3"
]
},
"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 distributios."
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 2
\n",
"\t- 2
\n",
"\t- 3
\n",
"\t- 5
\n",
"\t- 6
\n",
"\t- 3
\n",
"\t- 6
\n",
"\t- 8
\n",
"\t- 9
\n",
"\t- 9
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 2\n",
"\\item 2\n",
"\\item 3\n",
"\\item 5\n",
"\\item 6\n",
"\\item 3\n",
"\\item 6\n",
"\\item 8\n",
"\\item 9\n",
"\\item 9\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 2\n",
"2. 2\n",
"3. 3\n",
"4. 5\n",
"5. 6\n",
"6. 3\n",
"7. 6\n",
"8. 8\n",
"9. 9\n",
"10. 9\n",
"\n",
"\n"
],
"text/plain": [
" [1] 2 2 3 5 6 3 6 8 9 9"
]
},
"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 stnadard uniform distribution."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 0.649875837611035
\n",
"\t- 0.336419132305309
\n",
"\t- 0.0609497462864965
\n",
"\t- 0.451310850214213
\n",
"\t- 0.838755033444613
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 0.649875837611035\n",
"\\item 0.336419132305309\n",
"\\item 0.0609497462864965\n",
"\\item 0.451310850214213\n",
"\\item 0.838755033444613\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 0.649875837611035\n",
"2. 0.336419132305309\n",
"3. 0.0609497462864965\n",
"4. 0.451310850214213\n",
"5. 0.838755033444613\n",
"\n",
"\n"
],
"text/plain": [
"[1] 0.64987584 0.33641913 0.06094975 0.45131085 0.83875503"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"runif(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sampling form a uniform distribuiotn,"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 95.7463733432814
\n",
"\t- 93.5335037740879
\n",
"\t- 95.4742607823573
\n",
"\t- 98.9271859382279
\n",
"\t- 94.8999057058245
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 95.7463733432814\n",
"\\item 93.5335037740879\n",
"\\item 95.4742607823573\n",
"\\item 98.9271859382279\n",
"\\item 94.8999057058245\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 95.7463733432814\n",
"2. 93.5335037740879\n",
"3. 95.4742607823573\n",
"4. 98.9271859382279\n",
"5. 94.8999057058245\n",
"\n",
"\n"
],
"text/plain": [
"[1] 95.74637 93.53350 95.47426 98.92719 94.89991"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"runif(5, 90, 100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sampling from a stnadard normal distribution."
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- -0.94773529068477
\n",
"\t- 1.76797780755595
\n",
"\t- 0.917327777548805
\n",
"\t- -0.894775408854386
\n",
"\t- -0.688165944958397
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item -0.94773529068477\n",
"\\item 1.76797780755595\n",
"\\item 0.917327777548805\n",
"\\item -0.894775408854386\n",
"\\item -0.688165944958397\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. -0.94773529068477\n",
"2. 1.76797780755595\n",
"3. 0.917327777548805\n",
"4. -0.894775408854386\n",
"5. -0.688165944958397\n",
"\n",
"\n"
],
"text/plain": [
"[1] -0.9477353 1.7679778 0.9173278 -0.8947754 -0.6881659"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rnorm(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Looping"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] 0.2660734\n",
"[1] -0.09742615\n",
"[1] -0.3068994\n",
"[1] 0.2910631\n",
"[1] 0.1062726\n",
"[1] 0.05955272\n",
"[1] 0.4080255\n",
"[1] -0.1243275\n",
"[1] -0.779981\n",
"[1] -0.187303\n"
]
}
],
"source": [
"for (i in 1:10) {\n",
" print(mean(rnorm(10)))\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Saving variabels generated in a loop"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- -0.25116755575127
\n",
"\t- 0.708916638976887
\n",
"\t- 0.461551515258198
\n",
"\t- -0.475557761331935
\n",
"\t- -0.0598841272831985
\n",
"\t- 0.272569234838853
\n",
"\t- 0.0543736652184016
\n",
"\t- 0.135002662203851
\n",
"\t- -0.190719191248592
\n",
"\t- 0.0387989282162199
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item -0.25116755575127\n",
"\\item 0.708916638976887\n",
"\\item 0.461551515258198\n",
"\\item -0.475557761331935\n",
"\\item -0.0598841272831985\n",
"\\item 0.272569234838853\n",
"\\item 0.0543736652184016\n",
"\\item 0.135002662203851\n",
"\\item -0.190719191248592\n",
"\\item 0.0387989282162199\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. -0.25116755575127\n",
"2. 0.708916638976887\n",
"3. 0.461551515258198\n",
"4. -0.475557761331935\n",
"5. -0.0598841272831985\n",
"6. 0.272569234838853\n",
"7. 0.0543736652184016\n",
"8. 0.135002662203851\n",
"9. -0.190719191248592\n",
"10. 0.0387989282162199\n",
"\n",
"\n"
],
"text/plain": [
" [1] -0.25116756 0.70891664 0.46155152 -0.47555776 -0.05988413 0.27256923\n",
" [7] 0.05437367 0.13500266 -0.19071919 0.03879893"
]
},
"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": "markdown",
"metadata": {},
"source": [
"## Using `replicate`\n",
"\n",
"`replicate` is like `rep` but works for a function (such as a random number geenerator)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\t-0.9984839 | 0.15461594 | -0.88368060 |
\n",
"\t-2.1072414 | -0.80160087 | 0.07714041 |
\n",
"\t 0.8574190 | -0.04518119 | 0.83720136 |
\n",
"\t 1.1260757 | -1.03824075 | 0.09992556 |
\n",
"\t-0.1983684 | 1.55953311 | 0.30272834 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{lll}\n",
"\t -0.9984839 & 0.15461594 & -0.88368060\\\\\n",
"\t -2.1072414 & -0.80160087 & 0.07714041\\\\\n",
"\t 0.8574190 & -0.04518119 & 0.83720136\\\\\n",
"\t 1.1260757 & -1.03824075 & 0.09992556\\\\\n",
"\t -0.1983684 & 1.55953311 & 0.30272834\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| -0.9984839 | 0.15461594 | -0.88368060 | \n",
"| -2.1072414 | -0.80160087 | 0.07714041 | \n",
"| 0.8574190 | -0.04518119 | 0.83720136 | \n",
"| 1.1260757 | -1.03824075 | 0.09992556 | \n",
"| -0.1983684 | 1.55953311 | 0.30272834 | \n",
"\n",
"\n"
],
"text/plain": [
" [,1] [,2] [,3] \n",
"[1,] -0.9984839 0.15461594 -0.88368060\n",
"[2,] -2.1072414 -0.80160087 0.07714041\n",
"[3,] 0.8574190 -0.04518119 0.83720136\n",
"[4,] 1.1260757 -1.03824075 0.09992556\n",
"[5,] -0.1983684 1.55953311 0.30272834"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"replicate(3, rnorm(5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`replicate` is quite useful for simulations. For exampe, 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": null,
"metadata": {},
"outputs": [],
"source": [
"n_expts <- 1000\n",
"n <- 100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using for loop"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFoCAMAAAC46dgSAAAC8VBMVEUAAAABAQECAgIDAwME\nBAQFBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUW\nFhYXFxcYGBgZGRkaGhobGxscHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQlJSUmJiYnJyco\nKCgpKSkqKiorKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM0NDQ1NTU2NjY3Nzc4ODg5OTk6\nOjo7Ozs8PDw9PT0+Pj4/Pz9AQEBBQUFCQkJDQ0NERERFR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0p9I6bwHDSXbBt9L/ise/0dSaglK6uTxKffeB\npAIdWUWI9dY8k0E0ft4ujxHDSHbB22mGeLzNHAa4cPykYVT3bHZSSSB7gb6sIiVmkTKH6Opx\nNOrR62iCePo2DTDv1mw9OkA9Z9h2BrIX6MsqUmIWKSPYqHisqHmRNRRv/fnbQ2eor2ILZC/Q\nmFWkxExSRzCIDQhmDgQzB4KZA8HMgWDmQDBzIJg5EMwcCGYOBDMHgpkDwcyBYOZAMHMgmDkQ\nzBwIZg4EMweCmQPBzIFg5kAwcyCYORDMHAhmDgQzB4KZA8HMgWDmQDBzIJg5EMwcCGYOBDMH\ngpkDwcxJUcHzWpyb36vtpKNn7s5v9r2PRcHEpmbxOWvW5pcHtWg94k29CSojVQU3Hbfk9yvT\nBxQ+tPvpht0uhgn+CbUvmdE84z3dOaohVQXTo+LxRrpXPE6ng2GC23WvMIzdNFtvhqpIWcF7\nxeNCMqeDfJL22AVfyMivMozqPQf0ZqiKlBVszjm42JoHdHm4YGMc9VrxQZXe/NSRsoLN+dUX\n0z+MMMEVpuBv57ckan1f/SeCT0og2C74y8DaJ5U7Hu9N19R/LYdkBIKDghuZPjcLwQeX7zRr\njKDDWjNUBQQHBN9pztd8ukAIPkCDxQm4anCjCt1JKgGCA4I3U/MHFnb/p9w7jOqxNGDBbTn0\nsO4c1QDBxsocczGN0iuzOsw/M2eVYZx8pEeT1kXr679WR1KSooJTBwhmDgQzB4KZA8HMgWDm\nQDBzIJg5EMwcCGYOBDMHgpkDwcyBYOZAMHMgmDkQzBwIZg4EMweCmQPBzIFg5kAwcyCYORDM\nHAhmDgQzB4KZA8HMgWDmQDBz/h/V/QFevxwJCgAAAABJRU5ErkJggg==",
"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": 89,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFoCAMAAAC46dgSAAAC8VBMVEUAAAABAQECAgIDAwME\nBAQFBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUW\nFhYXFxcYGBgZGRkaGhobGxscHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQlJSUmJiYnJyco\nKCgpKSkqKiorKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM0NDQ1NTU2NjY3Nzc4ODg5OTk6\nOjo7Ozs8PDw9PT0+Pj4/Pz9AQEBBQUFCQkJDQ0NERERFRUVGRkZHR0dISEhJSUlKSkpLS0tM\nTExNTU1OTk5PT09QUFBRUVFSUlJTU1NUVFRVVVVWVlZXV1dYWFhZWVlaWlpbW1tcXFxdXV1e\nXl5fX19gYGBhYWFiYmJjY2NkZGRlZWVmZmZnZ2doaGhpaWlqampra2tsbGxtbW1ubm5vb29w\ncHBxcXFycnJzc3N0dHR1dXV2dnZ3d3d4eHh5eXl6enp7e3t8fHx9fX1+fn5/f3+AgICBgYGC\ngoKDg4OEhISFhYWHh4eIiIiJiYmKioqLi4uMjIyNjY2Ojo6Pj4+QkJCRkZGSkpKTk5OUlJSV\nlZWWlpaXl5eYmJiZmZmbm5ucnJydnZ2enp6fn5+goKChoaGioqKjo6OkpKSlpaWmpqanp6eo\nqKipqamqqqqrq6usrKytra2urq6vr6+wsLCxsbGysrK0tLS1tbW3t7e4uLi5ubm6urq7u7u8\nvLy9vb2+vr6/v7/AwMDBwcHCwsLDw8PExMTFxcXGxsbHx8fIyMjJycnKysrLy8vMzMzNzc3O\nzs7Pz8/Q0NDR0dHS0tLT09PU1NTV1dXW1tbX19fY2NjZ2dna2trb29vc3Nzd3d3e3t7f39/g\n4ODh4eHi4uLj4+Pk5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WjNUBQQHBN9pztd8ukAIPkCDxQm4anCjCt1JKgGCA4I3U/MHFnb/p9w7jOqxNGDBbTn0\nsO4c1QDBxsocczGN0iuzOsw/M2eVYZx8pEeT1kXr679WR1KSooJTBwhmDgQzB4KZA8HMgWDm\nQDBzIJg5EMwcCGYOBDMHgpkDwcyBYOZAMHMgmDkQzBwIZg4EMweCmQPBzIFg5kAwcyCYORDM\nHAhmDgQzB4KZA8HMgWDmQDBz/h/V/QFevxwJCgAAAABJRU5ErkJggg==",
"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": [
"## Makign a `data.frame` 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 ranodm 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 differnt 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)$"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\t0.6536426 | 1.334889 | 1.5432302 | 0.6573478 | 2.249920 |
\n",
"\t1.3585636 | 2.068446 | 0.8936747 | 4.2271172 | 3.146313 |
\n",
"\t3.4023661 | 2.388056 | 2.6394386 | 1.5861398 | 2.943069 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{lllll}\n",
"\t 0.6536426 & 1.334889 & 1.5432302 & 0.6573478 & 2.249920 \\\\\n",
"\t 1.3585636 & 2.068446 & 0.8936747 & 4.2271172 & 3.146313 \\\\\n",
"\t 3.4023661 & 2.388056 & 2.6394386 & 1.5861398 & 2.943069 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| 0.6536426 | 1.334889 | 1.5432302 | 0.6573478 | 2.249920 | \n",
"| 1.3585636 | 2.068446 | 0.8936747 | 4.2271172 | 3.146313 | \n",
"| 3.4023661 | 2.388056 | 2.6394386 | 1.5861398 | 2.943069 | \n",
"\n",
"\n"
],
"text/plain": [
" [,1] [,2] [,3] [,4] [,5] \n",
"[1,] 0.6536426 1.334889 1.5432302 0.6573478 2.249920\n",
"[2,] 1.3585636 2.068446 0.8936747 4.2271172 3.146313\n",
"[3,] 3.4023661 2.388056 2.6394386 1.5861398 2.943069"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"replicate(5, rnorm(3, 1:5, 1))"
]
},
{
"cell_type": "code",
"execution_count": 75,
"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": 76,
"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": 77,
"metadata": {},
"outputs": [],
"source": [
"df <- data.frame(\n",
" pid = pids,\n",
" grp = groups,\n",
" genes\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | pid | grp | gene001 | gene002 | gene003 | gene004 | gene005 |
\n",
"\n",
"\t16 | 38729 | B | 1.530708 | 8.613943 | 10.617613 | 9.215513 | 9.880912 |
\n",
"\t13 | 22215 | B | 14.545609 | 10.625483 | 6.288087 | 9.741466 | 9.624057 |
\n",
"\t14 | 89658 | A | 13.008224 | 14.321570 | 8.115676 | 12.608564 | 7.962775 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllll}\n",
" & pid & grp & gene001 & gene002 & gene003 & gene004 & gene005\\\\\n",
"\\hline\n",
"\t16 & 38729 & B & 1.530708 & 8.613943 & 10.617613 & 9.215513 & 9.880912 \\\\\n",
"\t13 & 22215 & B & 14.545609 & 10.625483 & 6.288087 & 9.741466 & 9.624057 \\\\\n",
"\t14 & 89658 & A & 13.008224 & 14.321570 & 8.115676 & 12.608564 & 7.962775 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | pid | grp | gene001 | gene002 | gene003 | gene004 | gene005 | \n",
"|---|---|---|\n",
"| 16 | 38729 | B | 1.530708 | 8.613943 | 10.617613 | 9.215513 | 9.880912 | \n",
"| 13 | 22215 | B | 14.545609 | 10.625483 | 6.288087 | 9.741466 | 9.624057 | \n",
"| 14 | 89658 | A | 13.008224 | 14.321570 | 8.115676 | 12.608564 | 7.962775 | \n",
"\n",
"\n"
],
"text/plain": [
" pid grp gene001 gene002 gene003 gene004 gene005 \n",
"16 38729 B 1.530708 8.613943 10.617613 9.215513 9.880912\n",
"13 22215 B 14.545609 10.625483 6.288087 9.741466 9.624057\n",
"14 89658 A 13.008224 14.321570 8.115676 12.608564 7.962775"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"sample_n(df, 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Breakdown of simjulation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set up simulation confiugration parameters."
]
},
{
"cell_type": "code",
"execution_count": 70,
"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": 62,
"metadata": {},
"outputs": [],
"source": [
"pids <- sample(min_pid:max_pid, n_groups*n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assign a group to each subject at ranodm"
]
},
{
"cell_type": "code",
"execution_count": 67,
"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": 68,
"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": 71,
"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": 72,
"metadata": {},
"outputs": [],
"source": [
"colnames(genes) <- gene_names"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create `data.frame` to storre simulated data"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"df <- data.frame(\n",
" pid = pids,\n",
" grp = groups,\n",
" genes\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Peek into `data.frame`"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | pid | grp | gene001 | gene002 | gene003 | gene004 | gene005 |
\n",
"\n",
"\t16 | 72254 | B | 10.726324 | 9.749522 | 9.772832 | 19.65021 | 13.226052 |
\n",
"\t18 | 21661 | B | 10.448878 | 12.689833 | 13.292832 | 19.66553 | 14.330836 |
\n",
"\t14 | 91408 | B | 9.125949 | 11.009273 | 13.052141 | 14.95065 | 8.745456 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllll}\n",
" & pid & grp & gene001 & gene002 & gene003 & gene004 & gene005\\\\\n",
"\\hline\n",
"\t16 & 72254 & B & 10.726324 & 9.749522 & 9.772832 & 19.65021 & 13.226052\\\\\n",
"\t18 & 21661 & B & 10.448878 & 12.689833 & 13.292832 & 19.66553 & 14.330836\\\\\n",
"\t14 & 91408 & B & 9.125949 & 11.009273 & 13.052141 & 14.95065 & 8.745456\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | pid | grp | gene001 | gene002 | gene003 | gene004 | gene005 | \n",
"|---|---|---|\n",
"| 16 | 72254 | B | 10.726324 | 9.749522 | 9.772832 | 19.65021 | 13.226052 | \n",
"| 18 | 21661 | B | 10.448878 | 12.689833 | 13.292832 | 19.66553 | 14.330836 | \n",
"| 14 | 91408 | B | 9.125949 | 11.009273 | 13.052141 | 14.95065 | 8.745456 | \n",
"\n",
"\n"
],
"text/plain": [
" pid grp gene001 gene002 gene003 gene004 gene005 \n",
"16 72254 B 10.726324 9.749522 9.772832 19.65021 13.226052\n",
"18 21661 B 10.448878 12.689833 13.292832 19.66553 14.330836\n",
"14 91408 B 9.125949 11.009273 13.052141 14.95065 8.745456"
]
},
"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": "r"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.4.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}