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)
library(posterior)
library(dplyr)
library(readr)
library(purrr)
library(tidyr)
library(ggplot2)
# -------------------------------------------------------------------------
# 1. Ensure Model Exists (NCP Version for Sparse Data)
Study End_Point Intervention_Events_n Intervention_Events_N Control_Events_n Control_Events_N HR CI_Low CI_High
APACHE-AF Any stroke or cardiovascular death 13 50 12 51 1.07 0.49 2.34
SOSTART Any stroke or cardiovascular death 12 100 23 101 0.54 0.27 1.09
NASPAF-ICH Any stroke or cardiovascular death 0 21 1 9 0.12 0 11.2
ELDERCARE-AF Any stroke or cardiovascular death 4 41 7 39 0.52 0.15 1.78
Overall Any stroke or cardiovascular death 0.68 0.42 1.1
APACHE-AF Ischaemic major adverse cardiovascular events 6 50 11 51 0.46 0.17 1.25
SOSTART Ischaemic major adverse cardiovascular events 3 100 21 101 0.15 0.04 0.49
NASPAF-ICH Ischaemic major adverse cardiovascular events 0 21 1 9 0.12 0 11.2
ELDERCARE-AF Ischaemic major adverse cardiovascular events 0 41 5 39 0.09 0 2.12
using Random
using Printf
import GLMakie
# -----------------------------
# Infection simulation
# -----------------------------
const EMPTY = 0
const SUSC = 1
const INF = 2
@ericnovik
ericnovik / normal-shiny.R
Created August 22, 2025 00:03
Shiny app for normal distribution
# Normal Distribution Explorer — exposes all parameters & base R functions
library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel("Normal Distribution Explorer"),
sidebarLayout(
sidebarPanel(
h4("Parameters"),
@ericnovik
ericnovik / UCM.R
Last active August 21, 2025 18:55
Uniform circular motion
# 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) }
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),
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 {