【发布时间】:2019-03-10 21:26:50
【问题描述】:
我试图模拟 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 时,您不必对多项式项或/和交互进行建模。这是不正确的吗?
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
有什么线索吗?
【问题讨论】:
标签: r regression linear-regression matching economics