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

Iterative categorical matching #60

Merged
merged 28 commits into from
Nov 1, 2024
Merged

Conversation

Hussein-Mahfouz
Copy link
Collaborator

As described in #13

@Hussein-Mahfouz Hussein-Mahfouz linked an issue Oct 15, 2024 that may be closed by this pull request
@sgreenbury
Copy link
Collaborator

sgreenbury commented Oct 18, 2024

Adding notes from discussion to check and implement, then can merge (happy to do both these):

  • Confirm we can run for the whole population
  • Add config table matching: for columns to include optional matching and a value for n_matches (if not passed, no threshold)

@Hussein-Mahfouz
Copy link
Collaborator Author

The new iterative_match_categorical function does not need to be iterative. We can run an exact match on specified column as shown in the last commit. We do the following:

  • add all columns to required_columns
  • keep optional_cols empty - that means we will only have one iteration see here
  • make n_matches = 0
# Define required columns for matching
required_columns_fixed = [
    "number_adults",
    "number_children",
    "number_cars",
    "num_pension_age",
    "rural_urban_2_categories",
]

# Define optional columns in order of importance (most to least important)
optional_columns_fixed = []

# # Match using iterative matching
matcher_fixed = MatcherExact(
    df_pop=spc_matching,
    df_pop_id="hid",
    df_sample=nts_matching,
    df_sample_id="HouseholdID",
    matching_dict = matching_dfs_dict,
    fixed_cols=required_columns_fixed,
    optional_cols=optional_columns_fixed,
    n_matches=0,  
    chunk_size=50000,
    show_progress=True,
)

matches_hh_level_fixed = matcher_fixed.iterative_match_categorical()

@sgreenbury
Copy link
Collaborator

sgreenbury commented Oct 18, 2024

  • also add docs in the config toml (base.toml) explaining order is the order in which features are dropped

}
assert result == expected_result


@pytest.mark.skip(reason="todo")
def test_match_individuals():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to have the test fixture and tests added here. Perhaps we can add a test for the match_psm too using this in a subsequent PR, I'll open an issue for this.

@sgreenbury
Copy link
Collaborator

Thanks for adding this @Hussein-Mahfouz, this looks great with the matching algorithm clearer, configurable and tested. As discussed, I'll go ahead and merge this now following running for Greater London as part of #58.

@sgreenbury sgreenbury merged commit 87fa6be into main Nov 1, 2024
4 checks passed
@sgreenbury sgreenbury deleted the iterative_categorical_matching branch November 1, 2024 17:43
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

Successfully merging this pull request may close these issues.

Improve matching approach
2 participants