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
# 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 |
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
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))) | |
} |
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
# 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 |
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
# 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): |
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
# Simple probabilistic simulation script | |
# Causal graph: | |
# NetIncome -> + CulturalActivities | |
# NetIncome -> + SportsActivities | |
# NetIncome -> - Debts | |
# NetIncome -> + SocialComparison | |
# SportsActivities -> + Health | |
# SportsActivities -> + Partnership | |
# CulturalActivities -> + SocialComparison |
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
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 |
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
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]) |
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
# 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 |
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
# 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) |
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
#' 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 | |
#' |
NewerOlder