Skip to content

Instantly share code, notes, and snippets.

@rmcelreath
Last active June 29, 2021 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmcelreath/c0b1b16f42151e46bae5db953e2c7910 to your computer and use it in GitHub Desktop.
Save rmcelreath/c0b1b16f42151e46bae5db953e2c7910 to your computer and use it in GitHub Desktop.
Code examples for RFDT
# full-luxury bayesian inference
library(rethinking)
library(cmdstanr)
dat <- list(N=N,M=M,D=D,B1=B1,B2=B2)
set.seed(1908)
flbi <- ulam(
alist(
# mom model
M ~ normal( mu , sigma ),
mu <- a1 + b*B1 + k*U[i],
# daughter model
D ~ normal( nu , tau ),
nu <- a2 + b*B2 + m*M + k*U[i],
# B1 and B2
B1 ~ bernoulli(p),
B2 ~ bernoulli(p),
# unmeasured confound
vector[N]:U ~ normal(0,1),
# priors
c(a1,a2,b,m) ~ normal( 0 , 0.5 ),
c(k,sigma,tau) ~ exponential( 1 ),
p ~ beta(2,2)
), data=dat , chains=4 , cores=4 , iter=2000 , cmdstan=TRUE )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment