-
-
Notifications
You must be signed in to change notification settings - Fork 122
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
Return interpret data #758
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
107bdb3
add arg. to return idata and observed data in a single dataframe
GStechschulte 068daa2
add tests for non-plotting functionality
GStechschulte ee3f0f0
handle returning idata when pps=True for predictions function
GStechschulte 0d1fe33
test return_idata for different models
GStechschulte 6208fe8
return data and idata for demonstration purposes
GStechschulte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
from statistics import mode | ||
from typing import Union | ||
|
||
import arviz as az | ||
import numpy as np | ||
from formulae.terms.call import Call | ||
import pandas as pd | ||
|
@@ -393,16 +394,10 @@ def make_group_values(x: np.ndarray, groups_n: int = 5) -> np.ndarray: | |
|
||
|
||
def get_group_offset(n, lower: float = 0.05, upper: float = 0.4) -> np.ndarray: | ||
# Complementary log log function, scaled. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for removing these comments :D |
||
# See following code to have an idea of how this function looks like | ||
# lower, upper = 0.05, 0.4 | ||
# x = np.linspace(2, 9) | ||
# y = get_group_offset(x, lower, upper) | ||
# fig, ax = plt.subplots(figsize=(8, 5)) | ||
# ax.plot(x, y) | ||
# ax.axvline(2, color="k", ls="--") | ||
# ax.axhline(lower, color="k", ls="--") | ||
# ax.axhline(upper, color="k", ls="--") | ||
""" | ||
When plotting categoric variables, this function computes the offset of the | ||
stripplot points based on the number of groups ``n``. | ||
""" | ||
intercept, slope = 3.25, 1 | ||
return lower + np.exp(-np.exp(intercept - slope * n)) * (upper - lower) | ||
|
||
|
@@ -434,3 +429,24 @@ def merge(y_hat_mean: xr.DataArray, y_hat_bounds: xr.DataArray, data: pd.DataFra | |
summary_df = pd.merge(left=data, right=preds_df, left_index=True, right_index=True) | ||
|
||
return summary_df.drop(columns=["hdi_x", "hdi_y"]) | ||
|
||
|
||
def get_posterior( | ||
response_obs: str, idata: az.InferenceData, pred_data: pd.DataFrame | ||
) -> pd.DataFrame: | ||
""" | ||
Merges the posterior or posterior predictive draws with the corresponding | ||
observation that produced that draw. | ||
""" | ||
# if `pps=True` in 'predictions', then use posterior predictive draws | ||
if "posterior_predictive" in idata.groups(): | ||
posterior_df = idata.posterior_predictive.to_dataframe().reset_index() | ||
else: | ||
posterior_df = idata.posterior.to_dataframe().reset_index() | ||
|
||
posterior_df = posterior_df.set_index(response_obs) | ||
posterior_df = posterior_df.merge(pred_data, left_index=True, right_index=True) | ||
posterior_df = posterior_df.rename(columns=lambda x: x.replace("_x", "")) | ||
posterior_df = posterior_df.rename(columns=lambda x: x.replace("_y", "_obs")) | ||
|
||
return posterior_df.reset_index(drop=True) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use a different name for this? From the name, I assumed the returned object would be an instance of InferenceData, but I see it's a data frame. Maybe
return_data
? Or, are you thinking we would be able to return an InferenceData instance in the future?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe "return_posterior_draws_dataframe".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zwelitunyiswa thanks for the suggestion! Tomas and I are looking into returning the
InferenceData
object after all. So stay tuned to this PR 😄