# Estimation of average treatment effect based on nearest neighbor matching [closed]

I would like to use R to duplicate the treatment effect estimation method used in Stata. Specifically, this is the Stata method I would like to duplicate.

I have tried the package MatchIt and Zelig in R, but the result was quite different from the result I had in Stata.

m.out1 = matchit(treatment ~ high_peak + hour, method="nearest", data=regres_LNG)
z.out1 = zelig(IPPLNG.gen ~ high_peak + hour,
data=match.data(m.out1,"control"), model="ls")
x.out1 = setx(z.out1, data=match.data(m.out1, "treat", cond=TRUE))

s.out1 = sim(z.out1, x=x.out1)
summary(s.out1)

z.out2  = zelig(IPPLNG.gen~hour+high_peak,
data=match.data(m.out1,"treat"), model="ls")
x.out2  = setx(z.out2, data=match.data(m.out1,"control"), cond=TRUE)
s.out2  = sim(z.out2, x=x.out2)
ate.all = c(s.out1$$sim.out[][][], -s.out2$$sim.out[][][])


(Also, I learned this method in the pdf)

Besides, I tried the linear regression model including treatment as a independent variable, but the coefficient and p-value was still different.

m.data   = match.data(m.out1)
lm_treat = lm(IPPLNG.gen~事件+hour+high_peak, data=m.data)
summary(lm_treat)


Hence, I would like to know how to duplicate the treatment effect estimation within nearest neighborhood matching in Stata to R and the difference between these two methods.

## closed as off-topic by gung♦Apr 16 at 18:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – gung
If this question can be reworded to fit the rules in the help center, please edit the question.

• One immediate thing is that matchit() uses the logit function, i.e., distance = "logit" by default for the distance metric. Specify distance = "mahalanobis" to match what they use in the youtube video. – user321627 Apr 7 at 14:41
• I just specified distance="mahalanobis". But the result was still different. Thank you for your reply. – Hudson C Apr 7 at 15:14
• Questions that are only about software (e.g. error messages, code or packages, etc.) are generally off topic here. If you have a substantive machine learning or statistical question, please edit to clarify. – gung Apr 16 at 18:49

## 1 Answer

Despite good documentation for teffects nnmatch and the R package Matching, the answers are slightly different, but I'm not exactly sure why. However, here is how you can get close to a similar answer in R:

d <- haven::read_dta("http://www.stata-press.com/data/r13/cattaneo2.dta")
library(Matching)
m.out <- with(d, Match(Y = bweight,
Tr = mbsmoke,
X = data.frame(fage, mage, mmarried, prenatal1),
Z = data.frame(fage, mage),
estimand = "ATE",
BiasAdjust = TRUE,
exact = c(fage = FALSE, mage = FALSE,
mmarried = TRUE, prenatal1 = TRUE)))
summary(m.out)


The MatchIt package cannot do this kind of analysis. The philosophy behind it is different from that underlying teffects nnmatch. The Matching package comes quite close in that it has many of the same options as teffects nnmatch and was built on a similar philosophy (and even uses fairly similar syntax elements). Using this code, I get an ATE of -250.18 (27.807), whereas teffects nnmatch in the video gave an ATE of -244.32 (27.269), which lead to the same substantive conclusion.

• I have tried it. I think it's the most similar method in R. Thank you – Hudson C Apr 10 at 10:36