匹配。使用 MatchIt 和 Matching 进行数据模拟和估计。如何找回真实模型?
Matching. Data simulation and estimation with MatchIt and Matching. How to retrieve true model?
我正在尝试模拟 matching 与回归 (OLS) 的敏感性,但我在某处做错了,因为我无法使用 matching
检索真实模型。
我正在生成 3 个变量:x
,一个背景特征,d
是治疗变量(二进制)和 y
结果。 d
与 x
关联。匹配的想法是,一旦以 x
为条件,治疗分配生成过程就像随机的一样好。在回归世界中,x
只是一个控制变量。我想测试当数据中存在非共同支持区域(未处理高于或低于特定值)时回归如何执行。
library(tidyverse)
library(Matching)
library(MatchIt)
N = 1000
# generate random variable normality dist #
x = rnorm(N, 0, 5)
这就是我在 x
和 d
(二进制)之间生成关联的方式。
# generate Treatement associated with x, with different probailities after a certain threshold #
d = ifelse(x > 0.7, rbinom(0.7 * N, 1, 0.6) , rbinom( (1 - 0.7) * N, 1, 0.3) )
# beyond 0.7 the proba is 0.6 to receive treatment and below is 0.3 #
对我来说似乎是正确的,但如果您有更好的方法,请告诉我。
# adding a bit more randomness #
d[ sample(length(d), 100) ] <- rbinom(100, 1, 0.5)
# also adding a cut-off point for the treated #
d[x < -10] <- 0
d[x > 10] <- 0
我正在使用 ifelse
生成 d
的效果,对我来说似乎是正确的,但我可能是错的。
# generate outcome y, w/ polyn relationship with x and a Treatment effect of 15 # sd == 10 #
y = x*1 + x^2 + rnorm(N, ifelse(d == 1, 15, 0), 10)
#
df = cbind(x,d,y) %>% as.data.frame()
# check out the "common support"
df %>% ggplot(aes(x, y, colour = factor(d) )) + geom_point()
#
该图显示了我想要为 3 种关系建模的方式。请注意处理的 10 以上和以下的截止值。
现在,当我使用 OLS 估计 d
对 y
的影响时,带有遗漏变量的模型和预期的错误指定模型给出了 d
的错误估计。
# omitted x #
lm(y ~ d, df) %>% summary()
# misspecification #
lm(y ~ d + x, df) %>% summary()
# true model #
虽然正确的规范让我得到 15
(d
的真实效果)。
lm(y ~ d + poly(x,2), df) %>% summary()
# we correctly retrieve 15 #
现在我的问题是了解为什么我无法使用匹配包达到 15
(d 的真实效果)。
使用 MatchIt
包。
我试过 mahalanobis
和这样的倾向得分:
m1 = matchit(d ~ x, df, distance = 'mahalanobis', method = 'genetic')
m2a = matchit(d ~ x, df, distance = 'logit', method = 'genetic')
m2b = matchit(d ~ x + I(x^2), df, distance = 'logit', method = 'genetic')
匹配数据
mat1 = match.data(m1)
mat2a = match.data(m2a)
mat2b = match.data(m2b)
# OLS #
lm(y ~ d, mat1) %>% summary()
lm(y ~ d, mat2a) %>% summary()
lm(y ~ d, mat2b) %>% summary()
所以这里我不检索15
。为什么?我误解了结果吗?
我的印象是,在执行 matching
时,您不必为多项式项 or/and 交互建模。那是不正确的吗?
lm(y ~ d + poly(x,2), mat1) %>% summary()
lm(y ~ d + poly(x,2), mat2a) %>% summary()
lm(y ~ d + poly(x,2), mat2b) %>% summary()
因为如果我在此处包含 poly(x,2)
项,我将检索到 15。
使用Matching
包,我也得到了完全不同的估计
x1 = df$x
gl = glm(d ~ x + I(x^2), df, family = binomial)
x1 = gl$fitted.values
# I thought that it could be because OLS only gives ATE #
m0 = Match(Y = y, Tr = d, X = x1, estimand = 'ATE')
# but no
m0$est
有什么线索吗?
匹配过程的一个重要输出是控制观察的权重。计算权重使得倾向得分的分布在治疗组和对照组中相似(一旦应用了权重)。
在你的情况下,这意味着(从你的 dgp 和你的符号开始):
lm(y ~ d, mat1, weights = weights) %>% summary()
lm(y ~ d, mat2a, weights = weights) %>% summary()
lm(y ~ d, mat2b, weights = weights) %>% summary()
现在是:15
回来了(实际上是 14.9)!
我正在尝试模拟 matching 与回归 (OLS) 的敏感性,但我在某处做错了,因为我无法使用 matching
检索真实模型。
我正在生成 3 个变量:x
,一个背景特征,d
是治疗变量(二进制)和 y
结果。 d
与 x
关联。匹配的想法是,一旦以 x
为条件,治疗分配生成过程就像随机的一样好。在回归世界中,x
只是一个控制变量。我想测试当数据中存在非共同支持区域(未处理高于或低于特定值)时回归如何执行。
library(tidyverse)
library(Matching)
library(MatchIt)
N = 1000
# generate random variable normality dist #
x = rnorm(N, 0, 5)
这就是我在 x
和 d
(二进制)之间生成关联的方式。
# generate Treatement associated with x, with different probailities after a certain threshold #
d = ifelse(x > 0.7, rbinom(0.7 * N, 1, 0.6) , rbinom( (1 - 0.7) * N, 1, 0.3) )
# beyond 0.7 the proba is 0.6 to receive treatment and below is 0.3 #
对我来说似乎是正确的,但如果您有更好的方法,请告诉我。
# adding a bit more randomness #
d[ sample(length(d), 100) ] <- rbinom(100, 1, 0.5)
# also adding a cut-off point for the treated #
d[x < -10] <- 0
d[x > 10] <- 0
我正在使用 ifelse
生成 d
的效果,对我来说似乎是正确的,但我可能是错的。
# generate outcome y, w/ polyn relationship with x and a Treatment effect of 15 # sd == 10 #
y = x*1 + x^2 + rnorm(N, ifelse(d == 1, 15, 0), 10)
#
df = cbind(x,d,y) %>% as.data.frame()
# check out the "common support"
df %>% ggplot(aes(x, y, colour = factor(d) )) + geom_point()
#
该图显示了我想要为 3 种关系建模的方式。请注意处理的 10 以上和以下的截止值。
现在,当我使用 OLS 估计 d
对 y
的影响时,带有遗漏变量的模型和预期的错误指定模型给出了 d
的错误估计。
# omitted x #
lm(y ~ d, df) %>% summary()
# misspecification #
lm(y ~ d + x, df) %>% summary()
# true model #
虽然正确的规范让我得到 15
(d
的真实效果)。
lm(y ~ d + poly(x,2), df) %>% summary()
# we correctly retrieve 15 #
现在我的问题是了解为什么我无法使用匹配包达到 15
(d 的真实效果)。
使用 MatchIt
包。
我试过 mahalanobis
和这样的倾向得分:
m1 = matchit(d ~ x, df, distance = 'mahalanobis', method = 'genetic')
m2a = matchit(d ~ x, df, distance = 'logit', method = 'genetic')
m2b = matchit(d ~ x + I(x^2), df, distance = 'logit', method = 'genetic')
匹配数据
mat1 = match.data(m1)
mat2a = match.data(m2a)
mat2b = match.data(m2b)
# OLS #
lm(y ~ d, mat1) %>% summary()
lm(y ~ d, mat2a) %>% summary()
lm(y ~ d, mat2b) %>% summary()
所以这里我不检索15
。为什么?我误解了结果吗?
我的印象是,在执行 matching
时,您不必为多项式项 or/and 交互建模。那是不正确的吗?
lm(y ~ d + poly(x,2), mat1) %>% summary()
lm(y ~ d + poly(x,2), mat2a) %>% summary()
lm(y ~ d + poly(x,2), mat2b) %>% summary()
因为如果我在此处包含 poly(x,2)
项,我将检索到 15。
使用Matching
包,我也得到了完全不同的估计
x1 = df$x
gl = glm(d ~ x + I(x^2), df, family = binomial)
x1 = gl$fitted.values
# I thought that it could be because OLS only gives ATE #
m0 = Match(Y = y, Tr = d, X = x1, estimand = 'ATE')
# but no
m0$est
有什么线索吗?
匹配过程的一个重要输出是控制观察的权重。计算权重使得倾向得分的分布在治疗组和对照组中相似(一旦应用了权重)。
在你的情况下,这意味着(从你的 dgp 和你的符号开始):
lm(y ~ d, mat1, weights = weights) %>% summary()
lm(y ~ d, mat2a, weights = weights) %>% summary()
lm(y ~ d, mat2b, weights = weights) %>% summary()
现在是:15
回来了(实际上是 14.9)!