Skip to content

Instantly share code, notes, and snippets.

View alexpkeil1's full-sized avatar

Alex Keil alexpkeil1

View GitHub Profile
@alexpkeil1
alexpkeil1 / dens_data.txt
Created June 30, 2015 17:17
x variable for density example on stackexchange
c(-38.3455, 0.54882, 5.19848, 0.492518, 0.566555, -0.0479212,
0.333422, 8.85725, 0.450325, 0.277257, -148.454, 0.504147, 0.356457,
0.341726, 0.405021, 0.28855, 0.972497, 0.605668, 0.539456, -136.618,
-105.166, 1.49517, 0.707087, 0.0541961, 0.452244, 442.601, 0.288529,
0.0149293, 48.3507, 606.591, 0.971479, 1.81285, 6.27816, 0.531303,
0.874542, 16.5209, 0.446185, 0.76069, 12.3285, 0.629471, 0.719511,
-0.061535, 0.828019, 0.233507, 0.170131, 0.682919, 0.880852,
0.577185, 0.88224, 0.489173, 0.539035, 0.385609, 232.693, 0.504418,
0.654281, 0.59836, 0.457291, 2.51278, 0.459031, 12366.9, 0.773534,
6.5793, 1.02045, 0.451943, 0.176133, 463.412, 1549.17, 0.403163,
@alexpkeil1
alexpkeil1 / scrape_newyorker.py
Last active April 20, 2016 15:38
scrape_newyorker.py
# coding: utf-8
# python-2.7
# new yorker caption contest
import requests
import bs4 # beautifulsoup
import mechanize
import cookielib
import getpass
def ny_url(num=500, pr=False):
@alexpkeil1
alexpkeil1 / pythonbotface
Created May 1, 2016 17:17
Python Bot - whatever strikes my fancy at the moment
#! /usr/local/anaconda/bin/python
def bot():
%magicstuff
@alexpkeil1
alexpkeil1 / weight_bias.R
Last active October 26, 2022 15:10
Comparing overall effect estimates of quantile g-computation and weighted quantile sum, demonstrating that weights from qgcomp need to be handled carefully when compared across simulations
######################################################################################################################
# Author: Alex Keil
# Program: weight_bias.R
# Language: R
# Date: Friday, February 21, 2020 at 3:18:41 PM
# Project: qgcomp
# Description: # addressing a conjecture that WQS will yield better estimates of weights than qgcomp
# when the assumptions of WQS hold in sample sizes of 500 in a scenario in which
# exposures are all uncorrelated
#
# Example in which weighted quantile sums estimates a significant, positive effect of exposure due to pure confounding
# even though there are no exposures that have a positive effect on the outcome
library(qgcomp)
library(gWQS)
dgm <- function(N = 100, b0=0, coef=c(1,0,0,0), family=gaussian(), ncor=0, corr=0.75, umc=FALSE){
# simulate under data structure where WQS is the truth: e.g. an exposure with
# simulation 1 from appendix of
# ”Per- and poly-fluoroalkyl substances and bone mineral density:
# Results from the Bayesian weighted quantile sum regression” by Colicino et al.
#
# simulate data and run the BWQS model
library(rstan)
library(qgcomp)
parallel:::setDefaultClusterOptions(setup_strategy = "sequential") # kludge for bug in parallel package with r4.0
options(mc.cores = parallel::detectCores())
@alexpkeil1
alexpkeil1 / weighted_quantiles_in_qgcomp.R
Created August 14, 2020 12:59
Use quantile g-computation with quantiles that are derived from weighted data, rather than the analytic sample
#!/usr/local/env R
# a way to base the quantiles off of weighted data
library(qgcomp)
library(DescTools) # need this to calculate weighted quantiles
data(metals)
# new function to calculate weighted quantiles
quantize.wtd <- function (data, expnms, q = 4, breaks = NULL, weights=NULL)
{
# using qgcomp.cox.noboot in a nested case-control analysis as a way to fit a conditional logit qgcomp model
library(qgcomp)
library(survival)
# read in data from a nested case control study
data(infert, package = "datasets")
head(infert)
### 1) check that it works, part A
library(qgcomp)
library(mvtnorm)
Niter = 1000
seedvals = round(runif(Niter)*.Machine$integer.max)
truemod <- function(X,beta){
X %*% beta
}
@alexpkeil1
alexpkeil1 / qgcomp_no_intercept.R
Last active July 1, 2021 17:18
quantile g-computation with a suppressed intercept
library(qgcomp)
msm.fit.noint <- function(f,
qdata,
intvals,
expnms,
rr=TRUE,
main=TRUE,
degree=1,
id=NULL,