I'm trying to run a propensity score matching with the nearest neighbor method while I need a exact match on one variable, called "type". My data looks like this:
> 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
Executing this code:
library(MatchIt)
match1 <- matchit(treat ~ market.cap + market.to.book + leverage + type,
method = "nearest", replace = TRUE, exact ="type",
data = df)
leads to the following error:
Error in Ops.data.frame(exact[itert, k], exact[clabels, k]) :
‘!=’ only defined for equally-sized data frames
The code works fine when I drop the exact
argument. A similiar code also works with the sample data from lalonde
provided by the MatchIt
package:
library(MatchIt)
match1 <- matchit(treat ~ age + educ + married, method = "nearest",
exact = "married", replace = TRUE, data = lalonde)
So I guess that the data itself causes the problem. Here is the code that reproduces the first 20 rows of my original data. Running the above matching code with this sample data causes the same error as the original data.
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))