Justin Nedzesky
Justin Nedzesky

Reputation: 1

R propensity score matching with additional criteria

I have a cohort that looks something like this:

id exposure sex age time
 1        0   F  50  300
 2        0   F  55  320
 3        0   F  60  365
 4        0   M  60  335
 5        0   M  65  350
 6        0   M  70  365
 7        1   F  50  310
 8        1   F  50  325
 9        1   M  65  340
10        1   M  70  345

I need to create separate matched groups based on exposure (dependent variable), matching on covariates age and sex (independent variables). BUT I must also impose that for each match, time is greater for the 'exposed' (exposure == 1) observation vs the 'unexposed' (exposure == 0) match.

I was planning to use the MatchIt command for my propensity score match, but I don't think there's a way to add criteria requiring time greater for exposed vs unexposed.

I appreciate any suggestions!

Upvotes: 0

Views: 149

Answers (1)

Noah
Noah

Reputation: 4424

There isn't a built-in way to do this in MatchIt, but matchit() allows you to supply any distance matrix you want, with forbidden matches marked with Inf. So we we can do is create a distance matrix, then for each exposed unit, set the distance to each control unit to Inf if the treated unit's time is less than or equal to the control unit's time. Here's how this would look:

# Estimate a propensity score
df$ps <- glm(exposure ~ sex * age * time, data = df,
          family = binomial)$fitted

# Create distance matrix of ps, with treated units
# on rows and control units on columns
ps_dist <- MatchIt::euclidean_dist(exposure ~ ps, data = df)

# Set to `Inf` any forbidden matches
exposed_times <- df$time[[df$exposure == 1]]
unexposed_times <- df$time[[df$exposure == 0]]
ps_dist[outer(exposed_times, unexposed_times, "<=")] <- Inf

# Supply the distance matrix to `matchit()`
m <- MatchIt::matchit(exposure ~ sex + age + time, data = df,
                      distance = ps_dist)

Upvotes: 0

Related Questions