傾向性評(píng)分匹配是一種控制混雜的技巧班利,本文主要寫操作,具體原理大家自己摸索哈犀变。
propensity score matching (PSM) is a “statistical matching technique that attempts to estimate the effect of a treatment, policy, or other intervention by accounting for the covariates that predict receiving the treatment”.
實(shí)例描述
我現(xiàn)在想要研究某種疾病病人的健康相關(guān)生活質(zhì)量是不是比常人高妹孙,我有病人的數(shù)據(jù),也有常人的數(shù)據(jù)弛作,但是他們無論是人口統(tǒng)計(jì)學(xué)還是患病情況都不可比涕蜂,所以我很難得出疾病是不是會(huì)影響健康相關(guān)生活質(zhì)量,或者說我其實(shí)很難說明兩組人群健康相關(guān)生活質(zhì)量的差異是由疾病引起的映琳。
這個(gè)時(shí)候我們就可以給每個(gè)組計(jì)算其患病的傾向分机隙,把兩組人中傾向分相同的匹配在一起我們就可以說明問題了蜘拉。原理請(qǐng)參見相關(guān)文獻(xiàn)。
所以傾向性評(píng)分解決的問題是協(xié)變量的可比性問題有鹿,為了更好地說明此方法的強(qiáng)大旭旭,我們依然進(jìn)行模擬數(shù)據(jù)的前后比較:
數(shù)據(jù)模擬
我先模擬一個(gè)病人的數(shù)據(jù)集df.patients:
df.patients <- r_data_frame(n = 250,
age(x = 30:78,
name = 'Age'),
sex(x = c("Male", "Female"),
prob = c(0.70, 0.30),
name = "Sex"))
df.patients$Sample <- as.factor('Patients')
df.patients這個(gè)數(shù)據(jù)集中有250個(gè)數(shù)據(jù),有年齡葱跋,性別持寄,兩個(gè)變量。
再模擬普通人群的數(shù)據(jù)集df.population:
df.population <- r_data_frame(n = 1000,
age(x = 18:80,
name = 'Age'),
sex(x = c("Male", "Female"),
prob = c(0.50, 0.50),
name = "Sex"))
df.population$Sample <- as.factor('Population')
df.population這個(gè)數(shù)據(jù)集中有1000個(gè)數(shù)據(jù)娱俺,也有年齡和性別兩個(gè)變量稍味。
然后把數(shù)據(jù)合并為一個(gè)大數(shù)據(jù)集,并且給他加上distress這個(gè)列荠卷,此時(shí)我還沒有進(jìn)行匹配:
mydata <- rbind(df.patients, df.population)
mydata$Group <- as.logical(mydata$Sample == 'Patients')
mydata$Distress <- ifelse(mydata$Sex == 'Male',
age(nrow(mydata), x = 0:42),
age(nrow(mydata), x = 15:42))
此時(shí)我們把兩組未匹配時(shí)的變量做一下比較:
table1 <- CreateTableOne(vars = c('Age', 'Sex', 'Distress'),
data = mydata,
factorVars = 'Sex',
strata = 'Sample')
table1 <- print(table1,
printToggle = FALSE,
noSpaces = TRUE)
kable(table1[,1:3],
format = "simple",
align = 'c',
caption = 'Table 1: 匹配之前先來比一比')
可以發(fā)現(xiàn)模庐,未匹配時(shí)兩組性別是有差異的。
接下來我們對(duì)數(shù)據(jù)進(jìn)行傾向性評(píng)分匹配:
match.it <- matchit(Group ~ Age + Sex, data = mydata, method="nearest", ratio=1)
a <- summary(match.it)
其實(shí)做傾向性匹配的代碼就是上面的第一行油宜,非常簡單掂碱,在這行代碼中matchit函數(shù)默認(rèn)計(jì)算距離的方法就是logit,也就是計(jì)算傾向分時(shí)使用的邏輯回歸慎冤,所以matchit函數(shù)的distance參數(shù)我們不用管:
This argument specifies the method used to estimate the distance measure. The default is logistic regression, "logit". A variety of other methods are available.
運(yùn)行上面的代碼我們就匹配好了疼燥,我們想看看匹配效果:
kable(a$sum.matched[,c(1,2,3)], digits = 2, align = 'c',
caption = 'Table 3: 匹配完了再瞅瞅')
看到?jīng)]我們匹配之后兩組誤差小于0.01哇,很棒蚁堤!
我們甚至還可以畫出來傾向分的分布圖:
plot(match.it, type = 'jitter', interactive = FALSE)
最后一步就是把我們匹配好的數(shù)據(jù)存下來做后續(xù)的分析醉者,因?yàn)槲覀儾∪私M只有250個(gè)人,1:1匹配披诗,那么總的數(shù)據(jù)集就是500人:
df.match <- match.data(match.it)[1:ncol(mydata)]
在匹配好的數(shù)據(jù)中我們?cè)僮鲆淮巫畛醯姆治觯?/p>
table4 <- CreateTableOne(vars = c('Age', 'Sex', 'Distress'),
data = df.match,
factorVars = 'Sex',
strata = 'Sample')
table4 <- print(table4,
printToggle = FALSE,
noSpaces = TRUE)
kable(table4[,1:3],
align = 'c',
caption = 'Table 4: 匹配之后再比比')
看到?jīng)]湃交,之前未匹配時(shí)有顯著差異的性別變量已經(jīng)沒有差異啦,也就是說我們兩組間除過疾病不同其他的協(xié)變量均是可比的藤巢,這么樣,如果兩組之間的健康相關(guān)生活質(zhì)量在匹配后的數(shù)據(jù)集中再有差異我們就可以放心地說這個(gè)差異是由疾病造成的息罗。
小結(jié)
今天給大做了一個(gè)傾向性評(píng)分匹配掂咒,感謝大家耐心看完,自己的文章都寫得很細(xì)迈喉,代碼都在原文中绍刮,希望大家都可以自己做一做,也歡迎大家的意見和建議挨摸。
如果你是一個(gè)大學(xué)本科生或研究生孩革,如果你正在因?yàn)槟愕慕y(tǒng)計(jì)作業(yè)、數(shù)據(jù)分析得运、論文膝蜈、報(bào)告锅移、考試等發(fā)愁,如果你在使用SPSS,R饱搏,Python非剃,Mplus, Excel中遇到任何問題,都可以聯(lián)系我推沸。因?yàn)槲铱梢越o您提供最好的备绽,最詳細(xì)和耐心的數(shù)據(jù)分析服務(wù)。
如果你對(duì)Z檢驗(yàn)鬓催,t檢驗(yàn)肺素,方差分析,多元方差分析宇驾,回歸倍靡,卡方檢驗(yàn),相關(guān)飞苇,多水平模型菌瘫,結(jié)構(gòu)方程模型,中介調(diào)節(jié)等等統(tǒng)計(jì)技巧有任何問題布卡,請(qǐng)私信我雨让,獲取最詳細(xì)和耐心的指導(dǎo)。
If you are a student and you are worried about you statistical #Assignments, #Data #Analysis, #Thesis, #reports, #composing, #Quizzes, Exams.. And if you are facing problem in #SPSS, #R-Programming, #Excel, Mplus, then contact me. Because I could provide you the best services for your Data Analysis.
Are you confused with statistical Techniques like z-test, t-test, ANOVA, MANOVA, Regression, Logistic Regression, Chi-Square, Correlation, Association, SEM, multilevel model, mediation and moderation etc. for your Data Analysis...??
Then Contact Me. I will solve your Problem...
加油吧忿等,打工人栖忠!
往期內(nèi)容:
R數(shù)據(jù)分析:傾向性評(píng)分匹配完整實(shí)例(R實(shí)現(xiàn))