Package {ipwCoxCSV}


Type: Package
Title: Corrected Sandwich Inference for Inverse Probability Weighted Cox Models
Version: 1.1
Date: 2026-08-01
Maintainer: Di Shu <shudi1991@gmail.com>
Description: An implementation of the corrected sandwich variance (CSV) method for inverse probability weighted (IPW) Cox models described in Shu et al (2021) <doi:10.1111/biom.13332>. The method accounts for the uncertainty in estimating propensity score weights to improve variance and confidence interval estimation for adjusted marginal hazard ratios (HRs) in observational and randomized studies. The package supports estimation of the average treatment effect (ATE) using conventional and stabilized ATE weights, and the average treatment effect in the treated (ATT) using ATT weights, for both independent and clustered data. Propensity scores are estimated using logistic regression.
License: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]
Encoding: UTF-8
Imports: survival, stats
NeedsCompilation: no
Config/roxygen2/version: 8.0.0
Packaged: 2026-08-01 20:36:55 UTC; dishu
Author: Di Shu [aut, cre], Rui Wang [aut]
Repository: CRAN
Date/Publication: 2026-08-02 18:30:09 UTC

Inference for Marginal Hazard Ratios (HRs) in Inverse Probability Weighted (IPW) Cox Models Using Corrected Sandwich Variance (CSV)

Description

This package implements the corrected sandwich variance (CSV) method for inverse probability weighted (IPW) Cox models described in Shu et al. (2021) <doi:10.1111/biom.13332>. The method accounts for the uncertainty in estimating propensity score weights, thereby improving variance estimation and confidence interval estimation for IPW-adjusted marginal hazard ratios (HRs) in observational and randomized studies. The package supports estimation of the average treatment effect (ATE) using conventional and stabilized ATE weights, and the average treatment effect in the treated (ATT) using ATT weights, for both independent and clustered data. Propensity scores are estimated using logistic regression.

Details

The ipwCoxCSV package implements the corrected sandwich variance method proposed by Shu et al. (2021) <doi:10.1111/biom.13332>, which provides statistically consistent variance estimates for adjusted marginal hazard ratios (HRs) when fitting inverse probability weighted (IPW) Cox models in observational and randomized studies. By accounting for the uncertainty associated with estimating propensity score weights through stacked estimating equations, the method improves the accuracy and efficiency of variance estimation and confidence interval construction. The function ipwCoxInd implements the corrected sandwich variance estimation method for the standard setting, assuming independence among observations (rows). The function ipwCoxCluster implements the corrected sandwich variance estimation method for clustered data, such as repeated measurements within individuals (i.e., multiple rows of observations for the same individual).

Author(s)

Di Shu and Rui Wang

Maintainer: Di Shu <shudi1991@gmail.com>

References

Shu D, Young JG, Toh S, Wang R (2021). Variance estimation in inverse probability weighted Cox models. Biometrics <doi:10.1111/biom.13332>


Corrected sandwich inference for the marginal HR in the IPW Cox model for clustered data

Description

Inference for the marginal hazard ratio (HR) in the inverse probability weighted (IPW) Cox model for clustered data, such as repeated measurements within individuals (i.e., multiple rows of observations for the same individual). Corrected sandwich variance (CSV) estimation method is used to estimate the variance of the estimated log HR and to construct confidence intervals for the HR.

Usage

ipwCoxCluster(
  data,
  indID,
  indA,
  indX,
  indStatus,
  indTime,
  weightType = "ATE",
  ties = "breslow",
  confidence = 0.95
)

Arguments

data

A clustered data set to be analyzed in the form of R data frame.

indID

A column name indicating the cluster variable. Observations from the same cluster (e.g., multiple rows of observations for the same individual) have the same indID value.

indA

A column name indicating the treatment variable (1 = treated, 0 = untreated).

indX

A vector of column names specifying the covariates to be included in the propensity score model. Any covariate with the same value across all observations will be excluded, as it does not contribute to propensity score estimation.

indStatus

A column name indicating the non-censoring status (1 = event, 0 = censored).

indTime

A column name indicating the outcome variable, i.e., min(true event time, censoring time).

weightType

A character string specifying the type of weight ("ATE" when the estimand is the average treatment effect, or "ATT" when the estimand is the average treatment effect in the treated); the default is "ATE".

ties

A character string specifying the method ("efron","breslow",or "exact") used to handle tied event times for point estimation; the default is "breslow". For variance estimation, the Breslow method is used to handle tied event times.

confidence

A confidence level between 0 and 1; the default is 0.95 corresponding to a 95 per cent confidence interval.

Value

