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

family() to optionally return the types of distributions in the mixture #110

Open
statasaurus opened this issue May 14, 2024 · 3 comments
Open

Comments

@statasaurus
Copy link
Contributor

At the moment, there really isn't an easy way of looking at the contents of mixture distributions. I am trying to determine if all the elements in a mixture are normal to see if I can just use conjugacy to calculate the posterior. But, family() just returns the that it is a mixture. So it would be good if there was maybe an additional parameter or something that you could add to family() to look at the distributions making up the mixture.


Brief description of the problem

x <- dist_mixture(dist_normal(0, 1), dist_normal(5, 2), weights = c(0.3, 0.7))
family(x)
foo <- vec_data(x)
foo[[1]]$dist |> 
  map_lgl(\(x) "dist_normal" %in% class(x))
@mitchelloharawild
Copy link
Owner

This seems a bit tricky to design, but a similar problem would apply to other distribution modifiers - dist_inflated(), dist_truncated(), and dist_transformed().

Here's a recursive approach:

library(distributional)
x <- dist_mixture(dist_normal(0, 1), dist_normal(5, 2), weights = c(0.3, 0.7))

get_base_families <- function(x) {
  fam <- family(x)
  is_modified <- family(x) %in% c("mixture", "transformed", "inflated", "truncated")
  if(any(is_modified)) {
    fam[is_modified] <- lapply(parameters(x[is_modified])$dist, function(dist) lapply(dist, get_base_families))
  }
  fam
}

get_base_families(x)
#> [[1]]
#> [[1]][[1]]
#> [1] "normal"
#> 
#> [[1]][[2]]
#> [1] "normal"

Created on 2024-05-15 with reprex v2.0.2

@statasaurus
Copy link
Contributor Author

Something like this in the package would be super helpful. Thank you

@joshwlambert
Copy link

I'm using {distributional} as a dependency in the {epiparameter} package and would also find the get_base_families() a super helpful function to be included in {distributional}.

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

No branches or pull requests

3 participants