[R]{MatchIt} 傾向スコアマッチングを実施する方法

{MatchIt}はRで傾向スコアマッチング(プロペンシティスコアマッチング)を実行するためのパッケージである。

A Step-by-Step Guide to Propensity Score Matching in Rというチュートリアルがよくできており、これだけ読めばとりあえず解析できる。教育学の研究例を元にRでの傾向スコアマッチングのやり方を詳しく説明してくれている。

以下に上記論文の再利用性を高めるべくポイントをまとめる。

(1) 実際にRで実行するための最小限のソースのまとめ

(2) 傾向スコアマッチングで選択可能なアルゴリズムの説明

ソースまとめ

ライブラリのインストール

install.packages("MatchIt",dependencies=T)

ライブラリのロード

library(MatchIt)

解析対象のCSVデータを読み込む

mydata <- read.csv ("C:/r/newyork.csv")
 # 研究データファイル(例:"C:/r/newyork.csv")をRに読み込む
attach(mydata) # 論文の説明のため。必須ではない。
mydata[1:10,]  # 論文の説明のため。必須ではない。

マッチングを実行する

# マッチングを実行する
 # 第1引数: グルーピング変数  ~ マッチング変数1 + マッチング変数 2 + ...の形式で記述
 # 第2引数(data): 使用するデータセット名を指定
 # 第3引数(method): マッチングに使用する手法(アルゴリズム)を指定(nearestの他、exact, subclass, optimal, genetic, cemが指定可能。一部の手法は別途パッケージの導入が必要)
 # 第4引数(ratio) : ケース1症例に対してコントロール何症例をマッチングさせるかを指定する。通常1-5を指定する。
 # 以下の第1引数は論文のサンプルデータの列名である
m.out = matchit(stw ~ tot + min + dis, data = mydata, method = "nearest", ratio = 1)

# マッチング結果を出力する。
# マッチング前後でケースとコントロールのMean Diffが減少していることを確認する。 
summary(m.out)

# マッチング結果をjitter plotする
plot(m.out, type = "hist")

# マッチング結果のhistgramを作成する
plot(m.out, type = "hist")

マッチング結果を出力する

# マッチング終了後のデータセットを作成する(=マッチング対象外となったデータ・レコードを削除する)
m.data1 <- match.data(m.out)

# マッチング結果をCSVファイルに出力する。上記では出力ファイル名が「C:/r/newyork_nearest100.csv」
write.csv(m.data1, file = "C:/r/newyork_nearest100.csv")

マッチング手法(アルゴリズム)まとめ

アルゴリズム内容説明
Exact MatchingThis treated unit with a control unit that has exactly the same values on each covariate. When there are many covariates and/or covariates that can take a large range of values, exact matching may not be possible (method = “exact”). 実際に使用することはほとんどないと思われる。
SubclassificationThis technique breaks the data set into subclasses such that the distributions of the covariates are similar in each subclass (method = “subclass”).
Nearest NeighborThis technique matches a treated unit to a control in terms of a distance measure such as a logit (method = “nearest”).
Optimal MatchingThis technique focuses onminimizing the average absolute distance across all matched pairs (method = “optimal”). This method of matching require optmatch package.
Genetic Matchingcomputationally intensive genetic search algorithm to match treatment and control units (method = “genetic”). It requires the Matching package.
Coarsened Exact Matchingtechnique matches on a covariate while maintaining the balance of other covariates. It is claimed to work “well for multicategory treatments, determining blocks in experimental designs, and evaluating extreme counterfactuals” (Ho, Kosuke, King, & Stuart, 2011, p.12) (method = “cem”). 介入手法(例えば治療法)が複数ある場合などに有用とのこと。

コメント