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(dplyr) | |
| library(tidyr) | |
| library(magrittr) | |
| library(bayesplot) | |
| plot_areas <- function(d, n, mu, s, cutoff, left = TRUE, h, v) { | |
| if (left) { | |
| Pr <- apply(d, 2, function (x) mean(x < cutoff)) | |
| } else { |
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(deSolve) | |
| library(tidyr) | |
| theme_set(theme_minimal()) | |
| # I: infected, S: susceptible, R: recovered | |
| sir <- function(t, state, pars) { | |
| with(as.list(c(state, pars)), { | |
| N <- S + I + R | |
| dSdt <- -b * I/N * S |
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
| # Avoids the following inconsitent behaviour from R's sample() function | |
| # From the doc: If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via | |
| # sample takes place from 1:x. Note that this convenience feature may lead to | |
| # undesired behaviour when x is of varying length in calls such as sample(x). See the examples. | |
| safe_sample <- function(x, ...) { | |
| lx <- length(x) | |
| if (lx == 1 & is.numeric(x)) { | |
| as.numeric(sample(as.character(x), ...)) | |
| } else { |
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
| draw_vector <- function(x, ...) { | |
| df <- data.frame(x1 = x[1], x2 = x[2]) | |
| a <- arrow(length = unit(0.03, "npc")) | |
| dims <- ceiling(abs(max(x))) | |
| p <- ggplot() + xlim(-dims, dims) + ylim(-dims, dims) | |
| p <- p + geom_segment(aes(x = 0, y = 0, xend = x1, yend = x2), | |
| arrow = a, data = df, ...) + | |
| geom_hline(yintercept = 0, size = 0.1) + | |
| geom_vline(xintercept = 0, size = 0.1) | |
| return(p) |
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
| data { | |
| int<lower=0> N; | |
| array[N] int<lower=0, upper=1> y; | |
| real<lower=0> a; | |
| real<lower=0> b; | |
| } | |
| parameters { | |
| real<lower=0, upper=1> theta; | |
| } | |
| 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
| library(cmdstanr) | |
| m1 <- cmdstan_model("bern-with-target.stan") # compile the model | |
| data <- list(N = 5, y = c(0, 1, 1, 0, 1), a = 2, b = 7) | |
| f1 <- m1$sample( # for other options to sample, help(sample) | |
| data = data, # pass data as a list, match the vars name to Stan | |
| seed = 123, # to reproduce results, Stan does not rely on R's seed | |
| chains = 4, # total chains, the more, the better | |
| parallel_chains = 4, # for multi-processor CPUs |
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(tidyverse) | |
| # Parameters | |
| rates <- seq(0.05, 0.20, length = 10) | |
| time <- seq(1, 50, length = 50) | |
| P <- 100 | |
| # Continuous compounding formula: A(t) = P * exp(r * t) | |
| df <- expand_grid(time = time, rate = rates) |> | |
| mutate(value = P * exp(rate * time), |
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
| # x direction | |
| x_t <- function(R, omega, t) { R * cos(omega * t) } | |
| v_x <- function(R, omega, t) { -R * omega * sin(omega * t) } | |
| a_x <- function(R, omega, t) { -R * omega^2 * cos(omega * t) } | |
| # y direction | |
| y_t <- function(R, omega, t) { R * sin(omega * t) } | |
| v_y <- function(R, omega, t) { R * omega * cos(omega * t) } | |
| a_y <- function(R, omega, t) { -R * omega^2 * sin(omega * t) } |
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
| # Normal Distribution Explorer — exposes all parameters & base R functions | |
| library(shiny) | |
| library(ggplot2) | |
| ui <- fluidPage( | |
| titlePanel("Normal Distribution Explorer"), | |
| sidebarLayout( | |
| sidebarPanel( | |
| h4("Parameters"), |
OlderNewer