Skip to content

Instantly share code, notes, and snippets.

View alexpkeil1's full-sized avatar

Alex Keil alexpkeil1

View GitHub Profile
@alexpkeil1
alexpkeil1 / bkmrhat_MI.R
Last active December 7, 2023 21:37
using bkmrhat with multiple imputation
# using BKMRhat with multiple imputation
library(bkmrhat)
library(mice)
library(bkmr)
library(future)
# create simulated dataset for testing
dat <- bkmr::SimData(n = 50, M = 4)
@alexpkeil1
alexpkeil1 / joint_exposure_response_qgcomp_cox_boot.R
Created May 30, 2023 14:37
Plotting an exposure response curve from qgcomp.cox.boot
library(qgcomp)
library(survival)
library(ggplot2)
set.seed(50)
N=200
dat <- data.frame(time=(tmg <- pmin(.1,rweibull(N, 10, 0.1))),
d=1.0*(tmg<0.1), x1=runif(N), x2=runif(N), z=runif(N))
expnms=paste0("x", 1:2)
f = survival::Surv(time, d)~x1 + x2 + I(x2*x1)
## Not run:
# simple example of using Bayes to penalize estimates in quantile g-computation
library(qgcomp)
library(MASS)
n= 50
Sigma <- matrix(c(10,3,3,2),2,2)
Sigma
set.seed(1232)
X = MASS::mvrnorm(n, mu=rep(0,2), Sigma)
@alexpkeil1
alexpkeil1 / qgcomp_time_varying_exposures.R
Created January 25, 2023 14:31
Effect of recent, time-varying exposures on a survival outcome using quantile-based g-computation
# simple application of qgcomp with time-varying exposures
# the simulated model is for the discrete hazard, given recent exposure and recent values of confounders
# this method can be used for alternative scenarios (e.g. cumulative exposure), which corresponds to
# different parametric assumptions
library(qgcomp)
library(survival)
# create simple data set with time-varying exposures
makedata <- function(N=100){
@alexpkeil1
alexpkeil1 / qgcomp_boot_transform_plot_workaround.R
Created October 6, 2022 14:13
Fix error in plotting qgcomp.cox.boot objects when using variable transformations (e.g. splines) on covariates
# scoping issue when using in-formula transformations
# example: spline basis functions
# problem: when including functions like `rms(x)` in a formula to put a spline
# function on 'x', plotting a qgcomp.cox.boot object yields an error that 'x' can't be found
# solution: explicitly create the spline basis functions as variables in a data.frame. Example follows.
library(qgcomp)
library(rms)
library(survival)
@alexpkeil1
alexpkeil1 / qgcomp_gee_longitudinal_or_clustered.R
Last active June 29, 2024 00:37
Using a GEE-like approach to address clustering or longitudinal data with qgcomp
# qgcomp with a gee like approach using bootstrapping or an estimating equation based approach - useful for clustered or longitudinal data when the interest is in effect of x -> y, pooled over multiple time points
library(qgcomp)
library(qgcompint)
set.seed(50)
####### simulate some clustered data -----
# linear model, binary modifier
# simulate cluster specific exposures and outcome means by just treating these like independent observations
dat <- qgcompint::simdata_quantized_emm(outcometype = "continuous",
@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,
library(qgcomp)
library(mvtnorm)
Niter = 1000
seedvals = round(runif(Niter)*.Machine$integer.max)
truemod <- function(X,beta){
X %*% beta
}
# 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
@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)
{