Skip to content

Instantly share code, notes, and snippets.

@vankesteren
vankesteren / app.R
Last active November 17, 2022 14:38
Item ranking app for multiple raters using rank-ordered logit models
library(shiny)
library(shinythemes)
library(rhandsontable)
library(shinyalert)
library(PlackettLuce)
selopts <- list(
plugins = list("remove_button"),
create = TRUE,
persist = TRUE
@vankesteren
vankesteren / metasynth_basic_example.py
Last active August 3, 2023 07:53
Basic example for metasynth, for more info see https://github.com/sodascience/metasynth
import polars as pl
from metasynth import MetaFrame
# example dataframe from polars website
df = pl.DataFrame(
{
"ID": [1, 2, 3, 4, 5],
"fruits": ["banana", "banana", "apple", "apple", "banana"],
"B": [5, 4, 3, 2, 1],
"cars": ["beetle", "audi", "beetle", "beetle", "beetle"],
@vankesteren
vankesteren / lasso_synthetic_control.R
Last active November 15, 2022 20:23
Performing the synthetic control method using glmnet. This allows easily validating the model using LOOCV
# Implementing synthetic control using glmnet
library(tidyverse)
library(tidysynth)
library(glmnet)
# we need a wide dataset, only cigsale
smoking_w <-
smoking |>
select(state, year, cigsale) |>
pivot_wider(names_from = state, values_from = cigsale)
@vankesteren
vankesteren / lightgbm_pmse.R
Created November 7, 2022 10:37
LightGBM for general utility
library(synthpop)
library(lightgbm)
library(tidyverse)
df <- SD2011
syn_res <- syn(df[,1:7])
sdf <- syn_res$syn
@vankesteren
vankesteren / t_mixture.R
Created October 24, 2022 20:20
Univariate t-mixture modelling with R. Also works for gaussian! (albeit a bit slow)
# t-mixture modeling with EM
priorp <- 0.6
m1 <- 0
m2 <- 3
s1 <- 1
s2 <- 0.707
df1 <- 2
df2 <- Inf
# generate some data with 2 classes
@vankesteren
vankesteren / densratio.R
Last active October 20, 2022 10:56
Low-dimensional visualisation and density ratio estimation
# Low-dimensional visualisation and density ratio estimation
library(tidyverse)
library(patchwork)
library(umap)
library(lfda)
library(densratio)
# generate data
N <- 500
@vankesteren
vankesteren / bayesboot.jl
Created October 11, 2022 17:56
Bayesian Bootstrap in Julia
using Downloads
using CSV
using StatsKit
using Gadfly
# Apply macro from StatsBase to create new weights type
BayesianBootstrapWeights = StatsBase.@weights BayesianBoostrapWeights
function StatsBase.varcorrection(w::BayesianBootstrapWeights, corrected::Bool=false)
s = w.sum
if corrected
@vankesteren
vankesteren / run_schelling.jl
Last active June 3, 2022 14:06
Performant schelling model in julia
# Showcase
env = Schelling([0.45, 0.45, 0.1], Ba = 0.5, dim = 200)
anim = @animate for i in 1:100
plot(env; title = i)
step!(env)
end
gif(anim, "anim.gif", fps = 20)
@vankesteren
vankesteren / metasynth.jl
Last active May 30, 2022 15:02
MetaSynth in Julia
using Distributions
using Random
using DataFrames
struct MetaVariable
name::String
p_missing::Float64
dist::UnivariateDistribution
end
@vankesteren
vankesteren / decay_ar.R
Created April 1, 2022 07:11
exponential decay with AR(1)
# exponential decay estimation with AR model
N <- 30
y <- numeric(N)
first_obs <- 2 # where do we start?
asymptote <- .5 # where do we go?
ar1_param <- .6 # how slow do we go there? (between -1 and 1)
sigma <- 0
y[1] <- first_obs
for (n in 2:N) {