Skip to content

Instantly share code, notes, and snippets.

@ito4303
ito4303 / Bayesian_Workflow_Ch04_Exercises.R
Created September 1, 2026 04:26
Exercises in the Chapter 4 of “Bayesian Workflow”
##
## Chapter 4 - Exercises
##
library(cmdstanr)
options(mc.cores = 4)
# Directories
orig_dir <- "multiple_choice"
@ito4303
ito4303 / Sec4-4.R
Last active August 27, 2026 10:29
Section 4.4 of “Bayesian Workflow”
##
## 4.4 Performing a simulation experiment to understand the model
##
library(cmdstanr)
library(ggplot2)
library(ggmcmc)
library(patchwork)
library(stringr)
library(bayesplot)
@ito4303
ito4303 / exercise32.cpp
Created August 23, 2026 00:49
Exercise 3.2 of ”Bayesian Workflow” (TMB)
#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
@ito4303
ito4303 / exercise_3.2.R
Last active August 21, 2026 08:29
Exercise 3.2 of ”Bayesian Workflow”
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)
@ito4303
ito4303 / exercise_3.1(b).R
Last active August 20, 2026 10:35
Exercise 3.1(b) in “Bayesian Workflow”
# 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
@ito4303
ito4303 / exercise_3.1.R
Last active August 21, 2026 08:21
Exercise 3.1(a) of “Bayesian Workflow”
# Bayesian Workflow Exercise 3.1
library(ggplot2)
library(purrr)
set.seed(123)
# Log likelihood function
# p: proportion of honest participants
# N: Number of participants
@ito4303
ito4303 / ggcube_map_animation.R
Last active June 20, 2026 02:23
Generate 3D animated map using ggcube
# 3D animated map from DEM
library(terra)
library(ggplot2)
library(geometry)
library(ggcube)
# Load DEM data from GeoTIFF
# 国土地理院基盤地図情報数値標高モデル(DEM10B)を使用
geotiff_file <- "data/543654.tif"
@ito4303
ito4303 / plot_dem_using_ggcube.R
Created June 2, 2026 10:15
Plot DEM data using ggcube
# Plot DEM data using ggcube
library(terra)
library(ggplot2)
library(geometry)
library(ggcube)
# Load DEM data from GeoTIFF
# 国土地理院基盤地図情報数値標高モデル(DEM5A)を使用
geotiff_file <- "data/543654.tif"
# 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)
@ito4303
ito4303 / ggplot2_aes_string.R
Last active October 16, 2023 10:46
Use string to specify variables in aes() function of ggplot2
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))