--- title: "pbox Package Vignette" author: "Ahmed T. Hammad" date: "2024-05-16" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{pbox Package Vignette} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include=FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` # 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. ```{r} library(pbox) 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. ```{r} 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. ```{r} # Set pbox pbx <- set_pbox(SEAex) print(pbx) ``` # 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. ```{r} # Marginal Distribution qpbox(pbx, mj = "Malaysia:33") # Joint Distribution qpbox(pbx, mj = "Malaysia:33 & Vietnam:34") # Conditional Distribution qpbox(pbx, mj = "Vietnam:31", co = "avgRegion:26") #Conditional Distribution with Fixed Conditions qpbox(pbx, mj = "Malaysia:33 & Vietnam:31", co = "avgRegion:26", fixed = TRUE) #Joint Distribution with Mean Values qpbox(pbx, mj = "mean:c(Vietnam, Thailand)", lower.tail = TRUE) # Joint Distribution with Median Values qpbox(pbx, mj = "median:c(Vietnam, Thailand)", lower.tail = TRUE) # Joint Distribution with Specific Values qpbox(pbx, mj = "Malaysia:33 & mean:c(Vietnam, Thailand)", lower.tail = TRUE) # Conditional Distribution with Mean Conditions qpbox(pbx, mj = "Malaysia:33 & median:c(Vietnam,Thailand)", co = "mean:c(avgRegion)") ``` # Confidence Intervals ```{r} qpbox(pbx, mj = "Malaysia:33 & median:c(Vietnam,Thailand)", co = "mean:c(avgRegion)", CI = TRUE, fixed = TRUE) ``` # Grid Search We can perform a grid search to explore the probabilistic space over a grid of values. ```{r} grid_results <- grid_pbox(pbx, mj = c("Vietnam", "Malaysia")) print(grid_results) print(grid_results[which.max(grid_results$probs),]) print(grid_results[which.min(grid_results$probs),]) ``` # Scenario Analysis We perform scenario analysis by modifying underlying parameters of the pbox object. ```{r} scenario_results <- scenario_pbox(pbx, mj = "Vietnam:31 & avgRegion:26", param_list = list(Vietnam = "mu")) print(scenario_results) ``` 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.