Skip to content

Instantly share code, notes, and snippets.

View Dpananos's full-sized avatar
🏠
Working from home

Demetri Pananos Dpananos

🏠
Working from home
View GitHub Profile
SET VARIABLE EXPERIMENT_START_DATE = TIMESTAMP '2024-10-01 00:00:00';
SET VARIABLE EXPERIMENT_END_DATE = TIMESTAMP '2024-10-31 00:00:00';
with prep_cleaned_exposures as (
select distinct
userid,
treatment,
min(exposure_time) over (partition by userid) as exposure_time,
(min(treatment) over (partition by userid)) <> (max(treatment) over (partition by userid)) as multiple_exposures
@Dpananos
Dpananos / simulation.R
Created August 22, 2024 14:41
Simulations of including instruments in a propensity score model
library(WeightIt)
library(tidyverse)
library(furrr)
plan(multisession, workers = 10)
sim <- function(n, l1, l2, l3, l4, ate, prop_br, ps_model_formula, simnum, ...) {
withr::with_seed(simnum, {
@Dpananos
Dpananos / ex.R
Created April 13, 2024 16:09
Confounding example
library(tidyverse)
# Genuine confounding example. Sex confounds relationship between drug and death
set.seed(0)
n <- 1000000
is_male <- rbinom(n, 1, 0.5)
drug <- rbinom(n, 1, 0.6 + 0.3*is_male)
y <- rbinom(n, 1, 0.4 - 0.1*drug + 0.4*is_male)
d <- tibble(drug, is_male, y)
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
@Dpananos
Dpananos / discrete_dynamic.py
Created October 1, 2023 18:58
Discrete dynamics
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import multinomial
import cmdstanpy
def simulate(n_transitions):
A = 1000
B = 100
@Dpananos
Dpananos / risk_diff.R
Created July 7, 2023 16:11
Risk DIfference using Marginal Effects in ANCOVA style logistic regression
library(tidyverse)
library(marginaleffects)
g <- LETTERS[1:4]
N <- rpois(length(g), 100000)
theta <- c(0.1, 0.12, 0.14, 0.08)
y <- rbinom(length(g), N, theta)
d <- tibble(g, N, y)
@Dpananos
Dpananos / marginal_effect.R
Created June 17, 2023 19:34
Marginal effect for cox model
library(survival)
library(tidyverse)
library(broom)
library(marginaleffects)
# Make a toy model
gr <- sample(letters[1:2], size = 10000, replace=T)
g <- as.numeric(gr=='a')
etime <- rexp(length(gr), rate = (5+1*g)/10)
@Dpananos
Dpananos / hierarchical_normal.py
Created July 23, 2022 20:54
Implementation of 8 schools, with a twist.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pymc as pm
import arviz as az
N = 50_000
treatment_conversions = np.array([541, 557, 559, 556, 530, 532, 516, 532, 528, 544, 519, 552])
control_conversions = np.array([496, 524, 486, 500, 516, 475, 507, 475, 490, 506, 512, 489])
import numpy as np
from statsmodels.nonparametric.smoothers_lowess import lowess
from sklearn.datasets import load_breast_cancer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import KFold, RepeatedKFold, GridSearchCV, cross_val_score
from sklearn.metrics import make_scorer, brier_score_loss
from sklearn.utils import resample
# LRT
data = c(27,24,3,14,9,14,4,6,7,8)
m = matrix(data, nrow = 2)
theta_null = colSums(m)/sum(m)
theta_1 = m[1,]/sum(m[1,])
theta_2 = m[2,]/sum(m[2,])
L0 = dmultinom(colSums(m), prob = theta_null, log=T)
L1 = dmultinom(m[1,], prob = theta_1, log=T) + dmultinom(m[2,], prob = theta_2, log=T)