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

parameters2natural and natural2parameters #88

Closed
rho62 opened this issue Feb 26, 2022 · 11 comments
Closed

parameters2natural and natural2parameters #88

rho62 opened this issue Feb 26, 2022 · 11 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@rho62
Copy link
Contributor

rho62 commented Feb 26, 2022

The two functions takes an argument of class "trunc_XXXX"

IF this should be maintained, this should be stated in the help file.
BUT I'm not sure that the function should be restricted to this class.
Perhaps there should be a class that distinguish beween the "natural" and "original" parameters!? (Btw "original" is not the best name) So that parameter2natural can only convert from the orignal to the natural scale and vice versa for natural2parameters.
For example: The example in the help file for natural2parameter:
samp <- rtrunc(n = 100, lambda = 2, family = "Poisson")

natural2parameters(init.parms(samp))
[1] 7.170676

This converts a parameter, which is on the "original" scale as if it was on the natural scale - makes no sense.

Perhaps we should just have more flexible functions (less class restrictive) that takes ordinary numeric vectors and converts between the two scales. Similarly to the dtrunc, rtrunc ... we could have a "family" parameter with a default "gaussian".

@wleoncio
Copy link
Member

wleoncio commented Mar 2, 2022

I agree that the example for natural2parameters() is rather non-sensical, maybe it should be something like the following?

samp <- rtrunc(n = 100, lambda = 2, family = "Poisson")
eta <- parameters2natural(samp)
natural2parameters(eta)

Then we'd show that the last line above is basically the same as samp.

@rho62
Copy link
Contributor Author

rho62 commented Mar 2, 2022 via email

@wleoncio
Copy link
Member

wleoncio commented Mar 3, 2022

I get it now. Sounds simple enough to add a family argument to those two functions so they also accept common numeric vectors.

@wleoncio wleoncio added the enhancement New feature or request label Apr 7, 2022
wleoncio added a commit that referenced this issue Sep 21, 2022
As pointed out by OP, the previous one made little sense.
wleoncio added a commit that referenced this issue Sep 21, 2022
This allows the function to be more flexible (https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Generic-functions-and-methods
itself says that "there need to be good arguments not to include a … argument"). More importantly for issue #88 and this commit in particular, this allows the `trunc_binomial` method to check for the presence of `nsize`.
wleoncio added a commit that referenced this issue Sep 21, 2022
So it can be used by other functions, which is expected in #88.
@wleoncio
Copy link
Member

To help solve this, commit 5ff8560 adds a function called extractParameters(), which takes in a numeric vector and, depending on the values passed to the family and natural arguments, will return an estimate for the parameters based on the data. I still need to write tests for all supported distributions to make sure this is working as expected, but I hope this is on the right track. A user can check example(extractParameters) to see an example of usage.

What I am imagining right now is that, from a user-level, extractParameters() will replace parameters2natural() and natural2parameters(). The latter two functions would still exist in the package, since they are indirectly called by the former.

@rho62
Copy link
Contributor Author

rho62 commented Oct 11, 2022 via email

@wleoncio
Copy link
Member

wleoncio commented Mar 28, 2023

  • Create a "distribution" class with some attributes (parms values, is_natural boolean, family?)
  • Have that class initiation do the conversion of parameters
  • Create a print method that prints the parameters (including natural)

wleoncio added a commit that referenced this issue Mar 29, 2023
Natural parameters are detected by the name of the parameters passed (they must begin with "eta")
wleoncio added a commit that referenced this issue Mar 29, 2023
wleoncio added a commit that referenced this issue Mar 29, 2023
wleoncio added a commit that referenced this issue Mar 29, 2023
Namely:
- Beta
- Binomial
- Chisq
- Contbern
- Exponential
- Gamma
- Log-normal
- Normal
wleoncio added a commit that referenced this issue Mar 29, 2023
@wleoncio
Copy link
Member

Hi René,

