Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"nearest" error #207

Closed
Huang-Yilin opened this issue Nov 25, 2024 · 1 comment
Closed

"nearest" error #207

Huang-Yilin opened this issue Nov 25, 2024 · 1 comment

Comments

@Huang-Yilin
Copy link

Dear author,
I am writing to report a potential issue I encountered while using the MatchIt package (version 4.6.0) with the method = "nearest" option. Below are the details:
Description of the Issue:
When using the nearest method with ratio > 2, I noticed that each treated case is only matched to a single control instead of the expected ratio number of controls. And the last case will match all rest unmatched controls,like 1:2000. This occurs even when there are enough controls to meet the specified ratio.
R version: 4.2.2
MatchIt version: 4.6.0
Operating system: macOS(15.1.1)
I have included a minimal reproducible example below for your reference:

set.seed(123)

扩展到上千的病例和对照数据

n_cases <- 1000 # 病例数量
n_controls <- 5000 # 对照数量

生成模拟数据

test_data <- data.frame(
case_control = c(rep(1, n_cases), rep(0, n_controls)), # 病例与对照分组
age = c(rnorm(n_cases, 60, 5), rnorm(n_controls, 60, 5)), # 年龄分布
sex = c(
sample(0:1, n_cases, replace = TRUE), # 病例性别分布
sample(0:1, n_controls, replace = TRUE) # 对照性别分布
),
ID = 1:(n_cases + n_controls) # 唯一标识符
)

使用 MatchIt 进行匹配

library(MatchIt)

m.out <- matchit(case_control ~ age + sex,
data = test_data,
method = "nearest", # 使用最近邻匹配
ratio = 10, # 每个病例匹配5个对照
distance = "mahalanobis", # 使用 Mahalanobis 距离
caliper = c(age = 0.32), # 对年龄限制在 ±1 岁内
exact = "sex") # 确保性别完全一致匹配

matched_data <- match.data(m.out)

Could you please confirm if this is an intended behavior or a potential bug? If it is the latter, I would be happy to provide additional details or assist in testing any fixes.
I attempted to revert to an earlier version of MatchIt to resolve this problem, but unfortunately, the error persists.

This issue is impacting my current research, and as I am on a tight schedule to finalize an article for publication, I would greatly appreciate any guidance or a quick resolution.

Thank you for your time and for all your efforts in maintaining this fantastic tool for the community.

@ngreifer
Copy link
Collaborator

Thanks for reporting. This has been fixed in #209, and version 4.7.0 is now on CRAN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants