############################################## # g-formula - példák # ############################################## library(gfoRmula) ## Example 1 ## Estimating the effect of static treatment strategies on risk of a ## failure event id <- 'id' time_points <- 7 time_name <- 't0' covnames <- c('L1', 'L2', 'A') outcome_name <- 'Y' outcome_type <- 'survival' covtypes <- c('binary', 'bounded normal', 'binary') histories <- c(lagged, lagavg) histvars <- list(c('A', 'L1', 'L2'), c('L1', 'L2')) covparams <- list(covmodels = c(L1 ~ lag1_A + lag_cumavg1_L1 + lag_cumavg1_L2 + L3 + t0, L2 ~ lag1_A + L1 + lag_cumavg1_L1 + lag_cumavg1_L2 + L3 + t0, A ~ lag1_A + L1 + L2 + lag_cumavg1_L1 + lag_cumavg1_L2 + L3 + t0)) ymodel <- Y ~ A + L1 + L2 + L3 + lag1_A + lag1_L1 + lag1_L2 + t0 intvars <- list('A', 'A') interventions <- list(list(c(static, rep(0, time_points))), list(c(static, rep(1, time_points)))) int_descript <- c('Never treat', 'Always treat') nsimul <- 10000 View(basicdata_nocomp) gform_basic <- gformula(obs_data = basicdata_nocomp, id = id, time_points = time_points, time_name = time_name, covnames = covnames, outcome_name = outcome_name, outcome_type = outcome_type, covtypes = covtypes, covparams = covparams, ymodel = ymodel, intvars = intvars, interventions = interventions, int_descript = int_descript, histories = histories, histvars = histvars, basecovs = c('L3'), nsimul = nsimul, sim_data_b = F, model_fits = F, boot_diag = T, seed = 1234, nsamples = 5, ci_method="normal") gform_basic summary(gform_basic) str(gform_basic) plot(gform_basic) ## Example 2 ## Estimating the effect of threshold interventions on the mean of a binary ## end of follow-up outcome outcome_type <- 'binary_eof' id <- 'id_num' time_name <- 'time' covnames <- c('cov1', 'cov2', 'treat') outcome_name <- 'outcome' histories <- c(lagged, cumavg) histvars <- list(c('treat', 'cov1', 'cov2'), c('cov1', 'cov2')) covtypes <- c('binary', 'zero-inflated normal', 'normal') covparams <- list(covmodels = c(cov1 ~ lag1_treat + lag1_cov1 + lag1_cov2 + cov3 + time, cov2 ~ lag1_treat + cov1 + lag1_cov1 + lag1_cov2 + cov3 + time, treat ~ lag1_treat + cumavg_cov1 + cumavg_cov2 + cov3 + time)) ymodel <- outcome ~ treat + cov1 + cov2 + lag1_cov1 + lag1_cov2 + cov3 intvars <- list('treat', 'treat') interventions <- list(list(c(static, rep(0, 7))), list(c(threshold, 1, Inf))) int_descript <- c('Never treat', 'Threshold - lower bound 1') nsimul <- 10000 ncores <- parallel::detectCores()-1 View(binary_eofdata) gform_bin_eof <- gformula(obs_data = binary_eofdata, outcome_type = outcome_type, id = id, time_name = time_name, covnames = covnames, outcome_name = outcome_name, covtypes = covtypes, covparams = covparams, ymodel = ymodel, intvars = intvars, interventions = interventions, int_descript = int_descript, histories = histories, histvars = histvars, basecovs = c("cov3"), seed = 1234, parallel = TRUE, nsamples = 0, nsimul = nsimul, sim_data_b = T, model_fits = T, ncores = ncores) gform_bin_eof summary(gform_bin_eof) str(gform_bin_eof) plot(gform_bin_eof) View(gform_bin_eof$sim_data$`Threshold - lower bound 1`) ## Example 3 ## Estimating the effect of treatment strategies on risk of a failure event ## when competing events exist id <- 'id' time_points <- 7 time_name <- 't0' covnames <- c('L1', 'L2', 'A') outcome_name <- 'Y' compevent_name <- 'D' outcome_type <- 'survival' covtypes <- c('binary', 'bounded normal', 'binary') histories <- c(lagged, lagavg) histvars <- list(c('A', 'L1', 'L2'), c('L1', 'L2')) covparams <- list(covlink = c('logit', 'identity', 'logit'), covmodels = c(L1 ~ lag1_A + lag_cumavg1_L1 + lag_cumavg1_L2 + L3 + as.factor(t0), L2 ~ lag1_A + L1 + lag_cumavg1_L1 + lag_cumavg1_L2 + L3 + as.factor(t0), A ~ lag1_A + L1 + L2 + lag_cumavg1_L1 + lag_cumavg1_L2 + L3 + as.factor(t0))) ymodel <- Y ~ A + L1 + L2 + lag1_A + lag1_L1 + lag1_L2 + L3 + as.factor(t0) compevent_model <- D ~ A + L1 + L2 + lag1_A + lag1_L1 + lag1_L2 + L3 + as.factor(t0) intvars <- list('A', 'A') interventions <- list(list(c(static, rep(0, time_points))), list(c(static, rep(1, time_points)))) int_descript <- c('Never treat', 'Always treat') nsimul <- 10000 View(basicdata) gform_basic <- gformula(obs_data = basicdata, id = id, time_points = time_points, time_name = time_name, covnames = covnames, outcome_name = outcome_name, outcome_type = outcome_type, compevent_name = compevent_name, covtypes = covtypes, covparams = covparams, ymodel = ymodel, compevent_model = compevent_model, intvars = intvars, interventions = interventions, int_descript = int_descript, histories = histories, histvars = histvars, basecovs = c('L3'), nsimul = nsimul, seed = 1234, model_fits = T) gform_basic summary(gform_basic) plot(gform_basic) str(gform_basic) ## Example 4 ## Estimating the effect of treatment strategies on the mean of a continuous ## end of follow-up outcome library('Hmisc') id <- 'id' time_name <- 't0' covnames <- c('L1', 'L2', 'A') outcome_name <- 'Y' outcome_type <- 'continuous_eof' covtypes <- c('categorical', 'normal', 'binary') histories <- c(lagged) histvars <- list(c('A', 'L1', 'L2')) covparams <- list(covmodels = c(L1 ~ lag1_A + lag1_L1 + L3 + t0 + rcspline.eval(lag1_L2, knots = c(-1, 0, 1)), L2 ~ lag1_A + L1 + lag1_L1 + lag1_L2 + L3 + t0, A ~ lag1_A + L1 + L2 + lag1_L1 + lag1_L2 + L3 + t0)) ymodel <- Y ~ A + L1 + L2 + lag1_A + lag1_L1 + lag1_L2 + L3 intvars <- list('A', 'A') interventions <- list(list(c(static, rep(0, 7))), list(c(static, rep(1, 7)))) int_descript <- c('Never treat', 'Always treat') nsimul <- 10000 View(continuous_eofdata) gform_cont_eof <- gformula(obs_data = continuous_eofdata, id = id, time_name = time_name, covnames = covnames, outcome_name = outcome_name, outcome_type = outcome_type, covtypes = covtypes, covparams = covparams, ymodel = ymodel, intvars = intvars, interventions = interventions, int_descript = int_descript, histories = histories, histvars = histvars, basecovs = c("L3"), nsimul = nsimul, seed = 1234, model_fits = T) gform_cont_eof summary(gform_cont_eof) str(gform_cont_eof) plot(gform_cont_eof)