From a9f460623db88e9c67408434e64ea37dceb496c3 Mon Sep 17 00:00:00 2001 From: corentinchoisy Date: Mon, 26 May 2025 17:23:24 +0200 Subject: [PATCH] Corrected bug in iptw function --- R/iptw.R | 7 +++++-- man/iptw.Rd | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/R/iptw.R b/R/iptw.R index 2dd0cf7..06c91b0 100644 --- a/R/iptw.R +++ b/R/iptw.R @@ -8,7 +8,7 @@ #' @param df data.frame containing the data #' @param Y string containing the name of the column where the dependent variable is stored in df #' @param X vector of strings containing the names of the columns where the independent variables are stored in df -#' @param target string containing the target causal effect of interest. Either "ate" (average treatment effect, default), "att" (average treatment effect on the treated) or "atu" (average treatment effect on the untreated) +#' @param target string containing the target causal effect of interest. Either "ate" (average treatment effect, default), "stab-ate" (stabilized ATE), "att" (average treatment effect on the treated) or "atu" (average treatment effect on the untreated) #' @return A vector of IPT weights #' @export @@ -19,7 +19,7 @@ iptw <- function(df=NULL,Y=NULL,X=NULL,target="ate") { if (!("id"%in%colnames(df))) { stop('ERROR: no column named id provided') } - if (target !="ate" & target != "att" & target !="atu") + if (target !="ate" & target != "att" & target !="atu" & target !="stab-ate") dff <- df[,c(Y,X)] if (length(X)==1) { formu <- paste0(Y,"~",X) @@ -33,6 +33,9 @@ iptw <- function(df=NULL,Y=NULL,X=NULL,target="ate") { lr_out <- glm(formula = as.formula(formu),data=df,family = binomial(link = 'logit')) if (target == "ate") { psw <- df$TT/fitted(lr_out) + (1-df$TT)/(1-fitted(lr_out)) + } else if (target=="stab-ate") { + pt <- sum(df$TT)/nrow(df) + psw <- pt*(df$TT/fitted(lr_out)) + (1-pt)*(1-df$TT)/(1-fitted(lr_out)) } else if (target=="att") { psw <- fitted(lr_out)/(1-fitted(lr_out)) psw[df[,Y]==1] <- 1 diff --git a/man/iptw.Rd b/man/iptw.Rd index b111456..a118f52 100644 --- a/man/iptw.Rd +++ b/man/iptw.Rd @@ -13,7 +13,7 @@ iptw(df = NULL, Y = NULL, X = NULL, target = "ate") \item{X}{vector of strings containing the names of the columns where the independent variables are stored in df} -\item{target}{string containing the target causal effect of interest. Either "ate" (average treatment effect, default), "att" (average treatment effect on the treated) or "atu" (average treatment effect on the untreated)} +\item{target}{string containing the target causal effect of interest. Either "ate" (average treatment effect, default), "stab-ate" (stabilized ATE), "att" (average treatment effect on the treated) or "atu" (average treatment effect on the untreated)} } \value{ A vector of IPT weights