The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

pbox Package Vignette

Ahmed T. Hammad

2024-05-16

Introduction

The pbox package is designed for probabilistic modeling and scenario analysis using Probability Boxes (p-boxes). This vignette demonstrates the main functions of the pbox package, from loading and visualizing data to creating and querying a pbox object.

Load and Prepare Data

First, we load the SEAex dataset included in the pbox package. We add a Year column and then reshape the data for plotting.


library(pbox)
#> Loading required package: gamlss.dist
library(data.table)
library(ggplot2)

data("SEAex", package = "pbox")
SEAex$Year <- 1901:2022
SEAex_long <- melt(SEAex, id.vars = "Year", variable.name = "Country")

Visualize Data

We use ggplot2 to create a time series plot of the temperature data for each country.

ggplot(SEAex_long, aes(x = Year, y = value, color = Country)) +
  geom_line(color = "black") +  # Set all lines to black
  labs(x = "Year", y = "Temperature °C") +
  ggtitle("") +
  facet_grid(Country ~ ., scales = "free_y") +
  theme(legend.position = "none", panel.spacing.y = unit(10, "pt")) +
  theme_bw()

Create PBOX Object

We create a pbox object from the SEAex dataset using the set_pbox function.

# Set pbox
pbx <- set_pbox(SEAex)
#> All features are continuous.
#>   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   2%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   8%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  12%  |                                                                              |==========                                                            |  14%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  18%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  24%  |                                                                              |==================                                                    |  25%  |                                                                              |===================                                                   |  27%  |                                                                              |=====================                                                 |  29%  |                                                                              |======================                                                |  31%  |                                                                              |=======================                                               |  33%  |                                                                              |=========================                                             |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  39%  |                                                                              |=============================                                         |  41%  |                                                                              |==============================                                        |  43%  |                                                                              |================================                                      |  45%  |                                                                              |=================================                                     |  47%  |                                                                              |==================================                                    |  49%  |                                                                              |====================================                                  |  51%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  55%  |                                                                              |========================================                              |  57%  |                                                                              |=========================================                             |  59%  |                                                                              |===========================================                           |  61%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  65%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  71%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[1,1] = 0
#>   |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  75%  |                                                                              |======================================================                |  76%  |                                                                              |=======================================================               |  78%  |                                                                              |========================================================              |  80%  |                                                                              |==========================================================            |  82%  |                                                                              |===========================================================           |  84%  |                                                                              |============================================================          |  86%  |                                                                              |==============================================================        |  88%  |                                                                              |===============================================================       |  90%  |                                                                              |=================================================================     |  92%  |                                                                              |==================================================================    |  94%  |                                                                              |===================================================================   |  96%  |                                                                              |===================================================================== |  98%  |                                                                              |======================================================================| 100%
#>   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   2%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   8%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  12%  |                                                                              |==========                                                            |  14%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  18%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  24%  |                                                                              |==================                                                    |  25%  |                                                                              |===================                                                   |  27%  |                                                                              |=====================                                                 |  29%  |                                                                              |======================                                                |  31%  |                                                                              |=======================                                               |  33%  |                                                                              |=========================                                             |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  39%  |                                                                              |=============================                                         |  41%  |                                                                              |==============================                                        |  43%  |                                                                              |================================                                      |  45%  |                                                                              |=================================                                     |  47%  |                                                                              |==================================                                    |  49%  |                                                                              |====================================                                  |  51%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  55%  |                                                                              |========================================                              |  57%  |                                                                              |=========================================                             |  59%  |                                                                              |===========================================                           |  61%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  65%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  71%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[1,1] = 0
#>   |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  75%  |                                                                              |======================================================                |  76%  |                                                                              |=======================================================               |  78%  |                                                                              |========================================================              |  80%  |                                                                              |==========================================================            |  82%  |                                                                              |===========================================================           |  84%  |                                                                              |============================================================          |  86%  |                                                                              |==============================================================        |  88%  |                                                                              |===============================================================       |  90%  |                                                                              |=================================================================     |  92%  |                                                                              |==================================================================    |  94%  |                                                                              |===================================================================   |  96%  |                                                                              |===================================================================== |  98%  |                                                                              |======================================================================| 100%
#>   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   2%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   8%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  12%  |                                                                              |==========                                                            |  14%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  18%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  24%  |                                                                              |==================                                                    |  25%  |                                                                              |===================                                                   |  27%  |                                                                              |=====================                                                 |  29%  |                                                                              |======================                                                |  31%  |                                                                              |=======================                                               |  33%  |                                                                              |=========================                                             |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  39%  |                                                                              |=============================                                         |  41%  |                                                                              |==============================                                        |  43%  |                                                                              |================================                                      |  45%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[4,4] = 0
#>   |                                                                              |=================================                                     |  47%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[4,4] = 0
#>   |                                                                              |==================================                                    |  49%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[4,4] = 0
#>   |                                                                              |====================================                                  |  51%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  55%  |                                                                              |========================================                              |  57%  |                                                                              |=========================================                             |  59%  |                                                                              |===========================================                           |  61%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  65%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  71%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[1,1] = 0
#>   |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  75%  |                                                                              |======================================================                |  76%  |                                                                              |=======================================================               |  78%  |                                                                              |========================================================              |  80%  |                                                                              |==========================================================            |  82%  |                                                                              |===========================================================           |  84%  |                                                                              |============================================================          |  86%  |                                                                              |==============================================================        |  88%  |                                                                              |===============================================================       |  90%  |                                                                              |=================================================================     |  92%  |                                                                              |==================================================================    |  94%  |                                                                              |===================================================================   |  96%  |                                                                              |===================================================================== |  98%  |                                                                              |======================================================================| 100%
#>   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   2%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   8%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  12%  |                                                                              |==========                                                            |  14%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  18%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  24%  |                                                                              |==================                                                    |  25%  |                                                                              |===================                                                   |  27%  |                                                                              |=====================                                                 |  29%  |                                                                              |======================                                                |  31%  |                                                                              |=======================                                               |  33%  |                                                                              |=========================                                             |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  39%  |                                                                              |=============================                                         |  41%  |                                                                              |==============================                                        |  43%  |                                                                              |================================                                      |  45%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[4,4] = 0
#>   |                                                                              |=================================                                     |  47%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[4,4] = 0
#>   |                                                                              |==================================                                    |  49%  |                                                                              |====================================                                  |  51%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  55%  |                                                                              |========================================                              |  57%  |                                                                              |=========================================                             |  59%  |                                                                              |===========================================                           |  61%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  65%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  71%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[1,1] = 0
#>   |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  75%  |                                                                              |======================================================                |  76%  |                                                                              |=======================================================               |  78%  |                                                                              |========================================================              |  80%  |                                                                              |==========================================================            |  82%  |                                                                              |===========================================================           |  84%  |                                                                              |============================================================          |  86%  |                                                                              |==============================================================        |  88%  |                                                                              |===============================================================       |  90%  |                                                                              |=================================================================     |  92%  |                                                                              |==================================================================    |  94%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[4,4] = 0
#>   |                                                                              |===================================================================   |  96%  |                                                                              |===================================================================== |  98%  |                                                                              |======================================================================| 100%
#>   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   2%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   8%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  12%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[3,3] = 0
#>   |                                                                              |==========                                                            |  14%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  18%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  24%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[3,3] = 0
#>   |                                                                              |==================                                                    |  25%  |                                                                              |===================                                                   |  27%  |                                                                              |=====================                                                 |  29%  |                                                                              |======================                                                |  31%  |                                                                              |=======================                                               |  33%  |                                                                              |=========================                                             |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  39%  |                                                                              |=============================                                         |  41%  |                                                                              |==============================                                        |  43%  |                                                                              |================================                                      |  45%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[4,4] = 0
#>   |                                                                              |=================================                                     |  47%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[4,4] = 0
#>   |                                                                              |==================================                                    |  49%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[4,4] = 0
#>   |                                                                              |====================================                                  |  51%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[3,3] = 0
#>   |                                                                              |=====================================                                 |  53%Error in solve.default(oout$hessian) : 
#>   system is computationally singular: reciprocal condition number = 1.61756e-16
#>   |                                                                              |======================================                                |  55%  |                                                                              |========================================                              |  57%  |                                                                              |=========================================                             |  59%  |                                                                              |===========================================                           |  61%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  65%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  71%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[1,1] = 0
#>   |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  75%  |                                                                              |======================================================                |  76%  |                                                                              |=======================================================               |  78%  |                                                                              |========================================================              |  80%  |                                                                              |==========================================================            |  82%  |                                                                              |===========================================================           |  84%  |                                                                              |============================================================          |  86%  |                                                                              |==============================================================        |  88%  |                                                                              |===============================================================       |  90%  |                                                                              |=================================================================     |  92%Error in solve.default(oout$hessian) : 
#>   Lapack routine dgesv: system is exactly singular: U[4,4] = 0
#>   |                                                                              |==================================================================    |  94%  |                                                                              |===================================================================   |  96%  |                                                                              |===================================================================== |  98%  |                                                                              |======================================================================| 100%
#> 
#> 
#> ---Final fitted copula---
#> Copula Type: ellipCopula 
#> Family: normal 
#> parameter: 0.4568983 
#> --------------------------
#> pbox object generated!
print(pbx)
#> Probabilistic Box Object of class pbox
#> 
#> ||--General Overview--||
#> ----------------
#> 1)Data Structure
#> Number of Rows:  122 
#> Number of Columns:  5 
#> 
#> 1.1)Variable Statistics:
#>          var     min     max       mean  median
#>       <char>   <num>   <num>      <num>   <num>
#> 1:  Malaysia   30.50   32.30   31.24344   31.20
#> 2:  Thailand   33.20   37.30   35.10656   35.10
#> 3:   Vietnam   30.90   32.90   31.63934   31.60
#> 4: avgRegion   25.21   26.66   25.78951   25.73
#> 5:      Year 1901.00 2022.00 1961.50000 1961.50
#> 
#> ----------------
#> 2)Copula Summary:
#> Type: ellipCopula 
#> Normal copula, dim. d = 5 
#> Dimension:  5 
#> Parameters:
#>   rho.1   = 0.4568983
#> dispstr:  ex 
#> 
#> 2.1)Copula margins:
#> [1] "RG"     "SN1"    "RG"     "RG"     "SHASHo"
#> 2.1)Kendall correlation:
#>            Malaysia    Thailand   Vietnam avgRegion        Year
#> Malaysia  1.0000000 0.175537766 0.3864290 0.5751234 0.377308066
#> Thailand  0.1755378 1.000000000 0.2246915 0.2472509 0.001384308
#> Vietnam   0.3864290 0.224691517 1.0000000 0.4424894 0.280789004
#> avgRegion 0.5751234 0.247250903 0.4424894 1.0000000 0.422692241
#> Year      0.3773081 0.001384308 0.2807890 0.4226922 1.000000000
#> 
#> -------------------------------

