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
| ## | |
| ## Chapter 4 - Exercises | |
| ## | |
| library(cmdstanr) | |
| options(mc.cores = 4) | |
| # Directories | |
| orig_dir <- "multiple_choice" |
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
| ## | |
| ## 4.4 Performing a simulation experiment to understand the model | |
| ## | |
| library(cmdstanr) | |
| library(ggplot2) | |
| library(ggmcmc) | |
| library(patchwork) | |
| library(stringr) | |
| library(bayesplot) |
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
| #include <TMB.hpp> | |
| template<class Type> | |
| Type objective_function<Type>::operator() () | |
| { | |
| DATA_INTEGER(N); // Num. participants (N >= 0) | |
| DATA_INTEGER(W); // Num. participants claim the prize (0 <= W <= N) | |
| DATA_SCALAR(q); // Prob. win the prize (0 <= q <= 1) | |
| PARAMETER(p); // Prob. honest (0 <= p <= 1) | |
| Type nll; // Negative log likelihood |
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
| library(cmdstanr) | |
| library(posterior) | |
| library(ggplot2) | |
| model_file <- file.path("models", "exercise_3.2.stan") | |
| stan_data <- list(N = 171, W = 111, q = 0.5) | |
| mod <- cmdstan_model(model_file, pedantic = TRUE) | |
| fit <- mod$sample(data = stan_data, seed = 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
| # Bayesian Workflow Exercise 3.1(b) | |
| library(purrr) | |
| set.seed(123) | |
| # Returns log likelihood | |
| # p: proportion of honest participants | |
| # N: Number of participants | |
| # W: Number of participants who claim prizes |
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
| # Bayesian Workflow Exercise 3.1 | |
| library(ggplot2) | |
| library(purrr) | |
| set.seed(123) | |
| # Log likelihood function | |
| # p: proportion of honest participants | |
| # N: Number of participants |
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
| # 3D animated map from DEM | |
| library(terra) | |
| library(ggplot2) | |
| library(geometry) | |
| library(ggcube) | |
| # Load DEM data from GeoTIFF | |
| # 国土地理院基盤地図情報数値標高モデル(DEM10B)を使用 | |
| geotiff_file <- "data/543654.tif" |
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
| # Plot DEM data using ggcube | |
| library(terra) | |
| library(ggplot2) | |
| library(geometry) | |
| library(ggcube) | |
| # Load DEM data from GeoTIFF | |
| # 国土地理院基盤地図情報数値標高モデル(DEM5A)を使用 | |
| geotiff_file <- "data/543654.tif" |
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
| # Mexican hat using ggcube | |
| # | |
| # https://tororolab.hatenablog.com/entry/2024/12/16/225348 | |
| library(ggplot2) | |
| library(ggcube) | |
| fun <- function(x, y) { | |
| r <- sqrt(x^2 + y^2) | |
| 1.5 * cos(r) - 0.75 * cos(3 * r) |
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
| library(ggplot2) | |
| library(palmerpenguins) | |
| point_plot <- function(d, x, y) { | |
| ggplot(d) + | |
| geom_point(aes(x = .data[[x]], y = .data[[y]])) | |
| } | |
| (var_names <- colnames(penguins)) |
NewerOlder