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.
First, we load the SEAex
dataset included in the
pbox
package. We add a Year column and then reshape the
data for plotting.
We use ggplot2
to create a time series plot of the
temperature data for each country.
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
#>
#> -------------------------------
We can query the probabilistic space of the pbox object using the qpbox function. Below are examples of different types of queries.
#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
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
We can perform a grid search to explore the probabilistic space over a grid of values.
grid_results <- grid_pbox(pbx, mj = c("Vietnam", "Malaysia"))
print(grid_results)
#> Vietnam Malaysia probs
#> <num> <num> <list>
#> 1: 30.9 30.5 0.0001235705
#> 2: 31.2 30.5 0.0004576423
#> 3: 31.3 30.5 0.0005318575
#> 4: 31.4 30.5 0.0005801543
#> 5: 31.5 30.5 0.0006092625
#> ---
#> 117: 31.7 32.3 0.620192
#> 118: 31.8 32.3 0.6975326
#> 119: 32.0 32.3 0.8132672
#> 120: 32.3 32.3 0.9104411
#> 121: 32.9 32.3 0.9725093
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.