Explore Probability Space

We can query the probabilistic space of the pbox object using the qpbox function. Below are examples of different types of queries.

# Marginal Distribution

qpbox(pbx, mj = "Malaysia:33")
#>         P 
#> 0.9986981

# Joint Distribution

qpbox(pbx, mj = "Malaysia:33 & Vietnam:34")
#>         P 
#> 0.9981035

# Conditional Distribution

qpbox(pbx, mj = "Vietnam:31", co = "avgRegion:26")
#>          P 
#> 0.03647037

#Conditional Distribution with Fixed Conditions

qpbox(pbx, mj = "Malaysia:33 & Vietnam:31", co = "avgRegion:26", fixed = TRUE)
#>         P 
#> 0.9688897

#Joint Distribution with Mean Values

qpbox(pbx, mj = "mean:c(Vietnam, Thailand)", lower.tail = TRUE)
#>         P 
#> 0.3740338

# Joint Distribution with Median Values

qpbox(pbx, mj = "median:c(Vietnam, Thailand)", lower.tail = TRUE)
#>        P 
#> 0.353339

# Joint Distribution with Specific Values

qpbox(pbx, mj = "Malaysia:33 & mean:c(Vietnam, Thailand)", lower.tail = TRUE)
#>         P 
#> 0.3740201

# Conditional Distribution with Mean Conditions

qpbox(pbx, mj = "Malaysia:33 & median:c(Vietnam,Thailand)", co = "mean:c(avgRegion)")
#>         P 
#> 0.6217401

Confidence Intervals

qpbox(pbx, mj = "Malaysia:33 & median:c(Vietnam,Thailand)", co = "mean:c(avgRegion)", CI = TRUE, fixed = TRUE)
#>         P      2.5%     97.5% 
#> 0.6505089 0.5493915 0.7573722

Scenario Analysis

We perform scenario analysis by modifying underlying parameters of the pbox object.

scenario_results <- scenario_pbox(pbx, mj = "Vietnam:31 & avgRegion:26", param_list = list(Vietnam = "mu"))
print(scenario_results)
#> $`SD-3`
#>          P 
#> 0.09561431 
#> 
#> $`SD-2`
#>          P 
#> 0.06737206 
#> 
#> $`SD-1`
#>          P 
#> 0.04488868 
#> 
#> $SD0
#>          P 
#> 0.02803826 
#> 
#> $SD1
#>        P 
#> 0.016256 
#> 
#> $SD2
#>           P 
#> 0.008648929 
#> 
#> $SD3
#>           P 
#> 0.004167532

This vignette demonstrates the main functionalities of the pbox package, including data preparation, visualization, probabilistic querying, and scenario analysis. The pbox package provides powerful tools for probabilistic modeling and analysis, making it a valuable asset for risk assessment and decision-making applications.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.