Added IPTW function options

This commit is contained in:
2025-05-26 16:55:38 +02:00
parent d979d08612
commit ce37af5ec4
2 changed files with 17 additions and 3 deletions

View File

@ -8,16 +8,18 @@
#' @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)
#' @return A vector of IPT weights
#' @export
iptw <- function(df=NULL,Y=NULL,X=NULL) {
iptw <- function(df=NULL,Y=NULL,X=NULL,target="ate") {
if (any(!(Y %in% colnames(df)))) {
stop("ERROR: provided Y variable name does not exist in df")
}
if (!("id"%in%colnames(df))) {
stop('ERROR: no column named id provided')
}
if (target !="ate" & target != "att" & type !="atu")
dff <- df[,c(Y,X)]
if (length(X)==1) {
formu <- paste0(Y,"~",X)
@ -29,6 +31,16 @@ iptw <- function(df=NULL,Y=NULL,X=NULL) {
}
}
lr_out <- glm(formula = as.formula(formu),data=df,family = binomial(link = 'logit'))
psw <- df$TT/fitted(lr_out) + (1-df$TT)/(1-fitted(lr_out))
if (target == "ate") {
psw <- df$TT/fitted(lr_out) + (1-df$TT)/(1-fitted(lr_out))
} else if (target=="att") {
psw <- rep(NA,nrow(df))
psw[df[,Y]==1] <- 1
psw[df[,Y]==0] <- fitted(lr_out)/(1-fitted(lr_out))
} else if (target=="atu") {
psw <- rep(NA,nrow(df))
psw[df[,Y]==0] <- 1
psw[df[,Y]==1] <- (1-fitted(lr_out))/(fitted(lr_out))
}
return(psw)
}

View File

@ -4,7 +4,7 @@
\alias{iptw}
\title{Compute inverse probability of treatement weights (IPTW) for use with PCM}
\usage{
iptw(df = NULL, Y = NULL, X = NULL)
iptw(df = NULL, Y = NULL, X = NULL, target = "ate")
}
\arguments{
\item{df}{data.frame containing the data}
@ -12,6 +12,8 @@ iptw(df = NULL, Y = NULL, X = NULL)
\item{Y}{string containing the name of the column where the dependent variable is stored in df}
\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)}
}
\value{
A vector of IPT weights