Skip to content

Instantly share code, notes, and snippets.

@vanAmsterdam
vanAmsterdam / gist:7f664aac394d1bfe6bcac0d1c440d9a1
Created December 4, 2019 15:44
number of columns with less than 33% na's
library(data.table)
library(purrr)
set.seed(12345)
x <- rnorm(100)
x[sample(1:100, size = 50)] = NA
df <- data.table(matrix(x, nrow=10))
df[, columnA:=rep(letters[1:5], 2)]
df[, list(n_missings=map_dbl(.SD, ~(mean(is.na(.x))<0.33))%>%sum), by='columnA']
@vanAmsterdam
vanAmsterdam / latentconfoundermodel.py
Created April 23, 2020 08:17
Latent confounder treatment effect estimation model in Numpyro
'''
define and run a latent variable model
'''
import numpyro
from jax import numpy as np, random
from jax.scipy.special import logsumexp
from numpyro import distributions as dist
from numpyro.distributions import constraints
from numpyro.handlers import mask, substitute, trace, seed
#' Latent confounder model
# dag:
#' W1 <- U -> W2
#' tx -> y
#' U -> y
#' U -> tx
library(lavaan)
@vanAmsterdam
vanAmsterdam / latentfactorpostpred.py
Created May 1, 2020 13:13
define and run a latent variable model, everything gaussian, with posterior predictive on new data for a description of the model see twitter thread: https://twitter.com/WvanAmsterdam/status/1251214875394740226?s=20
'''
define and run a latent variable model, everything gaussian, with posterior predictive on newdata
for a description of the model see twitter thread: https://twitter.com/WvanAmsterdam/status/1251214875394740226?s=20
DAG:
W1 <- U -> W2 # latent confounder with 2 proxies
U -> tx
U -> y
tx -> y
@vanAmsterdam
vanAmsterdam / ordinal-regression-with-improperuniform-in-numpyro.py
Last active June 2, 2020 19:48
Ordinal regression with ImproperUniform distribution in NumPyro
# Ordinal regression with ImproperUniform
from jax import numpy as np, random
import numpyro
from numpyro import sample
from numpyro.distributions import constraints, Normal, ImproperUniform, Categorical, OrderedLogistic
from numpyro.infer.mcmc import NUTS, MCMC
import pandas as pd
num_chains = 4