diff --git a/NAMESPACE b/NAMESPACE index 145233e..4fae426 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(bpcm) +export(iptw) export(pcbm) export(pcbsm) export(pcm) diff --git a/R/iptw.R b/R/iptw.R new file mode 100644 index 0000000..8ade6ce --- /dev/null +++ b/R/iptw.R @@ -0,0 +1,33 @@ +## File Name: iptw.R +## File version: 1.0 + +#' Compute inverse probability of treatement weights (IPTW) for use with PCM +#' +#' This function computes IPTW weights for causal inference with the pcm function +#' +#' @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 +#' @return A vector of IPT weights +#' @export + +iptw <- function(df=NULL,Y=NULL,X=NULL) { + 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') + } + dff <- df[,c(Y,X)] + if (length(X)==1) { + formu <- paste0(Y,"~",X) + } else { + formu <- paste0(Y,"~",X[1]) + for (k in 2:length(X)) { + xx <- X[k] + formu <- paste0(formu,"+",xx) + } + } + 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)) +} diff --git a/man/iptw.Rd b/man/iptw.Rd new file mode 100644 index 0000000..12557f5 --- /dev/null +++ b/man/iptw.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/iptw.R +\name{iptw} +\alias{iptw} +\title{Compute inverse probability of treatement weights (IPTW) for use with PCM} +\usage{ +iptw(df = NULL, Y = NULL, X = NULL) +} +\arguments{ +\item{df}{data.frame containing the data} + +\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} +} +\value{ +A vector of IPT weights +} +\description{ +This function computes IPTW weights for causal inference with the pcm function +}