A matrix of inference results from the inverse probability weighted Cox model for clustered data. The columns report the estimated log hazard ratio, the corrected sandwich standard error of the log hazard ratio, the estimated hazard ratio, and the confidence interval for the hazard ratio computed using the corrected sandwich variance, respectively. Each row corresponds to a weight type.

Examples

#simulate a clustered data set with a marginal hazard ratio of 1.5
set.seed(100)
n=300
oneGene<-function(id){
 tau=0.7
 alpha=(1/tau-1)/2
 u1=runif(1)
 u2=runif(1)
 af=(1-u2)^(-1/alpha)
 T0v1=-log(1-u2)
 T0v2=alpha*log((1-af)+af*((1-u1)^(-1/(1+alpha))))
 T0=c(T0v1,T0v2)
 ZZZ=rep(rnorm(1),2)
 X1before= 0.5*(T0+0.2)/(T0+1)+0.3*ZZZ
 X1=mean(X1before)
 X2= 1/log(1.3*T0+3)-0.3*ZZZ
 X3= rbinom(2,1,0.3+0.5/(T0+1))  
 A=rbinom(2,1,1/(1+exp(1-2*X1-X2-X3)))
 Ttime <- T0*exp(-log(1.5)*A)
 rateC=0.5
 C <- rexp(2, rate=rateC)
 time <- pmin(Ttime, C)
 status <- as.numeric(Ttime <= C)
 cbind(id=id,time=time,status=status,A=A,X1=X1,X2=X2,X3=X3)
} 
da=as.data.frame(do.call("rbind",lapply(1:n,oneGene)))
head(da)
#corrected sandwich variance-based inference results for the marginal hazard ratio 
ipwCoxCluster(data=da,indID="id",indA="A",indX=c("X1","X2","X3"),indStatus="status",indTime="time")


Corrected sandwich inference for the marginal HR in the IPW Cox model for the standard setting of independent data

Description

Inference for the marginal hazard ratio (HR) in the inverse probability weighted (IPW) Cox model for the standard setting, assuming independence among observations (rows). Corrected sandwich variance (CSV) estimation method is used to estimate the variance of the estimated log HR and to construct confidence intervals for the HR.

Usage

ipwCoxInd(
  data,
  indA,
  indX,
  indStatus,
  indTime,
  weightType = "ATE",
  ties = "breslow",
  confidence = 0.95
)

Arguments

data

A data set to be analyzed in the form of R data frame.

indA

A column name indicating the treatment variable (1 = treated, 0 = untreated).

indX

A vector of column names specifying the covariates to be included in the propensity score model. Any covariate with the same value across all observations will be excluded, as it does not contribute to propensity score estimation.

indStatus

A column name indicating the non-censoring status (1 = event, 0 = censored).

indTime

A column name indicating the outcome variable, i.e., min(true event time, censoring time).

weightType

A character string specifying the type of weight ("ATE" when the estimand is the average treatment effect, or "ATT" when the estimand is the average treatment effect in the treated); the default is "ATE".

ties

A character string specifying the method ("efron","breslow",or "exact") used to handle tied event times for point estimation; the default is "breslow". For variance estimation, the Breslow method is used to handle tied event times.

confidence

A confidence level between 0 and 1; the default is 0.95 corresponding to a 95 per cent confidence interval.

Value

A matrix of inference results from the inverse probability weighted Cox model for independent data. The columns report the estimated log hazard ratio, the corrected sandwich standard error of the log hazard ratio, the estimated hazard ratio, and the confidence interval for the hazard ratio computed using the corrected sandwich variance, respectively. Each row corresponds to a weight type.

Examples

#simulate a data set with a marginal hazard ratio of 1.5
set.seed(100)
n=700
T0=rexp(n, rate=0.01)
ZZZ=rnorm(n)
X1= 0.5*(T0+0.2)/(T0+1)+0.3*ZZZ
X2= 1/log(1.3*T0+3)-0.3*ZZZ
X3= rbinom(n,1,0.3+0.5/(T0+1))  
A=rbinom(n,1,1/(1+exp(0.53+X1-X2-X3)))
Ttime <- T0*exp(-log(1.5)*A)
rateC=0.0005
C <- rexp(n, rate=rateC)
time <- pmin(Ttime, C)
status <- as.numeric(Ttime <= C)
da=data.frame(id=1:n,time=time,status=status,A=A,X1=X1,X2=X2,X3=X3)
head(da)
#corrected sandwich variance-based inference results for the marginal hazard ratio 
ipwCoxInd(data=da,indA="A",indX=c("X1","X2","X3"),indStatus="status",indTime="time")