saudiwin / ordbetareg_pack

Repository for R package ordbetareg, used to fit continuous data with lower and upper bounds.
Other
17 stars 3 forks source link

Facilitate usage by exporting family #23

Open DominiqueMakowski opened 3 months ago

DominiqueMakowski commented 3 months ago

Firstly, thanks for the package and the paper :)

I'm trying to fix mixed ordbeta models with formulas (ideally, different formulas) on the location and on phi, but using the ordbeta() wrapper throws an error:

library(brms)
library(ordbetareg)

data <- datawizard::rescale(mtcars, select="wt", to=c(0, 1))

m <- ordbetareg::ordbetareg(wt ~  qsec + (1+qsec|gear),
                            data=data,
                            phi_reg = "both",
                            algorithm="meanfield",
                            init = 0)
#> Error: The following priors do not correspond to any model parameter: 
#> b_phi ~ normal(0,5)
#> Function 'default_prior' might be helpful to you.

While it might be fixed by changing the priors, I found it more straightforward and flexible to use the ordbeta family, made by the internal loard_ordbetareg() function (note that the default argument for phi_reg doesn't work currently, which should probably be fixed if this gets reexported).


# phi_reg must be specified as default throws an error
ordbeta <- ordbetareg:::.load_ordbetareg(phi_reg = "both") 

f <- brms::bf(wt ~  qsec + (1+qsec|gear),
              phi ~ qsec + (1+qsec|gear),
              family=ordbeta$family)

m <- brms::brm(formula=f, data=data, 
               stanvars=ordbeta$stanvars,
               algorithm="meanfield",
               init = 0)
#> Compiling Stan program...
#> Start sampling
#> Chain 1: ------------------------------------------------------------
#> Chain 1: EXPERIMENTAL ALGORITHM:
#> Chain 1:   This procedure has not been thoroughly tested and may be unstable
#> Chain 1:   or buggy. The interface is subject to change.
#> Chain 1: ------------------------------------------------------------
# ...

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

One can also then see and set priors on these models more easily using the standard brms interface. I'd like to suggest exporting load_ordbetareg() (one could rename it for example make_ordbetafamily()), which would make it more accessible and reusable.

Thanks!

saudiwin commented 2 months ago

so this one is a bit tricky as ordbetareg doesn't fit naturally into brms' family interface. That's part of why I had to make a package -- it required some legwork to get the priors right etc.

Can you explain a bit more as to what priors you wanted to set and why? It might be better to let people modify those more easily.

DominiqueMakowski commented 2 months ago

I see, I guess my goal was mainly to be able to set different formulas for location and phi (and why not testing with formulas on the additional auxiliary parameters). If I remember, the models I tested converged nicely without even setting any non-default priors - so tinkering with these was not particularly the goal (in my case).

this one is a bit tricky as ordbetareg doesn't fit naturally into brms' family interface.

Does that mean that re-using family and stanvars is not enough?

saudiwin commented 1 month ago

it does all fit in family and stanvars, but there can be some complications around using ordered parameters that brms doesn't support for custom distributions. That can also cause conflicts with multivariate models etc.

I think I might write a new gist that allows people to load the family separately, but I wouldn't want to include it in the package as it would potentially conflict with the package functions.

saudiwin commented 1 month ago

But I also will take care of the original error as well in the next release.