mitchelloharawild/distributional

Distributions differ from {invgamma} / {truncdist}

fkohrt opened this issue · 2 comments

I tried to update a density plot created with {invgamma} and {truncdist} to use {distributional}, but the resulting distributions seem to differ. See the following comparison between the original (black) and the reproduced (red) density plot.

library(invgamma)

x <- seq(5, 400, by=1)
y_truncdist <- truncdist::dtrunc(x,
                                 spec = "invgamma",
                                 shape = 1.15326986,
                                 scale = 0.04622745,
                                 a = 5,
                                 b = 1905)
y_distributional <- distributional::dist_inverse_gamma(shape = 1.15326986,
                                                       scale = 0.04622745) |>
  distributional::dist_truncated(lower = 5, upper = 1905) |>
  density(at = x) |>
  getElement(1)
plot(x, y_distributional, type="n") 
lines(x, y_truncdist, type="l", col="black", lwd=3)
lines(x, y_distributional, type="l", col="red", lwd=3)

comparison

sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Fedora Linux 36 (Workstation Edition)

Matrix products: default
BLAS/LAPACK: /usr/lib64/libflexiblas.so.3.2

locale:
 [1] LC_CTYPE=en_US.UTF-8      
 [2] LC_NUMERIC=C              
 [3] LC_TIME=de_DE.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=de_DE.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=de_DE.UTF-8      
 [8] LC_NAME=C                 
 [9] LC_ADDRESS=C              
[10] LC_TELEPHONE=C            
[11] LC_MEASUREMENT=de_DE.UTF-8
[12] LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices datasets 
[5] utils     methods   base     

other attached packages:
[1] invgamma_1.1

loaded via a namespace (and not attached):
 [1] rstudioapi_0.14      magrittr_2.0.3      
 [3] expint_0.1-7         tidyselect_1.2.0    
 [5] evd_2.3-3            munsell_0.5.0       
 [7] colorspace_2.0-3     colorDF_0.1.7       
 [9] R6_2.5.1             rlang_1.0.6         
[11] fansi_1.0.3          actuar_3.3-0        
[13] dplyr_1.0.10         CoprManager_0.3.10  
[15] tools_4.1.3          truncdist_1.0-2     
[17] grid_4.1.3           gtable_0.3.1        
[19] utf8_1.2.2           DBI_1.1.3           
[21] cli_3.4.1            assertthat_0.2.1    
[23] tibble_3.1.8         lifecycle_1.0.3     
[25] crayon_1.5.2         purrr_0.3.5         
[27] farver_2.1.1         ggplot2_3.3.6       
[29] vctrs_0.4.2          glue_1.6.2          
[31] compiler_4.1.3       pillar_1.8.1        
[33] generics_0.1.3       scales_1.2.1        
[35] stats4_4.1.3         distributional_0.3.1
[37] pkgconfig_2.0.3     

@mitchelloharawild I have identified the reason for the difference: {invgamma} has different semantics for its parameters and leads to the same results as {distributional} when setting scale = 1 / ….

See also dkahle/invgamma#1

Thought I'd tag this response here for future readers. TL;DR: the parameters in invgamma refer to the parameters of the pre-transformed distribution functions, e.g. dgamma().

Fuller explanation here. Sorry for the confusion!