Last active
June 29, 2021 08:55
-
-
Save rmcelreath/c0b1b16f42151e46bae5db953e2c7910 to your computer and use it in GitHub Desktop.
Code examples for RFDT
This file contains 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
# 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