I have finished the implementation outlined in our previous meeting. The idea is that the user calls the function probdist() with a named set of parameters and family and the function does the conversion to natural (or back, if the latter is provided). See the example below for reproducibility:

# Installing and loading the package ===========================================

remotes::install_github("ocbe-uio/TruncExpFam")
#> Using github PAT from envvar GITHUB_PAT
#> Skipping install of 'TruncExpFam' from a github remote, the SHA1 (46fa8b24) has not changed since last install.
#>   Use `force = TRUE` to force installation
library("TruncExpFam")
#> Welcome to TruncExpFam 1.0.0.9025.
#> Please read the documentation on ?TruncExpFam to learn more about the package.

# Examples ====================================================================

probdist(mean = 5, sd = .1, family = "normal")
#> Family:             Normal
#> Parameters:         mean = 5   sd   = 0.1 
#> Natural parameters: eta1 = 500 eta2 = -50
probdist(eta1 = 1, eta2 = -1, family = "normal")
#> Family:             Normal
#> Parameters:         mean = 0.5               sd   = 0.7071            
#> Natural parameters: eta1 = 1                 eta2 = -1

Please let me know how this looks. If approved, I'll close the issue.

@rho62
Copy link
Contributor Author

rho62 commented Mar 31, 2023 via email

@rho62
Copy link
Contributor Author

rho62 commented Mar 31, 2023 via email

@wleoncio wleoncio added this to the MVP 1.1.0 milestone Apr 13, 2023
@wleoncio wleoncio self-assigned this Apr 13, 2023
@wleoncio
Copy link
Member

Hi Rene, thanks for your feedback. To address your questions:

Why is it called “probdist”? The intuition is not clear to me. The function should just swap between the two scales. How about “swapscale” or “convertscale”? Any other suggestions?

The implementation was such that probdist() creates an object of class "probdist" (stands for "probability distribution") which contains its own attributes (family, parameters) and methods (initialize, print, convert...). We could definitely change the implementation so it's a simple function called swapscale().

Instead of having the parameters (both input and output) as two scalars, I’d prefer them as two vectors (which for some distribution will be of length 1 and for others of length 2 and might potentially be of other lengths). Look at the 2nd and third column of the table at the https://en.wikipedia.org/wiki/Exponential_family . The 2nd is just called “parameter” and the 3rd “Natural parameters” . We need another name instead of just “parameter”. I suggest “ordinary” and the other will be kept as “natural”. So either two functions “ordinary2natural” and “natural2ordinary”, each taking a vector and returning a vector, and having a distribution (familily) variable that detects which specific conversion to use or a single function taking and returning vector and having a distribution variable (family) and a direction variable taking values ‘natural2ordinary’ and ‘ordinary2natural’. There might be some vaidity checking (eg. the Poisson rate lambda cannot be negative and alike)

We already have two generic functions, parameters2natural() and natural2parameters() that do this, except they take only one argument each (parms and eta, respectively). I could simply edit the generic so it also takes a family parameter and calls the correct method depending on its value. Here's an example of how it currently works (needs some polishing, since the conversion functions are currently internal):

bilde

I’d prefer a more simple output: No need for info on the family info or info about the input variable.

Can do, though if we're simply going to use the functions from the point above, we're probably better off just scrapping probdist() altogether.

wleoncio added a commit that referenced this issue Aug 3, 2023
wleoncio added a commit that referenced this issue Aug 3, 2023
@wleoncio
Copy link
Member

wleoncio commented Aug 3, 2023

Hi, René! Please take a look at this new implementation, version 1.0.0.9027, which can be installed by

remotes::install_github("ocbe-uio/TruncExpFam", "issue-88")

Usage now is simpler and hopefully closer to how you imagined it. Basically, the user just supplies a named vector of parameters and a family, and the function (parameters2natural() or natural2parameters()) does the proper conversion. Here are a couple of examples:

bilde

wleoncio added a commit that referenced this issue Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants