2

我正在尝试运行与最近邻方法匹配的倾向得分,而我需要对一个称为“类型”的变量进行精确匹配。我的数据如下所示:

> df
# A tibble: 20 × 6
     symbol treat  type  market.cap market.to.book  leverage
     <fctr> <lgl> <dbl>       <dbl>          <dbl>     <dbl>
1   AT_ABCB  TRUE     1   228152.66      1.1929613  9.394726
2   AT_ACFC  TRUE     1   162562.19      1.8101484  6.174758
3   AT_ACGL  TRUE     4  4735844.75      1.1734933  3.434948
4   AT_ACNB FALSE     1    91063.20      1.0696958 10.241219
5   AT_AFSI FALSE     4   825553.85      1.9527673  3.257310
6   AT_AGII  TRUE     4  1291845.73      0.9330774  3.870854
7   AT_AINV  TRUE     2  1764811.40      0.9540821  1.948243
8   AT_AMNB  TRUE     1   122196.43      1.2037753  6.489334
9  AT_AMPLQ FALSE     2   426403.00      1.8064556  2.081252
10  AT_AMRB  TRUE     1    95820.71      1.5977307  6.329130
11  AT_AMSF  TRUE     3   291789.63      1.4095641  3.839128
12  AT_AMTD  TRUE     2 11929441.28      5.5359066  2.335973
13  AT_ANAT  TRUE     4  3210435.20      0.8591439  5.587567
14  AT_ANCX FALSE     1    65479.64      1.1297189  9.575704
15 AT_AOREF FALSE     4   133379.89      0.5524693  5.639680
16  AT_ARCC FALSE     2  1063369.56      0.9455956  1.662850
17  AT_AROW  TRUE     1   230742.30      1.8873699  7.329797
18  AT_ASFI  TRUE     2   367991.92      1.5492114  1.897770
19  AT_ASRV  TRUE     1    61463.53      0.6807045 14.029418
20  AT_ATAX FALSE     2    87023.72      1.3613197  2.160063

执行此代码:

library(MatchIt)
match1 <- matchit(treat ~ market.cap + market.to.book + leverage + type,
                  method = "nearest", replace = TRUE, exact ="type",
                  data = df)

导致以下错误:

Error in Ops.data.frame(exact[itert, k], exact[clabels, k]) : 
  ‘!=’ only defined for equally-sized data frames

当我放弃exact参数时,代码工作正常。类似的代码也适用于包lalonde提供的示例数据MatchIt

library(MatchIt)
match1 <- matchit(treat ~ age + educ + married, method = "nearest", 
               exact  = "married", replace = TRUE, data = lalonde)

所以我猜是数据本身导致了问题。这是重现我的原始数据的前 20 行的代码。使用此示例数据运行上述匹配代码会导致与原始数据相同的错误。

symbol <- c("AT_ABCB", "AT_ACFC", "AT_ACGL", "AT_ACNB", "AT_AFSI", "AT_AGII",
            "AT_AINV", "AT_AMNB", "AT_AMPLQ", "AT_AMRB", "AT_AMSF", "AT_AMTD",
            "AT_ANAT", "AT_ANCX", "AT_AOREF", "AT_ARCC", "AT_AROW", "AT_ASFI",
            "AT_ASRV", "AT_ATAX")

type <- c(1, 1, 4, 1, 4, 4, 2, 1, 2, 1, 3, 2, 4, 1, 4, 2, 1, 2, 1, 2)

treat <- c(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, 
           TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE)

market.cap <- c(228152.66, 162562.19, 4735844.75, 91063.20, 825553.85, 1291845.73, 
                1764811.40, 122196.43, 426403.00, 95820.71, 291789.63, 11929441.28,
                3210435.20, 65479.64, 133379.89, 1063369.56, 230742.30, 367991.92,
                61463.53, 87023.72)

market.to.book <- c(1.1929613, 1.8101484, 1.1734933, 1.0696958, 1.9527673, 
                    0.9330774, 0.9540821, 1.2037753, 1.8064556, 1.5977307, 
                    1.4095641, 5.5359066, 0.8591439, 1.1297189, 0.5524693,
                    0.9455956, 1.8873699, 1.5492114, 0.6807045, 1.3613197)

leverage <- c(9.394726, 6.174758, 3.434948, 10.241219, 3.257310, 3.870854,
              1.948243, 6.489334, 2.081252, 6.329130, 3.839128, 2.335973,
              5.587567, 9.575704, 5.639680, 1.662850, 7.329797, 1.897770, 
              14.029418, 2.160063)

library(dplyr)
df <- tbl_df(data.frame(symbol, treat, type, market.cap, 
                        market.to.book, leverage))
4

1 回答 1

1

做就是了:

df <- data.frame(symbol, treat, type, market.cap, market.to.book, leverage)
于 2017-03-05T21:27:39.380 回答