# Calculate Inverse Probability Weights for Kaplan-Meier survival curves in R

I am analysing HR data where event is leaving (so right-censored and many more survivors than not). My Kaplan-Meier survival curves all look like this (and many of them wilder, so Cox is not an option):

data("Melanoma")
fit <- survfit(Surv(time, ulcer) ~ status, data = Melanoma)
ggsurvplot(fit)


I am aware from the literature that I should apply Inverse Probability Weighting to the data. Answers here and on other websites mostly point to an R package called IPWsurvival - which, however, no longer exists. Could someone please help me as to how to calculate the IPWs for data that produces KM curves like in my example?

Edit: I am not asking for someone to point me to another package, so this is not a software package question. I would like someone to help with calculating IPWs for data like in the example. I am sure it can be done in base R - I just don't know how. Thank you.

• This is not a software specific question. The answer is not (necessarily) another package. I would like someone to tell me how to calculate IPWs. Because that package no longer exists, so there is no "automated" way. Feb 18, 2023 at 16:32

Depending on your goal in modeling, don't underestimate the ability of a Cox or other survival regression to handle "wild" unadjusted survival curves. Your example doesn't seem that "wild" to me. The approximately parallel shift in survival curves along the time axis (based on "Melanoma" data from the R MASS package) might indicate an accelerated failure time model in preference to a proportional hazards model, but for many purposes regression models can work very well.

For attempts at causal inference in observational studies, however, you do need to account properly for the covariates whose values differ between treatment groups. Inverse Probability Weighting is a way to do that. Austin and Stuart discuss this for binary treatments. @Noah outlines how to extend this to multiple treatments, with a reference for further study.

In outline, inverse probability weighting is as simple as the name sounds. Say that you are comparing treatment versus control in an observational study. You develop a model of the probability of having received the treatment as a function of the covariates for which you want to control. Use your favorite binary model for this, say logistic regression. Then calculate weights as follows (Austin and Stuart, Section 2.2):

Each subject’s weight is equal to the inverse of the probability of receiving the treatment that the subject received.

Plug those values into the weights argument of the survfit() function. That way, you have re-weighted your sample in a way that tries to get two treatment groups with similar covariate distributions. That helps estimate the "average treatment effect" (ATE) that would be seen if all received the treatment. Different weights described by Austin and Stuart can help estimate the "average treatment effect on the treated" (ATT).

That might be taken as an answer to your question. Yes, you can do all that in base R.

But you probably shouldn't.

@Noah explains, in a long answer that deserves detailed study:

A problem with this is that there is almost no hope in correctly modeling the treatment process to obtain propensity scores, and some evidence that even correctly modeling it parametrically is inefficient.

More advanced modeling of probabilities than logistic regression, or alternate ways of achieving covariate balance, are preferred. There are well-vetted packages that can accomplish this. The WeightIt package in R provides an interface to best practices.

For this type of work, take advantage of the accumulated understanding developed over years of study and argument among practitioners. "Don't try this at home" unless you have an experienced guide or have engaged in extensive study.

• Thank you very much for this, @EdM, I appreciate you taking the time to respond. I'll read the material you've linked in and try to proceed with caution. Feb 20, 2023 at 21:43