Skip to content

Instantly share code, notes, and snippets.

@vankesteren
vankesteren / causal_graph_sim.R
Created October 31, 2024 12:58
Simulating data, estimating effects, and computing intervention effects with uncertainty propagation in a simple causal graph
# Packages for this script
library(tidyverse)
library(lavaan)
library(dagitty)
library(glue)
library(rstanarm)
## SIMULATION ##
# here is a true data-generating process in lavaan (SEM) syntax
@vankesteren
vankesteren / virtualgrid.R
Last active September 6, 2024 19:25
Stop storing huge full-factorial grids, start using virtual grids. It behaves like a data frame but uses only a fraction of the memory!
virtual_grid <- function(...) {
pars <- list(...)
lens <- vapply(pars, length, 1L)
return(structure(list(pars = pars, lens = lens), class = c("vgrid", "tbl_lazy")))
}
dim.vgrid <- function(x) {
as.integer(c(prod(x$lens), length(x$lens)))
}
@vankesteren
vankesteren / scm_factor.R
Created August 22, 2024 15:05
SCM factor structure (WIP)
# panel data factor model simulation
# See hollingsworth & wing (2022) tactics for design..
# paragraph 2.3, assumption 2
n_timepoints <- 100
n_unobserved <- 1
n_units <- 200
n_covar <- 5
ru <- 0.6
mean_ly <- 1
@vankesteren
vankesteren / importance_sampling_vs_lintsampler.py
Last active June 26, 2024 18:25
Comparing lintsampler to basic uniform importance sampling
# Comparing lintsampler to basic uniform importance sampling
from scipy.stats import norm, uniform
import numpy as np
import matplotlib.pyplot as plt
from lintsampler import LintSampler
NSAMPLES = 1000000
# GMM example
def gmm_pdf(x):
@vankesteren
vankesteren / probsocsim.R
Created June 19, 2024 10:41
Example of a probabilistic social simulation
# Simple probabilistic simulation script
# Causal graph:
# NetIncome -> + CulturalActivities
# NetIncome -> + SportsActivities
# NetIncome -> - Debts
# NetIncome -> + SocialComparison
# SportsActivities -> + Health
# SportsActivities -> + Partnership
# CulturalActivities -> + SocialComparison
@vankesteren
vankesteren / permutefun.jl
Last active September 6, 2024 08:31
Permuting data to induce complex bivariate relations.
using StatsBase: sample, mean, cor
using LinearAlgebra: norm
using Plots, Random
"""
permutefun!(x::Vector, y::Vector, rule::Function, score::Real; tol::Number = 1e-3, max_iter::Int = 10_000, max_search::Number = 100, verbose::Bool = true)
Permute y values to approximate a functional constraint (rule) between x and y.
# Arguments
@vankesteren
vankesteren / rol_model.jl
Last active May 10, 2024 10:13
Bayesian inference for rank-ordered logit model in Julia's Turing.jl
using Turing
using LogExpFunctions: logsumexp
using DataFrames
# The rank ordered logit model in Turing
# The rank-ordered logit likelihood
function rank_ordered_logit(ordered_skills::Vector{<:Real})
ll = 0.0
for m in 1:(length(ordered_skills) - 1)
ll += ordered_skills[m] - logsumexp(ordered_skills[m:end])
@vankesteren
vankesteren / idealpoint.R
Last active April 25, 2024 19:55
Ideal point model in stan
# Ideal point model in stan / R
# example taken & simplified from https://medewitt.github.io/resources/stan_ideal_point.html
library(tidyverse)
library(cmdstanr)
# simulate data: 100 legislators, 150 votes
set.seed(1834)
N_legislators <- 50
N_bills <- 150
@vankesteren
vankesteren / elbo.jl
Last active March 21, 2024 15:50
Figuring out some ELBO stuff...
# Let's figure out this ELBO thing
using Distributions, StatsPlots, Optim, Random
Random.seed!(45)
# The target distribution. Assume we don't know it but
# we can compute the (unnormalized) logpdf and sample
# from it. For illustration, let's make it a weird mixture
comps = [Normal(2, 3), Normal(-3, 1.5), LogNormal(3, 0.4)]
probs = [.1, .1, .8]
p = MixtureModel(comps, probs)
@vankesteren
vankesteren / drplot_marginal.R
Last active December 9, 2023 19:50
Marginal density ratio plot
#' Marginal density ratio plot
#'
#' A plot to compare two (continuous) distributions in the
#' relative number of occurrences on a particular variable.
#' The transparency of the background histogram indicates
#' how much data is available at that location.
#'
#' @param dr_fit fitted model from the densityratio package
#' @param var <[`data-masked`][dplyr::dplyr_data_masking]> variable from the data
#'