为什么匹配结果取决于数据的顺序(MatchIt包)?

时间:2019-04-08 15:00:06

标签: r matching

使用matchit函数进行完全匹配时,结果因输入数据帧的顺序而异。即,如果数据的顺序改变,则结果也改变。这是令人惊讶的,因为根据我的理解,最佳完整算法应该只产生一个最佳解决方案。

我错过了什么吗?还是这是个错误?

最佳算法会出现相似的差异。

在下面您可以找到一个可复制的示例。这两个数据集的子类应该相同,但事实并非如此。 谢谢您的帮助!

# create data
nr <- c(1:100)
x1 <- rnorm(100, mean=50, sd=20)
x2 <- c(rep("a", 20),rep("b", 60), rep("c", 20))
x3 <- rnorm(100, mean=230, sd=2)
outcome <- rnorm(100, mean=500, sd=20)
group <- c(rep(0, 50),rep(1, 50))

df <- data.frame(x1=x1, x2=x2, outcome=outcome, group=group, row.names=nr, nr=nr)
df_neworder <- df[order(outcome),]  # re-order data.frame

# perform matching
model_oldorder <- matchit(group~x1, data=df, method="full", distance ="logit")
model_neworder <- matchit(group~x1, data=df_neworder, method="full", distance ="logit")

# store matching results
matcheddata_oldorder <- match.data(model_oldorder, distance="pscore")
matcheddata_neworder <- match.data(model_neworder, distance="pscore")


# Results based on original data.frame
head(matcheddata_oldorder[order(nr),], 10)
          x1 x2  outcome group nr    pscore weights subclass
1  69.773776  a 489.1769     0  1 0.5409943     1.0       27
2  63.949637  a 529.2733     0  2 0.5283582     1.0       32
3  52.217666  a 526.7928     0  3 0.5028106     0.5       17
4  48.936397  a 492.9255     0  4 0.4956569     1.0        9
5  36.501507  a 512.9301     0  5 0.4685876     1.0       16


# Results based on re-ordered data.frame
head(matcheddata_neworder[order(matcheddata_neworder$nr),], 10)
          x1 x2  outcome group nr    pscore weights subclass
1  69.773776  a 489.1769     0  1 0.5409943     1.0       25
2  63.949637  a 529.2733     0  2 0.5283582     1.0       31
3  52.217666  a 526.7928     0  3 0.5028106     0.5       15
4  48.936397  a 492.9255     0  4 0.4956569     1.0        7
5  36.501507  a 512.9301     0  5 0.4685876     2.0       14

显然,对象对子类的分配不同。据我了解,事实并非如此。

1 个答案:

答案 0 :(得分:0)

optmatch程序包(matchit函数调用)的开发人员提供了有用的帮助:

  

我认为我们在这里看到的是公差参数的结果   完全匹配。匹配算法需要整数距离,   因此我们必须先缩放然后截断浮点距离。为一个   给定一组整数距离,可能存在多个匹配项   达到最小值,因此求解器可以自由选择   非唯一解决方案。

Developing your example a little more:
> library(optmatch) 
> nr <- c(1:100) x1 <- rnorm(100, mean=50, sd=20)
> outcome <- rnorm(100, mean=500, sd=20) group <- c(rep(0, 50),rep(1, 50)) 
> df_oldorder <- data.frame(x1=x1, outcome=outcome, group=group, row.names=nr, nr=nr) > df_neworder <- df_oldorder[order(outcome),]  # > re-order data.frame 
> glm_oldorder <- match_on(glm(group~x1, > data=df_oldorder), data = df_oldorder) 
> glm_neworder <- > match_on(glm(group~x1, data=df_neworder), data = df_neworder) 
> fm_old <- fullmatch(glm_oldorder, data=df_oldorder) 
> fm_new <- fullmatch(glm_neworder, data=df_neworder)

> mean(sapply(matched.distances(fm_old, glm_oldorder), mean))
> ## 0.06216174 

> mean(sapply(matched.distances(fm_new, glm_neworder), mean))
> ## 0.062058 mean(sapply(matched.distances(fm_old, glm_oldorder), mean)) -  

> mean(sapply(matched.distances(fm_new, glm_neworder), mean))
> ## 0.00010373 
我们看到的

小于默认公差0.001。您可以随时降低容忍度,

  

需要增加运行时间,以便更接近真实情况   浮动看跌期权最低限度。我们发现0.001似乎在实践中效果很好,   但是这个值没有什么特别的。