├── Makefile ├── contents ├── chapter_07_reproducible │ ├── examples │ │ ├── example-23.Rmd │ │ ├── example-28.Rmd │ │ ├── example-22.Rmd │ │ ├── example-27.Rmd │ │ ├── example-29.Rmd │ │ ├── example-30.Rmd │ │ ├── example-44.Rmd │ │ ├── example-32.Rmd │ │ ├── example-26.Rmd │ │ ├── example-40.Rmd │ │ ├── example-31.Rmd │ │ ├── example-39.Rmd │ │ ├── example-33.Rmd │ │ ├── example-41.Rmd │ │ ├── python_code_1.Rmd │ │ ├── example-4.Rmd │ │ ├── example-43.Rmd │ │ ├── python_code_2.Rmd │ │ ├── example-24.Rmd │ │ ├── example-25.Rmd │ │ ├── example-34.Rmd │ │ ├── example-38.Rmd │ │ ├── example-21.Rmd │ │ ├── example-35.Rmd │ │ ├── example-37.Rmd │ │ ├── example-7.Rmd │ │ ├── example-20.Rmd │ │ ├── example-42.Rmd │ │ ├── example-36.Rmd │ │ ├── example-9.Rmd │ │ ├── example-10.Rmd │ │ ├── example-13.Rmd │ │ ├── example-19.Rmd │ │ ├── example-14.Rmd │ │ ├── example-15.Rmd │ │ ├── example-5.Rmd │ │ ├── example-1.Rmd │ │ ├── example-12.Rmd │ │ ├── example-6.Rmd │ │ ├── example-17.Rmd │ │ ├── example-16.Rmd │ │ ├── refs.bib │ │ ├── example-11.Rmd │ │ ├── example-18.Rmd │ │ ├── example-8.Rmd │ │ ├── utils.R │ │ ├── example-2.Rmd │ │ ├── example.csv │ │ └── example-3.Rmd │ ├── venv_setup.sh │ ├── include_graphics │ │ ├── knitDocument.png │ │ └── github_screenshot.png │ ├── utils.py │ └── Makefile ├── chapter_17_stan │ ├── requirements.txt │ ├── scripts │ │ ├── _m_dice_3.R │ │ ├── common.R │ │ ├── m_normal.R │ │ ├── lmm.R │ │ ├── m_dice_3_2.R │ │ ├── m_dice_4.R │ │ ├── m_dice_3.R │ │ ├── normlinear.R │ │ ├── mlreg.R │ │ ├── m_dice.R │ │ ├── negbinreg.R │ │ ├── poisreg.R │ │ └── logitreg.R │ ├── stan │ │ ├── coin_bernoulli.stan │ │ ├── loaded_dice.stan │ │ ├── loaded_dice_binomial.stan │ │ ├── loaded_dice_for.stan │ │ ├── loaded_dice_logit.stan │ │ ├── loaded_dice_logit2.stan │ │ ├── normal.stan │ │ ├── normlinear.stan │ │ ├── poisreg.stan │ │ ├── logitreg.stan │ │ ├── mlreg.stan │ │ ├── negbinreg.stan │ │ └── lmm.stan │ ├── header.tex │ ├── data │ │ └── loaded_dice.csv │ └── Makefile ├── chapter_15_hpc │ ├── smra-package.R │ ├── code.cpp │ ├── Makefile │ └── data │ │ └── housing.csv ├── chapter_16_shiny │ ├── include │ │ ├── learnr.png │ │ ├── zoomed.png │ │ ├── app_window.png │ │ ├── app_window_.png │ │ ├── create_app.pdf │ │ ├── rmarkdownshiny.png │ │ ├── false_discovery.png │ │ ├── new_shiny_dialog.png │ │ ├── new_shiny_dialog_.png │ │ ├── selected_region.png │ │ ├── app_window_gghist_.png │ │ ├── file_new_file_shiny.png │ │ ├── interacting_widgets.pdf │ │ ├── file_new_file_shiny_.png │ │ ├── interacting_widgets_a.png │ │ ├── interacting_widgets_b.png │ │ └── likelihood_screenshot_.png │ ├── shiny │ │ ├── selecting │ │ │ └── app.R │ │ ├── gghistogram │ │ │ ├── app.R │ │ │ └── housing.csv │ │ ├── zooming │ │ │ └── app.R │ │ ├── learnr │ │ │ └── example.Rmd │ │ ├── markdown │ │ │ └── report.Rmd │ │ └── likelihood │ │ │ └── app.R │ ├── Makefile │ └── data │ │ └── housing.csv ├── chapter_02_introduction │ ├── icons │ │ ├── outline.png │ │ ├── section.png │ │ ├── stock_new.png │ │ ├── FilterIcon.png │ │ ├── executeCode.png │ │ ├── viewerStop.png │ │ ├── import_dataset.png │ │ ├── installPackage.png │ │ ├── saveSourceDoc.png │ │ ├── updatePackages.png │ │ ├── refreshPackages.png │ │ └── sourceActiveDocument.png │ ├── rstudio_screenshots │ │ ├── read_csv.png │ │ ├── packages_pane.png │ │ ├── install_packages.png │ │ ├── rstudio_with_editor.png │ │ └── rstudio_when_open_first.png │ └── Makefile ├── chapter_03_wrangling │ ├── data │ │ ├── repeated_measured_a.csv │ │ └── repeated_measured_b.csv │ └── Makefile ├── chapter_13_nlregression │ ├── includes │ │ └── golf_putting_v2.pdf │ ├── data │ │ └── golf_putts.csv │ └── Makefile ├── chapter_11_generalized │ ├── data │ │ ├── golf_putts.csv │ │ ├── Insurance.csv │ │ └── lbw.csv │ └── Makefile ├── chapter_04_visualization │ ├── data │ │ ├── quartet.csv │ │ ├── FatRats.csv │ │ ├── carprice.csv │ │ └── sleepstudy.csv │ └── Makefile ├── chapter_12_multilevel │ ├── data │ │ ├── rats.csv │ │ └── mathachieveschool.csv │ ├── include │ │ ├── crossed_structures.tex │ │ ├── multilevel_sleepstudy.tex │ │ ├── crossed_structures_s.tex │ │ ├── multilevel_sleepstudy_s.tex │ │ ├── classroom.tex │ │ ├── classroom_s.tex │ │ ├── J_binomial.tex │ │ ├── J_binomial_s.tex │ │ ├── multilevel_binomial.tex │ │ ├── multilevel_binomial_s.tex │ │ ├── sleepstudy_j.tex │ │ ├── sleepstudy_j_s.tex │ │ ├── nonmultilevel_alcohol.tex │ │ ├── nonmultilevel_alcohol_s.tex │ │ ├── multilevel_alcohol.tex │ │ ├── multilevel_alcohol_s.tex │ │ ├── nonmultilevel_sleepstudy.tex │ │ └── nonmultilevel_sleepstudy_s.tex │ ├── Makefile │ └── preamble.tex ├── chapter_01_datascience │ └── Makefile ├── chapter_05_exploratory │ ├── Makefile │ └── data │ │ └── nominal_gdp_per_capita.csv ├── chapter_10_logistic │ ├── Makefile │ └── data │ │ └── lbw.csv ├── chapter_06_programming │ ├── Makefile │ └── chapter_06_programming_slides.Rmd ├── chapter_09_linear │ └── Makefile ├── chapter_08_inference │ ├── Makefile │ └── data │ │ └── housing.csv ├── chapter_14_sem │ ├── Makefile │ └── data │ │ └── grad.csv └── header.tex ├── .gitignore ├── docker ├── run_docker.sh └── Dockerfile ├── janacek.Rproj ├── setenv.sh └── readme.md /Makefile: -------------------------------------------------------------------------------- 1 | docker: 2 | docker build -t janacek -f docker/Dockerfile . 3 | 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-23.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | x^j_i 3 | $$ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | _book 6 | build 7 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-28.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \sum x_i 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-22.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | 2^{2^2}, 2_{i_j} 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-27.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \sum_{i=1}^n x_i 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-29.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \prod_{i=1}^n x_i 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-30.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \int_{0}^1 f(x) dx 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-44.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \sqrt[3]{125} = 5 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-32.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | (1 + \frac{1}{n})^n 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-26.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \frac{a}{b + \frac{1}{c}} 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-40.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | x = y,\quad this is text 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-31.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \left(1 + \frac{1}{n}\right)^n 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-39.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | x = y,\quad\text{this is text} 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-33.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \left. 1 + \frac{1}{x}\right\vert_a^b 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-41.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | s_i = 2^i,\quad \text{for $i \geq 0$} 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/python_code_1.Rmd: -------------------------------------------------------------------------------- 1 | ``` 2 | for x_i in x: 3 | y.append(x_i) 4 | 5 | ``` 6 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/requirements.txt: -------------------------------------------------------------------------------- 1 | powerline-shell==0.7.0 2 | powerline-status==2.7 3 | Pygments==2.5.2 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-4.Rmd: -------------------------------------------------------------------------------- 1 | ```{r, echo=T, comment='>'} 2 | rnorm(5) %>% round(2) 3 | ``` 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-43.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | s = \sqrt{\frac{\sum^n_{i=1} (x_i - \bar{x})^2}{n-1}} 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/python_code_2.Rmd: -------------------------------------------------------------------------------- 1 | ```python 2 | for x_i in x: 3 | y.append(x_i) 4 | 5 | ``` 6 | -------------------------------------------------------------------------------- /contents/chapter_15_hpc/smra-package.R: -------------------------------------------------------------------------------- 1 | #' @useDynLib smra, .registration = TRUE 2 | #' @importFrom Rcpp sourceCpp 3 | NULL 4 | -------------------------------------------------------------------------------- /docker/run_docker.sh: -------------------------------------------------------------------------------- 1 | docker run --rm -p 8788:8787 -e PASSWORD=foo -e ROOT=true -v $(pwd):/home/rstudio/book janacek:latest 2 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-24.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \frac{1}{2} + \frac{3}{4} = \frac{4 + 6}{8} = \frac{5}{4} 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-25.Rmd: -------------------------------------------------------------------------------- 1 | \begin{center} 2 | The result is $x = \frac{a + b}{c + d}$. 3 | \end{center} 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-34.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | (x) + \big(x\big) + \Big(x\Big) + \bigg(x\bigg) + \Bigg(x\Bigg) 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-38.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \begin{aligned} 3 | x = a + b\\ 4 | y = c + d + e 5 | \end{aligned} 6 | $$ 7 | -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/learnr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/learnr.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/zoomed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/zoomed.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/outline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/outline.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/section.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/section.png -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-21.Rmd: -------------------------------------------------------------------------------- 1 | Einstein's famous formula for mass-energy equivalence is 2 | $$ 3 | E = mc^2. 4 | $$ 5 | 6 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-35.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \begin{matrix} 3 | 1 & 2 & 3\\ 4 | 4 & 5 & 6\\ 5 | 7 & 8 & 9 6 | \end{matrix} 7 | $$ 8 | -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/app_window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/app_window.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/app_window_.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/app_window_.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/create_app.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/create_app.pdf -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/stock_new.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/stock_new.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/rmarkdownshiny.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/rmarkdownshiny.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/FilterIcon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/FilterIcon.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/executeCode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/executeCode.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/viewerStop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/viewerStop.png -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-37.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \begin{aligned} 3 | f &= (x + y)(x + y),\\ 4 | &= x^2 + 2xy + y^2 5 | \end{aligned} 6 | $$ 7 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-7.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \int_{-\infty}^1 \frac{1}{\sqrt{2\pi}} e^{-\frac{x^2}{2}} dx = `r round(pnorm(1), 3)` 3 | $$ 4 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/venv_setup.sh: -------------------------------------------------------------------------------- 1 | if [ -d "chapter_07_venv" ] 2 | then 3 | rm -rf chapter_07_venv 4 | fi 5 | 6 | virtualenv chapter_07_venv 7 | -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/false_discovery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/false_discovery.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/new_shiny_dialog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/new_shiny_dialog.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/new_shiny_dialog_.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/new_shiny_dialog_.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/selected_region.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/selected_region.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/import_dataset.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/import_dataset.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/installPackage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/installPackage.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/saveSourceDoc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/saveSourceDoc.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/updatePackages.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/updatePackages.png -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-20.Rmd: -------------------------------------------------------------------------------- 1 | \begin{center} 2 | Einstein's famous formula for mass-energy equivalence is $E = mc^2$. 3 | \end{center} 4 | -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/app_window_gghist_.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/app_window_gghist_.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/file_new_file_shiny.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/file_new_file_shiny.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/interacting_widgets.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/interacting_widgets.pdf -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/refreshPackages.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/refreshPackages.png -------------------------------------------------------------------------------- /contents/chapter_03_wrangling/data/repeated_measured_a.csv: -------------------------------------------------------------------------------- 1 | Subject,Neg,Neu,Pos 2 | Faye,26,12,42 3 | Jason,29,8,35 4 | Jim,32,15,45 5 | Ron,22,10,38 6 | Victor,30,13,40 7 | -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/file_new_file_shiny_.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/file_new_file_shiny_.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/interacting_widgets_a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/interacting_widgets_a.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/interacting_widgets_b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/interacting_widgets_b.png -------------------------------------------------------------------------------- /contents/chapter_16_shiny/include/likelihood_screenshot_.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_16_shiny/include/likelihood_screenshot_.png -------------------------------------------------------------------------------- /contents/chapter_13_nlregression/includes/golf_putting_v2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_13_nlregression/includes/golf_putting_v2.pdf -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/_m_dice_3.R: -------------------------------------------------------------------------------- 1 | dice_data_3 <- list(y = y, N = N, sigma = 1) 2 | M_dice_3 <- stan('loaded_dice_logit.stan', 3 | data = dice_data_3) -------------------------------------------------------------------------------- /contents/chapter_02_introduction/icons/sourceActiveDocument.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/icons/sourceActiveDocument.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/rstudio_screenshots/read_csv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/rstudio_screenshots/read_csv.png -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-42.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \underbrace{(x + y)(x - y)}_{\text{left hand side}} 3 | = \overbrace{x^2 - y^2}^{\text{right hand side}} 4 | $$ 5 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-36.Rmd: -------------------------------------------------------------------------------- 1 | $$ 2 | \left[ 3 | \begin{matrix} 4 | 1 & 2 & 3\\ 5 | 4 & 5 & 6\\ 6 | 7 & 8 & 9 7 | \end{matrix} 8 | \right] 9 | $$ 10 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-9.Rmd: -------------------------------------------------------------------------------- 1 | ```{r, echo=TRUE, eval=FALSE} 2 | n <- 10 3 | x <- rnorm(n) 4 | y <- 2.25 * x + rnorm(n) 5 | 6 | M <- lm(y ~ x) 7 | ``` 8 | 9 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/include_graphics/knitDocument.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_07_reproducible/include_graphics/knitDocument.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/rstudio_screenshots/packages_pane.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/rstudio_screenshots/packages_pane.png -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/include_graphics/github_screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_07_reproducible/include_graphics/github_screenshot.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/rstudio_screenshots/install_packages.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/rstudio_screenshots/install_packages.png -------------------------------------------------------------------------------- /contents/chapter_02_introduction/rstudio_screenshots/rstudio_with_editor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/rstudio_screenshots/rstudio_with_editor.png -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-10.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: pdf_document 3 | header-includes: 4 | - \pagenumbering{gobble} 5 | --- 6 | 7 | * Apple 8 | * Orange 9 | * Blueberry 10 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-13.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: pdf_document 3 | header-includes: 4 | - \pagenumbering{gobble} 5 | --- 6 | 7 | 1. Potato 8 | 1. Brocolli 9 | 1. Cabbage 10 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-19.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: pdf_document 3 | header-includes: 4 | - \pagenumbering{gobble} 5 | --- 6 | 7 | $$ 8 | \alpha, \beta, \gamma 9 | $$ 10 | -------------------------------------------------------------------------------- /contents/chapter_02_introduction/rstudio_screenshots/rstudio_when_open_first.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mark-andrews/ddsr/HEAD/contents/chapter_02_introduction/rstudio_screenshots/rstudio_when_open_first.png -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-14.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: pdf_document 3 | header-includes: 4 | - \pagenumbering{gobble} 5 | --- 6 | 7 | 1. Potato 8 | 4. Brocolli 9 | 10. Cabbage 10 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-15.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: pdf_document 3 | header-includes: 4 | - \pagenumbering{gobble} 5 | --- 6 | 7 | 12. Potato 8 | 4. Brocolli 9 | 10. Cabbage 10 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/common.R: -------------------------------------------------------------------------------- 1 | library(tidyverse) 2 | library(magrittr) 3 | library(modelr) 4 | 5 | stan <- function(file, ...){ 6 | rstan::stan(file = fs::path('stan', file), refresh = 0, ...) 7 | } 8 | 9 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-5.Rmd: -------------------------------------------------------------------------------- 1 | If $\Phi = \phi_1, \phi_2 \ldots \phi_k \ldots \phi_k$, where each $0 \leq \phi_k \leq 1$, 2 | and $\sum_{k=1}^K \phi_k = 1$, then $\Phi$ is a probability mass function. 3 | -------------------------------------------------------------------------------- /contents/chapter_03_wrangling/data/repeated_measured_b.csv: -------------------------------------------------------------------------------- 1 | Subject,Cued_Neg,Cued_Neu,Cued_Pos,Free_Neg,Free_Neu,Free_Pos 2 | Faye,15,16,14,13,13,12 3 | Jason,4,9,10,6,7,9 4 | Jim,7,9,10,8,9,5 5 | Ron,17,18,20,12,14,15 6 | Victor,16,13,14,12,13,14 7 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-1.Rmd: -------------------------------------------------------------------------------- 1 | This is some text. Some of it is *italicized*, and some is in **bold**. 2 | 3 | ```{r} 4 | x <- c(5, 12, 103) 5 | y <- x ^ 2 6 | ``` 7 | 8 | The mean of `x` is `r mean(x)`, and the min value of `y` is `r min(y)`. 9 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-12.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: pdf_document 3 | header-includes: 4 | - \pagenumbering{gobble} 5 | --- 6 | 7 | * Farm animal 8 | * Cow 9 | * Pig 10 | * Sheep 11 | * Wild animal 12 | * Fox 13 | * Wolf 14 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/coin_bernoulli.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int y[N]; 4 | } 5 | 6 | parameters { 7 | real theta; 8 | } 9 | 10 | model { 11 | theta ~ beta(1, 1); 12 | y ~ bernoulli(theta); 13 | } 14 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-6.Rmd: -------------------------------------------------------------------------------- 1 | The probability of the observed data is as follows: 2 | $$ 3 | \mathrm{P}(x_i \ldots x_n \vert \mu, \sigma^2) 4 | = \prod_{i=1}^n \mathrm{P}(x_i \vert \mu, \sigma^2), 5 | $$ 6 | where $\mu$ and $\sigma$ are parameters. 7 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-17.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: pdf_document 3 | header-includes: 4 | - \pagenumbering{gobble} 5 | --- 6 | 7 | * Tree 8 | 1. Fir 9 | 1. Pine 10 | 1. Oak 11 | * Flower 12 | 1. Rose 13 | 1. Tulip 14 | 1. Daffodil 15 | 16 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-16.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: pdf_document 3 | header-includes: 4 | - \pagenumbering{gobble} 5 | --- 6 | 7 | 1. Tree 8 | 1. Fir 9 | 1. Pine 10 | 1. Oak 11 | 1. Flower 12 | 1. Rose 13 | 1. Tulip 14 | 1. Daffodil 15 | 16 | -------------------------------------------------------------------------------- /janacek.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Yes 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | BuildType: Makefile 16 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/refs.bib: -------------------------------------------------------------------------------- 1 | @book{GelmanHill:2007, 2 | address = {New York}, 3 | author = {Gelman, Andrew and Hill, Jennifer}, 4 | title = {Data Analysis Using Regression and Multilevel/Hierarchical Models}, 5 | publisher = {Cambridge University Press}, 6 | year = 2007 7 | } 8 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/loaded_dice.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int y[N]; 4 | real alpha; 5 | real beta; 6 | } 7 | 8 | parameters { 9 | real theta; 10 | } 11 | 12 | model { 13 | theta ~ beta(alpha, beta); 14 | y ~ bernoulli(theta); 15 | } 16 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/loaded_dice_binomial.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int m; 4 | real alpha; 5 | real beta; 6 | } 7 | 8 | parameters { 9 | real theta; 10 | } 11 | 12 | model { 13 | theta ~ beta(alpha, beta); 14 | m ~ binomial(N, theta); 15 | } 16 | -------------------------------------------------------------------------------- /contents/chapter_11_generalized/data/golf_putts.csv: -------------------------------------------------------------------------------- 1 | distance,attempts,success 2 | 2,1443,1346 3 | 3,694,577 4 | 4,455,337 5 | 5,353,208 6 | 6,272,149 7 | 7,256,136 8 | 8,240,111 9 | 9,217,69 10 | 10,200,67 11 | 11,237,75 12 | 12,202,52 13 | 13,192,46 14 | 14,174,54 15 | 15,167,28 16 | 16,201,27 17 | 17,195,31 18 | 18,191,33 19 | 19,147,20 20 | 20,152,24 21 | -------------------------------------------------------------------------------- /contents/chapter_13_nlregression/data/golf_putts.csv: -------------------------------------------------------------------------------- 1 | distance,attempts,success 2 | 2,1443,1346 3 | 3,694,577 4 | 4,455,337 5 | 5,353,208 6 | 6,272,149 7 | 7,256,136 8 | 8,240,111 9 | 9,217,69 10 | 10,200,67 11 | 11,237,75 12 | 12,202,52 13 | 13,192,46 14 | 14,174,54 15 | 15,167,28 16 | 16,201,27 17 | 17,195,31 18 | 18,191,33 19 | 19,147,20 20 | 20,152,24 21 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/loaded_dice_for.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int y[N]; 4 | real alpha; 5 | real beta; 6 | } 7 | 8 | parameters { 9 | real theta; 10 | } 11 | 12 | model { 13 | theta ~ beta(alpha, beta); 14 | for (i in 1:N){ 15 | y[i] ~ bernoulli(theta); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/loaded_dice_logit.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int y[N]; 4 | real sigma; 5 | } 6 | 7 | parameters { 8 | real mu; 9 | } 10 | 11 | model { 12 | mu ~ normal(0, sigma); 13 | y ~ bernoulli_logit(mu); 14 | } 15 | 16 | generated quantities{ 17 | real theta; 18 | theta = inv_logit(mu); 19 | } 20 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/loaded_dice_logit2.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int y[N]; 4 | real sigma; 5 | } 6 | 7 | parameters { 8 | real mu; 9 | } 10 | 11 | transformed parameters { 12 | real theta; 13 | theta = inv_logit(mu); 14 | } 15 | 16 | model { 17 | mu ~ normal(0, sigma); 18 | y ~ bernoulli(theta); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/m_normal.R: -------------------------------------------------------------------------------- 1 | source("scripts/common.R") 2 | 3 | y <- read_csv('data/MathPlacement.csv') %>% select(SATM) %>% na.omit() %>% pull(SATM) 4 | 5 | N <- length(y) 6 | 7 | math_data <- list(y = y, N = N, nu = 50, tau = 25, phi = 0, omega = 10, kappa = 5) 8 | M_math <- stan('normal.stan', data = math_data) 9 | 10 | saveRDS(file = 'M_math.Rds', object = M_math) 11 | 12 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/normal.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | real nu; 4 | real tau; 5 | real phi; 6 | real omega; 7 | int kappa; 8 | vector[N] y; 9 | } 10 | 11 | parameters { 12 | real mu; 13 | real sigma; 14 | } 15 | 16 | model { 17 | sigma ~ student_t(kappa, phi, omega); 18 | mu ~ normal(nu, tau); 19 | y ~ normal(mu, sigma); 20 | } 21 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-11.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: pdf_document 3 | header-includes: 4 | - \pagenumbering{gobble} 5 | geometry: margin=7cm 6 | --- 7 | 8 | * If we start our item 9 | on one line, 10 | and continue to another 11 | line with no break, 12 | it will still appear as one item. 13 | * The same thing happens 14 | if you continue 15 | on the next line after 16 | and indention. 17 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-18.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: pdf_document 3 | header-includes: 4 | - \pagenumbering{gobble} 5 | geometry: margin=7cm 6 | --- 7 | 8 | participant-id 9 | : An integer that uniquely 10 | codes each participant. 11 | 12 | gender 13 | : A binary variable with 14 | values `female` and `male`. 15 | 16 | age 17 | : A numeric variable giving 18 | the participant's age in years. 19 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/lmm.R: -------------------------------------------------------------------------------- 1 | source('scripts/common.R') 2 | 3 | sleepstudy <- lme4::sleepstudy 4 | 5 | y <- sleepstudy$Reaction 6 | x <- sleepstudy$Days 7 | z <- sleepstudy$Subject %>% as.numeric() 8 | 9 | sleep_data <- list(N = length(y), 10 | J = length(unique(z)), 11 | y = y, 12 | x = x, 13 | z = z) 14 | 15 | M_lmm <- stan('lmm.stan', data = sleep_data) 16 | 17 | saveRDS(M_lmm, file='M_lmm.Rds') 18 | -------------------------------------------------------------------------------- /setenv.sh: -------------------------------------------------------------------------------- 1 | unset TEXINPUTS 2 | unset BIBINPUTS 3 | export TEXINPUTS=$HOME/book/contents/:$TEXINPUTS 4 | export BIBINPUTS=$HOME/book/contents/:$BIBINPUTS 5 | 6 | # This for the bug(?) with openmp 7 | export OMP_NUM_THREADS=1 8 | export OPENBLAS_NUM_THREADS=1 9 | 10 | alias ll='ls -lt' 11 | alias s='cd ..' 12 | alias lmr='ls -lt | head' 13 | 14 | # We need a Documents dir at $HOME for Chpt 7 15 | if [ -d "$HOME/Documents" ] 16 | then 17 | rm -rf "$HOME/Documents" 18 | fi 19 | 20 | mkdir "$HOME/Documents" -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/m_dice_3_2.R: -------------------------------------------------------------------------------- 1 | library(tidyverse) 2 | 3 | stan <- function(file, ...){ 4 | rstan::stan(file = fs::path('stan', file), refresh = 0, ...) 5 | } 6 | 7 | y <- read_csv('data/loaded_dice.csv') %>% 8 | mutate(is_six = ifelse(outcome == 6, 1, 0)) %>% 9 | pull(is_six) 10 | 11 | N <- length(y) 12 | 13 | dice_data_3 <- list(y = y, N = N, sigma = 1) 14 | M_dice_3 <- stan('loaded_dice_logit2.stan', 15 | data = dice_data_3) 16 | 17 | saveRDS(M_dice_3, file='M_dice_4.Rds') -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/m_dice_4.R: -------------------------------------------------------------------------------- 1 | library(tidyverse) 2 | 3 | stan <- function(file, ...){ 4 | rstan::stan(file = fs::path('stan', file), refresh = 0, ...) 5 | } 6 | 7 | y <- read_csv('data/loaded_dice.csv') %>% 8 | mutate(is_six = ifelse(outcome == 6, 1, 0)) %>% 9 | pull(is_six) 10 | 11 | N <- length(y) 12 | 13 | dice_data_3 <- list(y = y, N = N, sigma = 1) 14 | M_dice_3 <- stan('loaded_dice_logit2.stan', 15 | data = dice_data_3) 16 | 17 | saveRDS(M_dice_3, file='M_dice_4.Rds') -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/m_dice_3.R: -------------------------------------------------------------------------------- 1 | library(tidyverse) 2 | 3 | stan <- function(file, ...){ 4 | rstan::stan(file = fs::path('stan', file), refresh = 0, ...) 5 | } 6 | 7 | y <- read_csv('data/loaded_dice.csv') %>% 8 | mutate(is_six = ifelse(outcome == 6, 1, 0)) %>% 9 | pull(is_six) 10 | 11 | N <- length(y) 12 | 13 | dice_data_3 <- list(y = y, N = N, sigma = 1) 14 | M_dice_3 <- stan('loaded_dice_logit2.stan', 15 | data = dice_data_3) 16 | 17 | saveRDS(M_dice_3, file='M_dice_3.Rds') 18 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/normlinear.R: -------------------------------------------------------------------------------- 1 | source("scripts/common.R") 2 | math_df <- read_csv('data/MathPlacement.csv') 3 | 4 | math_df_2 <- math_df %>% 5 | select(SATM, PlcmtScore) %>% 6 | na.omit() 7 | 8 | x <- pull(math_df_2, SATM) 9 | y <- pull(math_df_2, PlcmtScore) 10 | 11 | math_data_2 <- list( 12 | x = x, 13 | y = y, 14 | N = length(x), 15 | tau = 50, omega = mad(y), kappa = 3 16 | ) 17 | 18 | M_math_2 <- stan('normlinear.stan', data = math_data_2) 19 | 20 | saveRDS(file = 'M_math_2.Rds', object = M_math_2) 21 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/normlinear.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | vector[N] x; 4 | vector[N] y; 5 | 6 | // hyperparameters 7 | real tau; 8 | real omega; 9 | int kappa; 10 | } 11 | 12 | parameters { 13 | real beta_0; 14 | real beta_1; 15 | real sigma; 16 | } 17 | 18 | model { 19 | // priors 20 | sigma ~ student_t(kappa, 0, omega); 21 | beta_0 ~ normal(0, tau); 22 | beta_1 ~ normal(0, tau); 23 | // data model 24 | y ~ normal(beta_0 + beta_1 * x, sigma); 25 | } 26 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/mlreg.R: -------------------------------------------------------------------------------- 1 | source('scripts/common.R') 2 | 3 | weight_df <- read_csv('data/weight.csv') %>% 4 | filter(race %in% c('black', 'white', 'hispanic')) 5 | 6 | X <- model_matrix(weight_df, weight ~ height + gender + race) %>% 7 | as.matrix() 8 | y <- pull(weight_df, weight) 9 | 10 | weight_data <- list( 11 | X = X, 12 | y = y, 13 | N = length(y), 14 | K = ncol(X) - 1, 15 | tau = 100, kappa = 3, omega = mad(y) 16 | ) 17 | 18 | M_weight <- stan('mlreg.stan', data = weight_data) 19 | 20 | saveRDS(M_weight, 'M_weight.Rds') 21 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/poisreg.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int K; 4 | matrix[N, K+1] X; 5 | int y[N]; 6 | 7 | // hyper parameters 8 | real tau; 9 | } 10 | 11 | parameters { 12 | vector[K+1] beta; 13 | } 14 | 15 | transformed parameters { 16 | vector[N] mu; 17 | mu = X * beta; 18 | } 19 | 20 | model { 21 | // priors 22 | beta ~ normal(0.0, tau); 23 | 24 | // data model 25 | y ~ poisson_log(mu); 26 | } 27 | 28 | generated quantities{ 29 | vector[N] lambda; 30 | lambda = exp(mu); 31 | } 32 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/m_dice.R: -------------------------------------------------------------------------------- 1 | library(tidyverse) 2 | 3 | stan <- function(file, ...){ 4 | rstan::stan(file = fs::path('stan', file), refresh = 0, ...) 5 | } 6 | 7 | y <- read_csv('data/loaded_dice.csv') %>% 8 | mutate(is_six = ifelse(outcome == 6, 1, 0)) %>% 9 | pull(is_six) 10 | 11 | N <- length(y) 12 | 13 | dice_data <- list(y = y, 14 | N = N, 15 | alpha = 1, 16 | beta = 1) 17 | M_dice <- stan('loaded_dice.stan', 18 | data = dice_data) 19 | 20 | saveRDS(M_dice_3, file='M_dice.Rds') -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/logitreg.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int K; 4 | matrix[N, K+1] X; 5 | int y[N]; 6 | 7 | // hyper parameters 8 | real tau; 9 | } 10 | 11 | parameters { 12 | vector[K+1] beta; 13 | } 14 | 15 | transformed parameters { 16 | vector[N] mu; 17 | mu = X * beta; 18 | } 19 | 20 | model { 21 | // priors 22 | beta ~ normal(0.0, tau); 23 | 24 | // data model 25 | y ~ bernoulli_logit(mu); 26 | } 27 | 28 | generated quantities{ 29 | vector[N] theta; 30 | theta = inv_logit(mu); 31 | } 32 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/mlreg.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int K; 4 | matrix[N, K+1] X; 5 | vector[N] y; 6 | 7 | // hyper parameters 8 | real tau; 9 | real omega; 10 | int kappa; 11 | } 12 | 13 | parameters { 14 | vector[K+1] beta; 15 | real sigma; 16 | } 17 | 18 | transformed parameters { 19 | vector[N] mu; 20 | mu = X * beta; 21 | } 22 | 23 | model { 24 | // priors 25 | beta ~ normal(0.0, tau); 26 | sigma ~ student_t(kappa, 0, omega); 27 | 28 | // data model 29 | y ~ normal(mu, sigma); 30 | } 31 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-8.Rmd: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | This is a sentence in the Introduction. 4 | 5 | ## Objectives of study 6 | 7 | This is a sentence in the first subsection 8 | of the Introduction. 9 | 10 | # Analysis 11 | 12 | This is a sentence in the Analysis section. 13 | 14 | ## Exploratory analysis 15 | 16 | This is a sentence of the first subsection 17 | of the Analysis section. 18 | 19 | ## Statistical model 20 | 21 | This is a sentence of the second subsection 22 | of the Analysis. 23 | 24 | # Conclusion 25 | 26 | This is a sentence in the last section 27 | of the document. 28 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/negbinreg.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; 3 | int K; 4 | matrix[N, K+1] X; 5 | int y[N]; 6 | 7 | // hyper parameters 8 | real tau; 9 | } 10 | 11 | parameters { 12 | vector[K+1] beta; 13 | real phi; 14 | } 15 | 16 | transformed parameters { 17 | vector[N] mu; 18 | mu = X * beta; 19 | } 20 | 21 | model { 22 | // priors 23 | beta ~ normal(0.0, tau); 24 | phi ~ cauchy(0, 10); 25 | 26 | // data model 27 | y ~ neg_binomial_2_log(mu, phi); 28 | } 29 | 30 | generated quantities{ 31 | vector[N] lambda; 32 | lambda = exp(mu); 33 | } 34 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/negbinreg.R: -------------------------------------------------------------------------------- 1 | source("scripts/common.R") 2 | 3 | 4 | biochem_df <- read_csv('data/biochemist.csv') 5 | 6 | X <- model_matrix(~ gender + married + I(children > 0) + prestige + mentor, 7 | data = biochem_df) %>% 8 | as.matrix() 9 | 10 | y <- biochem_df %>% pull(publications) 11 | 12 | biochem_data_count <- list(y = y, 13 | X = X, 14 | N = nrow(X), 15 | K = ncol(X) - 1, 16 | tau = 100) 17 | 18 | M_biochem_nb <- stan('negbinreg.stan', data = biochem_data_count) 19 | 20 | saveRDS(file = 'M_biochem_nb.Rds', object = M_biochem_nb) 21 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/poisreg.R: -------------------------------------------------------------------------------- 1 | source("scripts/common.R") 2 | 3 | 4 | biochem_df <- read_csv('data/biochemist.csv') 5 | 6 | X <- model_matrix(~ gender + married + I(children > 0) + prestige + mentor, 7 | data = biochem_df) %>% 8 | as.matrix() 9 | 10 | y <- biochem_df %>% pull(publications) 11 | 12 | biochem_data_count <- list(y = y, 13 | X = X, 14 | N = nrow(X), 15 | K = ncol(X) - 1, 16 | tau = 100) 17 | 18 | M_biochem_pois <- stan('poisreg.stan', data = biochem_data_count) 19 | 20 | saveRDS(file = 'M_biochem_pois.Rds', object = M_biochem_pois) 21 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/scripts/logitreg.R: -------------------------------------------------------------------------------- 1 | source("scripts/common.R") 2 | 3 | 4 | biochem_df <- read_csv('data/biochemist.csv') 5 | 6 | biochem_df %<>% mutate(published = publications > 0) 7 | 8 | X <- model_matrix(~ gender + married + I(children > 0) + prestige + mentor, 9 | data = biochem_df) %>% 10 | as.matrix() 11 | 12 | y <- biochem_df %>% pull(published) 13 | 14 | biochem_data <- list(y = y, 15 | X = X, 16 | N = nrow(X), 17 | K = ncol(X) - 1, 18 | tau = 100) 19 | 20 | M_biochem <- stan('logitreg.stan', data = biochem_data) 21 | 22 | saveRDS(file = 'M_biochem.Rds', object = M_biochem) 23 | -------------------------------------------------------------------------------- /contents/chapter_04_visualization/data/quartet.csv: -------------------------------------------------------------------------------- 1 | set,x,y 2 | I,10,8.04 3 | I,8,6.95 4 | I,13,7.58 5 | I,9,8.81 6 | I,11,8.33 7 | I,14,9.96 8 | I,6,7.24 9 | I,4,4.26 10 | I,12,10.84 11 | I,7,4.82 12 | I,5,5.68 13 | II,10,9.14 14 | II,8,8.14 15 | II,13,8.74 16 | II,9,8.77 17 | II,11,9.26 18 | II,14,8.1 19 | II,6,6.13 20 | II,4,3.1 21 | II,12,9.13 22 | II,7,7.26 23 | II,5,4.74 24 | III,10,7.46 25 | III,8,6.77 26 | III,13,12.74 27 | III,9,7.11 28 | III,11,7.81 29 | III,14,8.84 30 | III,6,6.08 31 | III,4,5.39 32 | III,12,8.15 33 | III,7,6.42 34 | III,5,5.73 35 | IV,8,6.58 36 | IV,8,5.76 37 | IV,8,7.71 38 | IV,8,8.84 39 | IV,8,8.47 40 | IV,8,7.04 41 | IV,8,5.25 42 | IV,19,12.5 43 | IV,8,5.56 44 | IV,8,7.91 45 | IV,8,6.89 46 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/utils.R: -------------------------------------------------------------------------------- 1 | library(knitr) 2 | library(kableExtra) 3 | 4 | nice_table <- function(data_df){ 5 | kable(data_df, format = "latex", 6 | booktabs = TRUE, 7 | digits = 2, 8 | align = 'c') %>% 9 | kable_styling(position = "center") 10 | } 11 | 12 | get_nice_coefficients_table <- function(model){ 13 | summary(model)$coefficients %>% 14 | as.data.frame() %>% 15 | rownames_to_column() %>% 16 | rename(Estimate = Estimate, 17 | SE = `Std. Error`, 18 | `t-statistic` = `t value`, 19 | `p-value` = `Pr(>|t|)`) %>% 20 | mutate(`p-value` = format.pval(`p-value`, eps = 0.01)) %>% 21 | column_to_rownames('rowname') 22 | } 23 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/header.tex: -------------------------------------------------------------------------------- 1 | \usepackage{minted} 2 | \usepackage{xspace} 3 | \usepackage{booktabs} 4 | \usepackage{xcolor} 5 | \usepackage{etoolbox} 6 | 7 | \newcommand{\data}{\mathcal{D}} 8 | \newcommand{\Prob}[1]{\mathrm{P}( #1 )} 9 | \newcommand{\PROB}[1]{\mathrm{P}\left( #1 \right)} 10 | \newcommand*{\given}{\vert} 11 | \newcommand{\mcmc}{\textsc{mcmc}\xspace} 12 | \newcommand{\ppl}{\textsc{ppl}\xspace} 13 | \newcommand{\mad}{\textsc{mad}\xspace} 14 | 15 | %\BeforeBeginEnvironment{minted}{\vspace{2mm}} 16 | %\AfterEndEnvironment{minted}{\vspace{2mm}} 17 | 18 | \definecolor{light-gray}{rgb}{0.96,0.96,0.96} 19 | \setminted{bgcolor=light-gray} % this line causes the problem 20 | 21 | \makeatletter 22 | \patchcmd{\minted@colorbg}{\noindent}{\medskip\noindent}{}{} 23 | \apptocmd{\endminted@colorbg}{\par\vspace{-7mm}\noindent}{}{} 24 | \makeatother -------------------------------------------------------------------------------- /contents/chapter_16_shiny/shiny/selecting/app.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | 3 | ui <- fluidPage( 4 | fluidRow( 5 | column(width = 10, 6 | align="center", 7 | plotOutput("scatterplot", 8 | height = 300, 9 | width = 400, 10 | brush = brushOpts(id = "selected_region"))) 11 | ), 12 | fluidRow( 13 | column(width = 10, 14 | h4("Selected points"), 15 | verbatimTextOutput("selected_region_info") 16 | ) 17 | ) 18 | ) 19 | 20 | server <- function(input, output){ 21 | 22 | output$scatterplot <- renderPlot({ 23 | ggplot(swiss, aes(x = Examination, y = Fertility)) + 24 | geom_point() + 25 | theme_classic() 26 | }) 27 | 28 | output$selected_region_info <- renderPrint({ 29 | brushedPoints(swiss, input$selected_region) 30 | }) 31 | } 32 | 33 | shinyApp(ui = ui, server = server) 34 | 35 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/data/rats.csv: -------------------------------------------------------------------------------- 1 | batch,m,n 2 | 1,0,20 3 | 2,0,20 4 | 3,0,20 5 | 4,0,20 6 | 5,0,20 7 | 6,0,20 8 | 7,0,20 9 | 8,0,19 10 | 9,0,19 11 | 10,0,19 12 | 11,0,19 13 | 12,0,18 14 | 13,0,18 15 | 14,0,17 16 | 15,1,20 17 | 16,1,20 18 | 17,1,20 19 | 18,1,20 20 | 19,1,19 21 | 20,1,19 22 | 21,1,18 23 | 22,1,18 24 | 23,3,27 25 | 24,2,25 26 | 25,2,24 27 | 26,2,23 28 | 27,2,20 29 | 28,2,20 30 | 29,2,20 31 | 30,2,20 32 | 31,2,20 33 | 32,2,20 34 | 33,1,10 35 | 34,5,49 36 | 35,2,19 37 | 36,5,46 38 | 37,2,17 39 | 38,7,49 40 | 39,7,47 41 | 40,3,20 42 | 41,3,20 43 | 42,2,13 44 | 43,9,48 45 | 44,10,50 46 | 45,4,20 47 | 46,4,20 48 | 47,4,20 49 | 48,4,20 50 | 49,4,20 51 | 50,4,20 52 | 51,4,20 53 | 52,10,48 54 | 53,4,19 55 | 54,4,19 56 | 55,4,19 57 | 56,5,22 58 | 57,11,46 59 | 58,12,49 60 | 59,5,20 61 | 60,5,20 62 | 61,6,23 63 | 62,5,19 64 | 63,6,22 65 | 64,6,20 66 | 65,6,20 67 | 66,6,20 68 | 67,16,52 69 | 68,15,46 70 | 69,15,47 71 | 70,9,24 72 | 71,4,14 73 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/stan/lmm.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int N; // no. observations 3 | int J; // no. groups 4 | 5 | vector[N] y; // outcome 6 | vector[N] x; // predictor 7 | int z[N]; // group index 8 | } 9 | 10 | parameters { 11 | vector[2] b; 12 | vector[2] beta[J]; 13 | real rho; 14 | vector[2] tau; 15 | real sigma; 16 | } 17 | 18 | transformed parameters { 19 | cov_matrix[2] Sigma; 20 | corr_matrix[2] Omega; 21 | Omega[1, 1] = 1; 22 | Omega[1, 2] = rho; 23 | Omega[2, 1] = rho; 24 | Omega[2, 2] = 1; 25 | Sigma = quad_form_diag(Omega, tau); 26 | } 27 | 28 | model { 29 | 30 | rho ~ uniform(-1, 1); 31 | tau ~ cauchy(0, 10); 32 | sigma ~ cauchy(0, 10); 33 | b ~ normal(0, 100); 34 | 35 | beta ~ multi_normal(b, Sigma); 36 | 37 | for (i in 1:N) 38 | y[i] ~ normal(beta[z[i], 1] + x[i] * beta[z[i], 2], sigma); 39 | 40 | } 41 | -------------------------------------------------------------------------------- /contents/chapter_15_hpc/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace Rcpp; 3 | 4 | //' Simple moving average of a numeric vector 5 | //' 6 | //' 7 | //' @param x A numeric vector 8 | //' @param k The window length of the moving average 9 | //' @return A vector of the same length of x 10 | //' @export 11 | // [[Rcpp::export]] 12 | NumericVector rolling_mean(NumericVector x, int k = 1) { 13 | 14 | // Declare outer loop counter 15 | int i; 16 | // Declare inner loop counter 17 | int j; 18 | // Declare vector length 19 | int n = x.size(); 20 | // Declare inner loop summation variable 21 | double total; 22 | // Declare output vector 23 | NumericVector y(n); 24 | 25 | for (i = 0; i < n; i++) { 26 | if (i < k - 1){ 27 | y[i] = NumericVector::get_na(); 28 | } else { 29 | total = 0; 30 | for (j = 0; j < k; j++) { 31 | total += x[i - j]; 32 | } 33 | y[i] = total/k; 34 | } 35 | } 36 | 37 | return y; 38 | } 39 | -------------------------------------------------------------------------------- /contents/chapter_16_shiny/shiny/gghistogram/app.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(tidyverse) 3 | 4 | houseprices_df <- read_csv("housing.csv") 5 | 6 | ui <- fluidPage( 7 | titlePanel("Histogram of house prices (Canadian dollars)."), 8 | 9 | sidebarLayout( 10 | sidebarPanel( 11 | sliderInput("binwidth", 12 | "Width of bins (Canadian dollars):", 13 | min = 1000, 14 | max = 12000, 15 | step = 1000, 16 | value = 5000), 17 | width = 4 18 | ), 19 | mainPanel( 20 | plotOutput("houseprices_hist"), 21 | width = 8 22 | ) 23 | ) 24 | ) 25 | 26 | server <- function(input, output) { 27 | 28 | output$houseprices_hist <- renderPlot({ 29 | 30 | ggplot(houseprices_df, aes(x = price)) + 31 | geom_histogram(binwidth = input$binwidth, colour = 'white') + 32 | theme_classic() 33 | 34 | }) 35 | 36 | } 37 | 38 | shinyApp(ui = ui, server = server) 39 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-2.Rmd: -------------------------------------------------------------------------------- 1 | 2 | --- 3 | title: "Data Analysis: A Report" 4 | author: "Mark Andrews" 5 | date: "October 25, 2019" 6 | output: pdf_document 7 | --- 8 | 9 | ```{r setup, echo=FALSE} 10 | knitr::opts_chunk$set(message = F, 11 | warning = F, 12 | out.width = "45%", 13 | fig.align='center') 14 | ``` 15 | 16 | # Introduction 17 | 18 | First, we will load the `tidyverse` packages, and read 19 | in the the from a `.csv` file. 20 | ```{r load_packages_data} 21 | library(tidyverse) 22 | data_df <- read_csv('example.csv') 23 | ``` 24 | 25 | # Analysis 26 | 27 | Here, we do a Pearson's correlation analysis. 28 | ```{r analysis} 29 | (corr_model <- cor.test(~ x + y, data = data_df)) 30 | ``` 31 | 32 | The correlation coefficient is 33 | `r round(corr_model$estimate, 3)`. 34 | 35 | # Visualization 36 | 37 | The scatterplot between $x$ and $y$ is shown 38 | in Figure \ref{fig:vis}. 39 | 40 | ```{r vis, echo=F, fig.cap='A scatterplot.'} 41 | ggplot(data_df, aes(x, y)) + 42 | geom_point() + 43 | theme_classic() 44 | ``` 45 | 46 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/crossed_structures.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center} 3 | \begin{tikzpicture}[every node/.style={draw,rectangle,rounded corners, inner sep=2mm}] 4 | \node (s1) at (0,0) {$s_1$}; 5 | \node[right=of s1] (s2) {$s_2$}; 6 | \node[draw=none,right=of s2] (sdots1) {$\ldots$}; 7 | \node[right=of sdots1] (s3) {$s_J$}; 8 | \node[right=30mm of s3] (w1) {$w_1$}; 9 | \node[right=of w1] (w2) {$w_2$}; 10 | \node[draw=none,right=of w2] (wdots1) {$\ldots$}; 11 | \node[right=of wdots1] (w3) {$w_K$}; 12 | \node (r1) at (4,-3) {$r_1$}; 13 | \node[right = of r1] (r2) {$r_2$}; 14 | \node[right = of r2] (r3) {$r_3$}; 15 | \node[draw=none,right = of r3] (rdots) {$...$}; 16 | \node[right = of rdots] (rn) {$r_n$}; 17 | 18 | \draw[->] (s1) to (r1); 19 | \draw[->] (w1) to (r1); 20 | \draw[->] (s3) to (r3); 21 | \draw[->] (w1) to (r3); 22 | \draw[->] (s2) to (r2); 23 | \draw[->] (w2) to (r2); 24 | \draw[->] (w3) to (rn); 25 | \draw[->] (s3) to (rn); 26 | \end{tikzpicture} 27 | \end{center} 28 | \caption{A \textit{crossed} multilevel data arrangement, such as would arise in a lexical decision experiment. Each $r_i$ is grouped under one of the $s_1, s_2 \ldots s_J$ and also one of the $w_1, w_2 \ldots w_K$.} 29 | \label{fig:crossed_diagram} 30 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/multilevel_sleepstudy.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \centering 3 | \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 4 | 5 | \node[mynode] (y_1) {$y_{jd}$}; 6 | \node[mynode,above = of y_1] (x_1) {$x_{jd}$}; 7 | \node[right=5mm of y_1] (i) {$d \in 1\ldots n_j$}; 8 | 9 | \node[mynodex, below=20mm of y_1] (beta) {$\vec{\beta}_j$}; 10 | \node[right=5mm of beta] (j) {$j \in 1\ldots J$}; 11 | \node[mynodex, left=13mm of beta] (sigma) {$\sigma$}; 12 | \node[right=25mm of beta] (phantom) {$$}; 13 | 14 | \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 15 | \node[draw,rounded corners=2mm,inner sep=10mm,fit=(y_1) (x_1) (beta) (phantom)] {}; 16 | 17 | \node[mynodex, below=13mm of beta] (b) {$\vec{b}$}; 18 | \node[mynodex, right= of b] (Sigma) {$\Sigma$}; 19 | 20 | \foreach \value in {x_1,beta} 21 | \draw[->] (\value) to (y_1); 22 | 23 | \draw[->] (sigma) [bend left] to (y_1); 24 | \draw[->] (b) to (beta); 25 | \draw[->] (Sigma) to (beta); 26 | \end{tikzpicture} 27 | \caption{Bayesian network diagrams for a multilevel varying slopes and intercepts linear model.} 28 | \label{fig:bda_sleepstudy_multilevel} 29 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/crossed_structures_s.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center}\resizebox{\textwidth}{!}{% 3 | \begin{tikzpicture}[every node/.style={draw,rectangle,rounded corners, inner sep=2mm}] 4 | \node (s1) at (0,0) {$s_1$}; 5 | \node[right=of s1] (s2) {$s_2$}; 6 | \node[draw=none,right=of s2] (sdots1) {$\ldots$}; 7 | \node[right=of sdots1] (s3) {$s_J$}; 8 | \node[right=30mm of s3] (w1) {$w_1$}; 9 | \node[right=of w1] (w2) {$w_2$}; 10 | \node[draw=none,right=of w2] (wdots1) {$\ldots$}; 11 | \node[right=of wdots1] (w3) {$w_K$}; 12 | \node (r1) at (4,-3) {$r_1$}; 13 | \node[right = of r1] (r2) {$r_2$}; 14 | \node[right = of r2] (r3) {$r_3$}; 15 | \node[draw=none,right = of r3] (rdots) {$...$}; 16 | \node[right = of rdots] (rn) {$r_n$}; 17 | 18 | \draw[->] (s1) to (r1); 19 | \draw[->] (w1) to (r1); 20 | \draw[->] (s3) to (r3); 21 | \draw[->] (w1) to (r3); 22 | \draw[->] (s2) to (r2); 23 | \draw[->] (w2) to (r2); 24 | \draw[->] (w3) to (rn); 25 | \draw[->] (s3) to (rn); 26 | \end{tikzpicture}} 27 | \end{center} 28 | \caption{A \textit{crossed} multilevel data arrangement, such as would arise in a lexical decision experiment. Each $r_i$ is grouped under one of the $s_1, s_2 \ldots s_J$ and also one of the $w_1, w_2 \ldots w_K$.} 29 | \label{fig:crossed_diagram} 30 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/multilevel_sleepstudy_s.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \centering 3 | \resizebox{0.5\textwidth}{!}{% 4 | \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 5 | 6 | \node[mynode] (y_1) {$y_{jd}$}; 7 | \node[mynode,above = of y_1] (x_1) {$x_{jd}$}; 8 | \node[right=5mm of y_1] (i) {$d \in 1\ldots n_j$}; 9 | 10 | \node[mynodex, below=20mm of y_1] (beta) {$\vec{\beta}_j$}; 11 | \node[right=5mm of beta] (j) {$j \in 1\ldots J$}; 12 | \node[mynodex, left=13mm of beta] (sigma) {$\sigma$}; 13 | \node[right=25mm of beta] (phantom) {$$}; 14 | 15 | \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 16 | \node[draw,rounded corners=2mm,inner sep=10mm,fit=(y_1) (x_1) (beta) (phantom)] {}; 17 | 18 | \node[mynodex, below=13mm of beta] (b) {$\vec{b}$}; 19 | \node[mynodex, right= of b] (Sigma) {$\Sigma$}; 20 | 21 | \foreach \value in {x_1,beta} 22 | \draw[->] (\value) to (y_1); 23 | 24 | \draw[->] (sigma) [bend left] to (y_1); 25 | \draw[->] (b) to (beta); 26 | \draw[->] (Sigma) to (beta); 27 | \end{tikzpicture}} 28 | \caption{Bayesian network diagrams for a multilevel varying slopes and intercepts linear model.} 29 | \label{fig:bda_sleepstudy_multilevel} 30 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_16_shiny/shiny/zooming/app.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(tidyverse) 3 | 4 | theme_set(theme_classic()) 5 | 6 | swiss_df <- swiss %>% as_tibble(rownames = 'region') 7 | 8 | ui <- fluidPage( 9 | fluidRow( 10 | column(width = 8, 11 | plotOutput("scatterplot", height = 400, width = 800, 12 | brush = brushOpts( 13 | id = "selected_region" 14 | ) 15 | ) 16 | ), 17 | column(width = 8, 18 | plotOutput("scattersubplot", height = 400, width = 800) 19 | ) 20 | ) 21 | ) 22 | 23 | server <- function(input, output) { 24 | 25 | xyrange <- reactiveValues(x = NULL, y = NULL) 26 | 27 | output$scatterplot <- renderPlot({ 28 | ggplot(swiss_df, 29 | aes(x = Examination, y = Fertility, label = region)) + 30 | geom_text() 31 | }) 32 | 33 | output$scattersubplot <- renderPlot({ 34 | ggplot(swiss_df, 35 | aes(x = Examination, y = Fertility, label = region)) + 36 | geom_text() + 37 | coord_cartesian(xlim = xyrange$x, ylim = xyrange$y) 38 | }) 39 | 40 | observe({ 41 | sel_reg <- input$selected_region 42 | xyrange$x <- c(sel_reg$xmin, sel_reg$xmax) 43 | xyrange$y <- c(sel_reg$ymin, sel_reg$ymax) 44 | }) 45 | 46 | } 47 | 48 | shinyApp(ui, server) -------------------------------------------------------------------------------- /contents/chapter_16_shiny/shiny/learnr/example.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "An Interactive Tutorial" 3 | author: "Mark Andrews" 4 | date: "Aug 23, 2020" 5 | output: learnr::tutorial 6 | runtime: shiny_prerendered 7 | --- 8 | 9 | ```{r setup, include=FALSE} 10 | library(learnr) 11 | knitr::opts_chunk$set(echo = FALSE) 12 | library(tidyverse) 13 | ``` 14 | 15 | ```{r quiz} 16 | quiz( 17 | question("Which of the following models are used for modelling binary variables?", 18 | answer("Linear regression"), 19 | answer("Ordinal regression"), 20 | answer("Binary logistic regression", correct = TRUE), 21 | answer("Probit regression", correct = TRUE), 22 | answer("Poisson regression") 23 | ), 24 | question("Which of the following are continous probability distributions?", 25 | answer("Normal distribution", correct = TRUE), 26 | answer("Binomial distribution"), 27 | answer("Poisson distribution"), 28 | answer("Negative binomial distribution") 29 | ) 30 | ) 31 | ``` 32 | 33 | The following code is for a linear regression. 34 | 35 | ```{r swiss_lm_1, exercise=TRUE, exercise.eval=FALSE} 36 | M <- lm(Fertility ~ Catholic, data = swiss) 37 | ``` 38 | 39 | Enter the `lm` model to predict `Fertility` from `Education`. 40 | ```{r swiss_lm_2, exercise = TRUE} 41 | 42 | ``` 43 | 44 | ```{r swiss_lm_2-solution} 45 | M <- lm(Fertility ~ Education, data = swiss) 46 | ``` 47 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/classroom.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center} 3 | \resizebox{\linewidth}{!}{ 4 | \begin{tikzpicture} 5 | \node {data} 6 | child {node[level 1] (s_1) {$\text{school}_1$} 7 | child {node[level 2] (s_1_c_1) {$\text{class}_{1}$} 8 | child {node[level 3] (S_1_c_1_p_1) {$\text{child}_{1}$}} 9 | child {node[level 3] (S_1_c_1_p_2) {$\text{child}_{2}$}} 10 | child {node (S_1_c_1_p_dots) {$\ldots$}} 11 | } 12 | child {node[level 2] (s_1_c_2) {$\text{class}_{2}$}} 13 | child {node (s_1_c_dots) {$\ldots$}} 14 | } 15 | child {node[level 1, xshift=10mm] (s_2) {$\text{school}_2$} 16 | child {node (S_2_c_dots) {$\ldots$}} 17 | child {node (S_2_c_dots2) {$\ldots$}} 18 | } 19 | child {node (s_dots) {$\ldots$}} 20 | child {node[level 1] (s_K) {$\text{school}_K$} 21 | child {node (s_K_c_J_dots) {$\ldots$}} 22 | child {node[level 2] (s_K_c_J_2) {$\text{class}_{J-1}$}} 23 | child {node[level 2] (s_K_c_J) {$\text{class}_{J}$} 24 | child {node (S_K_c_J_p_N_dots) {$\ldots$}} 25 | child {node[level 3] (S_K_c_J_p_N-1) {$\text{child}_{n-1}$}} 26 | child {node[level 3] (S_K_c_J_p_N) {$\text{child}_{n}$}} 27 | } 28 | } 29 | ; 30 | \end{tikzpicture} 31 | } 32 | \end{center} 33 | \caption{The hierarchical arrangement of the data in the \texttt{classroom\_df} data set. Each one of the $N$ children is in one of $J$ classes and each one of the $J$ classes is in one of $K$ schools.} 34 | \label{fig:classroom:tree} 35 | \end{figure} 36 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/classroom_s.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center} 3 | \resizebox{\linewidth}{!}{ 4 | \begin{tikzpicture} 5 | \node {data} 6 | child {node[level 1] (s_1) {$\text{school}_1$} 7 | child {node[level 2] (s_1_c_1) {$\text{class}_{1}$} 8 | child {node[level 3] (S_1_c_1_p_1) {$\text{child}_{1}$}} 9 | child {node[level 3] (S_1_c_1_p_2) {$\text{child}_{2}$}} 10 | child {node (S_1_c_1_p_dots) {$\ldots$}} 11 | } 12 | child {node[level 2] (s_1_c_2) {$\text{class}_{2}$}} 13 | child {node (s_1_c_dots) {$\ldots$}} 14 | } 15 | child {node[level 1, xshift=10mm] (s_2) {$\text{school}_2$} 16 | child {node (S_2_c_dots) {$\ldots$}} 17 | child {node (S_2_c_dots2) {$\ldots$}} 18 | } 19 | child {node (s_dots) {$\ldots$}} 20 | child {node[level 1] (s_K) {$\text{school}_K$} 21 | child {node (s_K_c_J_dots) {$\ldots$}} 22 | child {node[level 2] (s_K_c_J_2) {$\text{class}_{J-1}$}} 23 | child {node[level 2] (s_K_c_J) {$\text{class}_{J}$} 24 | child {node (S_K_c_J_p_N_dots) {$\ldots$}} 25 | child {node[level 3] (S_K_c_J_p_N-1) {$\text{child}_{n-1}$}} 26 | child {node[level 3] (S_K_c_J_p_N) {$\text{child}_{n}$}} 27 | } 28 | } 29 | ; 30 | \end{tikzpicture} 31 | } 32 | \end{center} 33 | \caption{The hierarchical arrangement of the data in the \texttt{classroom\_df} data set. Each one of the $N$ children is in one of $J$ classes and each one of the $J$ classes is in one of $K$ schools.} 34 | \label{fig:classroom:tree} 35 | \end{figure} 36 | -------------------------------------------------------------------------------- /contents/chapter_16_shiny/shiny/markdown/report.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "A Shiny RMarkdown Page" 3 | author: "Mark Andrews" 4 | date: "23/08/2020" 5 | output: html_document 6 | runtime: shiny 7 | --- 8 | 9 | ```{r setup, include=FALSE} 10 | knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE) 11 | ``` 12 | 13 | Choose a predictor variable from the list `Agriculture`, `Examination`, `Education`, `Catholic`, `Infant.Mortality`. 14 | A scatterplot of the points with their labels is shown, as is the line of the best fit. 15 | The line of best fit is 16 | $$ 17 | y = \beta_0 + \beta_1 x, 18 | $$ 19 | where $\beta_0$ and $\beta_1$ are the coefficients of the normal linear regression model predicting `Fertility` from the chosen predictor. 20 | ```{r swiss, echo=FALSE} 21 | library(tidyverse) 22 | library(ggrepel) 23 | 24 | inputPanel( 25 | selectInput("predictor", 26 | label = "Select predictor:", 27 | choices = c("Agriculture", 28 | "Examination", 29 | "Education", 30 | "Catholic", 31 | "Infant.Mortality") 32 | ) 33 | ) 34 | 35 | renderPlot({ 36 | swiss %>% 37 | as_tibble(rownames = 'region') %>% 38 | ggplot(aes_string(x = input$predictor, y = "Fertility", label = "region")) + 39 | stat_smooth(method = 'lm', se = F) + 40 | geom_point() + 41 | geom_text_repel() + 42 | theme_classic() 43 | }) 44 | ``` 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /contents/chapter_04_visualization/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_04_visualization 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | clean: 28 | rm -f $(main).aux 29 | rm -f $(main).log 30 | rm -f $(main).toc 31 | rm -rf $(main)_files 32 | rm -rf $(main)_slides_files 33 | rm -rf $(main)_slides.tex 34 | 35 | mrproper: clean 36 | rm -f $(main).tex 37 | rm -f $(main).pdf 38 | rm -f $(main).md 39 | rm -f $(main).html 40 | rm -f $(main).docx 41 | rm -f $(main)_slides.pdf 42 | rm -f $(bibtex) -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/J_binomial.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center} 3 | \begin{tikzpicture}[mynode/.style={draw,circle, minimum width=5mm,align=center}] 4 | \node[mynode, fill=red!10] (m_1) {$m_1$}; 5 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_1] (n_1) {$n_1$}; 6 | \node[mynode, above=1cm of m_1] (p_1) {$\beta_1$}; 7 | \node[mynode, fill=red!10, right=2cm of m_1] (m_2) {$m_2$}; 8 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_2] (n_2) {$n_2$}; 9 | \node[mynode, above=1cm of m_2] (p_2) {$\beta_2$}; 10 | \draw[->] (p_1) -- (m_1); 11 | \draw[->] (n_1) -- (m_1); 12 | \draw[->] (p_2) -- (m_2); 13 | \draw[->] (n_2) -- (m_2); 14 | \node[right=2cm of n_2] (dots) {$\cdots$}; 15 | \node[mynode, fill=red!10, right=6cm of m_1] (m_j) {$m_j$}; 16 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_j] (n_j) {$n_j$}; 17 | \node[mynode, above=1cm of m_j] (p_j) {$\beta_j$}; 18 | \draw[->] (p_j) -- (m_j); 19 | \draw[->] (n_j) -- (m_j); 20 | \node[right=2cm of n_j] (dots_2) {$\cdots$}; 21 | \node[mynode, fill=red!10, right=3cm of m_j] (m_J) {$m_J$}; 22 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_J] (n_J) {$n_J$}; 23 | \node[mynode, above=1cm of m_J] (p_J) {$\beta_J$}; 24 | \draw[->] (p_J) -- (m_J); 25 | \draw[->] (n_J) -- (m_J); 26 | \end{tikzpicture} 27 | \end{center} 28 | \caption{Inferring the log odds of a tumour $\beta_j$ in each of the $J$ batches is identical to $J$ independent binomial models.} 29 | \label{fig:J_binomial} 30 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_01_datascience/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_01_datascience 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | SHELL=/usr/bin/bash 5 | 6 | .PHONY: clean mrproper all 7 | 8 | all: $(main).pdf $(main).md $(main).html $(main).docx 9 | 10 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 11 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 12 | 13 | $(main).md: $(main).Rmd $(header) $(bibtex) 14 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 15 | 16 | $(main).html: $(main).Rmd $(header) $(bibtex) 17 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 18 | 19 | $(main).docx: $(main).Rmd $(header) $(bibtex) 20 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 21 | 22 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 23 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 24 | 25 | $(bibtex): 26 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 27 | 28 | clean: 29 | rm -f $(main).aux 30 | rm -f $(main).log 31 | rm -f $(main).toc 32 | rm -rf $(main)_files 33 | rm -rf $(main)_slides_files 34 | rm -rf $(main)_slides.tex 35 | 36 | mrproper: clean 37 | rm -f $(main).tex 38 | rm -f $(main).pdf 39 | rm -f $(main).md 40 | rm -f $(main).html 41 | rm -f $(main).docx 42 | rm -f $(main)_slides.pdf 43 | rm -f $(bibtex) 44 | -------------------------------------------------------------------------------- /contents/chapter_02_introduction/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_02_introduction 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | SHELL=/usr/bin/bash 5 | 6 | .PHONY: clean mrproper all 7 | 8 | all: $(main).pdf $(main).md $(main).html $(main).docx 9 | 10 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 11 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 12 | 13 | $(main).md: $(main).Rmd $(header) $(bibtex) 14 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 15 | 16 | $(main).html: $(main).Rmd $(header) $(bibtex) 17 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 18 | 19 | $(main).docx: $(main).Rmd $(header) $(bibtex) 20 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 21 | 22 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 23 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 24 | 25 | $(bibtex): 26 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 27 | 28 | clean: 29 | rm -f $(main).aux 30 | rm -f $(main).log 31 | rm -f $(main).toc 32 | rm -rf $(main)_files 33 | rm -rf $(main)_slides_files 34 | rm -rf $(main)_slides.tex 35 | 36 | mrproper: clean 37 | rm -f $(main).tex 38 | rm -f $(main).pdf 39 | rm -f $(main).md 40 | rm -f $(main).html 41 | rm -f $(main).docx 42 | rm -f $(main)_slides.pdf 43 | rm -f $(bibtex) 44 | -------------------------------------------------------------------------------- /contents/chapter_03_wrangling/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_03_wrangling 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | clean: 28 | rm -f $(main).aux 29 | rm -f $(main).log 30 | rm -f $(main).toc 31 | rm -rf $(main)_files 32 | rm -rf $(main)_slides_files 33 | rm -rf $(main)_slides_cache 34 | rm -rf $(main)_slides.tex 35 | 36 | mrproper: clean 37 | rm -f $(main).tex 38 | rm -f $(main).pdf 39 | rm -f $(main).md 40 | rm -f $(main).html 41 | rm -f $(main).docx 42 | rm -f $(main)_slides.pdf 43 | rm -f $(bibtex) 44 | -------------------------------------------------------------------------------- /contents/chapter_05_exploratory/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_05_exploratory 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | clean: 28 | rm -f $(main).aux 29 | rm -f $(main).log 30 | rm -f $(main).toc 31 | rm -rf $(main)_files 32 | rm -rf $(main)_slides_files 33 | rm -rf $(main)_slides.tex 34 | rm -rf include_graphics 35 | 36 | mrproper: clean 37 | rm -f $(main).tex 38 | rm -f $(main).pdf 39 | rm -f $(main).md 40 | rm -f $(main).html 41 | rm -f $(main).docx 42 | rm -f $(main)_slides.pdf 43 | rm -f $(bibtex) 44 | -------------------------------------------------------------------------------- /contents/chapter_10_logistic/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_10_logistic 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | clean: 28 | rm -f $(main).aux 29 | rm -f $(main).log 30 | rm -f $(main).toc 31 | rm -rf $(main)_files 32 | rm -rf $(main)_cache 33 | rm -f $(main).tex 34 | rm -rf $(main)_slides_files 35 | rm -rf $(main)_slides.tex 36 | 37 | 38 | mrproper: clean 39 | rm -f $(main).pdf 40 | rm -f $(main).md 41 | rm -f $(main).html 42 | rm -f $(main).docx 43 | rm -f $(main)_slides.pdf 44 | rm -f $(bibtex) 45 | 46 | 47 | -------------------------------------------------------------------------------- /contents/chapter_06_programming/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_06_programming 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | clean: 28 | rm -f $(main).aux 29 | rm -f $(main).log 30 | rm -f $(main).toc 31 | rm -rf $(main)_files 32 | rm -rf $(main)_slides_files 33 | rm -rf $(main)_slides.tex 34 | rm -rf include_graphics 35 | rm -f *data*.csv 36 | 37 | mrproper: clean 38 | rm -f $(main).tex 39 | rm -f $(main).pdf 40 | rm -f $(main).md 41 | rm -f $(main).html 42 | rm -f $(main).docx 43 | rm -f $(main)_slides.pdf 44 | rm -f $(bibtex) 45 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/J_binomial_s.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center} 3 | \resizebox{\textwidth}{!}{% 4 | \begin{tikzpicture}[mynode/.style={draw,circle, minimum width=5mm,align=center}] 5 | \node[mynode, fill=red!10] (m_1) {$m_1$}; 6 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_1] (n_1) {$n_1$}; 7 | \node[mynode, above=1cm of m_1] (p_1) {$\beta_1$}; 8 | \node[mynode, fill=red!10, right=2cm of m_1] (m_2) {$m_2$}; 9 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_2] (n_2) {$n_2$}; 10 | \node[mynode, above=1cm of m_2] (p_2) {$\beta_2$}; 11 | \draw[->] (p_1) -- (m_1); 12 | \draw[->] (n_1) -- (m_1); 13 | \draw[->] (p_2) -- (m_2); 14 | \draw[->] (n_2) -- (m_2); 15 | \node[right=2cm of n_2] (dots) {$\cdots$}; 16 | \node[mynode, fill=red!10, right=6cm of m_1] (m_j) {$m_j$}; 17 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_j] (n_j) {$n_j$}; 18 | \node[mynode, above=1cm of m_j] (p_j) {$\beta_j$}; 19 | \draw[->] (p_j) -- (m_j); 20 | \draw[->] (n_j) -- (m_j); 21 | \node[right=2cm of n_j] (dots_2) {$\cdots$}; 22 | \node[mynode, fill=red!10, right=3cm of m_j] (m_J) {$m_J$}; 23 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_J] (n_J) {$n_J$}; 24 | \node[mynode, above=1cm of m_J] (p_J) {$\beta_J$}; 25 | \draw[->] (p_J) -- (m_J); 26 | \draw[->] (n_J) -- (m_J); 27 | \end{tikzpicture} 28 | } 29 | 30 | \end{center} 31 | \caption{Inferring the log odds of a tumour $\beta_j$ in each of the $J$ batches is identical to $J$ independent binomial models.} 32 | \label{fig:J_binomial} 33 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_16_shiny/Makefile: -------------------------------------------------------------------------------- 1 | main = chapter_16_shiny 2 | bibtex = ../mjandrews.bib 3 | header = ../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(bibtex) $(header) 10 | Rscript -e "rmarkdown::render('$(main).Rmd')" 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | clean: 25 | rm -f $(main).log 26 | rm -rf $(main).tex 27 | rm -rf $(main).aux 28 | rm -f $(main).toc 29 | rm -rf $(main)_files 30 | rm -rf $(main)_cache 31 | rm -rf $(main)_slides_files 32 | rm -rf $(main)_slides_cache 33 | rm -rf $(main)_slides.tex 34 | rm -rf $(main)_slides.aux 35 | rm -rf $(main)_slides.log 36 | rm -rf $(main)_slides.toc 37 | 38 | mrproper: clean 39 | rm -f $(main).pdf 40 | rm -f $(bibtex) 41 | rm -rf $(main)_slides.pdf 42 | 43 | $(bibtex): 44 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 45 | -------------------------------------------------------------------------------- /contents/chapter_11_generalized/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_11_generalized 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | clean: 28 | rm -f $(main).aux 29 | rm -f $(main).log 30 | rm -f $(main).toc 31 | rm -rf $(main)_files 32 | rm -rf $(main)_cache 33 | rm -f $(main).tex 34 | rm -rf $(main)_slides_files 35 | rm -rf $(main)_slides.tex 36 | rm -rf $(main)_slides_cache 37 | 38 | 39 | mrproper: clean 40 | rm -f $(main).pdf 41 | rm -f $(main).md 42 | rm -f $(main).html 43 | rm -f $(main).docx 44 | rm -f $(main)_slides.pdf 45 | rm -f $(bibtex) 46 | 47 | 48 | -------------------------------------------------------------------------------- /contents/chapter_04_visualization/data/FatRats.csv: -------------------------------------------------------------------------------- 1 | "","Gain","Protein","Source" 2 | "1",73,"Hi","Beef" 3 | "2",102,"Hi","Beef" 4 | "3",118,"Hi","Beef" 5 | "4",104,"Hi","Beef" 6 | "5",81,"Hi","Beef" 7 | "6",107,"Hi","Beef" 8 | "7",100,"Hi","Beef" 9 | "8",87,"Hi","Beef" 10 | "9",117,"Hi","Beef" 11 | "10",111,"Hi","Beef" 12 | "11",98,"Hi","Cereal" 13 | "12",74,"Hi","Cereal" 14 | "13",56,"Hi","Cereal" 15 | "14",111,"Hi","Cereal" 16 | "15",95,"Hi","Cereal" 17 | "16",88,"Hi","Cereal" 18 | "17",82,"Hi","Cereal" 19 | "18",77,"Hi","Cereal" 20 | "19",86,"Hi","Cereal" 21 | "20",92,"Hi","Cereal" 22 | "21",94,"Hi","Pork" 23 | "22",79,"Hi","Pork" 24 | "23",96,"Hi","Pork" 25 | "24",98,"Hi","Pork" 26 | "25",102,"Hi","Pork" 27 | "26",102,"Hi","Pork" 28 | "27",108,"Hi","Pork" 29 | "28",91,"Hi","Pork" 30 | "29",120,"Hi","Pork" 31 | "30",105,"Hi","Pork" 32 | "31",90,"Lo","Beef" 33 | "32",76,"Lo","Beef" 34 | "33",90,"Lo","Beef" 35 | "34",64,"Lo","Beef" 36 | "35",86,"Lo","Beef" 37 | "36",51,"Lo","Beef" 38 | "37",72,"Lo","Beef" 39 | "38",90,"Lo","Beef" 40 | "39",95,"Lo","Beef" 41 | "40",78,"Lo","Beef" 42 | "41",107,"Lo","Cereal" 43 | "42",95,"Lo","Cereal" 44 | "43",97,"Lo","Cereal" 45 | "44",80,"Lo","Cereal" 46 | "45",98,"Lo","Cereal" 47 | "46",74,"Lo","Cereal" 48 | "47",74,"Lo","Cereal" 49 | "48",67,"Lo","Cereal" 50 | "49",89,"Lo","Cereal" 51 | "50",58,"Lo","Cereal" 52 | "51",49,"Lo","Pork" 53 | "52",82,"Lo","Pork" 54 | "53",75,"Lo","Pork" 55 | "54",86,"Lo","Pork" 56 | "55",81,"Lo","Pork" 57 | "56",97,"Lo","Pork" 58 | "57",106,"Lo","Pork" 59 | "58",70,"Lo","Pork" 60 | "59",61,"Lo","Pork" 61 | "60",82,"Lo","Pork" 62 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/utils.py: -------------------------------------------------------------------------------- 1 | def mktable(symbols, 2 | nrow = 3, 3 | justification = 'cc'): 4 | 5 | ''' 6 | This monstrosity returns a string that is a LaTeX tabular 7 | table of LaTeX math(s) commands and how they appear when 8 | rendered. 9 | ''' 10 | 11 | n = len(symbols) 12 | 13 | ncol = (n % nrow > 0) * 1 + n // nrow 14 | 15 | cc = '|'.join([justification] * ncol) 16 | 17 | # These four are for dealing with slashes and braces in 18 | # the verbatim of the \LaTeX command. 19 | G = lambda x: x.replace('\\', '\\textbackslash{}') 20 | 21 | F = lambda x: x.replace('{','LEFTCURLYBRACE').replace('}','RIGHTCURLYBRACE') 22 | 23 | H = lambda x: x.replace('LEFTCURLYBRACE', r'\{').replace('RIGHTCURLYBRACE', r'\}') 24 | 25 | J = lambda x: x.replace('^', r'\^{}').replace('_', '\_') 26 | 27 | def slice_keys(x, k): 28 | return ' & '.join([x[k,key[1]] for key in x.keys() if key[0] == k]) 29 | 30 | x = {} 31 | for i, symbol in enumerate(symbols): 32 | col, row = divmod(i, nrow) 33 | x[row, col] = "\\texttt{%s} & $%s$" % (J(H(G(F(symbol)))), symbol) 34 | 35 | S = [] 36 | for i in range(nrow): 37 | s = slice_keys(x, i) 38 | s = s + ' & ' * (ncol * 2 - s.count('$')) 39 | S.append(s) 40 | 41 | 42 | return '\n'.join([ 43 | '\\begin{center}', 44 | '\\begin{tabular}{%s}' % cc, 45 | '\\\\ \n'.join(S), 46 | '\\end{tabular}', 47 | '\\end{center}' 48 | ]) 49 | -------------------------------------------------------------------------------- /contents/chapter_09_linear/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_09_linear 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | clean: 28 | rm -f $(main).aux 29 | rm -f $(main).log 30 | rm -f $(main).toc 31 | rm -rf $(main)_files 32 | rm -rf $(main)_cache 33 | rm -f $(main).tex 34 | rm -f $(main)_slides.tex 35 | rm -f $(main)_slides.aux 36 | rm -f $(main)_slides.snm 37 | rm -f $(main)_slides.toc 38 | rm -f $(main)_slides.vrb 39 | rm -f $(main)_slides.nav 40 | rm -rf $(main)_slides_files 41 | 42 | mrproper: clean 43 | rm -f $(main).pdf 44 | rm -f $(main).md 45 | rm -f $(main).html 46 | rm -f $(main).docx 47 | rm -f $(bibtex) 48 | rm -f $(main)_slides.pdf -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_12_multilevel 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | clean: 28 | rm -f $(main).aux 29 | rm -f $(main).log 30 | rm -f $(main).toc 31 | rm -rf $(main)_files 32 | rm -rf $(main)_cache 33 | rm -f $(main).tex 34 | rm -rf $(main)_slides_files 35 | rm -rf $(main)_slides.tex 36 | rm -rf $(main)_slides.aux 37 | rm -rf $(main)_slides.nav 38 | rm -rf $(main)_slides.snm 39 | rm -rf $(main)_slides.toc 40 | rm -rf $(main)_slides.vrb 41 | 42 | 43 | mrproper: clean 44 | rm -f $(main).pdf 45 | rm -f $(main).md 46 | rm -f $(main).html 47 | rm -f $(main).docx 48 | rm -f $(main)_slides.pdf 49 | rm -f $(bibtex) 50 | 51 | 52 | -------------------------------------------------------------------------------- /contents/chapter_08_inference/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_08_inference 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | clean: 28 | rm -f $(main).aux 29 | rm -f $(main).log 30 | rm -f $(main).toc 31 | rm -rf $(main)_files 32 | rm -rf $(main)_cache 33 | rm -f $(main).tex 34 | rm -f $(main)_slides.tex 35 | rm -f $(main)_slides.aux 36 | rm -f $(main)_slides.snm 37 | rm -f $(main)_slides.toc 38 | rm -f $(main)_slides.vrb 39 | rm -f $(main)_slides.nav 40 | rm -rf $(main)_slides_files 41 | rm -rf $(main)_slides_cache 42 | 43 | mrproper: clean 44 | rm -f $(main).pdf 45 | rm -f $(main).md 46 | rm -f $(main).html 47 | rm -f $(main).docx 48 | rm -f $(bibtex) 49 | rm -f $(main)_slides.pdf 50 | 51 | -------------------------------------------------------------------------------- /contents/chapter_13_nlregression/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_13_nlregression 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | 28 | clean: 29 | rm -f $(main).aux 30 | rm -f $(main).log 31 | rm -f $(main).toc 32 | rm -rf $(main)_files 33 | rm -rf $(main)_cache 34 | rm -f $(main).tex 35 | rm -rf $(main)_slides_files 36 | rm -rf $(main)_slides.tex 37 | rm -rf $(main)_slides.aux 38 | rm -rf $(main)_slides.nav 39 | rm -rf $(main)_slides.snm 40 | rm -rf $(main)_slides.toc 41 | rm -rf $(main)_slides.vrb 42 | rm -rf $(main)_slides.log 43 | rm -rf $(main)_slides_cache 44 | 45 | 46 | mrproper: clean 47 | rm -f $(main).pdf 48 | rm -f $(main).md 49 | rm -f $(main).html 50 | rm -f $(main).docx 51 | rm -f $(main)_slides.pdf 52 | rm -f $(bibtex) 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /contents/chapter_14_sem/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_14_sem 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 11 | 12 | $(main).md: $(main).Rmd $(header) $(bibtex) 13 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 14 | 15 | $(main).html: $(main).Rmd $(header) $(bibtex) 16 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 17 | 18 | $(main).docx: $(main).Rmd $(header) $(bibtex) 19 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 20 | 21 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 22 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 23 | 24 | $(bibtex): 25 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 26 | 27 | 28 | 29 | 30 | clean: 31 | rm -f $(main).aux 32 | rm -f $(main).log 33 | rm -f $(main).toc 34 | rm -rf $(main)_files 35 | rm -rf $(main)_cache 36 | rm -f $(main).tex 37 | rm -rf $(main)_slides_files 38 | rm -rf $(main)_slides.tex 39 | rm -rf $(main)_slides.aux 40 | rm -rf $(main)_slides.nav 41 | rm -rf $(main)_slides.snm 42 | rm -rf $(main)_slides.toc 43 | rm -rf $(main)_slides.vrb 44 | rm -rf $(main)_slides.log 45 | rm -rf $(main)_slides_cache 46 | rm -rf tmp_*.txt 47 | 48 | 49 | mrproper: clean 50 | rm -f $(main).pdf 51 | rm -f $(main).md 52 | rm -f $(main).html 53 | rm -f $(main).docx 54 | rm -f $(main)_slides.pdf 55 | rm -f $(bibtex) 56 | 57 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_07_reproducible 2 | bibtex=../mjandrews.bib 3 | header=../header.tex 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).pdf: $(main).Rmd $(header) $(bibtex) 10 | bash ./venv_setup.sh 11 | Rscript -e 'rmarkdown::render("$(main).Rmd")' 12 | 13 | $(main).md: $(main).Rmd $(header) $(bibtex) 14 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 15 | 16 | $(main).html: $(main).Rmd $(header) $(bibtex) 17 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 18 | 19 | $(main).docx: $(main).Rmd $(header) $(bibtex) 20 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 21 | 22 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 23 | bash ./venv_setup.sh 24 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 25 | 26 | $(bibtex): 27 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 28 | 29 | clean: 30 | rm -f $(main).aux 31 | rm -f $(main).log 32 | rm -f $(main).toc 33 | rm -rf $(main)_files 34 | rm -rf chapter_07_venv 35 | rm -rf __pycache__ 36 | rm -rf examples/*.pdf 37 | rm -rf examples/*.tex 38 | rm -rf examples/*.html 39 | rm -f $(main).tex 40 | rm -f $(main)_slides.tex 41 | rm -f $(main)_slides.aux 42 | rm -f $(main)_slides.snm 43 | rm -f $(main)_slides.toc 44 | rm -f $(main)_slides.vrb 45 | rm -f $(main)_slides.nav 46 | 47 | mrproper: clean 48 | rm -f $(main).pdf 49 | rm -f $(main).md 50 | rm -f $(main).html 51 | rm -f $(main).docx 52 | rm -f $(bibtex) 53 | rm -f $(main)_slides.pdf 54 | -------------------------------------------------------------------------------- /contents/chapter_15_hpc/Makefile: -------------------------------------------------------------------------------- 1 | mkpdf = pdflatex -shell-escape -interaction=nonstopmode 2 | main = chapter_15_hpc 3 | bibtex = ../mjandrews.bib 4 | 5 | .PHONY: clean mrproper all 6 | 7 | all: $(main).pdf $(main).md $(main).html $(main).docx 8 | 9 | $(main).tex: $(main).Rmd $(bibtex) 10 | Rscript -e "rmarkdown::render('$(main).Rmd')" 11 | 12 | $(main).pdf: $(main).tex 13 | $(mkpdf) $(main).tex 14 | $(mkpdf) $(main).tex 15 | $(mkpdf) $(main).tex 16 | 17 | $(main).md: $(main).Rmd $(header) $(bibtex) 18 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 19 | 20 | $(main).html: $(main).Rmd $(header) $(bibtex) 21 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 22 | 23 | $(main).docx: $(main).Rmd $(header) $(bibtex) 24 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 25 | 26 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) 27 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 28 | 29 | clean: 30 | rm -f $(main).log 31 | rm -rf $(main).aux 32 | rm -rf _minted-$(main) 33 | rm -f $(main).toc 34 | rm -rf logs 35 | rm -rf derby.log 36 | rm -rf $(main)_files 37 | rm -rf $(main)_cache 38 | rm -rf $(main)_slides_files 39 | rm -rf $(main)_slides_cache 40 | rm -rf $(main)_slides.tex 41 | rm -rf $(main)_slides.aux 42 | rm -rf $(main)_slides.log 43 | rm -rf $(main)_slides.toc 44 | rm -rf _minted-$(main)_slides 45 | 46 | mrproper: clean 47 | rm -rf $(main).tex 48 | rm -f $(main).pdf 49 | rm -f $(bibtex) 50 | rm -rf $(main)_slides.pdf 51 | 52 | $(bibtex): 53 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 54 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/multilevel_binomial.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center}\resizebox{\textwidth}{!}{% 3 | \begin{tikzpicture}[mynode/.style={draw,circle, minimum width=5mm,align=center}] 4 | \node[mynode, fill=red!10] (m_1) {$m_1$}; 5 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_1] (n_1) {$n_1$}; 6 | \node[mynode, above=1cm of m_1] (p_1) {$\beta_1$}; 7 | \node[mynode, fill=red!10, right=2cm of m_1] (m_2) {$m_2$}; 8 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_2] (n_2) {$n_2$}; 9 | \node[mynode, above=1cm of m_2] (p_2) {$\beta_2$}; 10 | \draw[->] (p_1) -- (m_1); 11 | \draw[->] (n_1) -- (m_1); 12 | \draw[->] (p_2) -- (m_2); 13 | \draw[->] (n_2) -- (m_2); 14 | \node[right=2cm of n_2] (dots) {$\cdots$}; 15 | \node[mynode, fill=red!10, right=6cm of m_1] (m_j) {$m_j$}; 16 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_j] (n_j) {$n_j$}; 17 | \node[mynode, above=1cm of m_j] (p_j) {$\beta_j$}; 18 | \draw[->] (p_j) -- (m_j); 19 | \draw[->] (n_j) -- (m_j); 20 | \node[right=2cm of n_j] (dots_2) {$\cdots$}; 21 | \node[mynode, fill=red!10, right=3cm of m_j] (m_J) {$m_J$}; 22 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_J] (n_J) {$n_J$}; 23 | \node[mynode, above=1cm of m_J] (p_J) {$\beta_J$}; 24 | \draw[->] (p_J) -- (m_J); 25 | \draw[->] (n_J) -- (m_J); 26 | \node[mynode, above left =3cm and 1cm of dots] (b) {$b$}; 27 | \node[mynode, above right=3cm and 1cm of dots] (tau) {$\tau$}; 28 | \draw[->] (b) to (p_1); 29 | \draw[->] (b) -- (p_2); 30 | \draw[->] (b) -- (p_j); 31 | \draw[->] (b) to (p_J); 32 | \draw[->] (tau) to (p_1); 33 | \draw[->] (tau) -- (p_2); 34 | \draw[->] (tau) -- (p_j); 35 | \draw[->] (tau) to (p_J); 36 | \end{tikzpicture}} 37 | \end{center} 38 | \caption{The multilevel binomial logistic regression.} 39 | \label{fig:multilevel_binomial} 40 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/multilevel_binomial_s.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center}\resizebox{\textwidth}{!}{% 3 | \begin{tikzpicture}[mynode/.style={draw,circle, minimum width=5mm,align=center}] 4 | \node[mynode, fill=red!10] (m_1) {$m_1$}; 5 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_1] (n_1) {$n_1$}; 6 | \node[mynode, above=1cm of m_1] (p_1) {$\beta_1$}; 7 | \node[mynode, fill=red!10, right=2cm of m_1] (m_2) {$m_2$}; 8 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_2] (n_2) {$n_2$}; 9 | \node[mynode, above=1cm of m_2] (p_2) {$\beta_2$}; 10 | \draw[->] (p_1) -- (m_1); 11 | \draw[->] (n_1) -- (m_1); 12 | \draw[->] (p_2) -- (m_2); 13 | \draw[->] (n_2) -- (m_2); 14 | \node[right=2cm of n_2] (dots) {$\cdots$}; 15 | \node[mynode, fill=red!10, right=6cm of m_1] (m_j) {$m_j$}; 16 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_j] (n_j) {$n_j$}; 17 | \node[mynode, above=1cm of m_j] (p_j) {$\beta_j$}; 18 | \draw[->] (p_j) -- (m_j); 19 | \draw[->] (n_j) -- (m_j); 20 | \node[right=2cm of n_j] (dots_2) {$\cdots$}; 21 | \node[mynode, fill=red!10, right=3cm of m_j] (m_J) {$m_J$}; 22 | \node[mynode, fill=red!10, above left=0.5cm and 0.5cm of m_J] (n_J) {$n_J$}; 23 | \node[mynode, above=1cm of m_J] (p_J) {$\beta_J$}; 24 | \draw[->] (p_J) -- (m_J); 25 | \draw[->] (n_J) -- (m_J); 26 | \node[mynode, above left =3cm and 1cm of dots] (b) {$b$}; 27 | \node[mynode, above right=3cm and 1cm of dots] (tau) {$\tau$}; 28 | \draw[->] (b) to (p_1); 29 | \draw[->] (b) -- (p_2); 30 | \draw[->] (b) -- (p_j); 31 | \draw[->] (b) to (p_J); 32 | \draw[->] (tau) to (p_1); 33 | \draw[->] (tau) -- (p_2); 34 | \draw[->] (tau) -- (p_j); 35 | \draw[->] (tau) to (p_J); 36 | \end{tikzpicture}} 37 | \end{center} 38 | \caption{The multilevel binomial logistic regression.} 39 | \label{fig:multilevel_binomial} 40 | \end{figure} -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Doing Data Science using R: A Guide for Social Scientists 2 | 3 | # Building 4 | 5 | We assume that this will be built from inside an RStudio (server) session running in a docker container. 6 | 7 | ## Create docker image 8 | 9 | The docker image can be created from the accompanying [Dockerfile](Dockerfile). 10 | Run 11 | ``` 12 | make docker 13 | ``` 14 | to create this image. 15 | The resulting image will always be called `janacek:latest`. 16 | 17 | This image is also available on [DockerHub](https://hub.docker.com) and can be obtained with the `docker pull` command: 18 | ``` 19 | docker pull xmjandrews/janacek:latest 20 | ``` 21 | 22 | ## Run docker container 23 | 24 | The shell script [run_docker.sh](docker/run_docker.sh) will run the container and allow the rocker based RStudio server session to accessed through the browser. 25 | Do 26 | ``` 27 | source docker/run_docker.sh 28 | ``` 29 | The open a browser at `http://localhost:8788`. Log in with username "rstudio" and password "foo". 30 | 31 | 32 | ## Make individual chapters 33 | 34 | In the RStudio server session, open the Linux terminal (not R console). 35 | Do the following to access the book's home directory. 36 | ``` 37 | cd book 38 | ``` 39 | 40 | Next, source the [setenv.sh](setenv.sh), which sets up the environment for building the book. 41 | ``` 42 | source setenv.sh 43 | ``` 44 | 45 | Next, install `sparklyr`. (For some reason, this does not install automatically when making the docker container, despite being in the Dockerfile as a `run` command.) 46 | ``` 47 | Rscript -e "sparklyr::spark_install()" 48 | ``` 49 | 50 | The following script will make all chapter pdfs and copy them to a directory called `build`: 51 | ``` 52 | bash build.sh 53 | ``` 54 | 55 | The following script will make all chapter slides and copy them to a directory called `build_slides`: 56 | ``` 57 | bash build_slides.sh 58 | ``` 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/data/loaded_dice.csv: -------------------------------------------------------------------------------- 1 | outcome 2 | 6 3 | 1 4 | 6 5 | 2 6 | 1 7 | 4 8 | 5 9 | 6 10 | 4 11 | 1 12 | 1 13 | 2 14 | 3 15 | 6 16 | 2 17 | 5 18 | 5 19 | 4 20 | 2 21 | 2 22 | 1 23 | 6 24 | 4 25 | 6 26 | 3 27 | 5 28 | 6 29 | 6 30 | 6 31 | 5 32 | 3 33 | 4 34 | 1 35 | 3 36 | 4 37 | 1 38 | 5 39 | 1 40 | 1 41 | 6 42 | 1 43 | 4 44 | 2 45 | 6 46 | 6 47 | 1 48 | 5 49 | 4 50 | 6 51 | 1 52 | 6 53 | 1 54 | 6 55 | 5 56 | 1 57 | 5 58 | 4 59 | 4 60 | 1 61 | 2 62 | 4 63 | 5 64 | 2 65 | 2 66 | 6 67 | 6 68 | 5 69 | 4 70 | 6 71 | 4 72 | 2 73 | 2 74 | 2 75 | 4 76 | 3 77 | 3 78 | 6 79 | 2 80 | 2 81 | 5 82 | 4 83 | 4 84 | 6 85 | 4 86 | 4 87 | 6 88 | 3 89 | 6 90 | 3 91 | 6 92 | 6 93 | 3 94 | 4 95 | 6 96 | 1 97 | 2 98 | 3 99 | 1 100 | 2 101 | 5 102 | 6 103 | 5 104 | 6 105 | 2 106 | 2 107 | 2 108 | 3 109 | 3 110 | 4 111 | 4 112 | 4 113 | 5 114 | 5 115 | 4 116 | 6 117 | 6 118 | 2 119 | 6 120 | 6 121 | 3 122 | 5 123 | 6 124 | 6 125 | 2 126 | 6 127 | 4 128 | 5 129 | 2 130 | 6 131 | 6 132 | 4 133 | 1 134 | 6 135 | 2 136 | 1 137 | 6 138 | 4 139 | 3 140 | 3 141 | 3 142 | 3 143 | 3 144 | 5 145 | 2 146 | 6 147 | 6 148 | 4 149 | 6 150 | 6 151 | 3 152 | 6 153 | 4 154 | 6 155 | 3 156 | 3 157 | 3 158 | 1 159 | 4 160 | 6 161 | 1 162 | 3 163 | 6 164 | 1 165 | 4 166 | 5 167 | 1 168 | 4 169 | 5 170 | 6 171 | 3 172 | 1 173 | 3 174 | 6 175 | 3 176 | 3 177 | 2 178 | 6 179 | 5 180 | 6 181 | 2 182 | 5 183 | 2 184 | 5 185 | 6 186 | 6 187 | 3 188 | 1 189 | 6 190 | 4 191 | 5 192 | 1 193 | 6 194 | 2 195 | 6 196 | 6 197 | 2 198 | 3 199 | 5 200 | 4 201 | 5 202 | 6 203 | 5 204 | 6 205 | 6 206 | 2 207 | 6 208 | 5 209 | 4 210 | 1 211 | 3 212 | 4 213 | 6 214 | 4 215 | 6 216 | 6 217 | 2 218 | 1 219 | 2 220 | 3 221 | 6 222 | 3 223 | 6 224 | 6 225 | 6 226 | 6 227 | 4 228 | 1 229 | 4 230 | 4 231 | 2 232 | 2 233 | 1 234 | 2 235 | 4 236 | 2 237 | 5 238 | 3 239 | 6 240 | 1 241 | 5 242 | 6 243 | 4 244 | 2 245 | 6 246 | 4 247 | 2 248 | 3 249 | 6 250 | 3 251 | 6 252 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/sleepstudy_j.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} \centering 2 | \begin{subfigure}[b]{0.4\linewidth} 3 | \centering 4 | \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center}, 5 | mynodex/.style={draw,circle, minimum width=10mm,align=center}] 6 | \node[mynode] (y_1) {$y_1$}; 7 | \node[mynode,right =of y_1] (y_2) {$y_2$}; 8 | \node[right =of y_2] (y_dots) {$\ldots$}; 9 | \node[mynode,right =of y_dots] (y_n) {$y_n$}; 10 | 11 | \node[mynode,above = of y_1] (x_1) {$x_1$}; 12 | \node[mynode,right =of x_1] (x_2) {$x_2$}; 13 | \node[right =of x_2] (x_dots) {$\ldots$}; 14 | \node[mynode,right =of x_dots] (x_n) {$x_n$}; 15 | 16 | \node[mynodex, below=of y_2] (beta) {$\vec{\beta}$}; 17 | \node[mynodex, right=of beta] (sigma) {$\sigma$}; 18 | 19 | \foreach \value in {1,2,n}{ 20 | \draw[->] (x_\value) to (y_\value); 21 | \draw[->] (beta) to (y_\value); 22 | \draw[->] (sigma) to (y_\value); 23 | } 24 | \end{tikzpicture} 25 | \subcaption{} 26 | \end{subfigure} % 27 | \hspace{10mm} 28 | \begin{subfigure}[b]{0.4\linewidth} 29 | \centering 30 | \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 31 | 32 | \node[mynode] (y_1) {$y_d$}; 33 | \node[mynode,above = of y_1] (x_1) {$x_d$}; 34 | \node[right=5mm of y_1] (i) {$d \in 1\ldots n$}; 35 | 36 | \node[mynodex, below=15mm of y_1] (beta) {$\vec{\beta}$}; 37 | \node[mynodex, right=15mm of beta] (sigma) {$\sigma$}; 38 | 39 | \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 40 | 41 | \foreach \value in {x_1,beta,sigma} 42 | \draw[->] (\value) to (y_1); 43 | %\draw[->] (sigma) to (y_1); 44 | 45 | \end{tikzpicture} 46 | \subcaption{} 47 | \label{fig:b} 48 | \end{subfigure} 49 | \caption{Two equivalent Bayesian network diagrams representing a normal linear model with one predictor variable. Diagram b) uses a compact plate notation whereby all variables within the plate are repeated for all values of the index $i$, which takes values from $1$ to $n$.} 50 | \label{fig:bda_sleepstudy_j} 51 | \end{figure} 52 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example.csv: -------------------------------------------------------------------------------- 1 | x,y 2 | -0.3267791127616052,-0.07548530351697248 3 | 0.2806565000139498,1.394242863711284 4 | 1.3769682774184921,1.0529251206275247 5 | 0.566688554415096,1.3051256019456363 6 | -0.22838151303055787,0.854952676129855 7 | 1.1181719834037178,0.9508733088958504 8 | 0.9291288182691264,1.1140826086294506 9 | 1.1449648062958386,1.5136013382515228 10 | -1.0179617804379848,-0.06357343653120551 11 | 0.9636034870780498,0.1425832340229845 12 | -0.21876725597688468,-0.6424473282383885 13 | 0.1588330226802352,0.32442671175901733 14 | -0.3086588799284185,-0.7284130358383561 15 | 0.7144721456319776,-1.305933719788453 16 | -1.8260506639210041,-1.5468101738769198 17 | -0.27835708457620756,-0.8331622670953673 18 | -0.17489530429961028,2.1220167782194115 19 | 0.5052811351633415,-0.15065448743156457 20 | -1.1559542857166114,-0.441339085920836 21 | 1.538446439393133,-1.2703490031740061 22 | 0.9995207847381627,-0.8131939482035901 23 | -0.7639587564466249,0.35084242148208056 24 | 0.9259230815538804,-0.40063836125008256 25 | -0.29599655393288743,0.7467989682287598 26 | 1.7570803311010432,0.563660891049379 27 | 0.5095576399150538,0.5798519035927165 28 | 0.615218372259757,-0.496710008157511 29 | 0.11180861779872751,0.7296285301115366 30 | 2.86717788838265,1.6609966732273578 31 | 0.7290357084067856,0.5794875579127216 32 | 1.272819129290867,-0.392972405475754 33 | -1.2604356335617093,-0.868958369457268 34 | 0.07997667595809402,0.20689881513311673 35 | 1.2730405596563483,-0.0654623177399265 36 | 0.9532433195367976,-0.4201770289697453 37 | -0.9623934526831405,-0.02775337439948139 38 | 1.094168319384803,-0.653684940950604 39 | -0.2559729208845896,0.4397422355431788 40 | -1.1810921471209892,-0.43180027102630947 41 | -1.3329658762862295,-0.5055073289378088 42 | -1.4446217164788129,1.2525363018107099 43 | 0.927242827916264,1.3164175109379477 44 | 2.133648454560677,0.7837755529318027 45 | 2.0561831008144758,0.6730209866345058 46 | -1.1488012191044068,0.8361753042287655 47 | -0.6660162087373734,-2.0286947169150715 48 | 1.1239240006176974,0.6337011672199216 49 | -1.5564989442793384,0.7025658705742097 50 | 1.4955876729491213,1.747604682180729 51 | -0.1358920518042859,-1.5877976228199393 52 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/sleepstudy_j_s.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} \centering 2 | \begin{subfigure}[b]{0.4\linewidth} 3 | \centering 4 | \resizebox{\textwidth}{!}{% 5 | \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center}, 6 | mynodex/.style={draw,circle, minimum width=10mm,align=center}] 7 | \node[mynode] (y_1) {$y_1$}; 8 | \node[mynode,right =of y_1] (y_2) {$y_2$}; 9 | \node[right =of y_2] (y_dots) {$\ldots$}; 10 | \node[mynode,right =of y_dots] (y_n) {$y_n$}; 11 | 12 | \node[mynode,above = of y_1] (x_1) {$x_1$}; 13 | \node[mynode,right =of x_1] (x_2) {$x_2$}; 14 | \node[right =of x_2] (x_dots) {$\ldots$}; 15 | \node[mynode,right =of x_dots] (x_n) {$x_n$}; 16 | 17 | \node[mynodex, below=of y_2] (beta) {$\vec{\beta}$}; 18 | \node[mynodex, right=of beta] (sigma) {$\sigma$}; 19 | 20 | \foreach \value in {1,2,n}{ 21 | \draw[->] (x_\value) to (y_\value); 22 | \draw[->] (beta) to (y_\value); 23 | \draw[->] (sigma) to (y_\value); 24 | } 25 | \end{tikzpicture}} 26 | \subcaption{} 27 | \end{subfigure} % 28 | \hspace{10mm} 29 | \begin{subfigure}[b]{0.4\linewidth} 30 | \centering 31 | \resizebox{\textwidth}{!}{% 32 | \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 33 | 34 | \node[mynode] (y_1) {$y_d$}; 35 | \node[mynode,above = of y_1] (x_1) {$x_d$}; 36 | \node[right=5mm of y_1] (i) {$d \in 1\ldots n$}; 37 | 38 | \node[mynodex, below=15mm of y_1] (beta) {$\vec{\beta}$}; 39 | \node[mynodex, right=15mm of beta] (sigma) {$\sigma$}; 40 | 41 | \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 42 | 43 | \foreach \value in {x_1,beta,sigma} 44 | \draw[->] (\value) to (y_1); 45 | %\draw[->] (sigma) to (y_1); 46 | 47 | \end{tikzpicture}} 48 | \subcaption{} 49 | \label{fig:b} 50 | \end{subfigure} 51 | \caption{Two equivalent Bayesian network diagrams representing a normal linear model with one predictor variable. Diagram b) uses a compact plate notation whereby all variables within the plate are repeated for all values of the index $i$, which takes values from $1$ to $n$.} 52 | \label{fig:bda_sleepstudy_j} 53 | \end{figure} 54 | -------------------------------------------------------------------------------- /contents/chapter_11_generalized/data/Insurance.csv: -------------------------------------------------------------------------------- 1 | "","District","Group","Age","Holders","Claims" 2 | "1","1","<1l","<25",197,38 3 | "2","1","<1l","25-29",264,35 4 | "3","1","<1l","30-35",246,20 5 | "4","1","<1l",">35",1680,156 6 | "5","1","1-1.5l","<25",284,63 7 | "6","1","1-1.5l","25-29",536,84 8 | "7","1","1-1.5l","30-35",696,89 9 | "8","1","1-1.5l",">35",3582,400 10 | "9","1","1.5-2l","<25",133,19 11 | "10","1","1.5-2l","25-29",286,52 12 | "11","1","1.5-2l","30-35",355,74 13 | "12","1","1.5-2l",">35",1640,233 14 | "13","1",">2l","<25",24,4 15 | "14","1",">2l","25-29",71,18 16 | "15","1",">2l","30-35",99,19 17 | "16","1",">2l",">35",452,77 18 | "17","2","<1l","<25",85,22 19 | "18","2","<1l","25-29",139,19 20 | "19","2","<1l","30-35",151,22 21 | "20","2","<1l",">35",931,87 22 | "21","2","1-1.5l","<25",149,25 23 | "22","2","1-1.5l","25-29",313,51 24 | "23","2","1-1.5l","30-35",419,49 25 | "24","2","1-1.5l",">35",2443,290 26 | "25","2","1.5-2l","<25",66,14 27 | "26","2","1.5-2l","25-29",175,46 28 | "27","2","1.5-2l","30-35",221,39 29 | "28","2","1.5-2l",">35",1110,143 30 | "29","2",">2l","<25",9,4 31 | "30","2",">2l","25-29",48,15 32 | "31","2",">2l","30-35",72,12 33 | "32","2",">2l",">35",322,53 34 | "33","3","<1l","<25",35,5 35 | "34","3","<1l","25-29",73,11 36 | "35","3","<1l","30-35",89,10 37 | "36","3","<1l",">35",648,67 38 | "37","3","1-1.5l","<25",53,10 39 | "38","3","1-1.5l","25-29",155,24 40 | "39","3","1-1.5l","30-35",240,37 41 | "40","3","1-1.5l",">35",1635,187 42 | "41","3","1.5-2l","<25",24,8 43 | "42","3","1.5-2l","25-29",78,19 44 | "43","3","1.5-2l","30-35",121,24 45 | "44","3","1.5-2l",">35",692,101 46 | "45","3",">2l","<25",7,3 47 | "46","3",">2l","25-29",29,2 48 | "47","3",">2l","30-35",43,8 49 | "48","3",">2l",">35",245,37 50 | "49","4","<1l","<25",20,2 51 | "50","4","<1l","25-29",33,5 52 | "51","4","<1l","30-35",40,4 53 | "52","4","<1l",">35",316,36 54 | "53","4","1-1.5l","<25",31,7 55 | "54","4","1-1.5l","25-29",81,10 56 | "55","4","1-1.5l","30-35",122,22 57 | "56","4","1-1.5l",">35",724,102 58 | "57","4","1.5-2l","<25",18,5 59 | "58","4","1.5-2l","25-29",39,7 60 | "59","4","1.5-2l","30-35",68,16 61 | "60","4","1.5-2l",">35",344,63 62 | "61","4",">2l","<25",3,0 63 | "62","4",">2l","25-29",16,6 64 | "63","4",">2l","30-35",25,8 65 | "64","4",">2l",">35",114,33 66 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/nonmultilevel_alcohol.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center} 3 | \begin{tikzpicture}[mynode/.style={draw,circle, minimum width=10mm,align=center},font=\small] 4 | \node[mynode, fill=red!10] (y_1) {$y_1$}; 5 | \node[mynode, fill=red!10,right=of y_1] (y_2) {$y_2$}; 6 | \node[mynode, fill=red!10, right=of y_2] (y_3) {$y_3$}; 7 | \node[right=of y_3] (dots_1) {$\cdots$}; 8 | \node[mynode, fill=red!10,right=of dots_1] (y_i) {$y_i$}; 9 | \node[mynode, fill=red!10,right=of y_i] (y_i_1) {$y_{i+1}$}; 10 | \node[right=of y_i_1] (dots_2) {$\cdots$}; 11 | \node[mynode, fill=red!10,right=of dots_2] (y_n_1) {$y_{n-1}$}; 12 | \node[mynode, fill=red!10,right=of y_n_1] (y_n) {$y_{n}$}; 13 | 14 | \node[mynode, above right=of y_1] (mu_1) {$\mu_1$}; 15 | \node[mynode, right=of mu_1] (mu_2) {$\mu_2$}; 16 | 17 | \draw[->] (mu_1) to (y_1); 18 | \draw[->] (mu_1) to (y_2); 19 | \draw[->] (mu_2) to (y_3); 20 | \draw[->] (mu_2) to (dots_1); 21 | 22 | \node[mynode, above right=of y_i] (mu_j) {$\mu_j$}; 23 | \node[mynode, above left=of y_n] (mu_J) {$\mu_J$}; 24 | 25 | \draw[->] (mu_j) to (y_i); 26 | \draw[->] (mu_j) to (y_i_1); 27 | \draw[->] (mu_j) to (dots_2); 28 | \draw[->] (mu_J) to (y_n_1); 29 | \draw[->] (mu_J) to (y_n); 30 | 31 | 32 | 33 | \node[mynode, below = of y_i] (sigma) {$\sigma$}; 34 | \draw[->, bend left=10] (sigma) to (y_1); 35 | \draw[->] (sigma) to (y_2); 36 | \draw[->] (sigma) to (y_3); 37 | \draw[->] (sigma) to (dots_1); 38 | \draw[->] (sigma) to (y_i); 39 | \draw[->] (sigma) to (y_i_1); 40 | \draw[->, bend right=10] (sigma) to (y_n); 41 | \draw[->] (sigma) to (y_n_1); 42 | \draw[->] (sigma) to (dots_2); 43 | \end{tikzpicture} 44 | \end{center} 45 | \caption{A non-multilevel model for average alcohol consumption across countries. The $y_1, y_2 \ldots y_n$ variables are alcohol consumption averages for $J$ different countries over different years. As depicted here, we are assuming that $y_1$ and $y_2$ represent averages for the same country, country $j=1$, two different years. The overall average for this country is given by $\mu_1$, whose value is unknown. This non-multilevel model is identical to a one-way anova model with $J$ groups. The common standard deviation term $\sigma$ is simply the homogeneity of variance assumption of the one-way anova.} 46 | \label{fig:nonmultilevel_alcohol} 47 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/nonmultilevel_alcohol_s.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center}\resizebox{\textwidth}{!}{% 3 | \begin{tikzpicture}[mynode/.style={draw,circle, minimum width=10mm,align=center},font=\small] 4 | \node[mynode, fill=red!10] (y_1) {$y_1$}; 5 | \node[mynode, fill=red!10,right=of y_1] (y_2) {$y_2$}; 6 | \node[mynode, fill=red!10, right=of y_2] (y_3) {$y_3$}; 7 | \node[right=of y_3] (dots_1) {$\cdots$}; 8 | \node[mynode, fill=red!10,right=of dots_1] (y_i) {$y_i$}; 9 | \node[mynode, fill=red!10,right=of y_i] (y_i_1) {$y_{i+1}$}; 10 | \node[right=of y_i_1] (dots_2) {$\cdots$}; 11 | \node[mynode, fill=red!10,right=of dots_2] (y_n_1) {$y_{n-1}$}; 12 | \node[mynode, fill=red!10,right=of y_n_1] (y_n) {$y_{n}$}; 13 | 14 | \node[mynode, above right=of y_1] (mu_1) {$\mu_1$}; 15 | \node[mynode, right=of mu_1] (mu_2) {$\mu_2$}; 16 | 17 | \draw[->] (mu_1) to (y_1); 18 | \draw[->] (mu_1) to (y_2); 19 | \draw[->] (mu_2) to (y_3); 20 | \draw[->] (mu_2) to (dots_1); 21 | 22 | \node[mynode, above right=of y_i] (mu_j) {$\mu_j$}; 23 | \node[mynode, above left=of y_n] (mu_J) {$\mu_J$}; 24 | 25 | \draw[->] (mu_j) to (y_i); 26 | \draw[->] (mu_j) to (y_i_1); 27 | \draw[->] (mu_j) to (dots_2); 28 | \draw[->] (mu_J) to (y_n_1); 29 | \draw[->] (mu_J) to (y_n); 30 | 31 | 32 | 33 | \node[mynode, below = of y_i] (sigma) {$\sigma$}; 34 | \draw[->, bend left=10] (sigma) to (y_1); 35 | \draw[->] (sigma) to (y_2); 36 | \draw[->] (sigma) to (y_3); 37 | \draw[->] (sigma) to (dots_1); 38 | \draw[->] (sigma) to (y_i); 39 | \draw[->] (sigma) to (y_i_1); 40 | \draw[->, bend right=10] (sigma) to (y_n); 41 | \draw[->] (sigma) to (y_n_1); 42 | \draw[->] (sigma) to (dots_2); 43 | \end{tikzpicture}} 44 | \end{center} 45 | \caption{A non-multilevel model for average alcohol consumption across countries. The $y_1, y_2 \ldots y_n$ variables are alcohol consumption averages for $J$ different countries over different years. As depicted here, we are assuming that $y_1$ and $y_2$ represent averages for the same country, country $j=1$, two different years. The overall average for this country is given by $\mu_1$, whose value is unknown. This non-multilevel model is identical to a one-way anova model with $J$ groups. The common standard deviation term $\sigma$ is simply the homogeneity of variance assumption of the one-way anova.} 46 | \label{fig:nonmultilevel_alcohol} 47 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/multilevel_alcohol.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center} 3 | \begin{tikzpicture}[mynode/.style={draw,circle, minimum width=10mm,align=center},font=\small] 4 | \node[mynode, fill=red!10] (y_1) {$y_1$}; 5 | \node[mynode, fill=red!10,right=of y_1] (y_2) {$y_2$}; 6 | \node[mynode, fill=red!10, right=of y_2] (y_3) {$y_3$}; 7 | \node[right=of y_3] (dots_1) {$\cdots$}; 8 | \node[mynode, fill=red!10,right=of dots_1] (y_i) {$y_i$}; 9 | \node[mynode, fill=red!10,right=of y_i] (y_i_1) {$y_{i+1}$}; 10 | \node[right=of y_i_1] (dots_2) {$\cdots$}; 11 | \node[mynode, fill=red!10,right=of dots_2] (y_n_1) {$y_{n-1}$}; 12 | \node[mynode, fill=red!10,right=of y_n_1] (y_n) {$y_{n}$}; 13 | 14 | \node[mynode, above right=of y_1] (mu_1) {$\mu_1$}; 15 | \node[mynode, right=of mu_1] (mu_2) {$\mu_2$}; 16 | 17 | \draw[->] (mu_1) to (y_1); 18 | \draw[->] (mu_1) to (y_2); 19 | \draw[->] (mu_2) to (y_3); 20 | \draw[->] (mu_2) to (dots_1); 21 | 22 | \node[mynode, above right=of y_i] (mu_j) {$\mu_j$}; 23 | \node[mynode, above left=of y_n] (mu_J) {$\mu_J$}; 24 | 25 | \draw[->] (mu_j) to (y_i); 26 | \draw[->] (mu_j) to (y_i_1); 27 | \draw[->] (mu_j) to (dots_2); 28 | \draw[->] (mu_J) to (y_n_1); 29 | \draw[->] (mu_J) to (y_n); 30 | 31 | 32 | 33 | \node[mynode, below = of y_i] (sigma) {$\sigma$}; 34 | \draw[->, bend left=10] (sigma) to (y_1); 35 | \draw[->] (sigma) to (y_2); 36 | \draw[->] (sigma) to (y_3); 37 | \draw[->] (sigma) to (dots_1); 38 | \draw[->] (sigma) to (y_i); 39 | \draw[->] (sigma) to (y_i_1); 40 | \draw[->, bend right=10] (sigma) to (y_n); 41 | \draw[->] (sigma) to (y_n_1); 42 | \draw[->] (sigma) to (dots_2); 43 | 44 | 45 | \node[mynode, above = 3cm of y_i] (phi) {$\phi$}; 46 | \draw[->] (phi) to (mu_1); 47 | \draw[->] (phi) to (mu_2); 48 | \draw[->] (phi) to (mu_j); 49 | \draw[->] (phi) to (mu_J); 50 | 51 | \node[mynode, left = of phi] (tau) {$\tau$}; 52 | \draw[->] (tau) to (mu_1); 53 | \draw[->] (tau) to (mu_2); 54 | \draw[->] (tau) to (mu_j); 55 | \draw[->] (tau) to (mu_J); 56 | 57 | \end{tikzpicture} 58 | \end{center} 59 | \caption{A multilevel model for average alcohol consumption across. This model is an extension of the non-multilevel model shown in Figure \ref{fig:nonmultilevel_alcohol} by modelling the $\mu_1, \mu_2 \ldots \mu_j \ldots \mu_J$ as samples from a normal distribution with mean $\phi$ and standard deviation $\tau$.} 60 | \label{fig:multilevel_alcohol} 61 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/multilevel_alcohol_s.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} 2 | \begin{center}\resizebox{\textwidth}{!}{% 3 | \begin{tikzpicture}[mynode/.style={draw,circle, minimum width=10mm,align=center},font=\small] 4 | \node[mynode, fill=red!10] (y_1) {$y_1$}; 5 | \node[mynode, fill=red!10,right=of y_1] (y_2) {$y_2$}; 6 | \node[mynode, fill=red!10, right=of y_2] (y_3) {$y_3$}; 7 | \node[right=of y_3] (dots_1) {$\cdots$}; 8 | \node[mynode, fill=red!10,right=of dots_1] (y_i) {$y_i$}; 9 | \node[mynode, fill=red!10,right=of y_i] (y_i_1) {$y_{i+1}$}; 10 | \node[right=of y_i_1] (dots_2) {$\cdots$}; 11 | \node[mynode, fill=red!10,right=of dots_2] (y_n_1) {$y_{n-1}$}; 12 | \node[mynode, fill=red!10,right=of y_n_1] (y_n) {$y_{n}$}; 13 | 14 | \node[mynode, above right=of y_1] (mu_1) {$\mu_1$}; 15 | \node[mynode, right=of mu_1] (mu_2) {$\mu_2$}; 16 | 17 | \draw[->] (mu_1) to (y_1); 18 | \draw[->] (mu_1) to (y_2); 19 | \draw[->] (mu_2) to (y_3); 20 | \draw[->] (mu_2) to (dots_1); 21 | 22 | \node[mynode, above right=of y_i] (mu_j) {$\mu_j$}; 23 | \node[mynode, above left=of y_n] (mu_J) {$\mu_J$}; 24 | 25 | \draw[->] (mu_j) to (y_i); 26 | \draw[->] (mu_j) to (y_i_1); 27 | \draw[->] (mu_j) to (dots_2); 28 | \draw[->] (mu_J) to (y_n_1); 29 | \draw[->] (mu_J) to (y_n); 30 | 31 | 32 | 33 | \node[mynode, below = of y_i] (sigma) {$\sigma$}; 34 | \draw[->, bend left=10] (sigma) to (y_1); 35 | \draw[->] (sigma) to (y_2); 36 | \draw[->] (sigma) to (y_3); 37 | \draw[->] (sigma) to (dots_1); 38 | \draw[->] (sigma) to (y_i); 39 | \draw[->] (sigma) to (y_i_1); 40 | \draw[->, bend right=10] (sigma) to (y_n); 41 | \draw[->] (sigma) to (y_n_1); 42 | \draw[->] (sigma) to (dots_2); 43 | 44 | 45 | \node[mynode, above = 3cm of y_i] (phi) {$\phi$}; 46 | \draw[->] (phi) to (mu_1); 47 | \draw[->] (phi) to (mu_2); 48 | \draw[->] (phi) to (mu_j); 49 | \draw[->] (phi) to (mu_J); 50 | 51 | \node[mynode, left = of phi] (tau) {$\tau$}; 52 | \draw[->] (tau) to (mu_1); 53 | \draw[->] (tau) to (mu_2); 54 | \draw[->] (tau) to (mu_j); 55 | \draw[->] (tau) to (mu_J); 56 | 57 | \end{tikzpicture}} 58 | \end{center} 59 | \caption{A multilevel model for average alcohol consumption across. This model is an extension of the non-multilevel model shown in Figure \ref{fig:nonmultilevel_alcohol} by modelling the $\mu_1, \mu_2 \ldots \mu_j \ldots \mu_J$ as samples from a normal distribution with mean $\phi$ and standard deviation $\tau$.} 60 | \label{fig:multilevel_alcohol} 61 | \end{figure} -------------------------------------------------------------------------------- /contents/chapter_04_visualization/data/carprice.csv: -------------------------------------------------------------------------------- 1 | "","Type","Min.Price","Price","Max.Price","Range.Price","RoughRange","gpm100","MPG.city","MPG.highway" 2 | "6","Midsize",14.2,15.7,17.3,3.1,3.09,3.8,22,31 3 | "7","Large",19.9,20.8,21.7,1.8,1.79,4.2,19,28 4 | "8","Large",22.6,23.7,24.9,2.3,2.31,4.9,16,25 5 | "9","Midsize",26.3,26.3,26.3,0,-0.01,4.3,19,27 6 | "10","Large",33,34.7,36.3,3.3,3.3,4.9,16,25 7 | "11","Midsize",37.5,40.1,42.7,5.2,5.18,4.9,16,25 8 | "12","Compact",8.5,13.4,18.3,9.8,9.8,3.3,25,36 9 | "13","Compact",11.4,11.4,11.4,0,-0.01,3.4,25,34 10 | "14","Sporty",13.4,15.1,16.8,3.4,3.38,4.2,19,28 11 | "15","Midsize",13.4,15.9,18.4,5,5.01,4,21,29 12 | "16","Van",14.7,16.3,18,3.3,3.31,4.9,18,23 13 | "17","Van",14.7,16.6,18.6,3.9,3.9,5.7,15,20 14 | "18","Large",18,18.8,19.6,1.6,1.6,4.7,17,26 15 | "19","Sporty",34.6,38,41.5,6.9,6.88,4.8,17,25 16 | "20","Large",18.4,18.4,18.4,0,-0.01,4.2,20,28 17 | "21","Compact",14.5,15.8,17.1,2.6,2.59,3.9,23,28 18 | "22","Large",29.5,29.5,29.5,0,0.02,4.3,20,26 19 | "23","Small",7.9,9.2,10.6,2.7,2.68,3.2,29,33 20 | "24","Small",8.4,11.3,14.2,5.8,5.8,3.8,23,29 21 | "25","Compact",11.9,13.3,14.7,2.8,2.81,4.1,22,27 22 | "26","Van",13.6,19,24.4,10.8,10.77,5.3,17,21 23 | "27","Midsize",14.8,15.6,16.4,1.6,1.6,4.2,21,27 24 | "28","Sporty",18.5,25.8,33.1,14.6,14.6,4.8,18,24 25 | "29","Small",7.9,12.2,16.5,8.6,8.6,3.2,29,33 26 | "30","Large",17.5,19.3,21.2,3.7,3.69,4.2,20,28 27 | "31","Small",6.9,7.4,7.9,1,1,3.1,31,33 28 | "32","Small",8.4,10.1,11.9,3.5,3.49,3.8,23,30 29 | "33","Compact",10.4,11.3,12.2,1.8,1.82,4.1,22,27 30 | "34","Sporty",10.8,15.9,21,10.2,10.21,3.9,22,29 31 | "35","Sporty",12.8,14,15.2,2.4,2.4,3.7,24,30 32 | "36","Van",14.5,19.9,25.3,10.8,10.82,5.7,15,20 33 | "37","Midsize",15.6,20.2,24.8,9.2,9.21,3.9,21,30 34 | "38","Large",20.1,20.9,21.7,1.6,1.59,4.5,18,26 35 | "51","Midsize",33.3,34.3,35.3,2,1.99,4.7,17,26 36 | "52","Large",34.4,36.1,37.8,3.4,3.42,4.5,18,26 37 | "60","Sporty",13.3,14.1,15,1.7,1.71,4.1,23,26 38 | "61","Midsize",14.9,14.9,14.9,0,-0.02,4.4,19,26 39 | "68","Compact",13,13.5,14,1,0.99,3.6,24,31 40 | "69","Midsize",14.2,16.3,18.4,4.2,4.19,3.7,23,31 41 | "70","Van",19.5,19.5,19.5,0,0,4.9,18,23 42 | "71","Large",19.5,20.7,21.9,2.4,2.41,4.2,19,28 43 | "72","Sporty",11.4,14.4,17.4,6,6.01,3.8,23,30 44 | "73","Small",8.2,9,9.9,1.7,1.69,2.8,31,41 45 | "74","Compact",9.4,11.1,12.8,3.4,3.39,3.7,23,31 46 | "75","Sporty",14,17.7,21.4,7.4,7.4,4.2,19,28 47 | "76","Midsize",15.4,18.5,21.6,6.2,6.19,4.3,19,27 48 | "77","Large",19.4,24.4,29.4,10,10,4.2,19,28 49 | "79","Small",9.2,11.1,12.9,3.7,3.7,3,28,38 50 | -------------------------------------------------------------------------------- /contents/chapter_07_reproducible/examples/example-3.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Data Analysis: Report II" 3 | author: "Mark Andrews" 4 | date: "October 25, 2019" 5 | output: 6 | pdf_document: 7 | keep_tex: yes 8 | header-includes: 9 | - \usepackage{booktabs} 10 | bibliography: refs.bib 11 | biblio-style: apalike 12 | --- 13 | 14 | ```{r setup, echo=FALSE} 15 | knitr::opts_chunk$set(warning = FALSE, message = FALSE) 16 | ``` 17 | 18 | ```{r load_packages, echo = FALSE} 19 | library(tidyverse) 20 | library(knitr) 21 | library(kableExtra) 22 | 23 | weight_df <- read_csv('weight.csv') 24 | weight_df_grouped <- group_by(weight_df, gender) 25 | 26 | sample_size <- weight_df_grouped %>% 27 | summarise(n=n()) %>% 28 | deframe() 29 | 30 | weight_df_summary <- weight_df_grouped %>% 31 | summarise_at(vars(weight, height), 32 | list(avg=mean, stdev=sd)) 33 | ``` 34 | 35 | # Descriptive statistics 36 | 37 | In this data set, we have measured the weight (in kg) 38 | and height (in cm) of `r sum(sample_size)` participants 39 | (`r sample_size['Male']` males, `r sample_size['Female']` 40 | females). In the following table, we show the mean and 41 | the standard deviations of the weights and heights, 42 | separately for males and females. 43 | 44 | ```{r descriptives_table, echo=F} 45 | weight_df_summary %>% 46 | kable(format = "latex", 47 | booktabs = TRUE, 48 | digits = 2, 49 | align = 'c') %>% 50 | kable_styling(position = "center") 51 | ``` 52 | 53 | # Statistical model 54 | 55 | We will model the relationship between weight and height 56 | as a varying intercepts normal linear model as follows. 57 | For each $i \in 1 \ldots n$, 58 | $$ 59 | \begin{aligned} 60 | y_i &\sim N(\mu_i, \sigma^2),\\ 61 | \mu_i &= \beta_0 + \beta_1 x_i + \beta_2 z_i, 62 | \end{aligned} 63 | $$ 64 | where $y_i$, $x_i$, $z_i$ are the weight, height, 65 | and gender of participant $i$. 66 | 67 | In R, this analysis can be easily performed as follows. 68 | ```{r stat_model} 69 | model <- lm(weight ~ height + gender, data = weight_df) 70 | ``` 71 | ```{r model_results, echo=F} 72 | R_sq <- summary(model)$r.sq 73 | f_stat <- summary(model)$fstatistic 74 | p_value <- pf(f_stat[1], f_stat[2], f_stat[3], lower.tail = F) 75 | ``` 76 | 77 | The $R^2$ for this model is `r round(R_sq, 2)`, 78 | $F(`r round(f_stat[2])`, `r round(f_stat[3])`) = 79 | `r round(f_stat[1],2)`$, 80 | $p `r format.pval(p_value, eps = 0.01)`$. 81 | 82 | 83 | More information about varying intecept models can be 84 | found in @GelmanHill:2007. 85 | 86 | # References 87 | -------------------------------------------------------------------------------- /contents/chapter_17_stan/Makefile: -------------------------------------------------------------------------------- 1 | main=chapter_17_stan 2 | rm = rm -rf 3 | mkpdf = pdflatex -shell-escape -interaction=nonstopmode 4 | bibtex=../mjandrews.bib 5 | header=../header.tex 6 | 7 | .PHONY: clean mrproper all 8 | 9 | all: $(main).pdf $(main).md $(main).html $(main).docx 10 | 11 | $(main).tex: $(main).Rmd header.tex M_dice_3.Rds $(bibtex) M_math.Rds M_math_2.Rds M_weight.Rds M_biochem.Rds M_biochem_pois.Rds M_biochem_nb.Rds M_lmm.Rds 12 | Rscript -e "rmarkdown::render('$(main).Rmd')" 13 | 14 | $(main).md: $(main).Rmd $(header) $(bibtex) 15 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = rmarkdown::md_document(variant = "markdown"))' 16 | 17 | $(main).html: $(main).Rmd $(header) $(bibtex) 18 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "html_document")' 19 | 20 | $(main).docx: $(main).Rmd $(header) $(bibtex) 21 | Rscript -e 'rmarkdown::render("$(main).Rmd", output_format = "word_document")' 22 | 23 | $(main).pdf: $(main).tex 24 | $(mkpdf) $(main).tex 25 | $(mkpdf) $(main).tex 26 | $(mkpdf) $(main).tex 27 | $(mkpdf) $(main).tex 28 | $(mkpdf) $(main).tex 29 | $(mkpdf) $(main).tex 30 | 31 | $(main)_slides.pdf: $(main)_slides.Rmd $(header) $(bibtex) M_math_2.Rds 32 | Rscript -e "rmarkdown::render(input = '$(main)_slides.Rmd', output_file = '$(main)_slides.pdf')" 33 | 34 | M_dice_3.Rds: 35 | Rscript scripts/m_dice_3.R 36 | 37 | M_dice_4.Rds: 38 | Rscript scripts/m_dice_4.R 39 | 40 | M_math.Rds: 41 | Rscript scripts/m_normal.R 42 | 43 | M_math_2.Rds: 44 | Rscript scripts/normlinear.R 45 | 46 | M_weight.Rds: scripts/mlreg.R stan/mlreg.stan 47 | Rscript scripts/mlreg.R 48 | 49 | M_biochem.Rds: scripts/logitreg.R stan/logitreg.stan 50 | Rscript scripts/logitreg.R 51 | 52 | M_biochem_pois.Rds: scripts/poisreg.R stan/poisreg.stan 53 | Rscript scripts/poisreg.R 54 | 55 | M_biochem_nb.Rds: scripts/negbinreg.R stan/negbinreg.stan 56 | Rscript scripts/negbinreg.R 57 | 58 | M_lmm.Rds: scripts/lmm.R stan/lmm.stan 59 | Rscript scripts/lmm.R 60 | 61 | $(bibtex): 62 | wget --no-check-certificate --no-cache --no-cookies https://raw.githubusercontent.com/mark-andrews/bibtex/master/mjandrews.bib -O $(bibtex) 63 | 64 | clean: 65 | $(rm) _minted-$(main) 66 | $(rm) $(main).aux 67 | $(rm) _minted-$(main) 68 | $(rm) $(main).log 69 | $(rm) $(main).toc 70 | $(rm) $(main).aux 71 | $(rm) _minted-$(main) 72 | $(rm) $(main)_files 73 | $(rm) $(main)_cache 74 | rm -rf $(main)_slides_files 75 | rm -rf $(main)_slides_cache 76 | rm -rf $(main)_slides.tex 77 | rm -rf $(main)_slides.aux 78 | rm -rf $(main)_slides.log 79 | rm -rf $(main)_slides.toc 80 | rm -rf _minted-$(main)_slides 81 | 82 | mrproper: clean 83 | $(rm) *.Rds 84 | rm -rf $(main)_cache 85 | rm -f $(main).pdf 86 | rm -f $(main).tex 87 | rm -f $(main).md 88 | rm -f $(main).html 89 | rm -f $(main).docx 90 | rm -f $(bibtex) 91 | rm -rf $(main)_slides.pdf 92 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM xmjandrews/verse:20.07.15 2 | 3 | RUN Rscript -e 'devtools::install_version("locfit", version = "1.5-9.4", repos = "http://cran.r-project.org")' 4 | 5 | # Install extra packages not installed in xmjandrews/verse 6 | RUN install2.r --error microbenchmark \ 7 | zoo \ 8 | cowplot \ 9 | latex2exp \ 10 | magrittr \ 11 | modelr \ 12 | carData \ 13 | fivethirtyeight \ 14 | ggExtra \ 15 | ggrepel \ 16 | timetk \ 17 | aplpack \ 18 | psych \ 19 | moments \ 20 | sloop \ 21 | kableExtra \ 22 | reticulate \ 23 | ellipse \ 24 | WWGbook \ 25 | tikzDevice\ 26 | lavaan \ 27 | GGally \ 28 | pscl \ 29 | mixtools \ 30 | sparklyr \ 31 | Ecdat \ 32 | tidybayes \ 33 | learnr 34 | 35 | 36 | 37 | RUN rm -rf /tmp/downloaded_packages /tmp/*.rds 38 | 39 | RUN apt-get update && \ 40 | apt-get install -y --no-install-recommends \ 41 | zsh \ 42 | vim \ 43 | python3-pygments \ 44 | libglpk-dev libglpk40 \ 45 | texlive-xetex \ 46 | texlive-plain-generic \ 47 | texlive-latex-extra \ 48 | texlive-fonts-recommended \ 49 | texlive-fonts-extra \ 50 | libopenmpi-dev \ 51 | libzmq3-dev \ 52 | ghostscript \ 53 | libtcl8.6 \ 54 | libtk8.6 \ 55 | virtualenv \ 56 | openjdk-8-jdk 57 | 58 | # I don't think this worked? 59 | RUN Rscript -e "sparklyr::spark_install()" 60 | 61 | # Why exactly this is needed is not clear, but I get an error as described at https://www.mail-archive.com/rcpp-devel@lists.r-forge.r-project.org/msg10226.html 62 | # that seems to be solved when I do an all packages update. 63 | RUN Rscript -e 'install.packages(c("arrow", "backports", "bayesplot", "BH", "BiocManager", "bit", "bit64", "blob", "brew", "bridgesampling", "brms", "Brobdingnag", "broom", "callr", "checkmate", "clipr", "coda", "colorspace", "colourpicker", "commonmark", "covr", "crayon", "credentials", "crosstalk", "curl", "data.table", "DBI", "desc", "devtools", "digest", "DT", "dtplyr", "evaluate", "fansi", "farver", "fastmap", "forcats", "fs", "fst", "gert", "ggridges", "gh", "git2r", "gtools", "haven", "highr", "hms", "htmltools", "httpuv", "httr", "igraph", "inline", "isoband", "jsonlite", "knitr", "labeling", "Lahman", "later", "littler", "lme4", "loo", "matrixStats", "memoise", "mgcv", "mime", "mvtnorm", "nloptr", "nycflights13", "openssl", "packrat", "pkgbuild", "pkgload", "plyr", "processx", "promises", "ps", "R6", "rcmdcheck", "RColorBrewer", "Rcpp", "RcppEigen", "RcppParallel", "readr", "readxl", "remotes", "reprex", "rex", "rlang", "RMariaDB", "rmarkdown", "roxygen2", "RPostgres", "rsconnect", "RSQLite", "rstan", "rstantools", "rstudioapi", "rversions", "rvest", "scales", "sessioninfo", "shiny", "shinyjs", "shinystan", "shinythemes", "StanHeaders", "statmod", "stringi", "sys", "testthat", "tidyverse", "tinytex", "usethis", "utf8", "V8", "viridisLite", "vroom", "withr", "xfun", "xml2", "xts", "yaml"))' 64 | -------------------------------------------------------------------------------- /contents/chapter_14_sem/data/grad.csv: -------------------------------------------------------------------------------- 1 | hs,gre,col,grad 2 | 57,52,41,57 3 | 68,59,53,61 4 | 44,33,54,31 5 | 63,44,47,56 6 | 47,52,57,61 7 | 44,52,51,61 8 | 50,59,42,61 9 | 34,46,45,36 10 | 63,57,54,51 11 | 57,55,52,51 12 | 60,46,51,61 13 | 57,65,51,61 14 | 73,60,71,71 15 | 54,63,57,46 16 | 45,57,50,56 17 | 42,49,43,56 18 | 47,52,51,56 19 | 57,57,60,56 20 | 68,65,62,61 21 | 55,39,57,46 22 | 63,49,35,41 23 | 63,63,75,66 24 | 50,40,45,56 25 | 60,52,57,61 26 | 37,44,45,46 27 | 34,37,46,31 28 | 65,65,66,66 29 | 47,57,57,46 30 | 44,38,49,46 31 | 52,44,49,41 32 | 42,31,57,51 33 | 76,52,64,61 34 | 65,67,63,71 35 | 42,41,57,31 36 | 52,59,50,61 37 | 60,65,58,66 38 | 68,54,75,66 39 | 65,62,68,66 40 | 47,31,44,36 41 | 39,31,40,51 42 | 47,47,41,51 43 | 55,59,62,51 44 | 52,54,57,51 45 | 42,41,43,41 46 | 65,65,48,66 47 | 55,59,63,46 48 | 50,40,39,47 49 | 65,59,70,51 50 | 47,59,63,46 51 | 57,54,59,51 52 | 53,61,61,56 53 | 39,33,38,41 54 | 44,44,61,46 55 | 63,59,49,71 56 | 73,62,73,66 57 | 39,39,44,42 58 | 37,37,42,32 59 | 42,39,39,46 60 | 63,57,55,41 61 | 48,49,52,51 62 | 50,46,45,61 63 | 47,62,61,66 64 | 44,44,39,46 65 | 34,33,41,36 66 | 50,42,50,61 67 | 44,41,40,26 68 | 60,54,60,66 69 | 47,39,47,26 70 | 63,43,59,44 71 | 50,33,49,36 72 | 44,44,46,51 73 | 60,54,58,61 74 | 73,67,71,66 75 | 68,59,58,66 76 | 55,45,46,51 77 | 47,40,43,31 78 | 55,61,54,61 79 | 68,59,56,66 80 | 31,36,46,46 81 | 47,41,54,56 82 | 63,59,57,56 83 | 36,49,54,36 84 | 68,59,71,56 85 | 63,65,48,56 86 | 55,41,40,41 87 | 55,62,64,66 88 | 52,41,51,56 89 | 34,49,39,56 90 | 50,31,40,31 91 | 55,49,61,56 92 | 52,62,66,46 93 | 63,49,49,46 94 | 68,62,65,61 95 | 39,44,52,48 96 | 44,44,46,51 97 | 50,62,61,51 98 | 71,65,72,56 99 | 63,65,71,71 100 | 34,44,40,41 101 | 63,63,69,61 102 | 68,60,64,66 103 | 47,59,56,61 104 | 47,46,49,41 105 | 63,52,54,51 106 | 52,59,53,51 107 | 55,54,66,56 108 | 60,62,67,56 109 | 35,35,40,33 110 | 47,54,46,56 111 | 71,65,69,71 112 | 57,52,40,56 113 | 44,50,41,51 114 | 65,59,57,66 115 | 68,65,58,56 116 | 73,61,57,66 117 | 36,44,37,41 118 | 43,54,55,46 119 | 73,67,62,66 120 | 52,57,64,56 121 | 41,47,40,51 122 | 60,54,50,51 123 | 50,52,46,56 124 | 50,52,53,56 125 | 47,46,52,46 126 | 47,62,45,46 127 | 55,57,56,61 128 | 50,41,45,56 129 | 39,53,54,41 130 | 50,49,56,46 131 | 34,35,41,26 132 | 57,59,54,56 133 | 57,65,72,56 134 | 68,62,56,51 135 | 42,54,47,46 136 | 61,59,49,66 137 | 76,63,60,66 138 | 47,59,54,46 139 | 46,52,55,56 140 | 39,41,33,41 141 | 52,49,49,61 142 | 28,46,43,51 143 | 42,54,50,52 144 | 47,42,52,51 145 | 47,57,48,41 146 | 52,59,58,66 147 | 47,52,43,61 148 | 50,62,41,31 149 | 44,52,43,51 150 | 47,41,46,41 151 | 45,55,44,41 152 | 47,37,43,46 153 | 65,54,61,56 154 | 43,57,40,51 155 | 47,54,49,61 156 | 57,62,56,66 157 | 68,59,61,71 158 | 52,55,50,61 159 | 42,57,51,61 160 | 42,39,42,41 161 | 66,67,67,66 162 | 47,62,53,61 163 | 57,50,50,58 164 | 47,61,51,31 165 | 57,62,72,61 166 | 52,59,48,61 167 | 44,44,40,31 168 | 50,59,53,61 169 | 39,54,39,36 170 | 57,62,63,41 171 | 57,60,51,37 172 | 42,57,45,43 173 | 47,46,39,61 174 | 42,36,42,39 175 | 60,59,62,51 176 | 44,49,44,51 177 | 63,60,65,66 178 | 65,67,63,71 179 | 39,54,54,41 180 | 50,52,45,36 181 | 52,65,60,51 182 | 60,62,49,51 183 | 44,49,48,51 184 | 52,67,57,61 185 | 55,65,55,61 186 | 50,67,66,56 187 | 65,65,64,71 188 | 52,54,55,51 189 | 47,44,42,36 190 | 63,62,56,61 191 | 50,46,53,66 192 | 42,54,41,41 193 | 36,57,42,41 194 | 50,52,53,56 195 | 41,59,42,51 196 | 47,65,60,56 197 | 55,59,52,56 198 | 42,46,38,46 199 | 57,41,57,52 200 | 55,62,58,61 201 | 63,65,65,61 202 | -------------------------------------------------------------------------------- /contents/chapter_04_visualization/data/sleepstudy.csv: -------------------------------------------------------------------------------- 1 | Reaction,Days,Subject 2 | 249.56,0,s308 3 | 258.7047,1,s308 4 | 250.8006,2,s308 5 | 321.4398,3,s308 6 | 356.8519,4,s308 7 | 414.6901,5,s308 8 | 382.2038,6,s308 9 | 290.1486,7,s308 10 | 430.5853,8,s308 11 | 466.3535,9,s308 12 | 222.7339,0,s309 13 | 205.2658,1,s309 14 | 202.9778,2,s309 15 | 204.707,3,s309 16 | 207.7161,4,s309 17 | 215.9618,5,s309 18 | 213.6303,6,s309 19 | 217.7272,7,s309 20 | 224.2957,8,s309 21 | 237.3142,9,s309 22 | 199.0539,0,s310 23 | 194.3322,1,s310 24 | 234.32,2,s310 25 | 232.8416,3,s310 26 | 229.3074,4,s310 27 | 220.4579,5,s310 28 | 235.4208,6,s310 29 | 255.7511,7,s310 30 | 261.0125,8,s310 31 | 247.5153,9,s310 32 | 321.5426,0,s330 33 | 300.4002,1,s330 34 | 283.8565,2,s330 35 | 285.133,3,s330 36 | 285.7973,4,s330 37 | 297.5855,5,s330 38 | 280.2396,6,s330 39 | 318.2613,7,s330 40 | 305.3495,8,s330 41 | 354.0487,9,s330 42 | 287.6079,0,s331 43 | 285,1,s331 44 | 301.8206,2,s331 45 | 320.1153,3,s331 46 | 316.2773,4,s331 47 | 293.3187,5,s331 48 | 290.075,6,s331 49 | 334.8177,7,s331 50 | 293.7469,8,s331 51 | 371.5811,9,s331 52 | 234.8606,0,s332 53 | 242.8118,1,s332 54 | 272.9613,2,s332 55 | 309.7688,3,s332 56 | 317.4629,4,s332 57 | 309.9976,5,s332 58 | 454.1619,6,s332 59 | 346.8311,7,s332 60 | 330.3003,8,s332 61 | 253.8644,9,s332 62 | 283.8424,0,s333 63 | 289.555,1,s333 64 | 276.7693,2,s333 65 | 299.8097,3,s333 66 | 297.171,4,s333 67 | 338.1665,5,s333 68 | 332.0265,6,s333 69 | 348.8399,7,s333 70 | 333.36,8,s333 71 | 362.0428,9,s333 72 | 265.4731,0,s334 73 | 276.2012,1,s334 74 | 243.3647,2,s334 75 | 254.6723,3,s334 76 | 279.0244,4,s334 77 | 284.1912,5,s334 78 | 305.5248,6,s334 79 | 331.5229,7,s334 80 | 335.7469,8,s334 81 | 377.299,9,s334 82 | 241.6083,0,s335 83 | 273.9472,1,s335 84 | 254.4907,2,s335 85 | 270.8021,3,s335 86 | 251.4519,4,s335 87 | 254.6362,5,s335 88 | 245.4523,6,s335 89 | 235.311,7,s335 90 | 235.7541,8,s335 91 | 237.2466,9,s335 92 | 312.3666,0,s337 93 | 313.8058,1,s337 94 | 291.6112,2,s337 95 | 346.1222,3,s337 96 | 365.7324,4,s337 97 | 391.8385,5,s337 98 | 404.2601,6,s337 99 | 416.6923,7,s337 100 | 455.8643,8,s337 101 | 458.9167,9,s337 102 | 236.1032,0,s349 103 | 230.3167,1,s349 104 | 238.9256,2,s349 105 | 254.922,3,s349 106 | 250.7103,4,s349 107 | 269.7744,5,s349 108 | 281.5648,6,s349 109 | 308.102,7,s349 110 | 336.2806,8,s349 111 | 351.6451,9,s349 112 | 256.2968,0,s350 113 | 243.4543,1,s350 114 | 256.2046,2,s350 115 | 255.5271,3,s350 116 | 268.9165,4,s350 117 | 329.7247,5,s350 118 | 379.4445,6,s350 119 | 362.9184,7,s350 120 | 394.4872,8,s350 121 | 389.0527,9,s350 122 | 250.5265,0,s351 123 | 300.0576,1,s351 124 | 269.8939,2,s351 125 | 280.5891,3,s351 126 | 271.8274,4,s351 127 | 304.6336,5,s351 128 | 287.7466,6,s351 129 | 266.5955,7,s351 130 | 321.5418,8,s351 131 | 347.5655,9,s351 132 | 221.6771,0,s352 133 | 298.1939,1,s352 134 | 326.8785,2,s352 135 | 346.8555,3,s352 136 | 348.7402,4,s352 137 | 352.8287,5,s352 138 | 354.4266,6,s352 139 | 360.4326,7,s352 140 | 375.6406,8,s352 141 | 388.5417,9,s352 142 | 271.9235,0,s369 143 | 268.4369,1,s369 144 | 257.2424,2,s369 145 | 277.6566,3,s369 146 | 314.8222,4,s369 147 | 317.2135,5,s369 148 | 298.1353,6,s369 149 | 348.1229,7,s369 150 | 340.28,8,s369 151 | 366.5131,9,s369 152 | 225.264,0,s370 153 | 234.5235,1,s370 154 | 238.9008,2,s370 155 | 240.473,3,s370 156 | 267.5373,4,s370 157 | 344.1937,5,s370 158 | 281.1481,6,s370 159 | 347.5855,7,s370 160 | 365.163,8,s370 161 | 372.2288,9,s370 162 | 269.8804,0,s371 163 | 272.4428,1,s371 164 | 277.8989,2,s371 165 | 281.7895,3,s371 166 | 279.1705,4,s371 167 | 284.512,5,s371 168 | 259.2658,6,s371 169 | 304.6306,7,s371 170 | 350.7807,8,s371 171 | 369.4692,9,s371 172 | 269.4117,0,s372 173 | 273.474,1,s372 174 | 297.5968,2,s372 175 | 310.6316,3,s372 176 | 287.1726,4,s372 177 | 329.6076,5,s372 178 | 334.4818,6,s372 179 | 343.2199,7,s372 180 | 369.1417,8,s372 181 | 364.1236,9,s372 182 | -------------------------------------------------------------------------------- /contents/chapter_05_exploratory/data/nominal_gdp_per_capita.csv: -------------------------------------------------------------------------------- 1 | country,gdp 2 | Luxembourg,105863 3 | Switzerland,80637 4 | Macau,77111 5 | Norway,75389 6 | Iceland,70248 7 | Ireland,68710 8 | Qatar,61024 9 | United States,59792 10 | Singapore,57713 11 | Denmark,56630 12 | Australia,55692 13 | Sweden,52925 14 | Netherlands,48555 15 | San Marino,47595 16 | Austria,47347 17 | Hong Kong,46080 18 | Finland,45927 19 | Canada,45094 20 | Germany,44769 21 | Belgium,43488 22 | New Zealand,41572 23 | Israel,40272 24 | France,39932 25 | United Kingdom,39800 26 | Japan,38448 27 | United Arab Emirates,37732 28 | Italy,31996 29 | Bahamas,31660 30 | Puerto Rico,31581 31 | South Korea,29938 32 | Spain,28358 33 | Brunei,28278 34 | Kuwait,27393 35 | Malta,27326 36 | Cyprus,25380 37 | Bahrain,24325 38 | Taiwan,24292 39 | Slovenia,23654 40 | Portugal,21158 41 | Saudi Arabia,21096 42 | Czech Republic,20401 43 | Estonia,19735 44 | Greece,18637 45 | Barbados,17757 46 | Slovakia,17655 47 | Saint Kitts and Nevis,17397 48 | Oman,17128 49 | Uruguay,16941 50 | Lithuania,16730 51 | Antigua and Barbuda,16702 52 | Trinidad and Tobago,16637 53 | Palau,15980 54 | Seychelles,15859 55 | Latvia,15550 56 | Panama,15089 57 | Chile,15067 58 | Equatorial Guinea,14817 59 | Argentina,14462 60 | Hungary,14209 61 | Poland,13821 62 | Croatia,13271 63 | Maldives,12527 64 | Lebanon,12013 65 | Costa Rica,11728 66 | Russia,10955 67 | Romania,10785 68 | World,10728 69 | Turkey,10537 70 | Mauritius,10504 71 | Grenada,10405 72 | Brazil,9895 73 | Malaysia,9755 74 | Saint Lucia,9606 75 | Mexico,9318 76 | Kazakhstan,8762 77 | China,8643 78 | Nauru,8574 79 | Bulgaria,8077 80 | Dominica,7879 81 | Montenegro,7678 82 | Botswana,7584 83 | Dominican Republic,7477 84 | Gabon,7373 85 | Saint Vincent and the Grenadines,7124 86 | Venezuela,6890 87 | Peru,6731 88 | Turkmenistan,6642 89 | Thailand,6590 90 | Colombia,6379 91 | Ecuador,6216 92 | South Africa,6179 93 | Serbia,5901 94 | Suriname,5869 95 | Belarus,5727 96 | Paraguay,5600 97 | Namibia,5589 98 | Fiji,5527 99 | Macedonia,5474 100 | Iran,5289 101 | Jamaica,5193 102 | Bosnia and Herzegovina,5180 103 | Iraq,4950 104 | Belize,4806 105 | Libya,4739 106 | Guyana,4578 107 | Albania,4544 108 | Tonga,4520 109 | Guatemala,4469 110 | Angola,4465 111 | Samoa,4258 112 | Azerbaijan,4140 113 | Jordan,4135 114 | Georgia,4085 115 | Sri Lanka,4073 116 | Algeria,4033 117 | Kosovo,3944 118 | Swaziland,3915 119 | El Salvador,3894 120 | Indonesia,3876 121 | Armenia,3861 122 | Mongolia,3640 123 | Tuvalu,3638 124 | Marshall Islands,3625 125 | Tunisia,3496 126 | Bolivia,3353 127 | Cape Verde,3238 128 | Micronesia,3200 129 | Morocco,3151 130 | Vanuatu,3094 131 | Philippines,2976 132 | Bhutan,2903 133 | Papua New Guinea,2861 134 | Honduras,2766 135 | Ukraine,2583 136 | Laos,2542 137 | Egypt,2501 138 | Vietnam,2354 139 | Moldova,2280 140 | Nicaragua,2207 141 | East Timor,2104 142 | Solomon Islands,2081 143 | Nigeria,1994 144 | Djibouti,1989 145 | India,1983 146 | Republic of the Congo,1958 147 | São Tomé and Príncipe,1785 148 | Kiribati,1721 149 | Kenya,1702 150 | Ghana,1663 151 | Côte d'Ivoire,1617 152 | Bangladesh,1602 153 | Pakistan,1541 154 | Uzbekistan,1491 155 | Zambia,1480 156 | Sudan,1428 157 | Lesotho,1425 158 | Cameroon,1401 159 | Cambodia,1390 160 | Mauritania,1318 161 | Myanmar,1264 162 | Zimbabwe,1176 163 | Kyrgyzstan,1144 164 | Senegal,1038 165 | Tanzania,1034 166 | Eritrea,980 167 | Ethiopia,873 168 | Nepal,834 169 | Benin,830 170 | Tajikistan,824 171 | Mali,811 172 | Chad,810 173 | Guinea-Bissau,794 174 | Comoros,788 175 | Haiti,784 176 | Rwanda,772 177 | Guinea,749 178 | Liberia,729 179 | Uganda,699 180 | Burkina Faso,664 181 | Togo,611 182 | Afghanistan,588 183 | Yemen,551 184 | Sierra Leone,491 185 | "Gambia, The",480 186 | Democratic Republic of the Congo,478 187 | Madagascar,448 188 | Niger,440 189 | Mozambique,429 190 | Central African Republic,387 191 | Malawi,324 192 | Burundi,312 193 | South Sudan,228 194 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/preamble.tex: -------------------------------------------------------------------------------- 1 | \RequirePackage{pifont,manfnt} 2 | \RequirePackage{booktabs} 3 | \usepackage{subcaption} 4 | \RequirePackage[T1]{fontenc} 5 | \RequirePackage{mathpazo} 6 | \RequirePackage{eulervm} 7 | \linespread{1.05} 8 | \RequirePackage{tikz} 9 | \usepackage{pgfplots} 10 | \usetikzlibrary{arrows,positioning,matrix} 11 | \RequirePackage{xspace} 12 | %\RequirePackage{apacite} 13 | \RequirePackage{rotating} 14 | \RequirePackage{multirow} 15 | \usepackage{fontawesome} 16 | \usepackage{nth} 17 | \pgfplotsset{compat=1.16} 18 | \newcommand{\Prob}[1]{\mathrm{P}( #1 )} 19 | \newcommand{\dcat}[1]{\mathrm{dcat}( #1 )} 20 | \newcommand{\ddirichlet}[1]{\mathrm{ddirichlet}( #1 )} 21 | \newcommand*{\given}{\vert} 22 | \newcommand{\hdpmm}{\textsc{hdptm}\xspace} 23 | \newcommand{\bnc}{\textsc{bnc}\xspace} 24 | \newcommand{\brms}{Brms\xspace} 25 | \newcommand{\mcmc}{\textsc{mcmc}\xspace} 26 | \newcommand{\icc}{\textsc{icc}\xspace} 27 | \newcommand{\reml}{\textsc{reml}\xspace} 28 | \newcommand{\mad}{\textsc{mad}\xspace} 29 | 30 | \newcommand\iidsim{\mathrel{\overset{\makebox[0pt]{\mbox{\normalfont\tiny iid}}}{\sim}}} 31 | \newcommand\defeq{\mathrel{\overset{\makebox[0pt]{\mbox{\normalfont\tiny def}}}{=}}} 32 | \newcommand{\hpd}{\textsc{hpd}\xspace} 33 | \newcommand{\Probc}[1]{\mathrm{P}_{\text{\!\tiny \textsc{c}}}( #1 )} 34 | \newcommand{\Proba}[1]{\mathrm{P}_{\text{\!\tiny \textsc{a}}}( #1 )} 35 | \newcommand{\wnew}{w_{j}} 36 | \newcommand{\wjinew}{w_{ji}} 37 | \newcommand{\pinew}{\pi_{j}} 38 | \newcommand{\data}{\mathcal{D}} 39 | \newcommand{\dic}{\textsc{dic}\xspace} 40 | \newcommand{\studentt}[1]{t_{#1}} 41 | 42 | \RequirePackage{tikz} 43 | \usetikzlibrary{trees} 44 | \usetikzlibrary{matrix} 45 | 46 | \RequirePackage{amssymb,latexsym,amsmath,amsfonts,amscd} 47 | 48 | 49 | 50 | \newcommand{\rreallytiny}{\fontsize{3}{3}\selectfont} 51 | \newcommand{\reallytiny}{\fontsize{5}{5}\selectfont} 52 | 53 | \usetikzlibrary{decorations.pathmorphing} % noisy shapes 54 | \usetikzlibrary{fit} % fitting shapes to coordinates 55 | \usetikzlibrary{backgrounds} % drawing the background after the foreground 56 | \usetikzlibrary{matrix} 57 | 58 | \tikzstyle{background}=[rectangle, fill=none, 59 | draw=black, 60 | inner sep=0.3cm, 61 | rounded corners=3mm] 62 | 63 | \tikzstyle{observation}=[circle,font=\small,minimum size=5mm,inner sep=0mm, 64 | draw=black!70, 65 | fill=black!10] 66 | 67 | \tikzstyle{state}=[circle,font=\small,minimum size=5mm,inner sep=0mm, 68 | draw=black!70, 69 | fill=none] 70 | 71 | \tikzstyle{limit}=[rectangle,font=\small,minimum size=0mm,inner sep=0mm, 72 | fill=none] 73 | 74 | \tikzstyle{parameter}=[circle,font=\small,minimum size=5mm,inner sep=0mm, 75 | draw=black!70, 76 | fill=none] 77 | % tikz stuff 78 | \usepackage{tikz} 79 | \usetikzlibrary{shapes} 80 | \usetikzlibrary{positioning,shapes,trees,arrows,shadows,arrows.meta,backgrounds,fit} 81 | 82 | \tikzset{every path/.style={-latex,thick}} 83 | 84 | \tikzset{ 85 | basic/.style = {font=\sffamily}, 86 | % material/.style = {basic, text width=8mm, font=\footnotesize\sffamily, fill=yellow!60}, 87 | % revision/.style = {material, fill=blue!30}, 88 | % root/.style = {basic, align=center, fill=pink!60}, 89 | level 1/.style = {basic, align=center, sibling distance = 30mm}, 90 | level 2/.style = {basic, sibling distance = 20mm}, 91 | level 3/.style = {basic, sibling distance = 15mm}, 92 | % level 4/.style = {level distance=10mm,basic, fill=pink!60, sibling distance = 20mm} 93 | } 94 | 95 | 96 | \graphicspath{{../images/}} 97 | 98 | \DeclareSymbolFont{legacymaths}{OT1}{cmr}{m}{n} 99 | \DeclareMathAccent{\dot} {\mathalpha}{legacymaths}{95} 100 | \DeclareMathAccent{\bar} {\mathalpha}{legacymaths}{22} 101 | \DeclareMathAccent{\tilde} {\mathalpha}{legacymaths}{126} 102 | 103 | \setbeamertemplate{footline}[frame number] 104 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/nonmultilevel_sleepstudy.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} \centering 2 | \begin{subfigure}[b]{0.4\linewidth} 3 | \centering 4 | \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 5 | \node[mynode] (y_1) {$y_{jd}$}; 6 | \node[mynode,above = of y_1] (x_1) {$x_{jd}$}; 7 | \node[right=5mm of y_1] (i) {$d \in 1\ldots n_j$}; 8 | 9 | \node[mynodex, below=20mm of y_1] (beta) {$\vec{\beta}_j$}; 10 | \node[mynodex, right=20mm of beta] (sigma) {$\sigma_j$}; 11 | \node[below=3mm of beta] (j) {$j \in 1\ldots J$}; 12 | 13 | \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 14 | \node[draw,rounded corners=2mm,inner sep=10mm,fit=(y_1) (x_1) (beta) (sigma)] {}; 15 | 16 | \foreach \value in {x_1,beta,sigma} 17 | \draw[->] (\value) to (y_1); 18 | \draw[->] (sigma) to (y_1); 19 | \end{tikzpicture} 20 | \end{subfigure} 21 | \hspace{10mm} 22 | \begin{subfigure}[b]{0.4\linewidth} 23 | \centering 24 | 25 | \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 26 | 27 | \node[mynode] (y_1) {$y_{jd}$}; 28 | \node[mynode,above = of y_1] (x_1) {$x_{jd}$}; 29 | \node[right=5mm of y_1] (i) {$d \in 1\ldots n_j$}; 30 | 31 | \node[mynodex, below=20mm of y_1] (beta) {$\vec{\beta}_j$}; 32 | \node[below=3mm of beta] (j) {$j \in 1\ldots J$}; 33 | \node[mynodex, left=13mm of beta] (sigma) {$\sigma$}; 34 | \node[right=20mm of beta] (phantom) {$$}; 35 | 36 | \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 37 | \node[draw,rounded corners=2mm,inner sep=10mm,fit=(y_1) (x_1) (beta) (phantom)] {}; 38 | 39 | \foreach \value in {x_1,beta} 40 | \draw[->] (\value) to (y_1); 41 | \draw[->] (sigma) [bend left] to (y_1); 42 | 43 | \end{tikzpicture} 44 | \end{subfigure} 45 | \caption{Bayesian network diagrams for a) a set of $J$ independent normal linear models, and b) a varying slope and varying intercept linear model whereby the slope and intercept vary by a categorical variable with $J$ levels.} 46 | \label{fig:bda_sleepstudy} 47 | \end{figure} 48 | 49 | % 50 | % 51 | % \begin{figure} 52 | % \centering 53 | % \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 54 | % \node[mynode] (y_1) {$y_{jd}$}; 55 | % \node[mynode,above = of y_1] (x_1) {$x_{jd}$}; 56 | % \node[right=5mm of y_1] (i) {$d \in 1\ldots n_j$}; 57 | % 58 | % \node[mynodex, below=20mm of y_1] (beta) {$\vec{\beta}_j$}; 59 | % \node[mynodex, right=20mm of beta] (sigma) {$\sigma_j$}; 60 | % \node[below=3mm of beta] (j) {$j \in 1\ldots J$}; 61 | % 62 | % \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 63 | % \node[draw,rounded corners=2mm,inner sep=10mm,fit=(y_1) (x_1) (beta) (sigma)] {}; 64 | % 65 | % \foreach \value in {x_1,beta,sigma} 66 | % \draw[->] (\value) to (y_1); 67 | % %\draw[->] (sigma) to (y_1); 68 | % \end{tikzpicture} 69 | % 70 | % \caption{Bayesian network diagrams for a) a set of $J$ independent normal linear models, and b) a varying slope and varying intercept linear model whereby the slope and intercept vary by a categorical variable with $J$ levels.} 71 | % \label{fig:bda_sleepstudy} 72 | % \end{figure} 73 | % 74 | % \begin{subfigure}[b]{0.4\linewidth} 75 | % \centering 76 | % \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 77 | % 78 | % \node[mynode] (y_1) {$y_{jd}$}; 79 | % \node[mynode,above = of y_1] (x_1) {$x_{jd}$}; 80 | % \node[right=5mm of y_1] (i) {$d \in 1\ldots n_j$}; 81 | % 82 | % \node[mynodex, below=20mm of y_1] (beta) {$\vec{\beta}_j$}; 83 | % \node[below=3mm of beta] (j) {$j \in 1\ldots J$}; 84 | % \node[mynodex, left=13mm of beta] (sigma) {$\sigma$}; 85 | % \node[right=20mm of beta] (phantom) {$$}; 86 | % 87 | % \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 88 | % \node[draw,rounded corners=2mm,inner sep=10mm,fit=(y_1) (x_1) (beta) (phantom)] {}; 89 | % 90 | % \foreach \value in {x_1,beta} 91 | % \draw[->] (\value) to (y_1); 92 | % \draw[->] (sigma) [bend left] to (y_1); 93 | % 94 | % \end{tikzpicture} -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/include/nonmultilevel_sleepstudy_s.tex: -------------------------------------------------------------------------------- 1 | \begin{figure} \centering 2 | \begin{subfigure}[b]{0.4\linewidth} 3 | \centering 4 | \resizebox{\textwidth}{!}{% 5 | \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 6 | \node[mynode] (y_1) {$y_{jd}$}; 7 | \node[mynode,above = of y_1] (x_1) {$x_{jd}$}; 8 | \node[right=5mm of y_1] (i) {$d \in 1\ldots n_j$}; 9 | 10 | \node[mynodex, below=20mm of y_1] (beta) {$\vec{\beta}_j$}; 11 | \node[mynodex, right=20mm of beta] (sigma) {$\sigma_j$}; 12 | \node[below=3mm of beta] (j) {$j \in 1\ldots J$}; 13 | 14 | \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 15 | \node[draw,rounded corners=2mm,inner sep=10mm,fit=(y_1) (x_1) (beta) (sigma)] {}; 16 | 17 | \foreach \value in {x_1,beta,sigma} 18 | \draw[->] (\value) to (y_1); 19 | \draw[->] (sigma) to (y_1); 20 | \end{tikzpicture}} 21 | \end{subfigure} 22 | \hspace{10mm} 23 | \begin{subfigure}[b]{0.4\linewidth} 24 | \centering 25 | \resizebox{\textwidth}{!}{% 26 | \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 27 | 28 | \node[mynode] (y_1) {$y_{jd}$}; 29 | \node[mynode,above = of y_1] (x_1) {$x_{jd}$}; 30 | \node[right=5mm of y_1] (i) {$d \in 1\ldots n_j$}; 31 | 32 | \node[mynodex, below=20mm of y_1] (beta) {$\vec{\beta}_j$}; 33 | \node[below=3mm of beta] (j) {$j \in 1\ldots J$}; 34 | \node[mynodex, left=13mm of beta] (sigma) {$\sigma$}; 35 | \node[right=20mm of beta] (phantom) {$$}; 36 | 37 | \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 38 | \node[draw,rounded corners=2mm,inner sep=10mm,fit=(y_1) (x_1) (beta) (phantom)] {}; 39 | 40 | \foreach \value in {x_1,beta} 41 | \draw[->] (\value) to (y_1); 42 | \draw[->] (sigma) [bend left] to (y_1); 43 | 44 | \end{tikzpicture}} 45 | \end{subfigure} 46 | \caption{Bayesian network diagrams for a) a set of $J$ independent normal linear models, and b) a varying slope and varying intercept linear model whereby the slope and intercept vary by a categorical variable with $J$ levels.} 47 | \label{fig:bda_sleepstudy} 48 | \end{figure} 49 | 50 | % 51 | % 52 | % \begin{figure} 53 | % \centering 54 | % \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 55 | % \node[mynode] (y_1) {$y_{jd}$}; 56 | % \node[mynode,above = of y_1] (x_1) {$x_{jd}$}; 57 | % \node[right=5mm of y_1] (i) {$d \in 1\ldots n_j$}; 58 | % 59 | % \node[mynodex, below=20mm of y_1] (beta) {$\vec{\beta}_j$}; 60 | % \node[mynodex, right=20mm of beta] (sigma) {$\sigma_j$}; 61 | % \node[below=3mm of beta] (j) {$j \in 1\ldots J$}; 62 | % 63 | % \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 64 | % \node[draw,rounded corners=2mm,inner sep=10mm,fit=(y_1) (x_1) (beta) (sigma)] {}; 65 | % 66 | % \foreach \value in {x_1,beta,sigma} 67 | % \draw[->] (\value) to (y_1); 68 | % %\draw[->] (sigma) to (y_1); 69 | % \end{tikzpicture} 70 | % 71 | % \caption{Bayesian network diagrams for a) a set of $J$ independent normal linear models, and b) a varying slope and varying intercept linear model whereby the slope and intercept vary by a categorical variable with $J$ levels.} 72 | % \label{fig:bda_sleepstudy} 73 | % \end{figure} 74 | % 75 | % \begin{subfigure}[b]{0.4\linewidth} 76 | % \centering 77 | % \begin{tikzpicture}[mynode/.style={draw,circle, fill=red!10,minimum width=10mm,align=center},mynodex/.style={draw,circle, minimum width=10mm,align=center}] 78 | % 79 | % \node[mynode] (y_1) {$y_{jd}$}; 80 | % \node[mynode,above = of y_1] (x_1) {$x_{jd}$}; 81 | % \node[right=5mm of y_1] (i) {$d \in 1\ldots n_j$}; 82 | % 83 | % \node[mynodex, below=20mm of y_1] (beta) {$\vec{\beta}_j$}; 84 | % \node[below=3mm of beta] (j) {$j \in 1\ldots J$}; 85 | % \node[mynodex, left=13mm of beta] (sigma) {$\sigma$}; 86 | % \node[right=20mm of beta] (phantom) {$$}; 87 | % 88 | % \node[draw,rounded corners=2mm,inner sep=5mm,fit=(y_1) (x_1) (i)] {}; 89 | % \node[draw,rounded corners=2mm,inner sep=10mm,fit=(y_1) (x_1) (beta) (phantom)] {}; 90 | % 91 | % \foreach \value in {x_1,beta} 92 | % \draw[->] (\value) to (y_1); 93 | % \draw[->] (sigma) [bend left] to (y_1); 94 | % 95 | % \end{tikzpicture} -------------------------------------------------------------------------------- /contents/chapter_16_shiny/shiny/likelihood/app.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ggplot2) 3 | 4 | likelihood <- function(x, N, n){ 5 | x^n * (1-x)^(N-n) 6 | } 7 | 8 | find.roots <- function(n, N, z=1/4) { 9 | 10 | f <- function(x, N, n, z) { 11 | standardized.likelihood <- likelihood(x, N, n) / likelihood(n/N, N, n) 12 | standardized.likelihood - z 13 | } 14 | 15 | left.root <- uniroot(f, 16 | c(0, n/N), 17 | tol = 0.0001, 18 | N = N, 19 | n=n, 20 | z=z) 21 | 22 | right.root <- uniroot(f, 23 | c(n/N, 1), 24 | tol = 0.0001, 25 | N = N, 26 | n=n, 27 | z=z) 28 | 29 | list(x = left.root$root, 30 | y = likelihood(left.root$root, N, n), 31 | xend = right.root$root, 32 | yend = likelihood(right.root$root, N, n)) 33 | } 34 | 35 | 36 | # Define UI for application that draws a histogram 37 | ui <- fluidPage( 38 | 39 | withMathJax(), 40 | 41 | # Application title 42 | titlePanel("Likelihood function of a coin's bias"), 43 | 44 | # Sidebar with a slider input for number of bins 45 | sidebarLayout( 46 | sidebarPanel( 47 | helpText("Calculate the likelihood function of the bias on a coin, 48 | conditioned on having observed the outcomes of a sequence of flips of this coin. 49 | Use the controls to specify the total number of coin flips, 50 | and the total number of times a Heads is the observed outcome. 51 | Check boxes to choose which likelihood intervals to draw. 52 | The likelihood interval gives the set of values of the bias whose likelihoods are within the specified factor 53 | of the maximum likelihood value.", 54 | "All source code for this demo can be found on", 55 | a("GitHub.", 56 | href="https://github.com/lawsofthought/psypag-kent-2017", 57 | target='_blank')), 58 | sliderInput("N", 59 | "Number of coin flips:", 60 | min = 1, 61 | max = 250, 62 | value = 130, 63 | step = 1), 64 | uiOutput("n.obs.slider"), 65 | radioButtons("likelihood.interval", 66 | label="Likelihood interval:", 67 | choices=c("\\(\\tfrac{1}{2}\\)" = "1", 68 | "\\(\\tfrac{1}{4}\\)" = "2", 69 | "\\(\\tfrac{1}{8}\\)" = "3", 70 | "\\(\\tfrac{1}{16}\\)" = "4", 71 | "\\(\\tfrac{1}{32}\\)" = "5"), 72 | selected="5"), 73 | htmlOutput("text") 74 | ), 75 | # Show a plot of the generated distribution 76 | mainPanel( 77 | plotOutput("distPlot", width="100%") 78 | ) 79 | ) 80 | ) 81 | 82 | # Define server logic required to draw a histogram 83 | server <- function(input, output, session) { 84 | 85 | output$n.obs.slider <- renderUI({ 86 | sliderInput("n.obs", 87 | "Observed number of Heads: ", 88 | min=0, 89 | max=input$N, 90 | step=1, 91 | value=max(1, 0.4*input$N)) 92 | }) 93 | 94 | output$distPlot <- renderPlot({ 95 | 96 | withMathJax() 97 | 98 | expected.value <- input$N*input$theta 99 | 100 | if (is.null(input$n.obs)){ 101 | n.obs <- max(1, 0.4*input$N) 102 | } else { 103 | n.obs <- input$n.obs 104 | } 105 | 106 | theta <- seq(0, 1, by = 0.001) 107 | ll <- likelihood(theta, input$N, n.obs) 108 | 109 | mle <- n.obs/input$N 110 | 111 | Df <- data.frame(theta, 112 | ll = choose(input$N, n.obs) * ll) 113 | 114 | 115 | 116 | likelihood.interval <- switch(input$likelihood.interval, 117 | "1" = 1/2, 118 | "2" = 1/4, 119 | "3" = 1/8, 120 | "4" = 1/16, 121 | "5" = 1/32) 122 | 123 | roots <- find.roots(n.obs, input$N, likelihood.interval) 124 | 125 | ggplot(Df, 126 | mapping=aes(x = theta, y = ll)) + 127 | geom_line() + 128 | theme_classic() + 129 | xlab('theta') + 130 | ylab('P(theta)') + 131 | geom_segment(aes(x = roots$x, 132 | y = roots$y * choose(input$N, n.obs), 133 | xend = roots$xend, 134 | yend = roots$yend * choose(input$N, n.obs)), 135 | col='red') 136 | 137 | }, height = 600, width = 800) 138 | } 139 | 140 | # Run the application 141 | shinyApp(ui = ui, server = server) -------------------------------------------------------------------------------- /contents/header.tex: -------------------------------------------------------------------------------- 1 | % LaTeX packages 2 | \usepackage{xspace} 3 | \usepackage{booktabs} 4 | \usepackage{tikz} 5 | \usepackage{float} 6 | \usepackage{nth} 7 | \usepackage{lipsum} 8 | \usepackage{mathtools} 9 | \usepackage{pgfplots} 10 | \usepackage{fontawesome} 11 | 12 | % this is needed somewhere but not everywhere 13 | % and conflicts with subfigure 14 | %\usepackage{subcaption} 15 | 16 | % LaTeX newcommands etc 17 | \def\argmax{\mathop{\rm argmax}} 18 | \def\argmin{\mathop{\rm argmin}} 19 | \newcommand{\aicc}{$\textsc{aic}_{c}$\xspace} 20 | \newcommand*{\aic}{\textsc{aic}\xspace} 21 | \newcommand{\betatrue}{\vec{\beta}_{\star}} 22 | \newcommand{\data}{\mathcal{D}} 23 | \newcommand{\ecdf}{\textsc{ecdf}\xspace} 24 | \newcommand*{\elpd}{\textsc{elpd}\xspace} 25 | \newcommand{\gams}{\textsc{gam}s\xspace} 26 | \newcommand{\gam}{\textsc{gam}\xspace} 27 | \newcommand*{\gui}{\textsc{gui}\xspace} 28 | \newcommand*{\given}{\vert} 29 | \newcommand{\hatse}{\hat{\textrm{se}}} 30 | \newcommand{\hatsemu}{\hat{\textrm{se}}_{\mu_\iota}} 31 | \newcommand{\hatsey}{\hat{\textrm{se}}_{y_\iota}} 32 | \newcommand*{\hmc}{\textsc{hmc}\xspace} 33 | \newcommand*{\hpd}{\textsc{hpd}\xspace} 34 | \newcommand{\fmle}{f_{\textrm{mle}}} 35 | \newcommand{\icc}{\textsc{icc}\xspace} 36 | \newcommand*{\iid}{\textsc{iid}\xspace} 37 | \newcommand{\iqr}{\textsc{iqr}\xspace} 38 | \newcommand{\latex}{\LaTeX{}\xspace} 39 | \newcommand{\loocv}{\textsc{loocv}\xspace} 40 | \newcommand*{\looic}{\textsc{looic}\xspace} 41 | \newcommand{\mad}{\textsc{mad}\xspace} 42 | \newcommand*{\mcmc}{\textsc{mcmc}\xspace} 43 | \newcommand*{\model}{\mathcal{M}} 44 | \newcommand{\nse}{\textsc{nse}\xspace} 45 | \newcommand{\oop}{\textsc{oop}\xspace} 46 | \newcommand{\Pop}[1]{\mathcal{P}( #1 )} 47 | \newcommand{\Prob}[1]{\mathrm{P}( #1 )} 48 | \newcommand{\PROB}[1]{\mathrm{P}\left( #1 \right)} 49 | \newcommand*{\pvalues}{\textit{p}-values\xspace} 50 | \newcommand*{\pvalue}{\textit{p}-value\xspace} 51 | \newcommand{\rbfs}{\textsc{rbf}s\xspace} 52 | \newcommand{\rbf}{\textsc{rbf}\xspace} 53 | \newcommand{\reml}{\textsc{reml}\xspace} 54 | \newcommand{\rmsea}{\textsc{rmsea}\xspace} 55 | \newcommand{\rss}{\textsc{rss}\xspace} 56 | \newcommand{\sem}{\textsc{sem}\xspace} 57 | \newcommand{\sigmatrue}{\sigma_{\star}} 58 | \newcommand*{\thetahat}{\hat{\theta}} 59 | \newcommand*{\thetastar}{\theta_{\star}} 60 | \newcommand*{\thetatrue}{\thetastar} 61 | \newcommand*{\tpi}{\pi} 62 | \newcommand*{\tthetaprop}{\tilde{\theta}_\prime} 63 | \newcommand*{\ttheta}{\tilde{\theta}} 64 | \newcommand{\vcs}{\textsc{vcs}\xspace} 65 | \newcommand*{\waic}{\textsc{waic}\xspace} 66 | \renewcommand{\linethickness}{0.05em} % Used in Chapter 7 for making LaTeX tables 67 | 68 | \DeclarePairedDelimiter{\diagfences}{(}{)} 69 | \newcommand{\diag}{\operatorname{diag}\diagfences} 70 | 71 | % Not sure why I need this 72 | % But I get a warning recommending that I add it. 73 | \pgfplotsset{compat=1.16} 74 | 75 | % TikZ settings 76 | \usetikzlibrary{positioning,shapes,trees,arrows,shadows,arrows.meta,backgrounds,fit,matrix} 77 | 78 | \tikzset{ 79 | %Define style for boxes 80 | punkt/.style={ 81 | rectangle, 82 | rounded corners, 83 | draw=black, very thick, 84 | text width=6.5em, 85 | minimum height=2em, 86 | text centered}, 87 | } 88 | 89 | \tikzset{ 90 | treenode/.style = {shape=rectangle, 91 | rounded corners, 92 | draw, 93 | align=center, 94 | top color=white, 95 | font=\ttfamily\normalsize}, 96 | conditional/.style = {treenode, bottom color = red!20}, 97 | code/.style = {treenode, bottom color = gray!20}, 98 | dummy/.style = {treenode}, 99 | edge_label/.style = {font=\ttfamily\normalsize}, 100 | every picture/.style = {level distance = 8em, 101 | sibling distance=10em, 102 | level 1/.style = {sibling distance=15em}, 103 | level 2/.style = {sibling distance=8em}, 104 | edge from parent/.style = {draw, -latex}, 105 | sloped} 106 | } 107 | 108 | \tikzstyle{background}=[rectangle, fill=none, 109 | draw=black, 110 | inner sep=0.3cm, 111 | rounded corners=3mm] 112 | 113 | \tikzstyle{observation}=[circle,font=\small,minimum size=5mm,inner sep=0mm, 114 | draw=black!70, 115 | fill=black!10] 116 | 117 | \tikzstyle{state}=[circle,font=\small,minimum size=5mm,inner sep=0mm, 118 | draw=black!70, 119 | fill=none] 120 | 121 | \tikzstyle{limit}=[rectangle,font=\small,minimum size=0mm,inner sep=0mm, 122 | fill=none] 123 | 124 | \tikzstyle{parameter}=[circle,font=\small,minimum size=5mm,inner sep=0mm, 125 | draw=black!70, 126 | fill=none] 127 | 128 | 129 | 130 | % LaTeX float defaults 131 | % --------------------------- 132 | % See p.105 of "TeX Unbound" for suggested values. 133 | % See pp. 199-200 of Lamport's "LaTeX" book for details. 134 | % General parameters, for ALL pages: 135 | \renewcommand{\topfraction}{0.9} % max fraction of floats at top 136 | \renewcommand{\bottomfraction}{0.8} % max fraction of floats at bottom 137 | % Parameters for TEXT pages (not float pages): 138 | \setcounter{topnumber}{2} 139 | \setcounter{bottomnumber}{2} 140 | \setcounter{totalnumber}{4} % 2 may work better 141 | \setcounter{dbltopnumber}{2} % for 2-column pages 142 | \renewcommand{\dbltopfraction}{0.9} % fit big float above 2-col. text 143 | \renewcommand{\textfraction}{0.07} % allow minimal text w. figs 144 | % Parameters for FLOAT pages (not text pages): 145 | \renewcommand{\floatpagefraction}{0.7} % require fuller float pages 146 | % N.B.: floatpagefraction MUST be less than topfraction !! 147 | \renewcommand{\dblfloatpagefraction}{0.7} % require fuller float pages 148 | -------------------------------------------------------------------------------- /contents/chapter_15_hpc/data/housing.csv: -------------------------------------------------------------------------------- 1 | price 2 | 42000 3 | 38500 4 | 49500 5 | 60500 6 | 61000 7 | 66000 8 | 66000 9 | 69000 10 | 83800 11 | 88500 12 | 90000 13 | 30500 14 | 27000 15 | 36000 16 | 37000 17 | 37900 18 | 40500 19 | 40750 20 | 45000 21 | 45000 22 | 48500 23 | 65900 24 | 37900 25 | 38000 26 | 42000 27 | 42300 28 | 43500 29 | 44000 30 | 44500 31 | 44900 32 | 45000 33 | 48000 34 | 49000 35 | 51500 36 | 61000 37 | 61000 38 | 61700 39 | 67000 40 | 82000 41 | 54500 42 | 66500 43 | 70000 44 | 82000 45 | 92000 46 | 38000 47 | 44000 48 | 41000 49 | 43000 50 | 48000 51 | 54800 52 | 55000 53 | 57000 54 | 68000 55 | 95000 56 | 38000 57 | 25000 58 | 25245 59 | 56000 60 | 35500 61 | 30000 62 | 48000 63 | 48000 64 | 52000 65 | 54000 66 | 56000 67 | 60000 68 | 60000 69 | 67000 70 | 47000 71 | 70000 72 | 45000 73 | 51000 74 | 32500 75 | 34000 76 | 35000 77 | 36000 78 | 45000 79 | 47000 80 | 55000 81 | 63900 82 | 50000 83 | 35000 84 | 50000 85 | 43000 86 | 55500 87 | 57000 88 | 60000 89 | 78000 90 | 35000 91 | 44000 92 | 47000 93 | 58000 94 | 163000 95 | 128000 96 | 123500 97 | 39000 98 | 53900 99 | 59900 100 | 35000 101 | 43000 102 | 57000 103 | 79000 104 | 125000 105 | 132000 106 | 58000 107 | 43000 108 | 48000 109 | 58500 110 | 73000 111 | 63500 112 | 43000 113 | 46500 114 | 92000 115 | 75000 116 | 75000 117 | 85000 118 | 93000 119 | 94500 120 | 106500 121 | 116000 122 | 61500 123 | 80000 124 | 37000 125 | 59500 126 | 70000 127 | 95000 128 | 117000 129 | 122500 130 | 123500 131 | 127000 132 | 35000 133 | 44500 134 | 49900 135 | 50500 136 | 65000 137 | 90000 138 | 46000 139 | 35000 140 | 26500 141 | 43000 142 | 56000 143 | 40000 144 | 51000 145 | 51000 146 | 57250 147 | 44000 148 | 61000 149 | 62000 150 | 80000 151 | 50000 152 | 59900 153 | 35500 154 | 37000 155 | 42000 156 | 48000 157 | 60000 158 | 60000 159 | 60000 160 | 62000 161 | 63000 162 | 63900 163 | 130000 164 | 25000 165 | 50000 166 | 52900 167 | 62000 168 | 73500 169 | 38000 170 | 46000 171 | 48000 172 | 52500 173 | 32000 174 | 38000 175 | 46000 176 | 50000 177 | 57500 178 | 70000 179 | 69900 180 | 74500 181 | 42000 182 | 60000 183 | 50000 184 | 58000 185 | 63900 186 | 28000 187 | 54000 188 | 44700 189 | 47000 190 | 50000 191 | 57250 192 | 67000 193 | 52500 194 | 42000 195 | 57500 196 | 33000 197 | 34400 198 | 40000 199 | 40500 200 | 46500 201 | 52000 202 | 53000 203 | 53900 204 | 50000 205 | 55500 206 | 56000 207 | 60000 208 | 60000 209 | 69500 210 | 72000 211 | 92500 212 | 40500 213 | 42000 214 | 47900 215 | 52000 216 | 62000 217 | 41000 218 | 138300 219 | 42000 220 | 47000 221 | 64500 222 | 46000 223 | 58000 224 | 70100 225 | 78500 226 | 87250 227 | 70800 228 | 56000 229 | 48000 230 | 68000 231 | 79000 232 | 80000 233 | 87000 234 | 25000 235 | 32500 236 | 36000 237 | 42500 238 | 43000 239 | 50000 240 | 26000 241 | 30000 242 | 34000 243 | 52000 244 | 70000 245 | 27000 246 | 32500 247 | 37200 248 | 38000 249 | 42000 250 | 44500 251 | 45000 252 | 48500 253 | 52000 254 | 53900 255 | 60000 256 | 61000 257 | 64500 258 | 71000 259 | 75500 260 | 33500 261 | 41000 262 | 41000 263 | 46200 264 | 48500 265 | 48900 266 | 50000 267 | 51000 268 | 52500 269 | 52500 270 | 54000 271 | 59000 272 | 60000 273 | 63000 274 | 64000 275 | 64900 276 | 65000 277 | 66000 278 | 70000 279 | 65500 280 | 57000 281 | 52000 282 | 54000 283 | 74500 284 | 90000 285 | 45000 286 | 45000 287 | 65000 288 | 55000 289 | 62000 290 | 30000 291 | 34000 292 | 38000 293 | 39000 294 | 45000 295 | 47000 296 | 47500 297 | 49000 298 | 50000 299 | 50000 300 | 52900 301 | 53000 302 | 55000 303 | 56000 304 | 58500 305 | 59500 306 | 60000 307 | 64000 308 | 67000 309 | 68100 310 | 70000 311 | 72000 312 | 57500 313 | 69900 314 | 70000 315 | 75000 316 | 76900 317 | 78000 318 | 80000 319 | 82000 320 | 83000 321 | 83000 322 | 83900 323 | 88500 324 | 93000 325 | 98000 326 | 98500 327 | 99000 328 | 101000 329 | 110000 330 | 115442 331 | 120000 332 | 124000 333 | 175000 334 | 50000 335 | 55000 336 | 60000 337 | 61000 338 | 106000 339 | 155000 340 | 141000 341 | 62500 342 | 70000 343 | 73000 344 | 80000 345 | 80000 346 | 88000 347 | 49000 348 | 52000 349 | 59500 350 | 60000 351 | 64000 352 | 64500 353 | 68500 354 | 78500 355 | 86000 356 | 86900 357 | 75000 358 | 78000 359 | 95000 360 | 97000 361 | 107000 362 | 130000 363 | 145000 364 | 175000 365 | 72000 366 | 84900 367 | 99000 368 | 114000 369 | 120000 370 | 145000 371 | 79000 372 | 82000 373 | 85000 374 | 100500 375 | 122000 376 | 126500 377 | 133000 378 | 140000 379 | 190000 380 | 84000 381 | 97000 382 | 103500 383 | 112500 384 | 140000 385 | 74700 386 | 78000 387 | 78900 388 | 83900 389 | 85000 390 | 85000 391 | 86000 392 | 86900 393 | 94500 394 | 96000 395 | 106000 396 | 72000 397 | 74500 398 | 77000 399 | 80750 400 | 82900 401 | 85000 402 | 92500 403 | 76000 404 | 77500 405 | 80000 406 | 80000 407 | 86000 408 | 87000 409 | 87500 410 | 89000 411 | 89900 412 | 90000 413 | 95000 414 | 112000 415 | 31900 416 | 52000 417 | 90000 418 | 100000 419 | 91700 420 | 174500 421 | 94700 422 | 68000 423 | 80000 424 | 61100 425 | 62900 426 | 65500 427 | 66000 428 | 49500 429 | 50000 430 | 53500 431 | 58550 432 | 64500 433 | 65000 434 | 69000 435 | 73000 436 | 75000 437 | 75000 438 | 132000 439 | 60000 440 | 65000 441 | 69000 442 | 51900 443 | 57000 444 | 65000 445 | 79500 446 | 72500 447 | 104900 448 | 114900 449 | 120000 450 | 58000 451 | 67000 452 | 67000 453 | 69000 454 | 73000 455 | 73500 456 | 74900 457 | 75000 458 | 79500 459 | 120900 460 | 44555 461 | 47000 462 | 47600 463 | 49000 464 | 49000 465 | 49000 466 | 49500 467 | 52000 468 | 54000 469 | 55000 470 | 55000 471 | 56000 472 | 60000 473 | 60500 474 | 50000 475 | 64900 476 | 93000 477 | 85000 478 | 61500 479 | 88500 480 | 88000 481 | 89000 482 | 89500 483 | 95000 484 | 95500 485 | 51500 486 | 62900 487 | 118500 488 | 42900 489 | 44100 490 | 47000 491 | 50000 492 | 50000 493 | 53000 494 | 53000 495 | 54000 496 | 58500 497 | 59000 498 | 60000 499 | 62900 500 | 64000 501 | 65000 502 | 67900 503 | 68500 504 | 70000 505 | 70500 506 | 71500 507 | 71900 508 | 75000 509 | 75000 510 | 87000 511 | 64000 512 | 70000 513 | 47500 514 | 62600 515 | 66000 516 | 58900 517 | 53000 518 | 95000 519 | 96500 520 | 101000 521 | 102000 522 | 103000 523 | 105000 524 | 108000 525 | 110000 526 | 113000 527 | 120000 528 | 105000 529 | 106000 530 | 107500 531 | 108000 532 | 113750 533 | 120000 534 | 70000 535 | 71000 536 | 82000 537 | 82000 538 | 82500 539 | 83000 540 | 84000 541 | 85000 542 | 85000 543 | 91500 544 | 94000 545 | 103000 546 | 105000 547 | 105000 548 | -------------------------------------------------------------------------------- /contents/chapter_16_shiny/data/housing.csv: -------------------------------------------------------------------------------- 1 | price 2 | 42000 3 | 38500 4 | 49500 5 | 60500 6 | 61000 7 | 66000 8 | 66000 9 | 69000 10 | 83800 11 | 88500 12 | 90000 13 | 30500 14 | 27000 15 | 36000 16 | 37000 17 | 37900 18 | 40500 19 | 40750 20 | 45000 21 | 45000 22 | 48500 23 | 65900 24 | 37900 25 | 38000 26 | 42000 27 | 42300 28 | 43500 29 | 44000 30 | 44500 31 | 44900 32 | 45000 33 | 48000 34 | 49000 35 | 51500 36 | 61000 37 | 61000 38 | 61700 39 | 67000 40 | 82000 41 | 54500 42 | 66500 43 | 70000 44 | 82000 45 | 92000 46 | 38000 47 | 44000 48 | 41000 49 | 43000 50 | 48000 51 | 54800 52 | 55000 53 | 57000 54 | 68000 55 | 95000 56 | 38000 57 | 25000 58 | 25245 59 | 56000 60 | 35500 61 | 30000 62 | 48000 63 | 48000 64 | 52000 65 | 54000 66 | 56000 67 | 60000 68 | 60000 69 | 67000 70 | 47000 71 | 70000 72 | 45000 73 | 51000 74 | 32500 75 | 34000 76 | 35000 77 | 36000 78 | 45000 79 | 47000 80 | 55000 81 | 63900 82 | 50000 83 | 35000 84 | 50000 85 | 43000 86 | 55500 87 | 57000 88 | 60000 89 | 78000 90 | 35000 91 | 44000 92 | 47000 93 | 58000 94 | 163000 95 | 128000 96 | 123500 97 | 39000 98 | 53900 99 | 59900 100 | 35000 101 | 43000 102 | 57000 103 | 79000 104 | 125000 105 | 132000 106 | 58000 107 | 43000 108 | 48000 109 | 58500 110 | 73000 111 | 63500 112 | 43000 113 | 46500 114 | 92000 115 | 75000 116 | 75000 117 | 85000 118 | 93000 119 | 94500 120 | 106500 121 | 116000 122 | 61500 123 | 80000 124 | 37000 125 | 59500 126 | 70000 127 | 95000 128 | 117000 129 | 122500 130 | 123500 131 | 127000 132 | 35000 133 | 44500 134 | 49900 135 | 50500 136 | 65000 137 | 90000 138 | 46000 139 | 35000 140 | 26500 141 | 43000 142 | 56000 143 | 40000 144 | 51000 145 | 51000 146 | 57250 147 | 44000 148 | 61000 149 | 62000 150 | 80000 151 | 50000 152 | 59900 153 | 35500 154 | 37000 155 | 42000 156 | 48000 157 | 60000 158 | 60000 159 | 60000 160 | 62000 161 | 63000 162 | 63900 163 | 130000 164 | 25000 165 | 50000 166 | 52900 167 | 62000 168 | 73500 169 | 38000 170 | 46000 171 | 48000 172 | 52500 173 | 32000 174 | 38000 175 | 46000 176 | 50000 177 | 57500 178 | 70000 179 | 69900 180 | 74500 181 | 42000 182 | 60000 183 | 50000 184 | 58000 185 | 63900 186 | 28000 187 | 54000 188 | 44700 189 | 47000 190 | 50000 191 | 57250 192 | 67000 193 | 52500 194 | 42000 195 | 57500 196 | 33000 197 | 34400 198 | 40000 199 | 40500 200 | 46500 201 | 52000 202 | 53000 203 | 53900 204 | 50000 205 | 55500 206 | 56000 207 | 60000 208 | 60000 209 | 69500 210 | 72000 211 | 92500 212 | 40500 213 | 42000 214 | 47900 215 | 52000 216 | 62000 217 | 41000 218 | 138300 219 | 42000 220 | 47000 221 | 64500 222 | 46000 223 | 58000 224 | 70100 225 | 78500 226 | 87250 227 | 70800 228 | 56000 229 | 48000 230 | 68000 231 | 79000 232 | 80000 233 | 87000 234 | 25000 235 | 32500 236 | 36000 237 | 42500 238 | 43000 239 | 50000 240 | 26000 241 | 30000 242 | 34000 243 | 52000 244 | 70000 245 | 27000 246 | 32500 247 | 37200 248 | 38000 249 | 42000 250 | 44500 251 | 45000 252 | 48500 253 | 52000 254 | 53900 255 | 60000 256 | 61000 257 | 64500 258 | 71000 259 | 75500 260 | 33500 261 | 41000 262 | 41000 263 | 46200 264 | 48500 265 | 48900 266 | 50000 267 | 51000 268 | 52500 269 | 52500 270 | 54000 271 | 59000 272 | 60000 273 | 63000 274 | 64000 275 | 64900 276 | 65000 277 | 66000 278 | 70000 279 | 65500 280 | 57000 281 | 52000 282 | 54000 283 | 74500 284 | 90000 285 | 45000 286 | 45000 287 | 65000 288 | 55000 289 | 62000 290 | 30000 291 | 34000 292 | 38000 293 | 39000 294 | 45000 295 | 47000 296 | 47500 297 | 49000 298 | 50000 299 | 50000 300 | 52900 301 | 53000 302 | 55000 303 | 56000 304 | 58500 305 | 59500 306 | 60000 307 | 64000 308 | 67000 309 | 68100 310 | 70000 311 | 72000 312 | 57500 313 | 69900 314 | 70000 315 | 75000 316 | 76900 317 | 78000 318 | 80000 319 | 82000 320 | 83000 321 | 83000 322 | 83900 323 | 88500 324 | 93000 325 | 98000 326 | 98500 327 | 99000 328 | 101000 329 | 110000 330 | 115442 331 | 120000 332 | 124000 333 | 175000 334 | 50000 335 | 55000 336 | 60000 337 | 61000 338 | 106000 339 | 155000 340 | 141000 341 | 62500 342 | 70000 343 | 73000 344 | 80000 345 | 80000 346 | 88000 347 | 49000 348 | 52000 349 | 59500 350 | 60000 351 | 64000 352 | 64500 353 | 68500 354 | 78500 355 | 86000 356 | 86900 357 | 75000 358 | 78000 359 | 95000 360 | 97000 361 | 107000 362 | 130000 363 | 145000 364 | 175000 365 | 72000 366 | 84900 367 | 99000 368 | 114000 369 | 120000 370 | 145000 371 | 79000 372 | 82000 373 | 85000 374 | 100500 375 | 122000 376 | 126500 377 | 133000 378 | 140000 379 | 190000 380 | 84000 381 | 97000 382 | 103500 383 | 112500 384 | 140000 385 | 74700 386 | 78000 387 | 78900 388 | 83900 389 | 85000 390 | 85000 391 | 86000 392 | 86900 393 | 94500 394 | 96000 395 | 106000 396 | 72000 397 | 74500 398 | 77000 399 | 80750 400 | 82900 401 | 85000 402 | 92500 403 | 76000 404 | 77500 405 | 80000 406 | 80000 407 | 86000 408 | 87000 409 | 87500 410 | 89000 411 | 89900 412 | 90000 413 | 95000 414 | 112000 415 | 31900 416 | 52000 417 | 90000 418 | 100000 419 | 91700 420 | 174500 421 | 94700 422 | 68000 423 | 80000 424 | 61100 425 | 62900 426 | 65500 427 | 66000 428 | 49500 429 | 50000 430 | 53500 431 | 58550 432 | 64500 433 | 65000 434 | 69000 435 | 73000 436 | 75000 437 | 75000 438 | 132000 439 | 60000 440 | 65000 441 | 69000 442 | 51900 443 | 57000 444 | 65000 445 | 79500 446 | 72500 447 | 104900 448 | 114900 449 | 120000 450 | 58000 451 | 67000 452 | 67000 453 | 69000 454 | 73000 455 | 73500 456 | 74900 457 | 75000 458 | 79500 459 | 120900 460 | 44555 461 | 47000 462 | 47600 463 | 49000 464 | 49000 465 | 49000 466 | 49500 467 | 52000 468 | 54000 469 | 55000 470 | 55000 471 | 56000 472 | 60000 473 | 60500 474 | 50000 475 | 64900 476 | 93000 477 | 85000 478 | 61500 479 | 88500 480 | 88000 481 | 89000 482 | 89500 483 | 95000 484 | 95500 485 | 51500 486 | 62900 487 | 118500 488 | 42900 489 | 44100 490 | 47000 491 | 50000 492 | 50000 493 | 53000 494 | 53000 495 | 54000 496 | 58500 497 | 59000 498 | 60000 499 | 62900 500 | 64000 501 | 65000 502 | 67900 503 | 68500 504 | 70000 505 | 70500 506 | 71500 507 | 71900 508 | 75000 509 | 75000 510 | 87000 511 | 64000 512 | 70000 513 | 47500 514 | 62600 515 | 66000 516 | 58900 517 | 53000 518 | 95000 519 | 96500 520 | 101000 521 | 102000 522 | 103000 523 | 105000 524 | 108000 525 | 110000 526 | 113000 527 | 120000 528 | 105000 529 | 106000 530 | 107500 531 | 108000 532 | 113750 533 | 120000 534 | 70000 535 | 71000 536 | 82000 537 | 82000 538 | 82500 539 | 83000 540 | 84000 541 | 85000 542 | 85000 543 | 91500 544 | 94000 545 | 103000 546 | 105000 547 | 105000 548 | -------------------------------------------------------------------------------- /contents/chapter_08_inference/data/housing.csv: -------------------------------------------------------------------------------- 1 | price 2 | 42000 3 | 38500 4 | 49500 5 | 60500 6 | 61000 7 | 66000 8 | 66000 9 | 69000 10 | 83800 11 | 88500 12 | 90000 13 | 30500 14 | 27000 15 | 36000 16 | 37000 17 | 37900 18 | 40500 19 | 40750 20 | 45000 21 | 45000 22 | 48500 23 | 65900 24 | 37900 25 | 38000 26 | 42000 27 | 42300 28 | 43500 29 | 44000 30 | 44500 31 | 44900 32 | 45000 33 | 48000 34 | 49000 35 | 51500 36 | 61000 37 | 61000 38 | 61700 39 | 67000 40 | 82000 41 | 54500 42 | 66500 43 | 70000 44 | 82000 45 | 92000 46 | 38000 47 | 44000 48 | 41000 49 | 43000 50 | 48000 51 | 54800 52 | 55000 53 | 57000 54 | 68000 55 | 95000 56 | 38000 57 | 25000 58 | 25245 59 | 56000 60 | 35500 61 | 30000 62 | 48000 63 | 48000 64 | 52000 65 | 54000 66 | 56000 67 | 60000 68 | 60000 69 | 67000 70 | 47000 71 | 70000 72 | 45000 73 | 51000 74 | 32500 75 | 34000 76 | 35000 77 | 36000 78 | 45000 79 | 47000 80 | 55000 81 | 63900 82 | 50000 83 | 35000 84 | 50000 85 | 43000 86 | 55500 87 | 57000 88 | 60000 89 | 78000 90 | 35000 91 | 44000 92 | 47000 93 | 58000 94 | 163000 95 | 128000 96 | 123500 97 | 39000 98 | 53900 99 | 59900 100 | 35000 101 | 43000 102 | 57000 103 | 79000 104 | 125000 105 | 132000 106 | 58000 107 | 43000 108 | 48000 109 | 58500 110 | 73000 111 | 63500 112 | 43000 113 | 46500 114 | 92000 115 | 75000 116 | 75000 117 | 85000 118 | 93000 119 | 94500 120 | 106500 121 | 116000 122 | 61500 123 | 80000 124 | 37000 125 | 59500 126 | 70000 127 | 95000 128 | 117000 129 | 122500 130 | 123500 131 | 127000 132 | 35000 133 | 44500 134 | 49900 135 | 50500 136 | 65000 137 | 90000 138 | 46000 139 | 35000 140 | 26500 141 | 43000 142 | 56000 143 | 40000 144 | 51000 145 | 51000 146 | 57250 147 | 44000 148 | 61000 149 | 62000 150 | 80000 151 | 50000 152 | 59900 153 | 35500 154 | 37000 155 | 42000 156 | 48000 157 | 60000 158 | 60000 159 | 60000 160 | 62000 161 | 63000 162 | 63900 163 | 130000 164 | 25000 165 | 50000 166 | 52900 167 | 62000 168 | 73500 169 | 38000 170 | 46000 171 | 48000 172 | 52500 173 | 32000 174 | 38000 175 | 46000 176 | 50000 177 | 57500 178 | 70000 179 | 69900 180 | 74500 181 | 42000 182 | 60000 183 | 50000 184 | 58000 185 | 63900 186 | 28000 187 | 54000 188 | 44700 189 | 47000 190 | 50000 191 | 57250 192 | 67000 193 | 52500 194 | 42000 195 | 57500 196 | 33000 197 | 34400 198 | 40000 199 | 40500 200 | 46500 201 | 52000 202 | 53000 203 | 53900 204 | 50000 205 | 55500 206 | 56000 207 | 60000 208 | 60000 209 | 69500 210 | 72000 211 | 92500 212 | 40500 213 | 42000 214 | 47900 215 | 52000 216 | 62000 217 | 41000 218 | 138300 219 | 42000 220 | 47000 221 | 64500 222 | 46000 223 | 58000 224 | 70100 225 | 78500 226 | 87250 227 | 70800 228 | 56000 229 | 48000 230 | 68000 231 | 79000 232 | 80000 233 | 87000 234 | 25000 235 | 32500 236 | 36000 237 | 42500 238 | 43000 239 | 50000 240 | 26000 241 | 30000 242 | 34000 243 | 52000 244 | 70000 245 | 27000 246 | 32500 247 | 37200 248 | 38000 249 | 42000 250 | 44500 251 | 45000 252 | 48500 253 | 52000 254 | 53900 255 | 60000 256 | 61000 257 | 64500 258 | 71000 259 | 75500 260 | 33500 261 | 41000 262 | 41000 263 | 46200 264 | 48500 265 | 48900 266 | 50000 267 | 51000 268 | 52500 269 | 52500 270 | 54000 271 | 59000 272 | 60000 273 | 63000 274 | 64000 275 | 64900 276 | 65000 277 | 66000 278 | 70000 279 | 65500 280 | 57000 281 | 52000 282 | 54000 283 | 74500 284 | 90000 285 | 45000 286 | 45000 287 | 65000 288 | 55000 289 | 62000 290 | 30000 291 | 34000 292 | 38000 293 | 39000 294 | 45000 295 | 47000 296 | 47500 297 | 49000 298 | 50000 299 | 50000 300 | 52900 301 | 53000 302 | 55000 303 | 56000 304 | 58500 305 | 59500 306 | 60000 307 | 64000 308 | 67000 309 | 68100 310 | 70000 311 | 72000 312 | 57500 313 | 69900 314 | 70000 315 | 75000 316 | 76900 317 | 78000 318 | 80000 319 | 82000 320 | 83000 321 | 83000 322 | 83900 323 | 88500 324 | 93000 325 | 98000 326 | 98500 327 | 99000 328 | 101000 329 | 110000 330 | 115442 331 | 120000 332 | 124000 333 | 175000 334 | 50000 335 | 55000 336 | 60000 337 | 61000 338 | 106000 339 | 155000 340 | 141000 341 | 62500 342 | 70000 343 | 73000 344 | 80000 345 | 80000 346 | 88000 347 | 49000 348 | 52000 349 | 59500 350 | 60000 351 | 64000 352 | 64500 353 | 68500 354 | 78500 355 | 86000 356 | 86900 357 | 75000 358 | 78000 359 | 95000 360 | 97000 361 | 107000 362 | 130000 363 | 145000 364 | 175000 365 | 72000 366 | 84900 367 | 99000 368 | 114000 369 | 120000 370 | 145000 371 | 79000 372 | 82000 373 | 85000 374 | 100500 375 | 122000 376 | 126500 377 | 133000 378 | 140000 379 | 190000 380 | 84000 381 | 97000 382 | 103500 383 | 112500 384 | 140000 385 | 74700 386 | 78000 387 | 78900 388 | 83900 389 | 85000 390 | 85000 391 | 86000 392 | 86900 393 | 94500 394 | 96000 395 | 106000 396 | 72000 397 | 74500 398 | 77000 399 | 80750 400 | 82900 401 | 85000 402 | 92500 403 | 76000 404 | 77500 405 | 80000 406 | 80000 407 | 86000 408 | 87000 409 | 87500 410 | 89000 411 | 89900 412 | 90000 413 | 95000 414 | 112000 415 | 31900 416 | 52000 417 | 90000 418 | 100000 419 | 91700 420 | 174500 421 | 94700 422 | 68000 423 | 80000 424 | 61100 425 | 62900 426 | 65500 427 | 66000 428 | 49500 429 | 50000 430 | 53500 431 | 58550 432 | 64500 433 | 65000 434 | 69000 435 | 73000 436 | 75000 437 | 75000 438 | 132000 439 | 60000 440 | 65000 441 | 69000 442 | 51900 443 | 57000 444 | 65000 445 | 79500 446 | 72500 447 | 104900 448 | 114900 449 | 120000 450 | 58000 451 | 67000 452 | 67000 453 | 69000 454 | 73000 455 | 73500 456 | 74900 457 | 75000 458 | 79500 459 | 120900 460 | 44555 461 | 47000 462 | 47600 463 | 49000 464 | 49000 465 | 49000 466 | 49500 467 | 52000 468 | 54000 469 | 55000 470 | 55000 471 | 56000 472 | 60000 473 | 60500 474 | 50000 475 | 64900 476 | 93000 477 | 85000 478 | 61500 479 | 88500 480 | 88000 481 | 89000 482 | 89500 483 | 95000 484 | 95500 485 | 51500 486 | 62900 487 | 118500 488 | 42900 489 | 44100 490 | 47000 491 | 50000 492 | 50000 493 | 53000 494 | 53000 495 | 54000 496 | 58500 497 | 59000 498 | 60000 499 | 62900 500 | 64000 501 | 65000 502 | 67900 503 | 68500 504 | 70000 505 | 70500 506 | 71500 507 | 71900 508 | 75000 509 | 75000 510 | 87000 511 | 64000 512 | 70000 513 | 47500 514 | 62600 515 | 66000 516 | 58900 517 | 53000 518 | 95000 519 | 96500 520 | 101000 521 | 102000 522 | 103000 523 | 105000 524 | 108000 525 | 110000 526 | 113000 527 | 120000 528 | 105000 529 | 106000 530 | 107500 531 | 108000 532 | 113750 533 | 120000 534 | 70000 535 | 71000 536 | 82000 537 | 82000 538 | 82500 539 | 83000 540 | 84000 541 | 85000 542 | 85000 543 | 91500 544 | 94000 545 | 103000 546 | 105000 547 | 105000 548 | -------------------------------------------------------------------------------- /contents/chapter_16_shiny/shiny/gghistogram/housing.csv: -------------------------------------------------------------------------------- 1 | price 2 | 42000 3 | 38500 4 | 49500 5 | 60500 6 | 61000 7 | 66000 8 | 66000 9 | 69000 10 | 83800 11 | 88500 12 | 90000 13 | 30500 14 | 27000 15 | 36000 16 | 37000 17 | 37900 18 | 40500 19 | 40750 20 | 45000 21 | 45000 22 | 48500 23 | 65900 24 | 37900 25 | 38000 26 | 42000 27 | 42300 28 | 43500 29 | 44000 30 | 44500 31 | 44900 32 | 45000 33 | 48000 34 | 49000 35 | 51500 36 | 61000 37 | 61000 38 | 61700 39 | 67000 40 | 82000 41 | 54500 42 | 66500 43 | 70000 44 | 82000 45 | 92000 46 | 38000 47 | 44000 48 | 41000 49 | 43000 50 | 48000 51 | 54800 52 | 55000 53 | 57000 54 | 68000 55 | 95000 56 | 38000 57 | 25000 58 | 25245 59 | 56000 60 | 35500 61 | 30000 62 | 48000 63 | 48000 64 | 52000 65 | 54000 66 | 56000 67 | 60000 68 | 60000 69 | 67000 70 | 47000 71 | 70000 72 | 45000 73 | 51000 74 | 32500 75 | 34000 76 | 35000 77 | 36000 78 | 45000 79 | 47000 80 | 55000 81 | 63900 82 | 50000 83 | 35000 84 | 50000 85 | 43000 86 | 55500 87 | 57000 88 | 60000 89 | 78000 90 | 35000 91 | 44000 92 | 47000 93 | 58000 94 | 163000 95 | 128000 96 | 123500 97 | 39000 98 | 53900 99 | 59900 100 | 35000 101 | 43000 102 | 57000 103 | 79000 104 | 125000 105 | 132000 106 | 58000 107 | 43000 108 | 48000 109 | 58500 110 | 73000 111 | 63500 112 | 43000 113 | 46500 114 | 92000 115 | 75000 116 | 75000 117 | 85000 118 | 93000 119 | 94500 120 | 106500 121 | 116000 122 | 61500 123 | 80000 124 | 37000 125 | 59500 126 | 70000 127 | 95000 128 | 117000 129 | 122500 130 | 123500 131 | 127000 132 | 35000 133 | 44500 134 | 49900 135 | 50500 136 | 65000 137 | 90000 138 | 46000 139 | 35000 140 | 26500 141 | 43000 142 | 56000 143 | 40000 144 | 51000 145 | 51000 146 | 57250 147 | 44000 148 | 61000 149 | 62000 150 | 80000 151 | 50000 152 | 59900 153 | 35500 154 | 37000 155 | 42000 156 | 48000 157 | 60000 158 | 60000 159 | 60000 160 | 62000 161 | 63000 162 | 63900 163 | 130000 164 | 25000 165 | 50000 166 | 52900 167 | 62000 168 | 73500 169 | 38000 170 | 46000 171 | 48000 172 | 52500 173 | 32000 174 | 38000 175 | 46000 176 | 50000 177 | 57500 178 | 70000 179 | 69900 180 | 74500 181 | 42000 182 | 60000 183 | 50000 184 | 58000 185 | 63900 186 | 28000 187 | 54000 188 | 44700 189 | 47000 190 | 50000 191 | 57250 192 | 67000 193 | 52500 194 | 42000 195 | 57500 196 | 33000 197 | 34400 198 | 40000 199 | 40500 200 | 46500 201 | 52000 202 | 53000 203 | 53900 204 | 50000 205 | 55500 206 | 56000 207 | 60000 208 | 60000 209 | 69500 210 | 72000 211 | 92500 212 | 40500 213 | 42000 214 | 47900 215 | 52000 216 | 62000 217 | 41000 218 | 138300 219 | 42000 220 | 47000 221 | 64500 222 | 46000 223 | 58000 224 | 70100 225 | 78500 226 | 87250 227 | 70800 228 | 56000 229 | 48000 230 | 68000 231 | 79000 232 | 80000 233 | 87000 234 | 25000 235 | 32500 236 | 36000 237 | 42500 238 | 43000 239 | 50000 240 | 26000 241 | 30000 242 | 34000 243 | 52000 244 | 70000 245 | 27000 246 | 32500 247 | 37200 248 | 38000 249 | 42000 250 | 44500 251 | 45000 252 | 48500 253 | 52000 254 | 53900 255 | 60000 256 | 61000 257 | 64500 258 | 71000 259 | 75500 260 | 33500 261 | 41000 262 | 41000 263 | 46200 264 | 48500 265 | 48900 266 | 50000 267 | 51000 268 | 52500 269 | 52500 270 | 54000 271 | 59000 272 | 60000 273 | 63000 274 | 64000 275 | 64900 276 | 65000 277 | 66000 278 | 70000 279 | 65500 280 | 57000 281 | 52000 282 | 54000 283 | 74500 284 | 90000 285 | 45000 286 | 45000 287 | 65000 288 | 55000 289 | 62000 290 | 30000 291 | 34000 292 | 38000 293 | 39000 294 | 45000 295 | 47000 296 | 47500 297 | 49000 298 | 50000 299 | 50000 300 | 52900 301 | 53000 302 | 55000 303 | 56000 304 | 58500 305 | 59500 306 | 60000 307 | 64000 308 | 67000 309 | 68100 310 | 70000 311 | 72000 312 | 57500 313 | 69900 314 | 70000 315 | 75000 316 | 76900 317 | 78000 318 | 80000 319 | 82000 320 | 83000 321 | 83000 322 | 83900 323 | 88500 324 | 93000 325 | 98000 326 | 98500 327 | 99000 328 | 101000 329 | 110000 330 | 115442 331 | 120000 332 | 124000 333 | 175000 334 | 50000 335 | 55000 336 | 60000 337 | 61000 338 | 106000 339 | 155000 340 | 141000 341 | 62500 342 | 70000 343 | 73000 344 | 80000 345 | 80000 346 | 88000 347 | 49000 348 | 52000 349 | 59500 350 | 60000 351 | 64000 352 | 64500 353 | 68500 354 | 78500 355 | 86000 356 | 86900 357 | 75000 358 | 78000 359 | 95000 360 | 97000 361 | 107000 362 | 130000 363 | 145000 364 | 175000 365 | 72000 366 | 84900 367 | 99000 368 | 114000 369 | 120000 370 | 145000 371 | 79000 372 | 82000 373 | 85000 374 | 100500 375 | 122000 376 | 126500 377 | 133000 378 | 140000 379 | 190000 380 | 84000 381 | 97000 382 | 103500 383 | 112500 384 | 140000 385 | 74700 386 | 78000 387 | 78900 388 | 83900 389 | 85000 390 | 85000 391 | 86000 392 | 86900 393 | 94500 394 | 96000 395 | 106000 396 | 72000 397 | 74500 398 | 77000 399 | 80750 400 | 82900 401 | 85000 402 | 92500 403 | 76000 404 | 77500 405 | 80000 406 | 80000 407 | 86000 408 | 87000 409 | 87500 410 | 89000 411 | 89900 412 | 90000 413 | 95000 414 | 112000 415 | 31900 416 | 52000 417 | 90000 418 | 100000 419 | 91700 420 | 174500 421 | 94700 422 | 68000 423 | 80000 424 | 61100 425 | 62900 426 | 65500 427 | 66000 428 | 49500 429 | 50000 430 | 53500 431 | 58550 432 | 64500 433 | 65000 434 | 69000 435 | 73000 436 | 75000 437 | 75000 438 | 132000 439 | 60000 440 | 65000 441 | 69000 442 | 51900 443 | 57000 444 | 65000 445 | 79500 446 | 72500 447 | 104900 448 | 114900 449 | 120000 450 | 58000 451 | 67000 452 | 67000 453 | 69000 454 | 73000 455 | 73500 456 | 74900 457 | 75000 458 | 79500 459 | 120900 460 | 44555 461 | 47000 462 | 47600 463 | 49000 464 | 49000 465 | 49000 466 | 49500 467 | 52000 468 | 54000 469 | 55000 470 | 55000 471 | 56000 472 | 60000 473 | 60500 474 | 50000 475 | 64900 476 | 93000 477 | 85000 478 | 61500 479 | 88500 480 | 88000 481 | 89000 482 | 89500 483 | 95000 484 | 95500 485 | 51500 486 | 62900 487 | 118500 488 | 42900 489 | 44100 490 | 47000 491 | 50000 492 | 50000 493 | 53000 494 | 53000 495 | 54000 496 | 58500 497 | 59000 498 | 60000 499 | 62900 500 | 64000 501 | 65000 502 | 67900 503 | 68500 504 | 70000 505 | 70500 506 | 71500 507 | 71900 508 | 75000 509 | 75000 510 | 87000 511 | 64000 512 | 70000 513 | 47500 514 | 62600 515 | 66000 516 | 58900 517 | 53000 518 | 95000 519 | 96500 520 | 101000 521 | 102000 522 | 103000 523 | 105000 524 | 108000 525 | 110000 526 | 113000 527 | 120000 528 | 105000 529 | 106000 530 | 107500 531 | 108000 532 | 113750 533 | 120000 534 | 70000 535 | 71000 536 | 82000 537 | 82000 538 | 82500 539 | 83000 540 | 84000 541 | 85000 542 | 85000 543 | 91500 544 | 94000 545 | 103000 546 | 105000 547 | 105000 548 | -------------------------------------------------------------------------------- /contents/chapter_06_programming/chapter_06_programming_slides.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Chapter 6: Programming in R" 3 | author: | 4 | | Mark Andrews 5 | | Psychology Department, Nottingham Trent University 6 | | 7 | | \faEnvelopeO\ ```mark.andrews@ntu.ac.uk``` 8 | fontsize: 10pt 9 | output: 10 | beamer_presentation: 11 | keep_tex: true 12 | fonttheme: "serif" 13 | bibliography: 14 | - "`r system('kpsewhich mjandrews.bib', intern=TRUE)`" 15 | biblio-style: apalike 16 | header-includes: 17 | - \input{header.tex} 18 | --- 19 | 20 | ```{r, echo=F} 21 | knitr::opts_chunk$set(echo = TRUE, prompt = FALSE, warning = FALSE, message = FALSE, comment='#>') 22 | # Thanks to 23 | # https://github.com/ramnathv/slidify/issues/189#issuecomment-15850008 24 | hook1 <- function(x){ gsub("```\n*```r*\n*", "", x) } 25 | hook2 <- function(x){ gsub("```\n+```\n", "", x) } 26 | knitr::knit_hooks$set(document = hook1) 27 | ``` 28 | 29 | # Introduction 30 | 31 | In this chapter, we aim to provide an introduction to some of the major programming features of R. 32 | 33 | * We'll begin with *functions* both because they can be very simple to use and because of the major role functions play in programming in R generally. 34 | * We will then consider *conditionals*, which allow us to execute different blocks of code depending on whether certain conditions are true. 35 | * We will then consider *iterations*, also known as *loops*. 36 | * This will lead on to *functionals*, which can often take the place of traditional loops in R. 37 | 38 | Note, however, that there is more to programming in R than what we cover here. 39 | 40 | # Functions 41 | 42 | Let's say we have a vector of probabilities 43 | $$ 44 | p_1, p_2 \ldots p_n 45 | $$ 46 | and we want to calculate 47 | $$ 48 | \log_b\left(\frac{p_i}{1-p_i}\right)\quad\text{for $i \in 1 \ldots n$}. 49 | $$ 50 | 51 | We can define a function to implement this as follows: 52 | ```{r} 53 | log_odds <- function(p, b){ 54 | log(p/(1-p), base = b) 55 | } 56 | ``` 57 | 58 | # Function input arguments 59 | 60 | Notice that in the function definition, we stated that it takes two input arguments `p` and `b`, and that the code in the body explicitly operates on `p` and `b`. 61 | As we see in the following code, we may also explicitly indicate which variables are mapped to `p` and `b`. 62 | 63 | ```{r} 64 | probs <- c(0.1, 0.25, 0.5, 0.75) 65 | log_base = 2 66 | log_odds(p=probs, b=log_base) 67 | ``` 68 | When using explicit assignment like this, the order of the arguments no longer matters. Thus, we can write the above code as follows. 69 | ```{r} 70 | log_odds(b=log_base, p=probs) 71 | ``` 72 | 73 | # Anonymous functions 74 | 75 | Consider the following function: 76 | ```{r} 77 | f <- function(x, g){ 78 | g(sum(x)) 79 | } 80 | ``` 81 | Here, `f` takes an object `x` and a function `g` and calls `g(sum(x))` and returns the result. 82 | We can pass in any existing R function we wish as the value of `g`, as in the following examples. 83 | ```{r} 84 | x <- c(0.1, 1.1, 2.7) 85 | f(x, log10) 86 | ``` 87 | However, we don't have to assign a name to our custom function and pass in that name. 88 | We can instead just pass in the unnamed, or *anonymous*, function itself as in the following examples. 89 | ```{r} 90 | f(x, function(x) x^2) 91 | ``` 92 | Anonymous functions are widely used in R. 93 | 94 | 95 | # Conditionals 96 | 97 | Conditionals allows us to execute some code based on whether some condition is true or not. Consider the following simple example. 98 | ```{r} 99 | library(tidyverse) 100 | 101 | # Make a data frame 102 | data_df <- tibble(x = rnorm(10), 103 | y = rnorm(10)) 104 | 105 | 106 | write_data <- TRUE 107 | 108 | if (write_data) { 109 | write_csv(data_df, 'tmp_data.csv') 110 | } 111 | ``` 112 | 113 | Here, we write `data_df` to a `.csv` file if and only if `write_data` is true. 114 | 115 | 116 | # *if ... else* statments 117 | 118 | Sometimes, we want to execute one code block if the condition is true and execute an alternative code block if it is false. 119 | To do this, we use an *if ... else* statement, as in the following example. 120 | ```{r, echo=F} 121 | write_csv(tibble(x = 17), 122 | 'data_new.csv') 123 | write_csv(tibble(x = 42), 124 | 'data_old.csv') 125 | ``` 126 | ```{r} 127 | use_new_data <- TRUE 128 | 129 | if (use_new_data){ 130 | data_df <- read_csv('data_new.csv') 131 | } else { 132 | data_df <- read_csv('data_old.csv') 133 | } 134 | ``` 135 | ```{r, echo=F} 136 | # Just to be sure that the above 137 | # code worked. 138 | stopifnot(data_df$x == 17) 139 | ``` 140 | As we can see, if `use_new_data` is true, we read in the data from `data_new.csv`, and otherwise we read the data in from `data_old.csv`. 141 | 142 | ```{r, echo=F} 143 | write_data_foo <- function(i){ 144 | write_csv(tibble(x=i), paste0('data_', i, '.csv')) 145 | } 146 | for (i in seq(3)) {write_data_foo(i)} 147 | ``` 148 | 149 | 150 | # `for` loops 151 | 152 | In R, there are two types of iterations or *loops*, which we informally refer to as *`for` loops* and *`while` loops*. 153 | 154 | Let's say we had the following vector of 1000 elements to which we wished to apply the `relu` function. 155 | ```{r} 156 | N <- 1000 157 | x <- seq(-0.1, 1.1, length.out = N) 158 | 159 | relu <- function(x){ 160 | if (x < 0) { 0 } else { x } 161 | } 162 | ``` 163 | We can create a for loop as follows. 164 | ```{r} 165 | y <- numeric(N) 166 | for (i in 1:N) { 167 | y[i] <- relu(x[i]) 168 | } 169 | ``` 170 | 171 | For each value of `i` from `1` to `N`, we execute `y[i] <- relu(x[i])`. 172 | 173 | 174 | # `while` loops 175 | 176 | Unlike for loops, which iterate through each value in a sequence of values, while loops continuously execute the code body while a condition remains true. 177 | 178 | As a very simple example of a while loop, let's say we wish to find the largest value of $k$ such that $2^k \leq 10^6$. 179 | ```{r} 180 | k <- 0 181 | while (2^(k+1) <= 10^6) { 182 | k <- k + 1 183 | } 184 | k 185 | ``` 186 | 187 | 188 | # `lapply` 189 | 190 | Functionals are functions that take a function as input and return a vector. 191 | There are many functionals in the base R language. 192 | Here, we will look at one of the most widely used: `lapply` 193 | 194 | The `lapply` functional takes a vector and a function, applies the function to each element in the vector and returns a new list. 195 | ```{r} 196 | x <- c(-1, 0, 1) 197 | y <- lapply(x, relu) 198 | y 199 | ``` 200 | 201 | 202 | # Functionals with `purrr` 203 | 204 | The `purrr` package in the `tidyverse` provides functionals like `lapply`. 205 | We can load `purrr` with `library(purrr)`, but it is also loaded by `library(tidyverse)`. 206 | 207 | One of the main tools in `purrr` is `map` and its variants. 208 | It is very similar to `lapply`. 209 | ```{r} 210 | y <- map(x, relu) 211 | y 212 | ``` 213 | 214 | -------------------------------------------------------------------------------- /contents/chapter_12_multilevel/data/mathachieveschool.csv: -------------------------------------------------------------------------------- 1 | school,size,sector,pracad,disclim,himinty,meanses 2 | s1224,842,Public,0.35,1.597,0,-0.428 3 | s1288,1855,Public,0.27,0.174,0,0.128 4 | s1296,1719,Public,0.32,-0.137,1,-0.42 5 | s1308,716,Catholic,0.96,-0.622,0,0.534 6 | s1317,455,Catholic,0.95,-1.694,1,0.351 7 | s1358,1430,Public,0.25,1.535,0,-0.014 8 | s1374,2400,Public,0.5,2.016,0,-0.007 9 | s1433,899,Catholic,0.96,-0.321,0,0.718 10 | s1436,185,Catholic,1,-1.141,0,0.569 11 | s1461,1672,Public,0.78,2.096,0,0.683 12 | s1462,530,Catholic,0.46,-0.287,1,-0.663 13 | s1477,531,Catholic,0.81,-0.729,0,0.166 14 | s1499,1921,Public,0.09,0.682,1,-0.46 15 | s1637,100,Public,0.1,1.215,1,-0.699 16 | s1906,400,Catholic,0.87,-0.939,0,0.518 17 | s1909,1357,Public,0.45,0.875,0,0.254 18 | s1942,959,Public,0.78,0.159,0,0.688 19 | s1946,1646,Public,0.38,0.439,0,0.01 20 | s2030,1332,Public,0.4,0.677,0,0.334 21 | s2208,1061,Catholic,0.68,-0.864,0,0.429 22 | s2277,773,Catholic,0.67,-0.725,1,-0.756 23 | s2305,485,Catholic,0.69,-1.382,1,-0.622 24 | s2336,1523,Public,0.63,0.156,0,0.448 25 | s2458,545,Catholic,0.89,-1.484,1,0.234 26 | s2467,488,Public,0.23,0.207,0,-0.324 27 | s2526,572,Catholic,0.97,-2.154,0,0.333 28 | s2626,2142,Public,0.4,0.142,0,-0.059 29 | s2629,1314,Catholic,0.81,-0.613,0,-0.132 30 | s2639,2713,Public,0.14,-0.282,1,-0.958 31 | s2651,1524,Public,0.22,1.136,0,0.069 32 | s2655,2475,Public,0.35,-0.378,1,-0.696 33 | s2658,780,Catholic,0.79,-0.961,0,0.444 34 | s2755,270,Catholic,0.96,-0.354,0,0.562 35 | s2768,1680,Public,0.31,0.086,0,-0.048 36 | s2771,415,Public,0.24,1.048,0,-0.333 37 | s2818,760,Public,0.41,1.541,0,0.115 38 | s2917,2194,Public,0.05,0.08,1,-0.792 39 | s2990,963,Catholic,0.8,-0.434,1,0.661 40 | s2995,365,Public,0.18,0.422,0,-0.296 41 | s3013,760,Public,0.56,-0.213,0,-0.038 42 | s3020,457,Catholic,0.56,-0.299,0,0.236 43 | s3039,686,Catholic,1,-1.236,1,0.354 44 | s3088,1960,Public,0.38,0.195,1,-0.467 45 | s3152,1313,Public,0.59,1.098,0,0.037 46 | s3332,2212,Public,0.55,-0.048,0,0.617 47 | s3351,1125,Public,0.35,0.383,1,0.332 48 | s3377,2031,Public,0.18,0.664,1,-0.579 49 | s3427,687,Catholic,1,-1.262,0,0.159 50 | s3498,388,Catholic,0.92,-1.556,0,0.666 51 | s3499,509,Catholic,0.7,-0.039,1,0.456 52 | s3533,1015,Catholic,0.54,0.022,0,-0.128 53 | s3610,1431,Catholic,0.8,-0.621,0,0.126 54 | s3657,1137,Public,0.31,-0.216,0,-0.643 55 | s3688,375,Catholic,0.91,-1.521,0,0.411 56 | s3705,670,Catholic,0.71,-0.165,1,0.241 57 | s3716,2357,Public,0.5,1.297,1,-0.415 58 | s3838,1635,Catholic,0.85,-0.255,1,0.151 59 | s3881,1881,Public,0.33,0.84,0,0.134 60 | s3967,1061,Public,0.46,-1.059,0,-0.219 61 | s3992,1114,Catholic,0.73,-1.534,1,0.371 62 | s3999,1910,Public,0.41,0.838,1,-0.085 63 | s4042,940,Catholic,0.83,-0.94,0,0.408 64 | s4173,801,Catholic,0.65,-0.755,0,0.038 65 | s4223,663,Catholic,0.3,-0.967,0,-0.088 66 | s4253,510,Catholic,0.6,-0.455,1,-0.368 67 | s4292,1328,Catholic,0.76,-0.674,1,-0.48 68 | s4325,1217,Public,0.41,0.349,0,-0.043 69 | s4350,1661,Public,0.62,0.512,0,0.159 70 | s4383,1061,Public,0.54,1.065,0,0.079 71 | s4410,100,Public,0.38,0.673,0,0.102 72 | s4420,728,Public,0.32,0.877,0,-0.218 73 | s4458,643,Public,0.5,0.715,1,-1.043 74 | s4511,1068,Catholic,0.52,-1.872,0,-0.101 75 | s4523,393,Catholic,0.69,-0.784,1,-0.048 76 | s4530,435,Catholic,0.6,-0.245,1,-0.591 77 | s4642,493,Public,0.43,-0.086,0,0.121 78 | s4868,657,Catholic,1,-0.219,0,0.367 79 | s4931,603,Catholic,0.79,-1.172,0,0.371 80 | s5192,258,Catholic,0.37,-0.36,0,-0.058 81 | s5404,700,Catholic,0.9,-0.746,0,0.831 82 | s5619,1118,Catholic,0.77,-1.286,0,0.426 83 | s5640,1152,Public,0.41,0.256,0,-0.171 84 | s5650,720,Catholic,0.6,-0.07,1,0.028 85 | s5667,810,Catholic,0.59,-0.533,0,0.425 86 | s5720,381,Catholic,0.65,-0.352,0,0.039 87 | s5761,215,Catholic,0.63,-0.892,0,-0.317 88 | s5762,1826,Public,0.24,0.364,1,-1.188 89 | s5783,1832,Public,0.54,0.46,0,0.179 90 | s5815,949,Public,0.19,1.749,0,-0.674 91 | s5819,1302,Public,0.41,-0.26,0,0.188 92 | s5838,1627,Public,0.53,0.138,0,0.163 93 | s5937,1153,Public,0.58,-0.142,0,0.657 94 | s6074,2051,Catholic,0.32,-1.018,0,-0.271 95 | s6089,594,Public,0.53,-0.965,0,0.092 96 | s6144,1608,Public,0.17,1.329,0,-0.432 97 | s6170,2126,Public,0.25,0.432,1,-0.296 98 | s6291,1037,Public,0.26,-0.303,1,-0.516 99 | s6366,1566,Catholic,0.81,-0.667,0,0.316 100 | s6397,1234,Public,0.32,0.642,1,-0.211 101 | s6415,296,Public,0.48,1.04,0,-0.181 102 | s6443,1061,Public,0.1,-0.42,0,-0.347 103 | s6464,1453,Public,0.25,2.756,1,-0.67 104 | s6469,565,Catholic,1,-0.865,0,0.759 105 | s6484,726,Public,0.19,0.218,0,-0.179 106 | s6578,336,Catholic,0.19,-0.874,0,-0.511 107 | s6600,299,Public,0.28,-0.432,0,-0.048 108 | s6808,1700,Public,0.18,1.669,1,-0.09 109 | s6816,745,Catholic,0.96,-1.886,1,0.535 110 | s6897,1415,Public,0.55,-0.361,0,0.356 111 | s6990,1650,Public,0.59,0.475,1,-0.484 112 | s7011,100,Catholic,0.44,0.1,0,-0.025 113 | s7101,2403,Public,0.39,1.024,0,0.013 114 | s7172,280,Catholic,0.05,1.013,1,-0.284 115 | s7232,1154,Public,0.2,0.975,0,-0.084 116 | s7276,1630,Public,0.3,0.109,0,0.084 117 | s7332,737,Catholic,0.53,-0.864,0,0.303 118 | s7341,570,Public,0.14,0.583,0,-0.158 119 | s7342,1220,Catholic,0.46,0.38,1,-0.442 120 | s7345,978,Public,0.64,0.336,1,0.039 121 | s7364,1500,Catholic,0.35,-1.523,0,-0.083 122 | s7635,1495,Catholic,0.48,-0.397,0,0.249 123 | s7688,1410,Catholic,0.65,-0.575,0,0.192 124 | s7697,1734,Public,0.2,0.279,0,0.264 125 | s7734,1580,Public,0.4,2.027,1,-0.463 126 | s7890,311,Public,0.21,0.845,0,-0.517 127 | s7919,1451,Public,0.5,-0.402,0,0.464 128 | s8009,716,Catholic,0.68,-0.933,0,0.522 129 | s8150,868,Catholic,0.67,-0.057,0,0.312 130 | s8165,1346,Catholic,0.6,-0.37,0,0.315 131 | s8175,1016,Public,0.42,1.033,1,-0.166 132 | s8188,1896,Public,0.25,1.695,0,0.145 133 | s8193,106,Catholic,1,-1.186,0,-0.171 134 | s8202,1436,Public,0.39,1.373,0,0.092 135 | s8357,2070,Public,0.5,0.244,0,-0.101 136 | s8367,153,Public,0,1.793,0,0.032 137 | s8477,1400,Public,0.24,-0.001,0,-0.19 138 | s8531,2190,Public,0.58,0.132,0,0.414 139 | s8627,2452,Public,0.25,0.742,0,0.111 140 | s8628,652,Catholic,0.66,-0.367,0,-0.134 141 | s8707,1133,Public,0.48,1.542,0,0.161 142 | s8775,667,Public,0.28,1.783,0,-0.334 143 | s8800,296,Catholic,0.3,-1.142,1,-0.703 144 | s8854,745,Public,0.18,-0.228,0,-0.751 145 | s8857,1069,Catholic,0.65,-1.718,1,0.523 146 | s8874,2650,Public,0.2,1.742,0,-0.335 147 | s8946,204,Public,0.44,0.335,0,-0.345 148 | s8983,1061,Public,0.27,0.079,0,-0.288 149 | s9021,472,Catholic,0.82,-0.817,0,0.633 150 | s9104,1299,Catholic,0.97,-0.718,0,0.749 151 | s9158,822,Public,0.43,0.735,0,-0.396 152 | s9198,833,Catholic,0.97,-1.225,0,0.498 153 | s9225,1951,Public,0.56,0.048,0,0.259 154 | s9292,2350,Public,0.13,2.043,1,-0.588 155 | s9340,257,Public,0.47,0.274,0,-0.391 156 | s9347,1067,Catholic,0.58,-0.905,0,0.218 157 | s9359,1184,Catholic,0.69,-0.475,0,0.36 158 | s9397,1314,Public,0.44,-0.231,0,0.14 159 | s9508,1119,Catholic,0.52,-1.138,0,-0.132 160 | s9550,1532,Public,0.45,0.791,0,0.059 161 | s9586,262,Catholic,1,-2.416,0,0.627 162 | -------------------------------------------------------------------------------- /contents/chapter_10_logistic/data/lbw.csv: -------------------------------------------------------------------------------- 1 | "","low","smoke","race","age","lwt","ptl","ht","ui","ftv","bwt" 2 | "1",0,0,2,19,182,0,0,1,0,2523 3 | "2",0,0,3,33,155,0,0,0,3,2551 4 | "3",0,1,1,20,105,0,0,0,1,2557 5 | "4",0,1,1,21,108,0,0,1,2,2594 6 | "5",0,1,1,18,107,0,0,1,0,2600 7 | "6",0,0,3,21,124,0,0,0,0,2622 8 | "7",0,0,1,22,118,0,0,0,1,2637 9 | "8",0,0,3,17,103,0,0,0,1,2637 10 | "9",0,1,1,29,123,0,0,0,1,2663 11 | "10",0,1,1,26,113,0,0,0,0,2665 12 | "11",0,0,3,19,95,0,0,0,0,2722 13 | "12",0,0,3,19,150,0,0,0,1,2733 14 | "13",0,0,3,22,95,0,1,0,0,2750 15 | "14",0,0,3,30,107,1,0,1,2,2750 16 | "15",0,1,1,18,100,0,0,0,0,2769 17 | "16",0,1,1,18,100,0,0,0,0,2769 18 | "17",0,0,2,15,98,0,0,0,0,2778 19 | "18",0,1,1,25,118,0,0,0,3,2782 20 | "19",0,0,3,20,120,0,0,1,0,2807 21 | "20",0,1,1,28,120,0,0,0,1,2821 22 | "21",0,0,3,32,121,0,0,0,2,2835 23 | "22",0,0,1,31,100,0,0,1,3,2835 24 | "23",0,0,1,36,202,0,0,0,1,2836 25 | "24",0,0,3,28,120,0,0,0,0,2863 26 | "25",0,0,3,25,120,0,0,1,2,2877 27 | "26",0,0,1,28,167,0,0,0,0,2877 28 | "27",0,1,1,17,122,0,0,0,0,2906 29 | "28",0,0,1,29,150,0,0,0,2,2920 30 | "29",0,1,2,26,168,0,0,0,0,2920 31 | "30",0,0,2,17,113,0,0,0,1,2920 32 | "31",0,0,2,17,113,0,0,0,1,2920 33 | "32",0,1,1,24,90,1,0,0,1,2948 34 | "33",0,1,2,35,121,1,0,0,1,2948 35 | "34",0,0,1,25,155,0,0,0,1,2977 36 | "35",0,0,2,25,125,0,0,0,0,2977 37 | "36",0,1,1,29,140,0,0,0,2,2977 38 | "37",0,1,1,19,138,0,0,0,2,2977 39 | "38",0,1,1,27,124,0,0,0,0,2922 40 | "39",0,1,1,31,215,0,0,0,2,3005 41 | "40",0,1,1,33,109,0,0,0,1,3033 42 | "41",0,1,2,21,185,0,0,0,2,3042 43 | "42",0,0,1,19,189,0,0,0,2,3062 44 | "43",0,0,2,23,130,0,0,0,1,3062 45 | "44",0,0,1,21,160,0,0,0,0,3062 46 | "45",0,1,1,18,90,0,0,1,0,3076 47 | "46",0,1,1,18,90,0,0,1,0,3076 48 | "47",0,0,1,32,132,0,0,0,4,3080 49 | "48",0,0,3,19,132,0,0,0,0,3090 50 | "49",0,0,1,24,115,0,0,0,2,3090 51 | "50",0,1,3,22,85,0,0,0,0,3090 52 | "51",0,0,1,22,120,0,1,0,1,3100 53 | "52",0,0,3,23,128,0,0,0,0,3104 54 | "53",0,1,1,22,130,0,0,0,0,3132 55 | "54",0,1,1,30,95,0,0,0,2,3147 56 | "55",0,0,3,19,115,0,0,0,0,3175 57 | "56",0,0,3,16,110,0,0,0,0,3175 58 | "57",0,1,3,21,110,0,0,1,0,3203 59 | "58",0,0,3,30,153,0,0,0,0,3203 60 | "59",0,0,3,20,103,0,0,0,0,3203 61 | "60",0,0,3,17,119,0,0,0,0,3225 62 | "61",0,0,3,17,119,0,0,0,0,3225 63 | "62",0,0,3,23,119,0,0,0,2,3232 64 | "63",0,0,3,24,110,0,0,0,0,3232 65 | "64",0,0,1,28,140,0,0,0,0,3234 66 | "65",0,1,3,26,133,2,0,0,0,3260 67 | "66",0,0,3,20,169,1,0,1,1,3274 68 | "67",0,0,3,24,115,0,0,0,2,3274 69 | "68",0,1,3,28,250,0,0,0,6,3303 70 | "69",0,0,1,20,141,2,0,1,1,3317 71 | "70",0,0,2,22,158,1,0,0,2,3317 72 | "71",0,1,1,22,112,2,0,0,0,3317 73 | "72",0,1,3,31,150,0,0,0,2,3321 74 | "73",0,1,3,23,115,0,0,0,1,3331 75 | "74",0,0,2,16,112,0,0,0,0,3374 76 | "75",0,1,1,16,135,0,0,0,0,3374 77 | "76",0,0,2,18,229,0,0,0,0,3402 78 | "77",0,0,1,25,140,0,0,0,1,3416 79 | "78",0,1,1,32,134,1,0,0,4,3430 80 | "79",0,1,2,20,121,0,0,0,0,3444 81 | "80",0,0,1,23,190,0,0,0,0,3459 82 | "81",0,0,1,22,131,0,0,0,1,3460 83 | "82",0,0,1,32,170,0,0,0,0,3473 84 | "83",0,0,3,30,110,0,0,0,0,3475 85 | "84",0,0,3,20,127,0,0,0,0,3487 86 | "85",0,0,3,23,123,0,0,0,0,3544 87 | "86",0,1,3,17,120,0,0,0,0,3572 88 | "87",0,0,3,19,105,0,0,0,0,3572 89 | "88",0,0,1,23,130,0,0,0,0,3586 90 | "89",0,0,1,36,175,0,0,0,0,3600 91 | "90",0,0,1,22,125,0,0,0,1,3614 92 | "91",0,0,1,24,133,0,0,0,0,3614 93 | "92",0,0,3,21,134,0,0,0,2,3629 94 | "93",0,1,1,19,235,0,1,0,0,3629 95 | "94",0,1,1,25,95,3,0,1,0,3637 96 | "95",0,1,1,16,135,0,0,0,0,3643 97 | "96",0,0,1,29,135,0,0,0,1,3651 98 | "97",0,0,1,29,154,0,0,0,1,3651 99 | "98",0,1,1,19,147,0,0,0,0,3651 100 | "99",0,1,1,19,147,0,0,0,0,3651 101 | "100",0,0,1,30,137,0,0,0,1,3699 102 | "101",0,0,1,24,110,0,0,0,1,3728 103 | "102",0,1,1,19,184,0,1,0,0,3756 104 | "103",0,0,3,24,110,1,0,0,0,3770 105 | "104",0,0,1,23,110,0,0,0,1,3770 106 | "105",0,0,3,20,120,0,0,0,0,3770 107 | "106",0,0,2,25,241,0,1,0,0,3790 108 | "107",0,0,1,30,112,0,0,0,1,3799 109 | "108",0,0,1,22,169,0,0,0,0,3827 110 | "109",0,1,1,18,120,0,0,0,2,3856 111 | "110",0,0,2,16,170,0,0,0,4,3860 112 | "111",0,0,1,32,186,0,0,0,2,3860 113 | "112",0,0,3,18,120,0,0,0,1,3884 114 | "113",0,1,1,29,130,0,0,0,2,3884 115 | "114",0,0,1,33,117,0,0,1,1,3912 116 | "115",0,1,1,20,170,0,0,0,0,3940 117 | "116",0,0,3,28,134,0,0,0,1,3941 118 | "117",0,0,1,14,135,0,0,0,0,3941 119 | "118",0,0,3,28,130,0,0,0,0,3969 120 | "119",0,0,1,25,120,0,0,0,2,3983 121 | "120",0,0,3,16,95,0,0,0,1,3997 122 | "121",0,0,1,20,158,0,0,0,1,3997 123 | "122",0,0,3,26,160,0,0,0,0,4054 124 | "123",0,0,1,21,115,0,0,0,1,4054 125 | "124",0,0,1,22,129,0,0,0,0,4111 126 | "125",0,0,1,25,130,0,0,0,2,4153 127 | "126",0,0,1,31,120,0,0,0,2,4167 128 | "127",0,0,1,35,170,1,0,0,1,4174 129 | "128",0,1,1,19,120,0,0,0,0,4238 130 | "129",0,0,1,24,116,0,0,0,1,4593 131 | "130",0,0,1,45,123,0,0,0,1,4990 132 | "131",1,1,3,28,120,1,0,1,0,709 133 | "132",1,0,1,29,130,0,0,1,2,1021 134 | "133",1,1,2,34,187,0,1,0,0,1135 135 | "134",1,0,3,25,105,1,1,0,0,1330 136 | "135",1,0,3,25,85,0,0,1,0,1474 137 | "136",1,0,3,27,150,0,0,0,0,1588 138 | "137",1,0,3,23,97,0,0,1,1,1588 139 | "138",1,0,2,24,128,1,0,0,1,1701 140 | "139",1,0,3,24,132,0,1,0,0,1729 141 | "140",1,1,1,21,165,0,1,0,1,1790 142 | "141",1,1,1,32,105,0,0,0,0,1818 143 | "142",1,1,1,19,91,2,0,1,0,1885 144 | "143",1,0,3,25,115,0,0,0,0,1893 145 | "144",1,0,3,16,130,0,0,0,1,1899 146 | "145",1,1,1,25,92,0,0,0,0,1928 147 | "146",1,1,1,20,150,0,0,0,2,1928 148 | "147",1,0,2,21,200,0,0,1,2,1928 149 | "148",1,1,1,24,155,1,0,0,0,1936 150 | "149",1,0,3,21,103,0,0,0,0,1970 151 | "150",1,0,3,20,125,0,0,1,0,2055 152 | "151",1,0,3,25,89,2,0,0,1,2055 153 | "152",1,0,1,19,102,0,0,0,2,2082 154 | "153",1,1,1,19,112,0,0,1,0,2084 155 | "154",1,1,1,26,117,1,0,0,0,2084 156 | "155",1,0,1,24,138,0,0,0,0,2100 157 | "156",1,1,3,17,130,1,0,1,0,2125 158 | "157",1,1,2,20,120,0,0,0,3,2126 159 | "158",1,1,1,22,130,1,0,1,1,2187 160 | "159",1,0,2,27,130,0,0,1,0,2187 161 | "160",1,1,3,20,80,0,0,1,0,2211 162 | "161",1,1,1,17,110,0,0,0,0,2225 163 | "162",1,0,3,25,105,1,0,0,1,2240 164 | "163",1,0,3,20,109,0,0,0,0,2240 165 | "164",1,0,3,18,148,0,0,0,0,2282 166 | "165",1,1,2,18,110,1,0,0,0,2296 167 | "166",1,1,1,20,121,1,0,1,0,2296 168 | "167",1,0,3,21,100,1,0,0,4,2301 169 | "168",1,0,3,26,96,0,0,0,0,2325 170 | "169",1,1,1,31,102,1,0,0,1,2353 171 | "170",1,0,1,15,110,0,0,0,0,2353 172 | "171",1,1,2,23,187,0,0,0,1,2367 173 | "172",1,1,2,20,122,0,0,0,0,2381 174 | "173",1,1,2,24,105,0,0,0,0,2381 175 | "174",1,0,3,15,115,0,0,1,0,2381 176 | "175",1,0,3,23,120,0,0,0,0,2395 177 | "176",1,1,1,30,142,1,0,0,0,2410 178 | "177",1,1,1,22,130,0,0,0,1,2410 179 | "178",1,1,1,17,120,0,0,0,3,2414 180 | "179",1,1,1,23,110,1,0,0,0,2424 181 | "180",1,0,2,17,120,0,0,0,2,2438 182 | "181",1,0,3,26,154,1,1,0,1,2442 183 | "182",1,0,3,20,106,0,0,0,3,2450 184 | "183",1,1,1,26,190,0,0,0,0,2466 185 | "184",1,1,3,14,101,1,0,0,0,2466 186 | "185",1,1,1,28,95,0,0,0,2,2466 187 | "186",1,0,3,14,100,0,0,0,2,2495 188 | "187",1,1,3,23,94,0,0,0,0,2495 189 | "188",1,0,2,17,142,0,1,0,0,2495 190 | "189",1,1,1,21,130,0,1,0,3,2495 191 | -------------------------------------------------------------------------------- /contents/chapter_11_generalized/data/lbw.csv: -------------------------------------------------------------------------------- 1 | "","low","smoke","race","age","lwt","ptl","ht","ui","ftv","bwt" 2 | "1",0,0,2,19,182,0,0,1,0,2523 3 | "2",0,0,3,33,155,0,0,0,3,2551 4 | "3",0,1,1,20,105,0,0,0,1,2557 5 | "4",0,1,1,21,108,0,0,1,2,2594 6 | "5",0,1,1,18,107,0,0,1,0,2600 7 | "6",0,0,3,21,124,0,0,0,0,2622 8 | "7",0,0,1,22,118,0,0,0,1,2637 9 | "8",0,0,3,17,103,0,0,0,1,2637 10 | "9",0,1,1,29,123,0,0,0,1,2663 11 | "10",0,1,1,26,113,0,0,0,0,2665 12 | "11",0,0,3,19,95,0,0,0,0,2722 13 | "12",0,0,3,19,150,0,0,0,1,2733 14 | "13",0,0,3,22,95,0,1,0,0,2750 15 | "14",0,0,3,30,107,1,0,1,2,2750 16 | "15",0,1,1,18,100,0,0,0,0,2769 17 | "16",0,1,1,18,100,0,0,0,0,2769 18 | "17",0,0,2,15,98,0,0,0,0,2778 19 | "18",0,1,1,25,118,0,0,0,3,2782 20 | "19",0,0,3,20,120,0,0,1,0,2807 21 | "20",0,1,1,28,120,0,0,0,1,2821 22 | "21",0,0,3,32,121,0,0,0,2,2835 23 | "22",0,0,1,31,100,0,0,1,3,2835 24 | "23",0,0,1,36,202,0,0,0,1,2836 25 | "24",0,0,3,28,120,0,0,0,0,2863 26 | "25",0,0,3,25,120,0,0,1,2,2877 27 | "26",0,0,1,28,167,0,0,0,0,2877 28 | "27",0,1,1,17,122,0,0,0,0,2906 29 | "28",0,0,1,29,150,0,0,0,2,2920 30 | "29",0,1,2,26,168,0,0,0,0,2920 31 | "30",0,0,2,17,113,0,0,0,1,2920 32 | "31",0,0,2,17,113,0,0,0,1,2920 33 | "32",0,1,1,24,90,1,0,0,1,2948 34 | "33",0,1,2,35,121,1,0,0,1,2948 35 | "34",0,0,1,25,155,0,0,0,1,2977 36 | "35",0,0,2,25,125,0,0,0,0,2977 37 | "36",0,1,1,29,140,0,0,0,2,2977 38 | "37",0,1,1,19,138,0,0,0,2,2977 39 | "38",0,1,1,27,124,0,0,0,0,2922 40 | "39",0,1,1,31,215,0,0,0,2,3005 41 | "40",0,1,1,33,109,0,0,0,1,3033 42 | "41",0,1,2,21,185,0,0,0,2,3042 43 | "42",0,0,1,19,189,0,0,0,2,3062 44 | "43",0,0,2,23,130,0,0,0,1,3062 45 | "44",0,0,1,21,160,0,0,0,0,3062 46 | "45",0,1,1,18,90,0,0,1,0,3076 47 | "46",0,1,1,18,90,0,0,1,0,3076 48 | "47",0,0,1,32,132,0,0,0,4,3080 49 | "48",0,0,3,19,132,0,0,0,0,3090 50 | "49",0,0,1,24,115,0,0,0,2,3090 51 | "50",0,1,3,22,85,0,0,0,0,3090 52 | "51",0,0,1,22,120,0,1,0,1,3100 53 | "52",0,0,3,23,128,0,0,0,0,3104 54 | "53",0,1,1,22,130,0,0,0,0,3132 55 | "54",0,1,1,30,95,0,0,0,2,3147 56 | "55",0,0,3,19,115,0,0,0,0,3175 57 | "56",0,0,3,16,110,0,0,0,0,3175 58 | "57",0,1,3,21,110,0,0,1,0,3203 59 | "58",0,0,3,30,153,0,0,0,0,3203 60 | "59",0,0,3,20,103,0,0,0,0,3203 61 | "60",0,0,3,17,119,0,0,0,0,3225 62 | "61",0,0,3,17,119,0,0,0,0,3225 63 | "62",0,0,3,23,119,0,0,0,2,3232 64 | "63",0,0,3,24,110,0,0,0,0,3232 65 | "64",0,0,1,28,140,0,0,0,0,3234 66 | "65",0,1,3,26,133,2,0,0,0,3260 67 | "66",0,0,3,20,169,1,0,1,1,3274 68 | "67",0,0,3,24,115,0,0,0,2,3274 69 | "68",0,1,3,28,250,0,0,0,6,3303 70 | "69",0,0,1,20,141,2,0,1,1,3317 71 | "70",0,0,2,22,158,1,0,0,2,3317 72 | "71",0,1,1,22,112,2,0,0,0,3317 73 | "72",0,1,3,31,150,0,0,0,2,3321 74 | "73",0,1,3,23,115,0,0,0,1,3331 75 | "74",0,0,2,16,112,0,0,0,0,3374 76 | "75",0,1,1,16,135,0,0,0,0,3374 77 | "76",0,0,2,18,229,0,0,0,0,3402 78 | "77",0,0,1,25,140,0,0,0,1,3416 79 | "78",0,1,1,32,134,1,0,0,4,3430 80 | "79",0,1,2,20,121,0,0,0,0,3444 81 | "80",0,0,1,23,190,0,0,0,0,3459 82 | "81",0,0,1,22,131,0,0,0,1,3460 83 | "82",0,0,1,32,170,0,0,0,0,3473 84 | "83",0,0,3,30,110,0,0,0,0,3475 85 | "84",0,0,3,20,127,0,0,0,0,3487 86 | "85",0,0,3,23,123,0,0,0,0,3544 87 | "86",0,1,3,17,120,0,0,0,0,3572 88 | "87",0,0,3,19,105,0,0,0,0,3572 89 | "88",0,0,1,23,130,0,0,0,0,3586 90 | "89",0,0,1,36,175,0,0,0,0,3600 91 | "90",0,0,1,22,125,0,0,0,1,3614 92 | "91",0,0,1,24,133,0,0,0,0,3614 93 | "92",0,0,3,21,134,0,0,0,2,3629 94 | "93",0,1,1,19,235,0,1,0,0,3629 95 | "94",0,1,1,25,95,3,0,1,0,3637 96 | "95",0,1,1,16,135,0,0,0,0,3643 97 | "96",0,0,1,29,135,0,0,0,1,3651 98 | "97",0,0,1,29,154,0,0,0,1,3651 99 | "98",0,1,1,19,147,0,0,0,0,3651 100 | "99",0,1,1,19,147,0,0,0,0,3651 101 | "100",0,0,1,30,137,0,0,0,1,3699 102 | "101",0,0,1,24,110,0,0,0,1,3728 103 | "102",0,1,1,19,184,0,1,0,0,3756 104 | "103",0,0,3,24,110,1,0,0,0,3770 105 | "104",0,0,1,23,110,0,0,0,1,3770 106 | "105",0,0,3,20,120,0,0,0,0,3770 107 | "106",0,0,2,25,241,0,1,0,0,3790 108 | "107",0,0,1,30,112,0,0,0,1,3799 109 | "108",0,0,1,22,169,0,0,0,0,3827 110 | "109",0,1,1,18,120,0,0,0,2,3856 111 | "110",0,0,2,16,170,0,0,0,4,3860 112 | "111",0,0,1,32,186,0,0,0,2,3860 113 | "112",0,0,3,18,120,0,0,0,1,3884 114 | "113",0,1,1,29,130,0,0,0,2,3884 115 | "114",0,0,1,33,117,0,0,1,1,3912 116 | "115",0,1,1,20,170,0,0,0,0,3940 117 | "116",0,0,3,28,134,0,0,0,1,3941 118 | "117",0,0,1,14,135,0,0,0,0,3941 119 | "118",0,0,3,28,130,0,0,0,0,3969 120 | "119",0,0,1,25,120,0,0,0,2,3983 121 | "120",0,0,3,16,95,0,0,0,1,3997 122 | "121",0,0,1,20,158,0,0,0,1,3997 123 | "122",0,0,3,26,160,0,0,0,0,4054 124 | "123",0,0,1,21,115,0,0,0,1,4054 125 | "124",0,0,1,22,129,0,0,0,0,4111 126 | "125",0,0,1,25,130,0,0,0,2,4153 127 | "126",0,0,1,31,120,0,0,0,2,4167 128 | "127",0,0,1,35,170,1,0,0,1,4174 129 | "128",0,1,1,19,120,0,0,0,0,4238 130 | "129",0,0,1,24,116,0,0,0,1,4593 131 | "130",0,0,1,45,123,0,0,0,1,4990 132 | "131",1,1,3,28,120,1,0,1,0,709 133 | "132",1,0,1,29,130,0,0,1,2,1021 134 | "133",1,1,2,34,187,0,1,0,0,1135 135 | "134",1,0,3,25,105,1,1,0,0,1330 136 | "135",1,0,3,25,85,0,0,1,0,1474 137 | "136",1,0,3,27,150,0,0,0,0,1588 138 | "137",1,0,3,23,97,0,0,1,1,1588 139 | "138",1,0,2,24,128,1,0,0,1,1701 140 | "139",1,0,3,24,132,0,1,0,0,1729 141 | "140",1,1,1,21,165,0,1,0,1,1790 142 | "141",1,1,1,32,105,0,0,0,0,1818 143 | "142",1,1,1,19,91,2,0,1,0,1885 144 | "143",1,0,3,25,115,0,0,0,0,1893 145 | "144",1,0,3,16,130,0,0,0,1,1899 146 | "145",1,1,1,25,92,0,0,0,0,1928 147 | "146",1,1,1,20,150,0,0,0,2,1928 148 | "147",1,0,2,21,200,0,0,1,2,1928 149 | "148",1,1,1,24,155,1,0,0,0,1936 150 | "149",1,0,3,21,103,0,0,0,0,1970 151 | "150",1,0,3,20,125,0,0,1,0,2055 152 | "151",1,0,3,25,89,2,0,0,1,2055 153 | "152",1,0,1,19,102,0,0,0,2,2082 154 | "153",1,1,1,19,112,0,0,1,0,2084 155 | "154",1,1,1,26,117,1,0,0,0,2084 156 | "155",1,0,1,24,138,0,0,0,0,2100 157 | "156",1,1,3,17,130,1,0,1,0,2125 158 | "157",1,1,2,20,120,0,0,0,3,2126 159 | "158",1,1,1,22,130,1,0,1,1,2187 160 | "159",1,0,2,27,130,0,0,1,0,2187 161 | "160",1,1,3,20,80,0,0,1,0,2211 162 | "161",1,1,1,17,110,0,0,0,0,2225 163 | "162",1,0,3,25,105,1,0,0,1,2240 164 | "163",1,0,3,20,109,0,0,0,0,2240 165 | "164",1,0,3,18,148,0,0,0,0,2282 166 | "165",1,1,2,18,110,1,0,0,0,2296 167 | "166",1,1,1,20,121,1,0,1,0,2296 168 | "167",1,0,3,21,100,1,0,0,4,2301 169 | "168",1,0,3,26,96,0,0,0,0,2325 170 | "169",1,1,1,31,102,1,0,0,1,2353 171 | "170",1,0,1,15,110,0,0,0,0,2353 172 | "171",1,1,2,23,187,0,0,0,1,2367 173 | "172",1,1,2,20,122,0,0,0,0,2381 174 | "173",1,1,2,24,105,0,0,0,0,2381 175 | "174",1,0,3,15,115,0,0,1,0,2381 176 | "175",1,0,3,23,120,0,0,0,0,2395 177 | "176",1,1,1,30,142,1,0,0,0,2410 178 | "177",1,1,1,22,130,0,0,0,1,2410 179 | "178",1,1,1,17,120,0,0,0,3,2414 180 | "179",1,1,1,23,110,1,0,0,0,2424 181 | "180",1,0,2,17,120,0,0,0,2,2438 182 | "181",1,0,3,26,154,1,1,0,1,2442 183 | "182",1,0,3,20,106,0,0,0,3,2450 184 | "183",1,1,1,26,190,0,0,0,0,2466 185 | "184",1,1,3,14,101,1,0,0,0,2466 186 | "185",1,1,1,28,95,0,0,0,2,2466 187 | "186",1,0,3,14,100,0,0,0,2,2495 188 | "187",1,1,3,23,94,0,0,0,0,2495 189 | "188",1,0,2,17,142,0,1,0,0,2495 190 | "189",1,1,1,21,130,0,1,0,3,2495 191 | --------------------------------------------------------------------------------