Skip to content

Instantly share code, notes, and snippets.

View ericnovik's full-sized avatar
🎯
Focusing

Eric Novik ericnovik

🎯
Focusing
View GitHub Profile
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
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 {
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)
# 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 {
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
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 {
library(manipulate)
manipulate({
lambda <- seq(0, 5, length = 100)
prior <- dgamma(lambda, a, b)
post <- dgamma(lambda, a + sum(y), b + length(y))
tibble(lambda, prior, post) %>%
ggplot(aes(lambda, prior)) + geom_line() +
geom_line(aes(y = post), color = "red") +
ylab("density") +
xlab(expression(lambda))
data {
int<lower=2> K; // number of categories
int<lower=0> N; // number of observations
int<lower=1> D; // number of predictors
int<lower=1, upper=K> y[N];
matrix[N, D] x;
}
parameters {
vector[D] beta;
ordered[K-1] c; // cutpoints
library(cranlogs)
library(ggplot2)
d <- cran_downloads(
package = c("rstan", "brms", "rstanarm"),
from = "2015-01-01",
to = Sys.Date()
)
ggplot(d, aes(x = date, y = count, color = package)) +
geom_smooth() +
@ericnovik
ericnovik / mc_int.R
Last active September 9, 2019 10:24
library(cowplot)
f <- function(x) {
exp(-x^2) + sin(10*x)
}
n <- 3e3
bounds <- c(0, 1)
set.seed(123)
x <- runif(n, bounds[1], bounds[2])
y <- f(x)
p1 <- qplot(x, y, geom = "line")