### GNU GPL License Notice for Interactive Programs Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/LICENSE.html This code snippet shows the short notice that should be displayed by a program when it starts in interactive mode, informing users about its free software status, lack of warranty, and how to get more details on redistribution conditions. ```plaintext Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'. This is free software, and you are welcome to redistribute it under certain conditions; type 'show c' for details. ``` -------------------------------- ### Install 'adjustedCurves' Package using devtools Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Installs the 'adjustedCurves' R package from GitHub using the 'devtools' package. This method requires the 'devtools' package to be installed and may involve installing additional dependencies. ```r library(devtools) devtools::install_github("RobinDenz1/adjustedCurves") ``` -------------------------------- ### Install 'adjustedCurves' Package using remotes Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Installs the 'adjustedCurves' R package from GitHub using the 'remotes' package. This is an alternative to 'devtools' for package installation and may also require other packages to be installed. ```r library(remotes) remotes::install_github("RobinDenz1/adjustedCurves") ``` -------------------------------- ### Install adjustedCurves Package Source: https://github.com/robindenz1/adjustedcurves/blob/main/README.md Installs the stable version of the adjustedCurves package from CRAN or the developmental version from GitHub using the devtools R package. ```R install.packages("adjustedCurves") ``` ```R library(devtools) install_github("https://github.com/RobinDenz1/adjustedCurves") ``` -------------------------------- ### Example: Fine & Gray Model with Multiple Imputation - R Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/FGR_MI.html This example shows a complete workflow for using FGR_MI. It includes simulating data, introducing missing values, performing multiple imputation using 'mice', and then applying FGR_MI to calculate the Fine & Gray models. The output can be used with other functions like 'adjustedcif'. ```r library(survival) library(riskRegression) library(mice) library(prodlim) # simulate some data as example sim_dat <- sim_confounded_crisk(n=500, max_t=1.2) sim_dat$group <- as.factor(sim_dat$group) # introduce random missingness in x1 as example sim_dat$x1 <- ifelse(runif(n=500) < 0.5, sim_dat$x1, NA) # perform multiple imputation mids <- mice::mice(data=sim_dat, method="pmm", m=5, printFlag=FALSE) # use the function fgr_mods <- FGR_MI(mids=mids, formula=Hist(time, event) ~ x1 + x2 + x3 + x4 + x5 + x6 + group, cause=1) ``` -------------------------------- ### Install adjustedCurves Package using devtools Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/index.html This code snippet demonstrates how to install the 'adjustedCurves' R package from GitHub using the 'devtools' package. Ensure 'devtools' is installed before running this command. ```r library(devtools) devtools::install_github("https://github.com/RobinDenz1/adjustedCurves") ``` -------------------------------- ### Plotting Adjusted CIFs with Default Settings Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/plot.adjustedcif.html This example demonstrates the basic usage of plotting adjusted cumulative incidence functions (CIFs) using the `adjustedcif` object with default plotting parameters. It requires the `riskRegression` and `prodlim` packages and simulated data. ```r # construct a Cause-Specific-Cox model cox_mod <- CSC(Hist(time, event) ~ x1 + x3 + x5 + group, data=sim_dat) # calculate adjusted CIFs with bootstrapping (for cause = 1) adjcif <- adjustedcif(data=sim_dat, variable="group", ev_time="time", event="event", method="direct", outcome_model=cox_mod, conf_int=TRUE, bootstrap=TRUE, n_boot=500, cause=1) # plot the curves with default values plot(adjcif) ``` -------------------------------- ### Install adjustedCurves Package using remotes Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/index.html This code snippet shows an alternative method for installing the 'adjustedCurves' R package from GitHub using the 'remotes' package. The 'remotes' package is a dependency management tool for R. ```r library(remotes) remotes::install_github("https://github.com/RobinDenz1/adjustedCurves") ``` -------------------------------- ### Plotting Adjusted CIFs with Linetypes and Colors Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/plot.adjustedcif.html This example demonstrates plotting adjusted cumulative incidence functions (CIFs) with both different linetypes and colors for distinct groups, while disabling facets. This combination offers a rich visual distinction between multiple curves. ```r # plot with different linetypes and different colors plot(adjcif, linetype=TRUE, color=TRUE, facet=FALSE) ``` -------------------------------- ### Plotting Adjusted CIFs with Different Facets Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/plot.adjustedcif.html This example illustrates plotting adjusted cumulative incidence functions (CIFs) using different facets for different groups, while disabling linetypes and colors. Faceting can be effective for comparing curve behaviors across subgroups. ```r # plot with different facets only plot(adjcif, linetype=FALSE, color=FALSE, facet=TRUE) ``` -------------------------------- ### Calculate Adjusted CIFs using TMLE with SuperLearner Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/cif_tmle.html This example demonstrates how to calculate adjusted cumulative incidence functions (CIFs) using the 'tmle' method with 'SuperLearner' for estimating nuisance parameters. It simulates data, discretizes time, and specifies the SuperLearner library to be used for outcome and treatment models. Warnings indicate potential extrapolation or missing specifications. ```R # get simulation data sim_dat <- sim_confounded_crisk(n=200, max_t=5) # discretize time for TMLE sim_dat$time <- round(sim_dat$time*15) + 1 # calculate adjusted CIFs adjcif <- adjustedcif(data=sim_dat, variable="group", ev_time="time", event="event", cause=1, method="tmle", adjust_vars=c("x1", "x2", "x3", "x4", "x5", "x6"), SL.ftime=c("SL.glm"), SL.ctim=c("SL.glm"), SL.trt=c("SL.glm")) ``` -------------------------------- ### Plotting Adjusted CIFs with Bootstrapped Confidence Intervals Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/plot.adjustedcif.html This example demonstrates plotting adjusted cumulative incidence functions (CIFs) with confidence intervals estimated using bootstrapping. Bootstrapping is a resampling technique often used for more robust uncertainty estimation. ```r # plot with confidence intervals estimated using bootstrapping plot(adjcif, conf_int=TRUE, use_boot=TRUE) ``` -------------------------------- ### Plotting Adjusted CIFs with Asymptotic Confidence Intervals Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/plot.adjustedcif.html This example illustrates plotting adjusted cumulative incidence functions (CIFs) with confidence intervals estimated using asymptotic variances. This provides a measure of uncertainty for the estimated curves. ```r # plot with confidence intervals estimated using asymptotic variances plot(adjcif, conf_int=TRUE) ``` -------------------------------- ### Adjust Survival Curves Example (R) Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_strat_cupples.html This R code snippet demonstrates how to use the adjustedsurv function to estimate adjusted survival curves. It simulates data, applies the adjustment for categorical confounders using the 'strat_cupples' method, and then plots the resulting curves. The example highlights the use of survival analysis libraries and data simulation for practical application. It includes optional steps for confidence interval calculation. ```r # \dontrun{ library(survival) # simulate some data as example sim_dat <- sim_confounded_surv(n=500, max_t=1.2) sim_dat$group <- as.factor(sim_dat$group) # adjust survival curves for some categorical confounders adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="strat_cupples", adjust_vars=c("x1", "x3"), conf_int=FALSE) # plot the curves plot(adjsurv) #> Warning: Removed 22 row(s) containing missing values (geom_path). # } ``` -------------------------------- ### Plotting Adjusted CIFs with Isotonic Regression Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/plot.adjustedcif.html This example shows how to plot adjusted cumulative incidence functions (CIFs) after applying isotonic regression to ensure monotonicity. This is useful when the estimation method does not guarantee non-decreasing CIFs. ```r # plot after applying isotonic regression plot(adjcif, iso_reg=TRUE) ``` -------------------------------- ### Plotting Adjusted CIFs with Different Linetypes Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/plot.adjustedcif.html This example shows how to customize the plot of adjusted cumulative incidence functions (CIFs) by using different linetypes for different groups while disabling colors and facets. This can improve clarity when differentiating multiple curves. ```r # plot with different linetypes only plot(adjcif, linetype=TRUE, color=FALSE, facet=FALSE) ``` -------------------------------- ### Simulate Data for CIF IPTW Example Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/cif_iptw.html Generates simulated competing risks data for demonstrating the cif_iptw function. The data includes a 'group' variable which needs to be a factor for the analysis. This is a prerequisite for running the IPTW CIF estimation. ```r # \dontrun{ # simulate some data as example sim_dat <- sim_confounded_crisk(n=500, max_t=5) sim_dat$group <- as.factor(sim_dat$group) # estimate a treatment assignment model ``` -------------------------------- ### Estimate Adjusted Survival Curves using Stratification Source: https://context7.com/robindenz1/adjustedcurves/llms.txt Applies stratification-based adjustment methods for survival analysis, particularly useful when dealing with discrete confounders. This example demonstrates creating categorical variables from continuous ones and then using the 'strat_amato' method. Requires the `adjustedcurves` package. ```r # Create stratification variables sim_dat$x1_cat <- cut(sim_dat$x1, breaks=3, labels=c("Low", "Med", "High")) sim_dat$x2_cat <- cut(sim_dat$x2, breaks=3, labels=c("Low", "Med", "High")) # Estimate using stratification (Amato method) adjsurv_strat <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="strat_amato", strata=list("x1_cat", "x2_cat"), conf_int=TRUE) # Plot stratified adjustment results plot(adjsurv_strat, conf_int=TRUE) + labs(title="Stratification-Adjusted Survival Curves") ``` -------------------------------- ### Estimate Adjusted Survival Curves using IPTW Cox Model Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_iptw_cox.html This example demonstrates how to estimate adjusted survival curves using the Inverse Probability of Treatment Weighting (IPTW) method with a Cox proportional hazards model. It requires a data frame, variable names for treatment group, event time, and event status, and the method specification. The treatment model can be provided as a fitted GLM object, custom weights, or a formula. ```R # estimate a treatment assignment model glm_mod <- glm(group ~ x1 + x3 + x5 + x6, data=sim_dat, family="binomial") # use it to calculate adjusted survival curves adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="iptw_cox", treatment_model=glm_mod) ``` ```R # Alternatively, use custom weights # In this example we use weights calculated using the propensity score, # which is equal to using the glm model directly in the function ps_score <- glm_mod$fitted.values weights <- ifelse(sim_dat$group==1, 1/ps_score, 1/(1-ps_score)) adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="iptw_cox", treatment_model=weights) ``` ```R # And a third alternative: use the WeightIt package # here an example with equal results to the ones above: adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="iptw_cox", treatment_model=group ~ x1 + x3 + x5 + x6, weight_method="ps") ``` ```R # here an example using Optimization-Based Weighting: adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="iptw_cox", treatment_model=group ~ x1 + x3 + x5 + x6, weight_method="optweight") ``` -------------------------------- ### Estimate Adjusted Survival Curves with AIPTW Pseudo Method Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_aiptw_pseudo.html This example demonstrates estimating adjusted survival curves using the 'aiptw_pseudo' method. It involves fitting a treatment assignment model (glm) and then using this model along with simulated data to compute adjusted survival curves. The 'adjustedsurv' function is used, specifying data, treatment variable, event time, event status, outcome variables, and the treatment model. Confidence intervals are also computed and plotted. ```r # simulate some data as example sim_dat <- sim_confounded_surv(n=100, max_t=1.2) sim_dat$group <- as.factor(sim_dat$group) # estimate a treatment assignment model glm_mod <- glm(group ~ x1 + x3 + x5 + x6, data=sim_dat, family="binomial") # use it + pseudo values + geese model to calculate adjusted survival curves adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="aiptw_pseudo", outcome_vars=c("x1", "x2", "x3", "x4", "x5", "x6"), treatment_model=glm_mod, conf_int=TRUE) # plot the curves plot(adjsurv, conf_int=TRUE) ``` -------------------------------- ### GNU GPL License Notice for Source Files Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/LICENSE.html This code block provides the standard copyright and licensing notice to be included at the beginning of each source file when distributing software under the GNU General Public License (GPL) version 3 or later. It specifies redistribution rights and disclaims warranty. ```plaintext Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ``` -------------------------------- ### Overview of adjustedsurv Methods (R) Source: https://github.com/robindenz1/adjustedcurves/blob/main/vignettes/method_overview.rmd Generates a data frame summarizing supported methods in the adjustedsurv R package. This includes details on confounding, covariate types, variance approximation, bounds, monotonicity, robustness, censoring, and computational speed. The output is formatted using knitr::kable. ```r tab <- data.frame( method=c('"direct"', '"direct_pseudo"', '"iptw_km"', '"iptw_cox"', '"iptw_pseudo"', '"matching"', '"emp_lik"', '"aiptw"', '"aiptw_pseudo"', '"tmle"', '"strat_amato"', '"strat_nieto"', '"strat_cupples"', '"iv_2SRIF"', '"prox_iptw"', '"prox_aiptw"', '"km"'), supp_unmeasured=c(rep("no", 13), rep("yes", 3), "no"), supp_categorical=c(rep("yes", 5), rep("no", 3), "yes", "no", rep("yes", 3), "no", "no", "no", "yes"), supp_continuous_conf=c(rep("yes", 10), rep("no", 3), rep("yes", 3), "no"), supp_approx_var=c("yes", "no", "yes", "no", "yes", "no", "no", "yes", "yes", "yes", "no", "yes", "no", "no", "yes", "yes", "yes"), bounds=c(rep("yes", 4), "no", "yes", "yes", "no", "no", "yes", "yes", "yes", "yes", "yes", "no", "no", "yes"), monotonic=c("yes", "no", "yes", "yes", "no", "yes", "yes", "no", "no", "yes", "yes", "yes", "yes", "yes", "no", "no", "yes"), doubly_robust=c(rep("no", 7), "yes", "yes", "yes", rep("no", 5), "yes", "no"), dependent_censoring=c("no", "yes", "(no)", "(no)", "yes", "no", "no", "yes", "yes", "yes", "no", "no", "no", "no", "no", "no", "no"), type=c("outcome", "outcome", "treatment", "treatment", "treatment", "treatment", "treatment", "both", "both", "both", "-", "-", "-", "-", "treatment", "both", "none"), nonpara=c("no", "no", "depends", "depends", "depends", "depends", "yes", "no", "no", "no", "yes", "yes", "yes", "no", "no", "no", "yes"), speed=c("+", "- -", "++", "++", "-", "-", "+", "-", "- -", "- - -", "+++", "+++", "+++", "+", "- -", "- -", "+++"), dependencies=c("riskRegression", "geepack, prodlim", "-", "-", "prodlim", "Matching", "MASS", "riskRegression", "geepack, prodlim", "concrete", "-", "-", "-", "-", "numDeriv", "numDeriv", "-") ) cnames <- c("Method", "Supports Unmeasured Confounding", "Supports Categorical Treatments", "Supports Continuous Confounders", "Approximate SE available", "Always in Bounds", "Always non-increasing", "Doubly-Robust", "Supports Dependent Censoring", "Type of Adjustment", "Is Nonparametric", "Computation Speed", "Dependencies") tab <- subset(tab, method!='"tmle"') knitr::kable(tab, col.names=cnames) ``` -------------------------------- ### Allowed Input to treatment_model Argument (R) Source: https://github.com/robindenz1/adjustedcurves/blob/main/vignettes/method_overview.rmd Generates a data frame outlining the allowed inputs for the `treatment_model` argument in specific adjustedsurv methods. This helps users understand how to provide treatment models for methods like iptw_km, iptw_cox, and matching. ```r tab <- data.frame( method=c('"iptw_km"', '"iptw_cox"', '"iptw_pseudo"', '"matching"', '"aiptw"', '"aiptw_pseudo"', '"tmle"'), allows=c("glm or multinom object, weights, formula for weightit()", "glm or multinom object, weights, formula for weightit()", "glm or multinom object, weights, formula for weightit()", "glm object or propensity scores", "glm object", "glm or multinom object or propensity scores", "vector of SuperLearner libraries") ) tab <- subset(tab, method!='"tmle"') knitr::kable(tab, col.names=c("Method", "Allowed Input to treatment_model argument")) ``` -------------------------------- ### Using SuperLearner for TMLE Components Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_tmle.html Demonstrates the use of SuperLearner libraries for modeling the outcome (SL.ftime), censoring (SL.ctime), and treatment (SL.trt) mechanisms within the Targeted Maximum Likelihood Estimation framework. This allows for flexible and data-driven model selection. ```R adjustedsurv(..., method = "tmle", SL.ftime = c("SL.gam", "SL.glm"), SL.ctime = c("SL.ranger"), SL.trt = c("SL.glm")) ``` -------------------------------- ### Calculate Adjusted CIF using Direct Adjustment with Bootstrapping Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/adjustedcif.html Performs direct adjustment for the CIF using bootstrapping to estimate confidence intervals. The `bootstrap` argument must be set to TRUE, and `n_boot` specifies the number of bootstrap replications. Note that a higher `n_boot` is recommended for practical use. ```r adjcif <- adjustedcif(data=sim_dat, variable="group", ev_time="time", event="event", cause=1, method="direct", outcome_model=cox_mod, conf_int=TRUE, bootstrap=TRUE, n_boot=100) ``` -------------------------------- ### Display Head of Adjusted Survival Data Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Displays the first few rows of the `adjsurv` data frame, which contains the calculated adjusted survival probabilities, confidence intervals, and associated time points and groups. ```r head(adjsurv$adjsurv) ``` -------------------------------- ### Estimate Adjusted CIFs with FGR Model Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/cif_direct.html This snippet shows how to calculate adjusted cumulative incidences using the `adjustedcif` function with a Fine & Gray (FGR) model. It involves similar setup to the CSC model but uses the `FGR` function for the outcome model estimation. ```r # estimate a Fine & Gray model for the outcome instead fgr_mod <- FGR(Hist(time, event) ~ x1 + x2 + x3 + x4 + x5 + x6 + group, data=sim_dat, cause=1) # use it to calculate adjusted CIFs adjcif <- adjustedcif(data=sim_dat, variable="group", ev_time="time", event="event", cause=1, method="direct", outcome_model=fgr_mod, conf_int=FALSE) # plot the curves plot(adjcif) ``` -------------------------------- ### Using GLM for TMLE Components Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_tmle.html Illustrates specifying generalized linear models (GLM) using formula strings for the outcome (glm.ftime), censoring (glm.ctime), and treatment (glm.trt) mechanisms in Targeted Maximum Likelihood Estimation. This provides a more traditional modeling approach. ```R adjustedsurv(..., method = "tmle", glm.ftime = "trt + X1", glm.ctime = "trt + X2", glm.trt = "X1 + X2") ``` -------------------------------- ### Plot Adjusted Survival Curves with Censoring Indicators Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Visualizes the adjusted survival curves, including small lines indicating the times of censored observations. This provides additional context on the data distribution. ```r plot(adjsurv, color=TRUE, censoring_ind="lines") ``` -------------------------------- ### Augmented Inverse Probability of Treatment Weighting (AIPTW) Survival Analysis Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Estimates adjusted survival curves using the AIPTW method. Requires specification of both a treatment model and an outcome model. The output can be visualized with confidence intervals. ```R treatment_model <- glm(group ~ x1 + x2 + x3 + x4 + x5 + x6, data=data, family="binomial"(link="logit")) outcome_model <- survival::coxph(Surv(time, event) ~ x1 + x2 + x3 + x4 + x5 + x6 + group, data=data, x=TRUE) adjsurv <- adjustedsurv(data=data, variable="group", ev_time="time", event="event", method="aiptw", treatment_model=treatment_model, outcome_model=outcome_model, conf_int=TRUE) plot(adjsurv, conf_int=TRUE) ``` -------------------------------- ### Simulate and Adjust Survival Data Source: https://github.com/robindenz1/adjustedcurves/blob/main/vignettes/comparing_groups.rmd Simulates confounded survival data and estimates confounder-adjusted survival curves using inverse probability of treatment weighting (IPW-KM). It utilizes bootstrapping for confidence intervals, though a limited number of bootstrap samples are used for computational efficiency in this example. ```r library(adjustedCurves) library(survival) library(ggplot2) library(pammtools) library(cowplot) set.seed(34253) data <- sim_confounded_surv(n=500, group_beta=0) data$group <- factor(data$group) adjsurv <- adjustedsurv(data=data, variable="group", ev_time="time", event="event", method="iptw_km", treatment_model=group ~ x2 + x5, conf_int=TRUE, bootstrap=TRUE, n_boot=100, stabilize=TRUE) plot(adjsurv, conf_int=TRUE, risk_table=TRUE, risk_table_stratify=TRUE, risk_table_digits=0, x_n_breaks=10) ``` -------------------------------- ### Simulate Survival Data with Default Values (R) Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/sim_confounded_surv.html This code snippet demonstrates how to simulate survival data using the default parameters of the sim_confounded_surv function. It requires no external dependencies beyond the adjusted curves package. ```r sim_dat <- sim_confounded_surv(n=500) ``` -------------------------------- ### Calculate Unadjusted Kaplan-Meier Survival Curves Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_km.html Demonstrates how to calculate unadjusted Kaplan-Meier survival curves using the `adjustedsurv` function with `method='km'`. This example simulates data, sets a factor variable for groups, calls `adjustedsurv`, and then plots the resulting curves. It relies on the `survival` and `adjustedCurves` packages. ```r # \dontrun{ library(survival) # simulate some data as example sim_dat <- sim_confounded_surv(n=500, max_t=1.2) sim_dat$group <- as.factor(sim_dat$group) # calculate un-adjusted kaplan-meier survival curves adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="km") # plot the curves plot(adjsurv) # } ``` -------------------------------- ### Overview of adjustedcif() Methods in R Source: https://github.com/robindenz1/adjustedcurves/blob/main/vignettes/method_overview.rmd This R code snippet generates a table summarizing the methods supported by the adjustedcif() function. It includes details on confounding support, estimation properties, and dependencies. The 'tmle' method is excluded from the displayed table. ```r tab <- data.frame( method=c('"direct"', '"direct_pseudo"', '"iptw"', '"iptw_pseudo"', '"matching"', '"aiptw"', '"aiptw_pseudo"', '"tmle"', '"aalen_johansen"'), supp_unmeasured="no", supp_categorical=c("yes", "yes", "yes", "yes", "no", "no", "yes", "no", "yes"), supp_continuous_conf=c(rep("yes", 8), "no"), supp_approx_var=c("yes", "no", "yes", "yes", "no", "yes", "yes", "yes", "yes"), bounds=c("yes", "yes", "yes", "no", "yes", "no", "no", "yes", "yes"), monotonic=c("yes", "no", "yes", "no", "yes", "no", "no", "yes", "yes"), doubly_robust=c("no", "no", "no", "no", "no", "yes", "yes", "yes", "no"), dependent_censoring=c("no", "no", "yes", "no", "no", "yes", "no", "yes", "no"), type=c("outcome", "outcome", "treatment", "treatment", "treatment", "both", "both", "both", "none"), non_parametric=c("no", "no", "no", "depends", "depends", "no", "no", "no", "yes"), speed=c("+", "- -", "+", "+", "-", "-", "- -", "- - -", "++"), dependencies=c("riskRegression", "geepack, prodlim", "riskRegression", "prodlim", "Matching", "riskRegression", "geepack, prodlim", "concrete", "cmprsk") ) cnames <- c("Method", "Supports Unmeasured Confounding", "Supports Categorical Treatments", "Supports Continuous Confounders", "Approximate SE available", "Always in Bounds", "Always non-increasing", "Doubly-Robust", "Supports Dependent Censoring", "Type of Adjustment", "Is Nonparametric", "Computation Speed", "Dependencies") tab <- subset(tab, method!='"tmle"') knitr::kable(tab, col.names=cnames) ``` -------------------------------- ### Calculate and Plot Adjusted Survival Curves (IPTW) Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/index.html This example illustrates calculating and plotting adjusted survival curves using the Inverse Probability of Treatment Weighting (IPTW) method. It requires fitting a treatment assignment model (e.g., logistic regression) and then using 'adjustedsurv' with the 'iptw_km' method. ```r # estimate a treatment assignment model glm_mod <- glm(group ~ x2 + x3 + x5 + x6, data=sim_dat, family="binomial"(link="logit")) # use it to calculate adjusted survival curves adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="iptw_km", treatment_model=glm_mod, conf_int=TRUE) # plot with confidence intervals plot(adjsurv, conf_int=TRUE) ``` -------------------------------- ### Calculate and Plot Adjusted Survival Curves (Direct Adjustment) Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/index.html This example demonstrates calculating and plotting confounder-adjusted survival curves using the 'direct' method. It involves simulating data, fitting a Cox proportional hazards model, and then using the 'adjustedsurv' function to compute and plot the curves with confidence intervals. ```r library(adjustedCurves) library(survival) # simulate some data as example set.seed(31) sim_dat <- sim_confounded_surv(n=250, max_t=1.2, group_beta=0) sim_dat$group <- as.factor(sim_dat$group) # estimate a cox-regression for the outcome cox_mod <- coxph(Surv(time, event) ~ x1 + x2 + x4 + x5 + group, data=sim_dat, x=TRUE) # use it to calculate adjusted survival curves adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="direct", outcome_model=cox_mod, conf_int=TRUE) # plot with confidence intervals plot(adjsurv, conf_int=TRUE) ``` -------------------------------- ### Calculate Unadjusted Aalen-Johansen Estimates and Plot Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/cif_aalen_johansen.html This example demonstrates how to simulate data, calculate unadjusted Aalen-Johansen cumulative incidence functions stratified by a group variable using the 'aalen_johansen' method, and then plot the resulting curves. It requires the cmprsk package for data simulation and the adjustedCurves package for the main function. ```r # \dontrun{ library(cmprsk) # simulate some data as example sim_dat <- sim_confounded_crisk(n=500, max_t=5) sim_dat$group <- as.factor(sim_dat$group) # calculate un-adjusted aalen-johansen estimates adjcif <- adjustedcif(data=sim_dat, variable="group", ev_time="time", event="event", cause=1, method="aalen_johansen") # plot the curves plot(adjcif) # } ``` -------------------------------- ### Simulate Data and Calculate Adjusted Survival Curves (R) Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_ostmle.html This R code snippet demonstrates how to simulate survival data and then calculate adjusted survival curves using the OSTMLE method. It specifies the data, outcome variable, event time, event status, adjustment variables, and the SuperLearner libraries to be used for time, competing risk, and treatment mechanism modeling. It also shows how to plot the resulting curves. Note that the method is sensitive to the discretization of time. ```R # simulate some data as example (needs a binary integer "variable") sim_dat <- sim_confounded_surv(n=500, max_t=1.2) # only works with integer time, only unbiased with small amounts of them sim_dat$time <- round(sim_dat$time*15) + 1 # calculate adjusted survival curves, using SuperLearner but only # using the SL.glm library. In practice you would want to use more than # that. See ?MOSS and ?survtmle adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="ostmle", adjust_vars=c("x1", "x2", "x3", "x4", "x5", "x6"), SL.ftime=c("SL.glm"), SL.ctim=c("SL.glm"), SL.trt=c("SL.glm")) #> censoring sl error #> Warning: Max number of iteration reached, stop TMLE #> Warning: Max number of iteration reached, stop TMLE # plot the curves plot(adjsurv) ``` -------------------------------- ### Checking R Package with devtools::check() Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/CONRTIBUTING.html This snippet demonstrates how to use the devtools::check() function in R to verify package integrity. It's essential for ensuring code quality and catching potential errors before submission. Aim for zero errors and warnings. ```r devtools::check() ``` -------------------------------- ### Calculate Adjusted Survival Curves (R) Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_direct_pseudo.html This code snippet demonstrates how to calculate adjusted survival curves using the `adjustedsurv` function. It shows examples of specifying time as a factor and modeling time as a B-Spline with a specified degrees of freedom. The function requires a data frame with survival data, including group variable, event time, event status, and outcome variables. ```r # simulate some data as example sim_dat <- sim_confounded_surv(n=100, max_t=1.2) sim_dat$group <- as.factor(sim_dat$group) # calculate adjusted survival curves, with time as factor adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="direct_pseudo", outcome_vars=c("x1", "x2", "x3", "x4", "x5", "x6"), type_time="factor") # with time modelled as B-Spline using 5 degrees of freedom adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="direct_pseudo", outcome_vars=c("x1", "x2", "x3", "x4", "x5", "x6"), type_time="bs", spline_df=5) # plot the curves plot(adjsurv) ``` -------------------------------- ### Simulate Survival Data with Confounders using adjustedCurves Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Simulates survival data with confounding factors using the `sim_confounded_surv` function from the 'adjustedCurves' package. This function generates covariates, a binary group variable, event times, and event indicators, suitable for survival analysis. The output is a data frame compatible with standard time-to-event analysis. ```r library(survival) library(ggplot2) library(adjustedCurves) # set random number generator seed to make this reproducible set.seed(41) # simulate standard survival data with 300 rows data <- sim_confounded_surv(n=300, max_t=1.1, group_beta=-0.6) # code the grouping variable as a factor data$group <- as.factor(data$group) # take a look at the first few rows head(data) ``` -------------------------------- ### Estimate Adjusted Survival Curves with aiptw Method Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_aiptw.html This snippet demonstrates how to estimate adjusted survival curves using the 'aiptw' method. It involves fitting a Cox proportional hazards model for the outcome and a binomial generalized linear model for the treatment assignment. The `adjustedsurv` function then uses these models to compute the adjusted survival curves. Confidence intervals are set to FALSE in this example. ```r library(survival) # simulate some data as example sim_dat <- sim_confounded_surv(n=500, max_t=1.2) sim_dat$group <- as.factor(sim_dat$group) # estimate a cox-regression for the outcome cox_mod <- coxph(Surv(time, event) ~ x1 + x2 + x3 + x4 + x5 + x6 + group, data=sim_dat, x=TRUE) # estimate a treatment assignment model glM_mod <- glm(group ~ x1 + x3 + x5 + x6, data=sim_dat, family="binomial") # use it to calculate adjusted survival curves adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="aiptw", outcome_model=cox_mod, treatment_model=glm_mod, conf_int=FALSE) ``` -------------------------------- ### Calculate Adjusted RMST with Bootstrapping Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Calculates the adjusted Restricted Mean Survival Time (RMST) for different groups. It supports calculating standard deviations and confidence intervals when bootstrapping was used in the `adjustedsurv` function by setting `use_boot` to TRUE. Requires the `adjustedsurv` object as input. ```R adjsurv <- adjustedsurv(data=data, variable="group", ev_time="time", event="event", method="iptw_km", treatment_model=treatment_model, conf_int=TRUE, bootstrap=TRUE, n_boot=300) adjrmst <- adjusted_rmst(adjsurv, to=1, use_boot=TRUE) print(adjrmst) plot(adjrmst) ``` -------------------------------- ### Estimate Adjusted Survival Curves with Custom Weights Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_iptw_km.html Calculates adjusted survival curves using custom-calculated weights derived from propensity scores. This method involves fitting a GLM to get fitted values (propensity scores), calculating weights based on these scores and the treatment group, and then passing these weights to the `adjustedsurv` function. It's an alternative to directly passing the GLM object. ```r sim_dat <- sim_confounded_surv(n=500, max_t=1.2) sim_dat$group <- as.factor(sim_dat$group) # estimate a treatment assignment model glM_mod <- glm(group ~ x1 + x3 + x5 + x6, data=sim_dat, family="binomial") # use it to calculate adjusted survival curves ps_score <- glm_mod$fitted.values weights <- ifelse(sim_dat$group==1, 1/ps_score, 1/(1-ps_score)) adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="iptw_km", treatment_model=weights) ``` -------------------------------- ### Fit Cox Model for Direct Standardization Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Fits a Cox proportional hazards model to predict the outcome. This model is a prerequisite for using the 'direct' method in the `adjustedsurv` function. Ensure `X=TRUE` is set to include necessary model matrices for standardization. ```r outcome_model <- survival::coxph(Surv(time, event) ~ x1 + x2 + x3 + x4 + x5 + x6 + group, data=data, x=TRUE) ``` -------------------------------- ### Example: RMST with Simulated Survival Data (R) Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/adjusted_rmtl.html This R code demonstrates how to use the adjusted_rmtl function with simulated single-event survival data. It first simulates data, fits a Cox proportional hazards model, and then uses the 'adjustedsurv' function to estimate adjusted survival curves with bootstrapping. Finally, it calls 'adjusted_rmtl' to calculate the confounder-adjusted restricted mean time lost over a specified interval. ```R # \dontrun{ library(survival) ###### when using single-event survival data # simulate some data as example sim_dat <- sim_confounded_surv(n=500, max_t=1.2) sim_dat$group <- as.factor(sim_dat$group) # estimate a cox-regression for the outcome cox_mod <- coxph(Surv(time, event) ~ x1 + x2 + x3 + x4 + x5 + x6 + group, data=sim_dat, x=TRUE) # use it to calculate adjusted survival curves with bootstrapping adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="direct", outcome_model=cox_mod, conf_int=FALSE, bootstrap=TRUE, ``` -------------------------------- ### Plot Adjusted Survival Curves with Median Survival Lines Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Plots the adjusted survival curves and adds indicator lines for the median survival time for each group. This highlights a key summary statistic for each curve. ```r plot(adjsurv, color=TRUE, median_surv_lines=TRUE) ``` -------------------------------- ### Input to treatment_model Argument in R Source: https://github.com/robindenz1/adjustedcurves/blob/main/vignettes/method_overview.rmd This R code generates a table detailing the acceptable inputs for the 'treatment_model' argument in specific methods of the adjustedcif() function. The 'tmle' method is excluded from this table. It clarifies which types of model objects or functions are compatible with each adjustment method. ```r tab <- data.frame( method=c('"iptw"', '"iptw_pseudo"', '"matching"', '"aiptw"', '"aiptw_pseudo"', '"tmle"'), allows=c("glm or multinom object", "glm or multinom object, weights, formula for weightit()", "glm object or propensity scores", "glm object", "glm or multinom object or propensity scores", "vector of SuperLearner libraries") ) tab <- subset(tab, method!='"tmle"') knitr::kable(tab, col.names=c("Method", "Allowed Input to treatment_model argument")) ``` -------------------------------- ### IPTW Estimation with Binary Treatment Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Calculates Inverse Probability of Treatment Weighting (IPTW) for survival analysis with a binary treatment variable. Requires a fitted binomial logistic regression model for the treatment. Outputs an adjusted CIF object and can generate plots. ```r treatment_model <- glm(group ~ x1 + x2 + x3 + x4 + x5 + x6, data=data, family="binomial"(link="logit")) adjcif <- adjustedcif(data=data, variable="group", ev_time="time", event="event", method="iptw", treatment_model=treatment_model, cause=1, conf_int=TRUE) plot(adjcif) ``` -------------------------------- ### Calculate Adjusted RMST without Bootstrapping (R) Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/adjusted_rmst.html This R code snippet shows how to calculate adjusted restricted mean survival times (RMST) from adjusted survival curves without using bootstrapping. It follows a similar setup to the bootstrapped version, estimating a Cox model, obtaining adjusted survival curves, and then calculating RMST from 0 to 1 using `adjusted_rmst` with `use_boot=FALSE`. ```R library(survival) sim_dat <- sim_confounded_surv(n=500, max_t=1.2) sim_dat$group <- as.factor(sim_dat$group) cox_mod <- coxph(Surv(time, event) ~ x1 + x2 + x3 + x4 + x5 + x6 + group, data=sim_dat, x=TRUE) adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="direct", outcome_model=cox_mod, conf_int=FALSE, bootstrap=TRUE, n_boot=500) adjrmst <- adjusted_rmst(adjsurv, from=0, to=1, use_boot=FALSE) print(adjrmst) ``` -------------------------------- ### Estimate Adjusted Survival Curves using WeightIt Package (PS) Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/surv_iptw_km.html Estimates adjusted survival curves using the `WeightIt` package for propensity score weighting. This approach leverages the `weight_method="ps"` argument within `adjustedsurv`, allowing the function to internally handle the propensity score calculation and weighting based on the provided treatment model formula. ```r sim_dat <- sim_confounded_surv(n=500, max_t=1.2) sim_dat$group <- as.factor(sim_dat$group) adjsurv <- adjustedsurv(data=sim_dat, variable="group", ev_time="time", event="event", method="iptw_km", treatment_model=group ~ x1 + x3 + x5 + x6, weight_method="ps") ``` -------------------------------- ### Plot Adjusted Survival Curves Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/articles/introduction.html Generates a plot of the adjusted survival curves from the `adjsurv` object. The default plot shows survival probability against time for different groups. ```r plot(adjsurv) ``` -------------------------------- ### Parallel Bootstrapping for Direct Adjustment CIF Source: https://github.com/robindenz1/adjustedcurves/blob/main/docs/reference/adjustedcif.html Calculates the adjusted CIF using direct adjustment with bootstrapping, utilizing multiple processor cores for parallel computation. Requires the `foreach`, `parallel`, and `doRNG` packages. `n_cores` can be set to leverage available processors, reducing computation time. ```r library(foreach) library(parallel) library(doRNG) adjcif <- adjustedcif(data=sim_dat, variable="group", ev_time="time", event="event", cause=1, method="direct", outcome_model=cox_mod, conf_int=TRUE, bootstrap=TRUE, n_boot=100, n_cores=parallel::detectCores()-1) ```