This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ---- setup ---- | |
| # nice printing | |
| options(digits = 3) | |
| # load packages | |
| library(tidyverse) | |
| library(posterior) | |
| library(foreach) | |
| library(doParallel) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ---- setup ---- | |
| # nice printing | |
| options(digits = 3) | |
| # load packages | |
| library(tidyverse) | |
| library(posterior) | |
| library(foreach) | |
| library(doParallel) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # load packages | |
| library(survival) | |
| # load data | |
| canc <- survival::cancer |> | |
| mutate(sex = case_when(sex == 1 ~ "Male", | |
| sex == 2 ~ "Female")) | |
| # quick look | |
| glimpse(canc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # a function for rejection algorithm | |
| rej <- function(f, S, M) { | |
| # record start time | |
| start_time <- Sys.time() | |
| # create containers and initialize counters | |
| samples <- numeric(S) # container to store samples | |
| rejects <- NULL # container to track rejected values; for teaching; slow! | |
| s <- 1 # currently trying to take sample 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # prior | |
| alpha_star <- 3 | |
| beta_star <- 15 | |
| # data | |
| N <- 150 | |
| k <- 8 | |
| # posterior | |
| alpha_prime <- alpha_star + k |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # load packages | |
| library(tidyverse) | |
| library(marginaleffects) | |
| # data | |
| devtools::install_github("jrnold/ZeligData") | |
| turnout <- ZeligData::turnout | |
| # fit model | |
| f <- vote ~ age + educate + income + race |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # load package | |
| library(tidyverse) | |
| library(marginaleffects) | |
| library(modelsummary) | |
| library(glmmTMB) | |
| # load data for santiago | |
| sant <- crdata::holland2015 |> | |
| filter(city == "santiago") |> | |
| glimpse() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # load packages | |
| library(tidyverse) | |
| library(tinytable) | |
| # data | |
| devtools::install_github("jrnold/ZeligData") | |
| turnout <- ZeligData::turnout | |
| # fit model | |
| f <- vote ~ age + educate + income + race |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ---- fit negative binomial model with optim() ---- | |
| # data; see ?crdata::holland2015 | |
| holland <- crdata::holland2015 |> | |
| filter(city == "santiago") | |
| # formula corresponds to model 1 for each city in holland (2015) table 2 | |
| f <- operations ~ lower + vendors + budget + population | |
| # ---- create a function to fit the model ---- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ---- fit poisson model with optim() ---- | |
| # data; see ?crdata::holland2015 | |
| holland <- crdata::holland2015 |> | |
| filter(city == "santiago") | |
| # formula corresponds to model 1 for each city in holland (2015) table 2 | |
| f <- operations ~ lower + vendors + budget + population |