tidyverts / fabletools

General fable features useful for extension packages
http://fabletools.tidyverts.org/
89 stars 31 forks source link

handling of <S3: distribution> objects #354

Closed gcoder1990 closed 2 years ago

gcoder1990 commented 2 years ago

Reading the documentation, it is not clear to me how to handle objects.

When producing forecasts, the forecast distribution is stored as an object in the resulting fable. These objects seemed to be handled by the package distributional, but their structure still elludes me.

To be more specific, if x is the first element of the column distribution, when I apply str(x) I get the following output:

str(x)

<distribution[1]>
[1] N(759, 125)
dist [1:1] 
$ :List of 2
 ..$ mu   : num 759
 ..$ sigma: num 11.2
 ..- attr(*, "class")= chr [1:2] "dist_normal" "dist_default"
@ vars: chr "Close"

It is apparently a list, but I am failing miserably at extracting its components. I have tried multiple things, but only unlist has worked for me:

x2 <- unlist(x)
x2[["mu"]]

758.88

Please what would be the right way to do this without having to use unlist?

The output of dput(x) is:

dput(x)

structure(list(structure(list(mu = 758.880005, sigma = 11.1895832820955), class = c("dist_normal", 
"dist_default"))), vars = "Close", class = c("distribution", 
"vctrs_vctr", "list"))

With this you should be able to reproduce the object as follows:

x <- structure(list(structure(list(mu = 758.880005, sigma = 11.1895832820955), class = c("dist_normal", 
"dist_default"))), vars = "Close", class = c("distribution", 
"vctrs_vctr", "list"))
mitchelloharawild commented 2 years ago

You can obtain the distribution's parameters using the parameters() function. More details can be found in the distributional package documentation (https://github.com/mitchelloharawild/distributional/)

gcoder1990 commented 2 years ago

You can obtain the distribution's parameters using the parameters() function. More details can be found in the distributional package documentation (https://github.com/mitchelloharawild/distributional/)

Thanks you for your answer Mitchell.

However, do you have any idea as to why x[[1]]$mu might be returning NULL for me? Shouldn't it be returning the mean?

Thank you and sorry, this issue seems so simple yet I cannot get it to appropriately run as expected.

gcoder1990 commented 2 years ago

I came across the answer: loading the fpp3 package loads tidyverse along with other libraries that override the manner in which the operators $ and [[ operate.

I restarted R and ran the following code and was able to access the elements mu and sigma

x <- structure(list(structure(list(mu = 758.880005, sigma = 11.1895832820955), class = c("dist_normal",  "dist_default"))), vars = "Close", class = c("distribution",  "vctrs_vctr", "list"))

x[[1]]$mu
#> [1] 758.88
mitchelloharawild commented 2 years ago

The distributional package provides a [[.distribution method which doesn't drop the distribution class. If you'd like to peek into the object, you should use vctrs::vec_data().