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

New function spread_draws for creating tidybayes compliant MCMC output #57

Conversation

SamuelBrand1
Copy link
Collaborator

This PR proposes a new function: spread_draws

"""
spread_draws(chn::Chains)
Converts a `Chains` object into a DataFrame in `tidybayes` format.
# Arguments
- `chn::Chains`: The `Chains` object to be converted.
# Returns
- `df::DataFrame`: The converted DataFrame.
"""
function spread_draws(chn::Chains)
df = DataFrame(chn)
df = hcat(DataFrame(draw = 1:size(df, 1)), df)
@rename!(df, $(".draw") = :draw)
@rename!(df, $(".chain") = :chain)
@rename!(df, $(".iteration") = :iteration)
return df
end

This converts a Chains object return from running Bayesian inference using the Turing sample method into a DataFrame which conforms to the standard output expected from the tidybayes function of the same name.

Added dependencies:

  • DataFramesMeta. This exports DataFrames functionality as well as additional macros for dplyr-like interaction with dataframe objects. In particular, the constructor DataFrame dispatches on Chains objects to create a DataFrame that is nearly tidybayes compliant.
  • CSV. This exports utilities for using .csv files. This has been added to the /test folder environment to expose CSV.write.

Closes #55

@@ -67,6 +67,9 @@ using StatsPlots
using Random
using DynamicPPL
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this intensionally missing things from the test environmment? Like StatsPlots?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

StatsPlots is in the test env but not a dependency of EpiAware; which I think is the right move.

@seabbs seabbs self-requested a review February 21, 2024 10:53
Copy link
Contributor

@seabbs seabbs left a comment

Choose a reason for hiding this comment

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

This looks good. We might need an issue to resolve the test environment once we sort out documentation as there are things like StatsPlot that don't appear.

@seabbs seabbs merged commit 2e18d00 into 43-do-inference-on-generated-data-using-this-in-conjunction-with-the-already-existing-rw-latent-process Feb 21, 2024
2 checks passed
@seabbs seabbs deleted the 55-pipe-output-to-a-tidybayes-compliant-output branch February 21, 2024 10:55
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