Skip to content

Commit

Permalink
Updates 12
Browse files Browse the repository at this point in the history
  • Loading branch information
awamaeva committed Jan 30, 2024
1 parent 13ddf78 commit 0972b6f
Show file tree
Hide file tree
Showing 56 changed files with 877 additions and 1,253 deletions.
Binary file modified .DS_Store
Binary file not shown.
15 changes: 10 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
Package: trajmsm
Type: Package
Title: Marginal Structural Models with Latent Class Growth Analysis of Treatment Trajectories
Version: 0.1.0
Author: Awa Diop, Denis Talbot
Version: 0.1.2
Authors@R: c(
person("Awa", "Diop", email = "awa.diop.2@ulaval.ca", role = c("aut", "cre")),
person("Denis", "Talbot", role = "aut"))
Maintainer: Awa Diop <awa.diop.2@ulaval.ca>
Description: The package trajmsm is based on the paper Marginal Structural Models with Latent Class Growth Analysis of Treatment Trajectories:https://journals.sagepub.com/doi/pdf/10.1177/09622802231202384.
License: GPL-3
Description: Implements marginal structural models combined with latent class growth analysis framework for assessing the causal effect of treatment trajectories. Based on the approach described in "Marginal Structural Models with Latent Class Growth Analysis of Treatment Trajectories" (https://journals.sagepub.com/doi/pdf/10.1177/09622802231202384).
License: GPL (>= 3)
Encoding: UTF-8
Imports:
class,
e1071,
flexmix,
geepack,
ggplot2,
survival
survival,
sandwich
LazyData: true
Suggests:
knitr,
rmarkdown
VignetteBuilder: knitr
RoxygenNote: 7.2.0
URL: https://github.com/awamaeva/R-package-trajmsm
BugReports: https://github.com/awamaeva/R-package-trajmsm/issues
Binary file removed Data/Obsdata.kml.Rdata
Binary file not shown.
Binary file modified Meta/.DS_Store
Binary file not shown.
Binary file removed Meta/vignette.rds
Binary file not shown.
33 changes: 18 additions & 15 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Generated by roxygen2: do not edit by hand

export(IPCW_sw)
export(IPCW_w)
export(IPTW_sw)
export(IPTW_w)
export(IPW)
export(buildtraj)
export(gendatatraj)
export(genobsdata_msm)
export(ggplotraj)
export(longtowide)
export(trajMSM_IPW)
export(trajMSM_gform)
export(trajMSM_pooled_ltmle)
export(widetolong)
export(build_traj)
export(gendata)
export(gformula)
export(ggtraj)
export(inverse_probability_weighting)
export(pltmle)
export(predict_traj)
export(split_data)
export(stabilized_ipcw)
export(stabilized_iptw)
export(trajhrmsm_gform)
export(trajhrmsm_ipw)
export(trajmsm_gform)
export(trajmsm_ipw)
export(trajmsm_pltmle)
export(unstabilized_ipcw)
export(unstabilized_iptw)
import(e1071)
import(flexmix)
import(ggplot2)
importFrom(geepack,geeglm)
import(sandwich)
importFrom(survival,coxph)
11 changes: 6 additions & 5 deletions R/IPW.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
#' @export
#' @author Awa Diop, Denis Talbot
#' @examples
#' Obsdata = gendata(n = 1000, format = "wide",total_followup = 3, seed = 945)
#' obsdata = gendata(n = 1000, format = "wide",total_followup = 3, seed = 945)
#' baseline_var <- c("age","sex")
#' covariates <- list(c("hyper2011", "bmi2011"),c("hyper2012", "bmi2012"),c("hyper2013", "bmi2013"))
#' covariates <- list(c("hyper2011", "bmi2011"),
#' c("hyper2012", "bmi2012"),c("hyper2013", "bmi2013"))
#' treatment_var <- c("statins2011","statins2012","statins2013")
#' stabilized_weights = inverse_probability_weighting(numerator = "stabilized", identifier = "id",
#' covariates = covar, treatment = treatment_var, baseline = baseline_var,
#' total_followup = 3,obsdata = Obsdata)
#' stabilized_weights = inverse_probability_weighting(numerator = "stabilized",
#' identifier = "id", covariates = covariates, treatment = treatment_var,
#' baseline = baseline_var, total_followup = 3,obsdata = obsdata)

inverse_probability_weighting <- function(numerator = c("stabilized", "unstabilized"), identifier,
baseline, covariates, treatment,
Expand Down
4 changes: 2 additions & 2 deletions R/build_traj.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#' @import flexmix
#' @export build_traj
#' @examples
#' obsdata_long = gendata_trajmsm(n = 1000,format = "long", seed = 345)
#' obsdata_long = gendata(n = 1000,format = "long", total_followup = 6, seed = 945)
#' formula = as.formula(cbind(statins, 1 - statins) ~ time)
#' restraj = build_traj(obsdata = obsdata_long, number_traj = 3, formula = Formula, identifier = "id")
#' restraj = build_traj(obsdata = obsdata_long, number_traj = 3, formula = formula, identifier = "id")

build_traj <- function(obsdata, formula, number_traj, identifier, family = "binomial", seed = 945,
control = list(iter.max = 1000, minprior = 0), ...) {
Expand Down
8 changes: 3 additions & 5 deletions R/gendata.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @export gendata
#' @return A data frame with generated trajectories.
#' @examples
#' gendata(n = 100, include_censor = FALSE, format = "wide",total_followup = 3)
#' gendata(n = 100, include_censor = FALSE, format = "wide",total_followup = 3, seed = 945)

gendata<- function(n, include_censor = FALSE, format = c("long", "wide"),start_year = 2011, total_followup, timedep_outcome = FALSE, seed) {
set.seed(seed)
Expand All @@ -21,13 +21,13 @@ gendata<- function(n, include_censor = FALSE, format = c("long", "wide"),start_y
statins <- hyper <- bmi <- censor <- matrix(NA, nrow = n, ncol = total_followup)
bmi[, 1] <- rbinom(n, 1, plogis(0.15 * age + 0.7 * sex))
hyper[, 1] <- rbinom(n, 1, plogis(.15 * age + 0.7 * sex + 0.1 * bmi[, 1]))
statins[, 1] <- rbinom(n, 1, plogis(-.15 + 0.4 * age + 0.25* sex - 0.1 * bmi[, 1] - 0.2*hyper[, 1]))
statins[, 1] <- rbinom(n, 1, plogis(.5 + 0.4 * age + 0.25* sex - 0.1 * bmi[, 1] - 0.2*hyper[, 1]))
censor[, 1] <- rbinom(n, 1, plogis(-2 + 0.2 * age + 0.01 * sex + 0.1 * bmi[, 1] - 0.2*hyper[, 1] - 0.5*statins[, 1]))
# Generate data based on conditions
for (i in 2:total_followup) {
bmi[, i] <- rbinom(n, 1, plogis(0.15 * age + 0.7 * sex - 0.25 * statins[, i-1]))
hyper[, i] <- rbinom(n, 1, plogis(0.15 * age + 0.7 * sex + 0.1 * bmi[, i] - 0.35 * statins[, i-1]))
statins[, i] <- rbinom(n, 1, plogis(-0.15 + 0.1 * age + 0.1 * sex - 0.1 * bmi[, i] - 0.2*hyper[, i] + (seq(0.15,1,length.out = total_followup)[i-1])* statins[, i-1] ))
statins[, i] <- rbinom(n, 1, plogis(.5 + 0.1 * age + 0.1 * sex - 0.1 * bmi[, i] - 0.2*hyper[, i] + (seq(0.15,1,length.out = total_followup)[i-1])* statins[, i-1] ))
if (include_censor) {
censor[, i] <- rbinom(n, 1, plogis(-2 + 0.02 * age + 0.01 * sex - 0.5 * statins[, i] + 0.1 * hyper[, i] + 0.2 * bmi[, i]))
}
Expand Down Expand Up @@ -124,5 +124,3 @@ gendata<- function(n, include_censor = FALSE, format = c("long", "wide"),start_y
return(obsdata)
}

obsdata <- gendata(n = 100, format = "long",timedep_outcome = FALSE, total_followup = 8, seed = 945)
obsdata <- gendata(n = 100, format = "long",timedep_outcome = TRUE, total_followup = 8, seed = 945)
11 changes: 2 additions & 9 deletions R/gformula.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,9 @@
#' @param obsdata observed data in wide format.
#' @returns \item{list_gform_countermeans}{Counterfactual means obtained with g-formula.}
#' @import e1071
#' @export
#' @author Awa Diop, Denis Talbot
#' @examples
#' obsdata_wide = gendata_trajmsm(n = 1000, format = "wide", seed = 945)
#' baseline_var <- c("age","sex")
#' covariates <- list(c("hyper2011", "bmi2011"),c("hyper2012", "bmi2012"),c("hyper2013", "bmi2013"))
#' treatment_var <- c("statins2011","statins2012","statins2013")
#' res_gform = gformula(formula = as.formula(" y ~ statins2011 + statins2012 + statins2013 + hyper2011 + bmi2011 + hyper2012 + bmi2012 +
#' hyper2013 + bmi2013 + age + sex " ), baseline = baseline, covariates = covariates,
#' treatment = treatment, outcome = outcome, ntimes_interval = 3, obsdata = obsdata_wide)
#' res_gform$counter_means


gformula <- function(formula, baseline, covariates, treatment, outcome, ntimes_interval, obsdata) {
nregimes <- 2^ntimes_interval # Number of treatment regimes
Expand Down
3 changes: 2 additions & 1 deletion R/ggtraj.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#' @export ggtraj
#' @examples
#' obsdata_long = gendata(n = 1000, format = "long", total_followup = 12, seed = 945)
#' restraj = build_traj(obsdata = obsdata_long, number_traj = 3, formula = as.formula(cbind(statins, 1 - statins) ~ time), identifier = "id")
#' restraj = build_traj(obsdata = obsdata_long, number_traj = 3,
#' formula = as.formula(cbind(statins, 1 - statins) ~ time), identifier = "id")
#' datapost = restraj$data_post
#' head(datapost)
#' traj_data_long <- merge(obsdata_long, datapost, by = "id")
Expand Down
31 changes: 18 additions & 13 deletions R/pltmle.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @title Counterfactual means for a Pooled LTMLE
#' @description function to estimate counterfactual means for a pooled LTMLE.
#' @name sub_pltmle
#' @name pltmle
#' @param formula specification of the model for the outcome to be fitted.
#' @param identifier name of the column for unique identifiant.
#' @param covariates covariates.
Expand All @@ -13,32 +13,37 @@
#' @returns \item{list_pltmle_countermeans}{Counterfactual means and influence functions with the pooled ltmle.}
#' \item{D}{Influence functions}
#' @import e1071
#' @export
#' @examples
#' Obsdata_long = gendata_trajmsm(n = 2000, format = "long", seed = 945)
#' obsdata_long = gendata(n = 2000, format = "long",total_followup = 3, seed = 945)
#' baseline_var <- c("age","sex")
#' covariates <- list(c("hyper2011", "bmi2011"),c("hyper2012", "bmi2012"),c("hyper2013", "bmi2013"))
#' covariates <- list(c("hyper2011", "bmi2011"),
#' c("hyper2012", "bmi2012"),c("hyper2013", "bmi2013"))
#' treatment_var <- c("statins2011","statins2012","statins2013")
#' time_values <- c(2011,2012,2013)
#' formulaA = as.formula(cbind(statins, 1 - statins) ~ time)
#' restraj = build_traj(obsdata = Obsdata_long, number_traj = 3, formula = formulaA, identifier = "id")
#' restraj = build_traj(obsdata = obsdata_long, number_traj = 3,
#' formula = formulaA, identifier = "id")
#' datapost = restraj$data_post
#' trajmsm_long <- merge(Obsdata_long, datapost, by = "id")
#' trajmsm_long <- merge(obsdata_long, datapost, by = "id")
#' AggFormula <- as.formula(paste("statins", "~", "time", "+", "class"))
#' AggTrajData <- aggregate(AggFormula, data = trajmsm_long, FUN = mean)
#' AggTrajData
#' trajmsm_long[ , "traj_group"] <- trajmsm_long[ , "class"]
#' trajmsm_wide = reshape(trajmsm_long, direction = "wide", idvar = "id",
#' v.names = c("statins","bmi","hyper"), timevar = "time", sep ="")
#' formulaY = as.formula(" y ~ statins2011 + statins2012 + statins2013 + hyper2011 + bmi2011 + hyper2012 + bmi2012 +
#' hyper2013 + bmi2013 + age + sex ")
#' formula = as.formula(" y ~ statins2011 + statins2012 + statins2013 +
#' hyper2011 + bmi2011 + hyper2012 + bmi2012 +
#' hyper2013 + bmi2013 + age + sex ")
#' class = factor(predict_traj(identifier = "id", total_followup = 3,
#' treatment = "statins", time = "time", time_values = time_values,
#' trajmodel = restraj$traj_model)$post_class);
#' traj_indic=t(sapply(1:nregimes,function(x)sapply(1:number_traj,function(i) ifelse(class[x]==i,1,0))))
#' traj_indic[,1]=1
#' res_pltmle = pltmle(formula = formulaY, outcome = outcome,treatment = treatment_var,
#' covariates = covar, baseline = baseline_var, ntimes_interval = 3, number_traj = 3,
#' time = "Time",time_values = time_values,identifier = "id",obsdata = trajmsm_wide,traj=traj_indic, treshold = 0.99)
#' traj=t(sapply(1:8,function(x)sapply(1:3,function(i)ifelse(class[x]==i,1,0))))
#' traj[,1]=1
#' res_pltmle = pltmle(formula = formula, outcome = "y",treatment = treatment_var,
#' covariates = covariates, baseline = baseline_var, ntimes_interval = 3, number_traj = 3,
#' time = "time",time_values = time_values,identifier = "id",obsdata = trajmsm_wide,
#' traj=traj, treshold = 0.99)
#' res_pltmle$counter_means
#' @author Awa Diop, Denis Talbot

Expand Down Expand Up @@ -83,7 +88,7 @@ pltmle <- function(formula, outcome, treatment, covariates, baseline, ntimes_int
Weights = inverse_probability_weighting(identifier = identifier, covariates = covariates,
treatment = treatment, baseline = baseline,
total_followup = total_followup, numerator = "unstabilized",
include_censor = include_censor, censor = censor,obsdata = obsdata)[[1]];
include_censor = FALSE, censor = censor,obsdata = obsdata)[[1]];

weights_trunc <- sapply(1:ntimes_interval, function(x){
weights <- ifelse(quantile(Weights[, x], treshold, na.rm = TRUE)> Weights[, x], quantile(Weights[, x], treshold, na.rm = TRUE), Weights[, x])
Expand Down
153 changes: 0 additions & 153 deletions R/pltmle_countermeans2.R

This file was deleted.

Loading

0 comments on commit 0972b6f

Please sign in to comment.