Instantly share code, notes, and snippets.

# Richard McElreath rmcelreath

🦔
Created September 11, 2023 11:29
Demonstration of how normal and student-t distributions interact in Bayesian updating
View prior_likelihood_conflict.r
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
 # prior - likelihood conflict library(rethinking) yobs <- 0 mtt <- ulam( alist( y ~ dstudent(2,mu,1), mu ~ dstudent(2,10,1)
Created July 7, 2023 14:24
distribution of pvalues under null for t.test and binomial models
View p_under_null.r
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
 # distribution of null p-values in binomial glm library(rethinking) # t test # pval has uniform distribution under null f <- function(N=10) { y1 <- rnorm(N) y2 <- rnorm(N)
Created January 22, 2022 14:28
Copernican model with epicycles animation
View kop_epi_ani.r
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
 # simulation of simple Kopernican model showing epicycle library(rethinking) library(animation) my_circle <- function(x=0,y=0,r=1,angle=0,...) { a <- seq(angle, angle + 2 * pi, length = 360) lines( r*cos(a)+x , r*sin(a)+y , ... ) } # plot(NULL,xlim=c(-1,1),ylim=c(-1,1)); my_circle(0,0,0.5,lty=2)
Created October 16, 2021 08:15
Monte Carlo posterior predictive simulation
View MCpostpred.r
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
 library(rethinking) library(animation) ani.saveqz <- function (list , dpi=150) { if (missing(list)) list = animation:::.ani.env\$.images lapply(1:length(list), function(x) { dev.hold() replayPlot( list[[x]] ) ani.pause()
Created June 29, 2021 11:12
Code example for RFDT
View RFDT7.R
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
 # version that marginalizes out the missing data flbi_plus <- ulam( alist( c(M,D) ~ multi_normal( c(mu,nu) , Rho , Sigma ), mu <- a1 + b*B1, nu <- a2 + b*B2 + m*M, c(a1,a2,b,m) ~ normal( 0 , 0.5 ), Rho ~ lkj_corr( 2 ), Sigma ~ exponential( 1 ) ), data=dat , chains=4 , cores=4 , cmdstan=TRUE )
Created June 29, 2021 10:57
Code eamples for RFDT
View RFDT6.R
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
 # more exotic example - no instrument (B1 -> D), but have two measures of U set.seed(1908) N <- 200 # number of pairs U <- rnorm(N,0,1) # simulate confound V <- rnorm(N,U,1) W <- rnorm(N,U,1) # birth order and family sizes B1 <- rbinom(N,size=1,prob=0.5) # 50% first borns M <- rnorm( N , 2*B1 + U ) B2 <- rbinom(N,size=1,prob=0.5)
Created June 29, 2021 09:26
Code examples for RFDT
View RFDT5.R
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
 # simulate interventions # B1 -> D (should be b*m) post <- extract.samples(flbi) quantile( with(post,b*m) ) # now do the same with simulation # B1 = 0 # B1 -> M
Last active June 29, 2021 08:55
Code examples for RFDT
View RFDT4.R
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],
Created June 18, 2021 18:46
Code examples for RFDT
View RFDT3.R
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
 set.seed(1908) N <- 200 # number of pairs U <- rnorm(N,0,1) # simulate confounds # birth order and family sizes B1 <- rbinom(N,size=1,prob=0.5) # 50% first borns M <- rnorm( N , 2*B1 + U ) B2 <- rbinom(N,size=1,prob=0.5) D <- rnorm( N , 2*B2 + U + 0*M ) f <- function(data,indices) with(data[indices,], cov(B1,D) / cov(B1,M) )
Created June 15, 2021 18:15
Code examples for RFDT
View RFDT2.R
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
 # fit the two regression models summary( lm( D ~ M ) ) summary( lm( D ~ M + B1 + B2 ) ) # compare the models with AIC AIC( lm( D ~ M ) ) AIC( lm( D ~ M + B1 + B2 ) )