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 panel covariates to plot_cap() and make it more flexible #596

Merged
merged 7 commits into from
Dec 2, 2022

Conversation

tomicapretto
Copy link
Collaborator

@tomicapretto tomicapretto commented Nov 26, 2022

This is still WIP, but I'm excited about this PR since it makes plot_cap() much more flexible

Now we can do something like

data = pd.read_csv("https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv")
data["cyl"] = data["cyl"].replace({4: "low", 6: "medium", 8: "high"})
data["cyl"] = pd.Categorical(data["cyl"], categories=["low", "medium", "high"], ordered=True)
data["gear"] = data["gear"].replace({3: "A", 4: "B", 5: "C"})
model = bmb.Model("mpg ~ 0 + hp * wt + cyl + gear", data)
idata = model.fit(draws=500, target_accept=0.9, random_seed=1234)

fig, axes = plot_cap(
    model, 
    idata, 
    {"horizontal": "hp", "color": "cyl","panel": "gear"},
    use_hdi=False, 
    fig_kwargs={"figsize": (15, 5), "dpi": 120, "sharey": True}
);

image

All the previous examples still work. I still need to update the the internal function _plot_cap_categoric() and clean some details in _plot_cap_numeric(). In particular I need to handle legends using the figure and not the axes.

Edit: This is done. I decided to create a notebook to show the new features of plot_cap(). Basically, it still works for all the cases it used to work, but now it's more flexible. We can map groups to panels.

https://gist.github.com/tomicapretto/1388437357d8520ff5ff2fcee39b7d1b

@tomicapretto tomicapretto marked this pull request as ready for review November 27, 2022 02:34
@codecov-commenter
Copy link

codecov-commenter commented Nov 27, 2022

Codecov Report

Merging #596 (1291406) into main (3af8328) will decrease coverage by 2.49%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##             main     #596      +/-   ##
==========================================
- Coverage   85.36%   82.86%   -2.50%     
==========================================
  Files          38       38              
  Lines        2944     3034      +90     
==========================================
+ Hits         2513     2514       +1     
- Misses        431      520      +89     
Impacted Files Coverage Δ
bambi/plots/plot_cap.py 0.00% <0.00%> (ø)
bambi/tests/test_built_models.py 96.50% <0.00%> (+0.29%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@ -133,9 +150,12 @@ def plot_cap(model, idata, covariates, use_hdi=True, hdi_prob=None, legend=True,
Changing the global variable ``az.rcParam["stats.hdi_prob"]`` affects this default.
legend : bool, optional
Whether to automatically include a legend in the plot. Defaults to ``True``.
transforms : dict, optional
Transformations that are applied to each of the variables being plotted. The keys are the
name of the variables, and the values are functions to be applied. Defaults to ``None``.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
name of the variables, and the values are functions to be applied. Defaults to ``None``.
name of the variables, and the values are functions to be applied. Defaults to `None`.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, just curious why are the double ticks needed? I see it in other docstrings too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If I remember correctly it's because it doesn't render properly if it only has a single backtick. Maybe something changed and now it works with a single backtick, but previously I remember trying a single backtick without working

@canyon289
Copy link
Collaborator

This is incredible, you basically rewrote this whole module, and now it can do so much more

@canyon289
Copy link
Collaborator

canyon289 commented Nov 27, 2022 via email

@tomicapretto tomicapretto merged commit fbd409c into bambinos:main Dec 2, 2022
@tomicapretto tomicapretto deleted the plot_cap_more_flexibility branch December 2, 2022 12:23
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.

3 participants