倾向性评分匹配完整实例(R实现)
倾向性评分匹配(propensity score matching, PSM)主要是在随机对照试验(Randomized controlled trials,RCT)中用于衡量treat组和control组样本的其他各项特征(如年龄、体重、身高、人种等)的整体均衡性的度量。比如说研究一种药物对疾病的影响,在临床实验中,treat组和control组除了使用药物(安慰剂)不同外,其他的临床特征(如年龄、体重等)都应该基本是相似的,这样treat和control组才有可比性,进而才能验证药物的有效性。
如下图所示,该治疗方法实际上是无效的,但是由于分组中年龄的不平衡导致得出错误的结论。
对于衡量同一特征的组间差异或者距离,我们通常使用标准均值误差
(Standardized mean difference,SMD,PMC3144483)。
对于连续性
特征变量公式如下:
对于
离散型
特征变量,公式如下:下面我们使用
tableone
包来统计每个变量的标准均值误差SMD,数据来源是右心导管插入(right heart catheterization, RHC)数据,treat组是"RHC",control是"No RHC"(变量 swang1)
library(tableone)
## PS matching
library(Matching)
## Weighted analysis
library(survey)
library(reshape2)
library(ggplot2)
## Right heart cath dataset
rhc <- read.csv("http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/rhc.csv")
# 待统计的协变量:
vars <- c("age","sex","race","edu","income","ninsclas","cat1","das2d3pc","dnr1",
"ca","surv2md1","aps1","scoma1","wtkilo1","temp1","meanbp1","resp1",
"hrt1","pafi1","paco21","ph1","wblc1","hema1","sod1","pot1","crea1",
"bili1","alb1","resp","card","neuro","gastr","renal","meta","hema",
"seps","trauma","ortho","cardiohx","chfhx","dementhx","psychhx",
"chrpulhx","renalhx","liverhx","gibledhx","malighx","immunhx",
"transhx","amihx")
## Construct a table
tabUnmatched <- CreateTableOne(vars = vars, strata = "swang1", data = rhc, test = FALSE)
## Show table with SMD
print(tabUnmatched, smd = TRUE)
下图是统计结果,第一列是协变量,第二列是按照有无RHC(treat、contol组)各变量的统计值(mean和SD),最后一列是SMD,可以看出treat和control组的age和sex差异都较小(<10%),income较高(>10%)。
接下来计算通过logit回归计算每个样本的倾向性分数(Propensity Score, PS),也就是被分配为RHC的概率.
## Fit model
psModel <- glm(formula = swang1 ~ age + sex + race + edu + income + ninsclas +
cat1 + das2d3pc + dnr1 + ca + surv2md1 + aps1 + scoma1 +
wtkilo1 + temp1 + meanbp1 + resp1 + hrt1 + pafi1 +
paco21 + ph1 + wblc1 + hema1 + sod1 + pot1 + crea1 +
bili1 + alb1 + resp + card + neuro + gastr + renal +
meta + hema + seps + trauma + ortho + cardiohx + chfhx +
dementhx + psychhx + chrpulhx + renalhx + liverhx + gibledhx +
malighx + immunhx + transhx + amihx,
family = binomial(link = "logit"),
data = rhc)
## Predicted probability of being assigned to RHC
rhc$pRhc <- predict(psModel, type = "response")
## Predicted probability of being assigned to no RHC
rhc$pNoRhc <- 1 - rhc$pRhc
## Predicted probability of being assigned to the treatment actually assigned (either RHC or no RHC)
rhc$pAssign <- NA
rhc$pAssign[rhc$swang1 == 1] <- rhc$pRhc[rhc$swang1 == 1]
rhc$pAssign[rhc$swang1 == 0] <- rhc$pNoRhc[rhc$swang1 == 0]
## Smaller of pRhc vs pNoRhc for matching weight
rhc$pMin <- pmin(rhc$pRhc, rhc$pNoRhc)
然后使用Matching
包进行匹配一致性样本(1:1匹配)。
listMatch <- Match(Tr = (rhc$swang1 == 1), # Need to be in 0,1
## logit of PS,i.e., log(PS/(1-PS)) as matching scale
X = log(rhc$pRhc / rhc$pNoRhc),
## 1:1 matching
M = 1,
## caliper = 0.2 * SD(logit(PS))
caliper = 0.2,
replace = FALSE,
ties = TRUE,
version = "fast")
# determining if balance exists in any unmatched dataset and in matched datasets
mb <- MatchBalance(psModel$formula, data=rhc, match.out=listMatch, nboots=50)
将匹配的样本提取出来:
rhcMatched <- rhc[unlist(listMatch[c("index.treated","index.control")]), ]
再看下现在匹配后的SMD,现在所有变量的SMD都小于10%了。
## Construct a table
tabMatched <- CreateTableOne(vars = vars, strata = "swang1", data = rhcMatched, test = FALSE)
## Show table with SMD
print(tabMatched, smd = TRUE)
然后给样本进行加权,使得各组中的倾向性评分基本一致,进而消除混杂因素,作为
标准平衡数据
参考。一般有两种加权方法:逆概率处理加权法
(the inverse probability of treatment weighting,IPTW)和标准化死亡比加权法
(the standardized mortality ratio weighting,SMRW),本次我们是有IPTW的进阶版(PMID:26238958):
## Matching weight
rhc$mw <- rhc$pMin / rhc$pAssign
# IPTW:
rhc$mw1=ifelse(rhc$swang1==1,1/(rhc$pRhc),1/(1-rhc$pRhc))
## Weighted data
rhcSvy <- svydesign(ids = ~ 1, data = rhc, weights = ~ mw)
## Construct a table (This is a bit slow.)
tabWeighted <- svyCreateTableOne(vars = vars, strata = "swang1", data = rhcSvy, test = FALSE)
## Show table with SMD
print(tabWeighted, smd = TRUE)
加权后变量组间差异(很小):
进行作图比较 Unmatched、Mathced和Weighted结果:
library(data.table)
## Construct a data frame containing variable name and SMD from all methods
dataPlot <- data.table(variable = rownames(ExtractSmd(tabUnmatched)),
Unmatched = ExtractSmd(tabUnmatched),
Matched = ExtractSmd(tabMatched),
Weighted = ExtractSmd(tabWeighted))
colnames(dataPlot) <- c("variable","Unmatched","Matched","Weighted")
## Create long-format data for ggplot2
dataPlotMelt <- melt(data = dataPlot,
id.vars = c("variable"),
variable.name = "Method",
value.name = "SMD")
## Order variable names by magnitude of SMD
varNames <- as.character(dataPlot$variable)[order(dataPlot$Unmatched)]
## Order factor levels in the same order
dataPlotMelt$variable <- factor(dataPlotMelt$variable,
levels = varNames)
## Plot using ggplot2
ggplot(data = dataPlotMelt, mapping = aes(x = variable, y = SMD,
group = Method, color = Method)) +
geom_line() +
geom_point() +
geom_hline(yintercept = 0.1, color = "black", size = 0.1) +
coord_flip() +
theme_bw() + theme(legend.key = element_blank())
可以看出加权后的"标准数据"和我们PSM后的结果基本是一致的。最后还看看右心导管插不插对于生存是否有影响,使用
ShowRegTable
函数计算风险比(hazard ratio,HR)[95% CI)]和pvalue。
## Unmatched model (unadjsuted)
glmUnmatched <- glm(formula = (death == "Yes") ~ swang1,
family = binomial(link = "logit"),
data = rhc)
## Matched model
glmMatched <- glm(formula = (death == "Yes") ~ swang1,
family = binomial(link = "logit"),
data = rhcMatched)
## Weighted model
glmWeighted <- svyglm(formula = (death == "Yes") ~ swang1,
family = binomial(link = "logit"),
design = rhcSvy)
## Show results together
resTogether <- list(Unmatched = ShowRegTable(glmUnmatched, printToggle = FALSE),
Matched = ShowRegTable(glmMatched, printToggle = FALSE),
Weighted = ShowRegTable(glmWeighted, printToggle = FALSE))
print(resTogether, quote = FALSE)
。
参考:
https://cran.r-project.org/web/packages/tableone/vignettes/smd.html
https://www.mediecogroup.com/method_topic_article_detail/131/
更多原创精彩视频敬请关注生信杂谈: