Skip to content

Instantly share code, notes, and snippets.

View rmcelreath's full-sized avatar
🦔

Richard McElreath rmcelreath

🦔
View GitHub Profile
@rmcelreath
rmcelreath / RFDT1.R
Last active June 18, 2021 18:39
Code examples for RFDT
set.seed(1908)
N <- 200 # number of pairs
U <- rnorm(N) # 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 ) # change the 0 to turn on causal influence of mom
@rmcelreath
rmcelreath / data_caring.md
Last active December 31, 2020 19:13
Data sharing, caring and access conventions

Data Sharing, Caring, and Access

Most major scientific journals now require data archiving after publication. Many require it for review. So anthropology must figure out how to responsibly provide access to data. Luckily, other fields have already invested a lot in figured this out.

The important thing to realize is that providing access does not mean complete loss of control, either for the communities that provide the data or for researchers. For example, access can be restricted to researchers affiliated with research institutions, and limits can be placed on rights of reuse. But some level of access is necessary, if for no other reason than to allow other scholars the ability to verify our analyses. At the same time, researchers who want access to data must respect that data are political and continued collaboration with communities that provide data requires privacy and limits on reuse.

A standard data sharing memorandum grants all collaborators non-exclusive use of the data for scientific analysis

@rmcelreath
rmcelreath / nested_effects_ulam.R
Last active April 21, 2020 18:06
Nested varying effects in ulam example
# nested varying effects in ulam
# simulate an example
# people in nations, allowing varying among people to vary by nation
N_nations <- 10
N_id_per_nation <- sample( 5:100 , size=N_nations , replace=TRUE )
N_id <- sum(N_id_per_nation)
# variation among individuals in each nation
@rmcelreath
rmcelreath / deconfounding_proxies.R
Last active April 21, 2020 08:09
Deconfounding with proxies example
# proxy confounder problem
# cf https://twitter.com/y2silence/status/1251151157264674816
# note that I rename X1,X2 to A,B and A to X
# The DAG:
# X -> Y
# X <- U -> Y
# A <- U -> B
# Can we use A and B to deconfound X->Y?
@rmcelreath
rmcelreath / dirichlet_outcome.R
Created August 6, 2019 07:13
Dirichlet outcome model
library(rstan)
# sim observations
library(gtools)
y <- rdirichlet(50,c(10,2,5))
# model
dir_model <- "
data{
int N;
@rmcelreath
rmcelreath / bayes_instrument.R
Created July 11, 2019 13:06
Bayesian implementation of instrumental variable regression (using rstan)
# install rethinking library with instructions here:
# https://github.com/rmcelreath/statrethinking_winter2019
library(rethinking)
library(dagitty)
library(shape)
# the structural model
# W: wages
# Q: quarter of year of birth
@rmcelreath
rmcelreath / EHBEA_2019.R
Created April 25, 2019 21:41
Causal salad examples from EHBEA 2019 plenary
# R script to accompany EHBEA 2019 presentation "Causal Salad in Human Evolution & Ecology"
# to install latest rethinking package, see:
# https://github.com/rmcelreath/statrethinking_winter2019
library(rethinking)
# Example 1: Grandparent -> child
# what is influence of grandparent presence on grandchild welfare?
# confounded by parent condition (wealth),
# because grandparents live with wealthier children
@rmcelreath
rmcelreath / simple_HMC_1d.R
Created December 30, 2018 17:38
Simple Hamiltonian Monte Carlo example, 1-dimensional Gaussian, location against time
HMC2 <- function (U, grad_U, epsilon, L, current_q , ... ) {
q = current_q
p = rnorm(length(q),0,1) # independent standard normal variates
current_p = p
# Make a half step for momentum at the beginning
p = p - epsilon * grad_U( q , ... ) / 2
# Alternate full steps for position and momentum
qtraj <- matrix(NA,nrow=L+1,ncol=length(q))
ptraj <- qtraj
qtraj[1,] <- current_q
@rmcelreath
rmcelreath / simpleHMC.R
Last active January 24, 2020 05:45
Basic Hamiltonian Monte Carlo demo - 2D Gaussian mu,sigma example
library(rethinking)
HMC2 <- function (U, grad_U, epsilon, L, current_q , ... ) {
q = current_q
p = rnorm(length(q),0,1) # independent standard normal variates
current_p = p
# Make a half step for momentum at the beginning
p = p - epsilon * grad_U( q , ... ) / 2
# Alternate full steps for position and momentum
qtraj <- matrix(NA,nrow=L+1,ncol=length(q))
@rmcelreath
rmcelreath / breen_collider.R
Last active January 24, 2020 05:45
Collider bias example as described in Breen 2018 doi:10.1093/esr/jcy037
# script to illustrate collider bias described in Breen 2018 doi:10.1093/esr/jcy037
library(rethinking)
N <- 200 # number of grandparent-parent-child triads
b_gp <- 1 # direct effect of G on P
b_gc <- 0 # direct effect of G on C
b_pc <- 1 # direct effect of P on C
b_U <- 2 # direct effect of U on P and C