Skip to content

Instantly share code, notes, and snippets.

View rmcelreath's full-sized avatar
🦔

Richard McElreath rmcelreath

🦔
View GitHub Profile
@rmcelreath
rmcelreath / gist:1e3fb52480ae7c79931d
Created February 27, 2015 21:16
TeX code for dumping code boxes to a common plain text file
\newwrite\tempfile
\usepackage{verbatim}
\makeatletter
\newwrite\Code@out % temp file for writing out and reading back in for display
\newcommand\VerbSaver{\obeylines\expandafter\VerbSaverArg\noexpand}
\newcommand\VerbSaverArg[1][code.txt]{%
\gdef\FName{xcodetempx.txt}%
@rmcelreath
rmcelreath / VDL_simpson_paradox.R
Last active January 24, 2020 05:47
Replication and reanalysis for: van der Lee and Ellemers 10.1073/pnas.1510159112 . Published reanalysis: 10.1073/pnas.1518936112
######################
# Replication and reanalysis for:
# van der Lee and Ellemers 10.1073/pnas.1510159112
# load data from supplemental Table S1
# found at http://www.pnas.org/content/early/2015/09/16/1510159112/suppl/DCSupplemental
d <- read.csv("http://xcelab.net/VDL_data.csv")
d$rejects <- d$applications - d$awards
@rmcelreath
rmcelreath / garden plots.R
Created November 4, 2016 09:34
Code for drawing the forking data gardens in Chapter 2 of "Statistical Rethinking" textbook
# functions for plotting garden of forking data plots
library(rethinking)
polar2screen <- function( dist, origin, theta ) {
## takes dist, angle and origin and returns x and y of destination point
vx <- cos(theta) * dist;
vy <- sin(theta) * dist;
c( origin[1]+vx , origin[2]+vy );
}
@rmcelreath
rmcelreath / discrete_missingness.R
Created March 8, 2017 10:39
Discrete missing values in Stan
# "impute" missing binary predictor
# really just marginalizes over missingness
# imputed values produced in generated quantities
N <- 1000 # number of cases
N_miss <- 100 # number missing values
x_baserate <- 0.25 # prob x==1 in total sample
a <- 0 # intercept in y ~ N( a+b*x , 1 )
b <- 1 # slope in y ~ N( a+b*x , 1 )
@rmcelreath
rmcelreath / rstan_error_hiding_chains_exampe.R
Created March 31, 2017 15:36
RStan error passing issue with parallel chains
library(rstan)
# simple model with a bug
model_code <- "
parameters{
real a;
}
model{
a ~ normal(0,1);
}
@rmcelreath
rmcelreath / berkson_journal.R
Created April 13, 2017 11:22
Example of how selecting top 10% of papers can induce negative correlation between positively associated desired features
# berkson's paradox journal example
library(rethinking)
n <- 500
rho <- 0.25
y <- rmvnorm2( n , Mu=c(0,0) , sigma=c(1,1) , Rho=matrix( c(1,rho,rho,1) , 2 , 2 ) )
b <- 1
score <- y[,1] + b*y[,2]
theshold <- quantile( score , 0.9 )
@rmcelreath
rmcelreath / bayes_Lund_2017.R
Created April 19, 2017 05:56
Bayes@Lund2017 examples
# script for examples in Bayes@Lund2017 presentation
# joint model example
notes_max <- 10
rate_max <- 5
pm <- matrix( NA , nrow=rate_max+1 , ncol=notes_max+1 )
for ( i in 1:(rate_max+1) )
for ( j in 1:(notes_max+1) )
pm[i,j] <- dpois( j-1 , lambda=i ) * (1/(rate_max+1))
@rmcelreath
rmcelreath / ch2_globe_toss_bayes_update.R
Created April 23, 2017 08:12
Code for producing sequential updating plots for globe tossing example in Chapter 2 of Statistical Rethinking (Figure 2.5, page 30)
# chapter 2 code
library(rethinking)
col1 <- "black"
col2 <- col.alpha( "black" , 0.7 )
#####
# introducing the golem
# show posterior as data comes in
@rmcelreath
rmcelreath / entropy_as_logways.R
Created April 24, 2017 08:49
Figure 9.1 from Statistical Rethinking (bottom-right plot)
p <- list()
p$A <- c(0,0,10,0,0)
p$B <- c(0,1,8,1,0)
p$C <- c(0,2,6,2,0)
p$D <- c(1,2,4,2,1)
p$E <- c(2,2,2,2,2)
p_norm <- lapply( p , function(q) q/sum(q))
( H <- sapply( p_norm , function(q) -sum(ifelse(q==0,0,q*log(q))) ) )
@rmcelreath
rmcelreath / shade_gist_for_dave.R
Created April 26, 2017 16:45
Example of using shade to paint tail on distribution
library(rethinking)
p_grid <- seq( from=0 , to=1 , length.out=100 )
prior <- rep( 1 , 100 )
likelihood <- dbinom( 6 , size=9 , prob=p_grid )
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)
plot( posterior ~ p_grid , type="l" )
shade( posterior ~ p_grid , lim=c(0,0.5) , col=rangi2 )