### Installation Source: https://tlverse.org/tmle3shift/index.html You can install the development version of tmle3shift from GitHub via remotes. ```R remotes::install_github("tlverse/tmle3shift") ``` -------------------------------- ### MSM_linear example Source: https://tlverse.org/tmle3shift/articles/vimshift.html Example output from MSM_linear function, showing estimated parameters and confidence intervals. ```R ## 2: MSM_linear MSM(slope) 0.8840211 0.8840059 0.009393223 0.8655955 ## upper psi_transformed lower_transformed upper_transformed ## 1: 1.6306034 1.5195548 1.4085062 1.6306034 ## 2: 0.9024162 0.8840059 0.8655955 0.9024162 ``` -------------------------------- ### Reproducing data with dput() Source: https://tlverse.org/tmle3shift/CONTRIBUTING.html Example of how to use dput() to generate R code for recreating a dataset, and how to include it in a reproducible script. ```R mtcars <- # Paste dput(mtcars) output here ``` -------------------------------- ### TMLE Fit Output Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html Example output from a tmle3_Fit object, showing the estimated parameter, initial estimate, TMLE estimate, standard error, and confidence intervals. ```R ## A tmle3_Fit that took 1 step(s) ## type param init_est tmle_est se lower upper ## 1: TSM E[Y_{A=NULL}] 2.051413 2.05467 0.06087795 1.935351 2.173989 ## psi_transformed lower_transformed upper_transformed ## 1: 2.05467 1.935351 2.173989 ``` -------------------------------- ### Example of tmle_msm_fit output Source: https://tlverse.org/tmle3shift/articles/vimshift.html This snippet shows the typical output of a tmle_msm_fit function, indicating the number of steps taken and the estimated parameters, standard errors, and confidence intervals. ```R tmle_msm_fit ## A tmle3_Fit that took 100 step(s) ## type param init_est tmle_est se lower ## 1: MSM_linear MSM(intercept) 1.5196350 1.5195548 0.056658489 1.4085062 ``` -------------------------------- ### Example of potential errors during model fitting Source: https://tlverse.org/tmle3shift/articles/vimshift.html This code block contains example error messages that might occur during the fitting process, specifically related to the 'haldensify' model failing to select an optimal lambda via cross-validation. These are presented as raw output. ```text ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## ## Iter: 1 fn: 1419.0430 Pars: 0.75107 0.24893 ## Iter: 2 fn: 1419.0430 Pars: 0.75107 0.24893 ## solnp--> Completed in 2 iterations ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ## Error : Optimal lambda not selected by CV in fitted haldensify model ``` -------------------------------- ### Load Packages and Set Seed Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html Loads necessary R packages and sets a seed for reproducible simulations. ```r library(data.table) library(sl3) library(tmle3) library(tmle3shift) set.seed(429153) ``` -------------------------------- ### Initialize a TMLE specification Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html Initializes a TMLE specification object for shift interventions. ```R tmle_spec <- tmle_shift(shift_val = 0.5, shift_fxn = shift_additive, shift_fxn_inv = shift_additive_inv) ``` -------------------------------- ### List available density learners in sl3 Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html Extracts a list of learners suitable for conditional density estimation from the sl3 package. ```R sl3_list_learners("density") ``` -------------------------------- ### Constructor Source: https://tlverse.org/tmle3shift/reference/Param_MSM_linear.html Constructor for defining a parameter for a linear working marginal structural model. ```R define_param(Param_MSM_linear, observed_likelihood, intervention_list, ..., outcome_node) ``` -------------------------------- ### Initializing tmle_vimshift through its tmle3_Spec Source: https://tlverse.org/tmle3shift/articles/vimshift.html This code initializes a tmle3_Spec object for the tmle_vimshift parameter, specifying a grid of shifts to consider for assessing the mean counterfactual outcome. ```R # what's the grid of shifts we wish to consider? delta_grid <- seq(-1, 1, 1) # initialize a tmle specification tmle_spec <- tmle_vimshift_delta(shift_fxn = shift_additive_bounded, shift_fxn_inv = shift_additive_bounded_inv, shift_grid = delta_grid, max_shifted_ratio = 2) ``` -------------------------------- ### Step-by-step procedure for fitting the TML estimator (NOT RUN) Source: https://tlverse.org/tmle3shift/articles/vimshift.html This code block outlines the detailed, step-by-step procedure for fitting a TML estimator. It includes creating the TMLE task, defining the likelihood, setting up the updater, and fitting the TML estimator. This is marked as 'NOT RUN' and is intended for illustrative purposes, with a more convenient wrapper function provided later. ```R # NOT RUN -- SEE NEXT CODE CHUNK # define data (from tmle3_Spec base class) tmle_task <- tmle_spec$make_tmle_task(data, node_list) # define likelihood (from tmle3_Spec base class) likelihood_init <- tmle_spec$make_initial_likelihood(tmle_task, learner_list) # define update method (fluctuation submodel and loss function) updater <- tmle_spec$make_updater() likelihood_targeted <- Targeted_Likelihood$new(likelihood_init, updater) # invoke params specified in spec tmle_params <- tmle_spec$make_params(tmle_task, likelihood_targeted) updater$tmle_params <- tmle_params # fit TML estimator update tmle_fit <- fit_tmle3(tmle_task, likelihood_targeted, tmle_params, updater) # extract results from tmle3_Fit object tmle_fit ``` -------------------------------- ### Simulate Data Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html Simulates a simple dataset for demonstrating the tmle-shift functionality, including baseline covariates (W), treatment (A), and outcome (Y). ```r # simulate simple data for tmle-shift sketch n_obs <- 1000 # number of observations n_w <- 1 # number of baseline covariates tx_mult <- 2 # multiplier for the effect of W = 1 on the treatment ## baseline covariates -- simple, binary W <- as.numeric(replicate(n_w, rbinom(n_obs, 1, 0.5))) ## create treatment based on baseline W A <- as.numeric(rnorm(n_obs, mean = tx_mult * W, sd = 1)) ## create outcome as a linear function of A, W + white noise Y <- A + W + rnorm(n_obs, mean = 0, sd = 0.5) ``` -------------------------------- ### Organize Data and Nodes for TMLE3 Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html Organizes the simulated data into a data.table and defines the node list, specifying the roles of variables (W, A, Y) in the directed acyclic graph (DAG) for tmle3. ```r # organize data and nodes for tmle3 data <- data.table(W, A, Y) node_list <- list(W = "W", A = "A", Y = "Y") head(data) ``` -------------------------------- ### Specify outcome and treatment regressions and create learner list Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html Bundles the ensemble learners into a list object, specifying their roles in fitting the outcome and treatment regressions. ```R # specify outcome and treatment regressions and create learner list Q_learner <- sl_lrn g_learner <- sl_lrn_dens learner_list <- list(Y = Q_learner, A = g_learner) ``` -------------------------------- ### Fitting the TML estimator using the tmle3 wrapper function Source: https://tlverse.org/tmle3shift/articles/vimshift.html This code snippet shows the convenient way to fit the series of TML estimators using the `tmle3` wrapper function, which takes the TMLE specification, data, node list, and learner list as arguments. ```R # fit the TML estimator tmle_fit <- tmle3(tmle_spec, data, node_list, learner_list) ``` -------------------------------- ### Fitting a TMLE model with shift interventions Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html This code snippet shows how to fit a TMLE model using the tmle3shift package, including specifying the outcome, treatment, and covariates, and defining the shift intervention. ```r tmle_fit <- tmle3_fit( data = tmle_data, outcome = outcome_node, treatment = treatment_node, confounding = confounding_nodes, intervention = shift_intervention ) ``` -------------------------------- ### Specifying Outcome and Treatment Regressions and Creating a Learner List Source: https://tlverse.org/tmle3shift/articles/vimshift.html This code snippet defines the learners for the outcome regression (Q_learner) and the treatment mechanism regression (g_learner), and then combines them into a list for use in TMLE. ```R Q_learner <- sl_lrn g_learner <- sl_lrn_dens learner_list <- list(Y = Q_learner, A = g_learner) ``` -------------------------------- ### TMLE Specification for MSM Parameter Source: https://tlverse.org/tmle3shift/articles/vimshift.html This R code demonstrates how to initialize a TMLE specification to directly target the parameters of a working marginal structural model using the `tmle_vimshift_msm` Spec. ```r # what's the grid of shifts we wish to consider? delta_grid <- seq(-1, 1, 1) # initialize a tmle specification tmle_msm_spec <- tmle_vimshift_msm(shift_fxn = shift_additive_bounded, shift_fxn_inv = shift_additive_bounded_inv, shift_grid = delta_grid, max_shifted_ratio = 2) # fit the TML estimator and examine the results tmle_msm_fit <- tmle3(tmle_msm_spec, data, node_list, learner_list) ``` -------------------------------- ### Define learners for conditional density regression (propensity score) Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html Configures learners for estimating the propensity score using haldensify and semiparametric density estimation. ```R # learners used for conditional density regression (i.e., propensity score) haldensify_lrnr <- Lrnr_haldensify$new( n_bins = 3, grid_type = "equal_mass", lambda_seq = exp(seq(-1, -9, length = 100)) ) hse_lrnr <- Lrnr_density_semiparametric$new(mean_learner = Lrnr_glm$new()) mvd_lrnr <- Lrnr_density_semiparametric$new(mean_learner = Lrnr_glm$new(), var_learner = Lrnr_mean$new()) sl_lrn_dens <- Lrnr_sl$new( learners = list(haldensify_lrnr, hse_lrnr, mvd_lrnr), metalearner = Lrnr_solnp_density$new() ) ``` -------------------------------- ### Simulate simple data for tmle-shift sketch Source: https://tlverse.org/tmle3shift/articles/vimshift.html Generates a synthetic dataset with baseline covariates (W), a treatment variable (A) dependent on W, and an outcome variable (Y) dependent on A and W, with added random noise. This data is structured for use with the tmle3 package. ```r # simulate simple data for tmle-shift sketch n_obs <- 1000 # number of observations n_w <- 1 # number of baseline covariates tx_mult <- 2 # multiplier for the effect of W = 1 on the treatment # baseline covariates -- simple, binary W <- as.numeric(replicate(n_w, rbinom(n_obs, 1, 0.5))) # create treatment based on baseline W A <- as.numeric(rnorm(n_obs, mean = tx_mult * W, sd = 1)) # create outcome as a linear function of A, W + white noise Y <- A + W + rnorm(n_obs, mean = 0, sd = 0.5) ``` -------------------------------- ### Define Super Learner for outcome regression Source: https://tlverse.org/tmle3shift/articles/vimshift.html This code defines a Super Learner for estimating the outcome regression (Q-function) using a combination of mean, GLM, and xgboost learners, with a non-negative least squares meta-learner. ```R # learners used for conditional expectation regression (e.g., outcome) mean_lrnr <- Lrnr_mean$new() glM_lrnr <- Lrnr_glm$new() xgb_lrnr <- Lrnr_xgboost$new() sl_lrn <- Lrnr_sl$new( learners = list(mean_lrnr, glm_lrnr, xgb_lrnr), metalearner = Lrnr_nnls$new() ) ``` -------------------------------- ### tmle_fit object output Source: https://tlverse.org/tmle3shift/articles/vimshift.html The output of the print method for a tmle_fit object, showing estimated parameters, standard errors, and confidence intervals. ```R ## A tmle3_Fit that took 1 step(s) ## type param init_est tmle_est se lower ## 1: TSM E[Y_{A=NULL}] 0.6246468 0.6374655 0.056849607 0.5260423 ## 2: TSM E[Y_{A=NULL}] 1.5441493 1.5436275 0.059872139 1.4262803 ## 3: TSM E[Y_{A=NULL}] 2.3926234 2.3798978 0.055192276 2.2717229 ## 4: MSM_linear MSM(intercept) 1.5204731 1.5203303 0.056654560 1.4092894 ## 5: MSM_linear MSM(slope) 0.8839883 0.8712162 0.009177361 0.8532289 ## upper psi_transformed lower_transformed upper_transformed ## 1: 0.7488886 0.6374655 0.5260423 0.7488886 ## 2: 1.6609748 1.5436275 1.4262803 1.6609748 ## 3: 2.4880727 2.3798978 2.2717229 2.4880727 ## 4: 1.6313712 1.5203303 1.4092894 1.6313712 ## 5: 0.8892035 0.8712162 0.8532289 0.8892035 ``` -------------------------------- ### Define learners for conditional expectation regression (outcome) Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html Configures learners for estimating the outcome regression using mean, GLM, and xgboost algorithms. ```R # learners used for conditional expectation regression (e.g., outcome) mean_lrnr <- Lrnr_mean$new() glm_lrnr <- Lrnr_glm$new() xgb_lrnr <- Lrnr_xgboost$new() sl_lrn <- Lrnr_sl$new( learners = list(mean_lrnr, glm_lrnr, xgb_lrnr), metalearner = Lrnr_nnls$new() ) ``` -------------------------------- ### Additive Shift Functions Source: https://tlverse.org/tmle3shift/reference/additive_shifting.html Functions for applying additive shifts to continuous-valued interventions without bounds. ```r shift_additive(tmle_task, delta = 0, ...) shift_additive_inv(tmle_task, delta = 0, ...) ``` -------------------------------- ### Fit TMLE model Source: https://tlverse.org/tmle3shift/articles/shift_tmle.html Fits the TMLE model using the specified TMLE specification, data, node list, and learner list. ```R tmle_fit <- tmle3(tmle_spec, data, node_list, learner_list) ``` -------------------------------- ### tmle_vimshift_delta function signature Source: https://tlverse.org/tmle3shift/reference/tmle_vimshift_delta.html The function signature for tmle_vimshift_delta, showing its main arguments. ```R tmle_vimshift_delta( shift_fxn = shift_additive_bounded, shift_fxn_inv = shift_additive_bounded_inv, shift_grid = seq(-1, 1, by = 0.5), max_shifted_ratio = 2, weighting = c("identity", "variance"), ... ) ``` -------------------------------- ### Citation Source: https://tlverse.org/tmle3shift/authors.html The citation details for the tmle3shift R package in BibTeX format. ```bibtex @Manual{ title = {{tmle3shift}: Targeted Learning of the Causal Effects of Stochastic Interventions}, author = {Nima S Hejazi and Jeremy R Coyle and Mark J {van der Laan}}, year = {2021}, note = {R package version 0.2.0}, doi = {10.5281/zenodo.4603372}, url = {https://github.com/tlverse/tmle3shift}, } ``` -------------------------------- ### tmle_vimshift_msm Function Signature Source: https://tlverse.org/tmle3shift/reference/tmle_vimshift_msm.html The function signature for tmle_vimshift_msm, showing its main arguments and their default values. ```R tmle_vimshift_msm( shift_fxn = shift_additive_bounded, shift_fxn_inv = shift_additive_bounded_inv, shift_grid = seq(-1, 1, by = 0.5), max_shifted_ratio = 2, weighting = c("identity", "variance"), ... ) ``` -------------------------------- ### tmle_shift function signature Source: https://tlverse.org/tmle3shift/reference/tmle_shift.html The signature of the tmle_shift function, showing its main arguments. ```R tmle_shift( shift_fxn = shift_additive_bounded, shift_fxn_inv = shift_additive_bounded_inv, shift_val = 1, max_shifted_ratio = 2, ... ) ``` -------------------------------- ### trend_msm function signature Source: https://tlverse.org/tmle3shift/reference/trend_msm.html Function signature for trend_msm. ```R trend_msm( tmle_fit_estimates, delta_grid, level = 0.95, weighting = c("identity", "variance") ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.