我试着模拟
matching
但是我在某个地方做了一些错误的事情,因为我无法使用
matching
.
我正在生成3个变量:
x
d
哪个是治疗变量(二进制)和
y
结果。
与…关联
十
. 匹配的概念是,一旦
十
十
library(tidyverse)
library(Matching)
library(MatchIt)
N = 1000
# generate random variable normality dist #
x = rnorm(N, 0, 5)
和
(二进制)。
# 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
d
ifelse
,在我看来是对的,但我可能错了。
# 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()
#
现在当我估计
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
. 为什么?我是不是误解了结果?
我的印象是
,您不必对多项式项或/和交互进行建模。这不对吗?
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)
这里是术语。
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
有什么线索吗?