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

Add categorical covariate model #57

Merged
merged 12 commits into from
Oct 7, 2024
Merged

Add categorical covariate model #57

merged 12 commits into from
Oct 7, 2024

Conversation

zhengp0
Copy link
Member

@zhengp0 zhengp0 commented Jun 27, 2024

Add CatCovModel in the mrtool.core.cov_model module, this is try to process the categorical risks and potentially network analysis.

Simple Example

Simulate data, here we simulate data with four categories, 10 studies and 10 observations per study.

import itertools

import numpy as np
import pandas as pd

from mrtool import MRData, LinearCatCovModel, MRBRT

np.random.seed(0)
num_studies = 10
num_obs_per_study = 10
gamma = 0.1
obs_se = np.sqrt(0.1)

effects = pd.DataFrame(dict(cat=["A", "B", "C", "D"], effect=[0, 1, 2, 3]))
all_pairs = list(itertools.combinations(effects.cat, 2))
# sample pairs for each observation
pairs = [
    all_pairs[np.random.randint(0, len(all_pairs))]
    for _ in range(num_studies * num_obs_per_study)
]

df = pd.DataFrame(pairs, columns=["ref_cat", "alt_cat"])
df["study_id"] = np.repeat(range(num_studies), num_obs_per_study)
df["re"] = np.repeat(
    np.random.normal(0, scale=np.sqrt(gamma), size=num_studies), num_obs_per_study
)
df["obs_se"] = obs_se
df["noise"] = np.random.normal(scale=df["obs_se"])

# merge with the true effect sizes
df = df.merge(
    effects.rename(columns={"cat": "ref_cat", "effect": "ref_effect"}),
    on="ref_cat",
).merge(
    effects.rename(columns={"cat": "alt_cat", "effect": "alt_effect"}),
    on="alt_cat",
)

df["obs"] = df.eval("alt_effect - ref_effect + noise + re")

df.head(5)

Fit model

data = MRData()
data.load_df(
    df,
    col_obs="obs",
    col_obs_se="obs_se",
    col_covs=["ref_cat", "alt_cat"],
    col_study_id="study_id",
)

covmodels = [
    LinearCatCovModel(
        alt_cov="alt_cat", ref_cov="ref_cat", ref_cat="A", use_re=True
    )
]

model = MRBRT(data, covmodels)

model.fit_model()

# check solution and compare with the truth
model.beta_soln # vs effects
model.gamma_soln # vs gamma

@zhengp0 zhengp0 requested a review from n-gilbertson June 27, 2024 20:54
@zhengp0 zhengp0 merged commit 2ee00fa into main Oct 7, 2024
1 check passed
@zhengp0 zhengp0 deleted the feature/cat-covmodel branch October 7, 2024 02:21
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.

2 participants