I’m working with Propensity Score weighting, specifically inverse probability of treatment weighting (IPW), and have been exploring the PSweight package in R. Currently, I’m trying to understand how the weights for IPW are calculated using the SumStat()-function.
To clarify my question, I’ve prepared a small example dataset to illustrate the challenges I’m facing:
To be more concrete I brought a small example data to demonstrate my struggles:
# Set seed for reproducibility
set.seed(42)
# Step 1: Simulate sample size and covariates
n <- 1000 # Sample size
X1 <- rnorm(n, mean = 0, sd = 1) # Covariate 1
X2 <- rnorm(n, mean = 0, sd = 1) # Covariate 2
# Step 2: Generate treatment assignment based on covariates
logit_p <- -1 + 0.5 * X1 + 0.3 * X2 # Linear predictor
p <- exp(logit_p) / (1 + exp(logit_p)) # Propensity scores
trt <- rbinom(n, 1, p) # Treatment assignment (binary: 0 or 1)
# Step 3: Simulate outcomes
Y <- 2 + 1.5 * trt + 0.7 * X1 + 0.4 * X2 + rnorm(n, mean = 0, sd = 1)
# Step 4: Data frame
data <- data.frame(x1 = X1,
x2 = X2,
Y = Y,
trt = trt)
# Step 5: Use SumStat()-function
g <- SumStat(
ps.formula = trt ~ X1 + X2,
data = data,
weight = "IPW")
# Step 6: extract estimated propensity score and weights from g and use cbind()
weight <- g[["ps.weights"]] # IPW calculated by function
ps <- g[["propensity"]][, 2] # propensity score P(Z=1|X1,X2), probability for being treated
data <- cbind(data, weight, ps) # add weights and PS to data frame
Now my theoretical understanding for these weights was that they are calculated for individuals of group 1 (trt = 1) by W_1 = 1/PS and for individuals beloying to group 0 (trt= 0) by W_0 = 1/(1-PS) in order to estimate the ATE.
However, the data does not show such behaviour:
In my example, the first individual was assigned to group 0. Theoretically, the IPW should be calculated as:
IPW = 1/(1-ps)=1/(1-0.6364669)= 2.750781. When standardized (divided by 1,000), this becomes
0.002750781. However, the observed value in the dataset is slightly different: IPW=0.002750136.
Similarly, for individual 2, who belongs to group 1, the theoretical calculation is: IPW = 1/ps = 1/0.2608333 = 3.833866 (standardized: 0.003833866), however we observe a value of IPW = 0.003853106.
I’m trying to understand where these discrepancies originate. Could anyone provide insights or explanations?
Kind regards