Skip to contents

Calculate posterior probabilities and probability densities, cumulative posterior probabilities, and mixtures thereof. Output the "revisability" of such probabilities if more training data were available, and the Monte Carlo Standard Error for the calculated posterior probabilities.

Usage

Pr(
  Y,
  X = NA,
  K = NULL,
  tails = NULL,
  priorY = NULL,
  nsamples = "all",
  quantiles = c(0.055, 0.25, 0.75, 0.945),
  parallel = TRUE,
  sep = ",",
  solidus = "|",
  verbose = FALSE,
  keepYX = TRUE
)

Arguments

Y

Matrix or data.table: set of values of variates whose probabilities are sought. One variate per column, one set of values per row.

X

Matrix or data.table, or NULL or NA (default): set of values of variates in the conditional of the probability of Y. If NA or NULL, no conditioning is made (besides the conditioning on knowledge K). One variate per column, one set of values per row. See "Details" for the interpretation of unnamed arguments.

K

One of these alternatives:

  • A "K" (more precisely "prova_K") object.

  • A character string of the path to an RDS file (typically created by learn()) containing a valid "K" object.

  • A character string of the path to a directory (also typically created by learn()) containing such an RDS file of a "K" object.

  • For function getK() it can also be NULL or omitted, if a default K = argument has been defined with useK().

Alternatively, a default K = argument can be set with useK(), in which case the K = argument may be omitted. See "Details" for the interpretation of unnamed arguments.

tails

Named vector or list, or NULL (default). The names must match some or all of the variates in arguments Y and X. For variates in this list, the probability arguments are understood in a semi-open interval sense: \(Y \le y\) or \(Y \ge y\), an so on. This is true for Y and X variates (on the left and on the right of the conditional sign \(\,\vert\,\)). A left-open interval \(Y \le y\) is indicated by '<=' or 'lower' or'left' or -1; a right-open interval \(Y \ge y\) is indicated by '>=' or 'upper' or 'right' or +1. Values NULL, '==', 0 indicate that a point value Y = y (not an interval) should be calculated. Warning: the semi-open intervals always include the given value; this is important for ordinal or rounded variates. For instance, if \(Y\) is an integer variate, then to calculate \(\mathrm{Pr}(Y < 3)\) the user should require \(\mathrm{Pr}(Y \le 2)\); for this reason we also have that \(\mathrm{Pr}(Y \le 2)\) and \(\mathrm{Pr}(Y \ge 2)\) generally add up to more than 1.

priorY

Numeric vector with the same length as the rows of Y, or TRUE, or NULL (default): prior probabilities or base rates for the Y values. If TRUE, the prior probabilities are assumed to be all equal.

nsamples

Integer or NULL or 'all' (default): desired number of samples of the revisability of the probability for Y. If NULL or 0, no samples are reported. If 'all' or Inf, all samples obtained by the learn() function are used.

quantiles

Numeric vector, between 0 and 1, or NULL: desired quantiles of the revisability of the probability for Y. Default c(0.055, 0.25, 0.75, 0.945), that is, the 5.5%, 25%, 75%, 94.5% quantiles. These are typical quantile values in the Bayesian literature: they give 50% and 89% credibility intervals, which correspond to 1 shannons and 0.5 shannons of uncertainty (see doi:10.5281/zenodo.17072199). If NULL, no quantiles are calculated.

parallel

One of the following values:

  • A "cluster" object previously created with parallel::makeCluster().

  • Positive integer: create a parallel cluster with this number of nodes (it will be stopped at the end).

  • FALSE: do not use clusters (one node is still generated, in order to eliminate temporary objects from the computation).

  • TRUE (default): use the cluster that was set as default with parallel::setDefaultCluster(); if no such object exists, then generate a cluster with as many nodes as in the option "cl.cores"; if this option is unset, then use 2 nodes.

sep

character, default ',': character to separate the output's variate names and values.

solidus

character, default '|': character prepended to the output's names of the variates in the conditional (typically the X variates).

verbose

Logical, default FALSE: give messages about parallel processing?

keepYX

Logical, default TRUE: keep a copy of the Y and X arguments in the output? This is used for plot.prova_pr().

Value

An object of class "prova_pr" (probability), which is a list consisting of the following elements:

  • 'value': a matrix with the probabilities \(\mathrm{Pr}(Y = y \vert X = x, K)\), for all joint values \(y\) of the \(Y\)-variates (rows) and all joint values \(x\) of the \(X\)-variates (columns).

  • 'quantiles' (possibly NULL): an array with the revisability quantiles (3rd dimension of the array) for such probabilities.

  • 'samples' (possibly NULL): an array with the revisability samples (3rd dimension of the array) for such probabilities.

  • 'value.acc', quantiles.acc: arrays with the numerical accuracies (roughly speaking a standard deviation) of the Monte Carlo calculations for the 'values' and 'quantiles' elements.

  • 'density': numerical vector as long as number of rows in Y, used mainly for plot.prova_pr(). It is the order of the probability density the Y-values: values with 0 are actual probabilities; values with 1 are one-dimensional probability densities \(\mathrm{p}(\dotso)\,\mathrm{d}y\); values with 2 are two-dimensional probability densities \(\mathrm{p}(\dotso)\,\mathrm{d}y_1\,\mathrm{d}y_2\); and so on.

  • 'Y', 'X', 'tails': copies of the Y, X, tails arguments.

  • 'K': name of the "K" object used in the calculation.

Details

This function calculates the posterior probability \(\mathrm{Pr}(Y = y \vert X = x, K)\), where \(Y = y\) and \(X = x\) are two (non overlapping) sets of joint variate values, inputted as data frame arguments Y and X, and \(K\) is the information in the data and metadata. It is somewhat analogous to the d...-variants and p...-variants of R distribution functions. If X is NA, then the posterior probability \(\mathrm{Pr}(Y = y \vert K)\) is calculated.

Tail probabilities. For some variates in Y or X, tail values can also be prescribed, so that this function calculates mixed probabilities such as $$\mathrm{Pr}(Y_1 = y_1, Y_2 \le y_2, \dotsc \vert X_1 = x_1, X_2 \ge x_2, \dotsc, K)\ .$$ Tail values are inputted via the 'tails' argument; see "Usage". Note that nominal variates, strictly speaking, have no meaningful ordering, so the "\(\ge\)" and "\(\le\)" relations are not meaningful for them. For this kind of variates the "\(\ge\)" relation is understood in the sense of the value order appearing in the metadata file.

Arguments. A default value for the K = argument can be set (and unset) with useK(). When this is done, the K = argument may be omitted, saving a lot of K = ...-typing in this and other functions.

Therefore if Pr() is called with one unnamed argument, Pr(...), then it is interpreted as Pr(Y = ...), with X = NA and the default K = argument set by useK(). If Pr() is called with two unnamed arguments, Pr(..., ...), then it is interpreted as Pr(Y = ..., X = ...) and the default K = argument set by useK(). If it is called with three unnamed arguments, then it is interpreted as Pr(Y = ..., X = ..., K = ...), with K explicitly given, overriding any default set by useK(). In order to specify Y and K without naming the arguments, the form Pr(..., NA, ...) must be used, explicitly giving X as second argument.

Revisabilities. This function also outputs the "revisability" of the posterior probabilities above, that is, probabilities such as \(\mathrm{Pr}(Y = y \vert X = x, \text{new data}, K)\) that we could have if more learning data were provided, as well as a number of samples of the possible values of such probability. This revisability can be outputted in two ways; the user can choose either, or both, or none:

  • As samples (default 3600 samples, depending on the 'nsamples' argument given to the learn() function) of the alternative values that the posterior probability could have.

  • As quantiles (default 5.5%, 25%, 75%, 94.5%) of the possible revisability.

If several joint values are given for Y or X, the function will create a 2D grid of results for all possible combinations of the given Y and X values.

Prior specification. This function also allows for base-rate or other prior-probability corrections: If a prior (for instance, a base rate) for the data corresponding to rows Y is given, the function will calculate the probability \(\mathrm{Pr}(Y = y \vert X = x, K, \text{prior})\) from \(\mathrm{Pr}(X = x \vert Y = y, K)\) and the prior, by means of Bayes's theorem $$\mathrm{Pr}(Y = y \vert X = x, K, \text{prior}) = \frac{ \mathrm{Pr}(X = x \vert Y = y, K) \cdot \mathrm{Pr}(Y = y \vert \text{prior}) }{ \sum_{y'} \mathrm{Pr}(X = x \vert Y = y', K) \cdot \mathrm{Pr}(Y = y' \vert \text{prior}) } \ .$$ Important: any values not present in the Y data frame are given zero prior probability. In other words, the normalization \(\sum_{y'}\) only counts the \(y'\) values appearing in the data frame Y.

Each variate in each argument Y, X can be specified either as a point-value \(Y = y\) or as a left-open interval \(Y \le y\) or as a right-open interval \(Y \ge y\), through the argument tails.

See vignette('intro') for example uses.

References

See also

learn(), to generate the Knowledge objects required by Pr().

useK() to set a "K" object as default in all K = arguments.

plot.prova_pr() to plot probabilities and quantiles calculated by Pr().

hist.prova_pr() to plot histograms of the probability distributions calculated by Pr().

print.prova_pr() to print the main elements of the probabilities calculated by Pr().

qPr() to calculate quantiles for a specific variate, that is, the variate values having given probabilities.

rPr() to generate datapoints.

Kexample an example "K" object included with Prova.

Examples


## Use the "K" object 'Kexample' included in *Prova*,
## calculated from the "penguins" dataset.
## Variates: 'species' and 'bill_len'

## ## Example 1:
## Probability that a new penguin from this population is of species 'Adelie'
probs <- Pr(Y = data.frame(species = 'Adelie'), K = Kexample)
## alternatively: Pr(data.frame(species = 'Adelie'), NA, Kexample)

## display the probability value
probs$value
#>         
#> species           
#>   Adelie 0.4417818

## The full-population frequency of 'Adelie' penguins is unknown.
## Display the 5.5%- and 94.5%-probability values for such frequency:
## thus the true frequency is, with 89% probability, in this range:
probs$quantiles[, , c('5.5%', '94.5%')]
#>      5.5%     94.5% 
#> 0.3993814 0.4865208 

## we can also plot the probability distribution
## for this full-population frequency
hist(probs, legend = 'topright')



## ## Example 2:
## The probabilities that a new penguin from this population
## is of species 'Adelie', 'Chinstrap', 'Gentoo'
probs <- Pr(Y = data.frame(species = c('Adelie', 'Chinstrap', 'Gentoo')), K = Kexample)
## alternatively:
## Pr(data.frame(species = c('Adelie', 'Chinstrap', 'Gentoo')), NA, Kexample)

## display the 3 probability values
probs$value
#>            
#> species              
#>   Adelie    0.4417818
#>   Chinstrap 0.1976989
#>   Gentoo    0.3605192

## The full-population frequencies of the three species are unknown.
## Display the 5.5%- and 94.5%-probability values for such frequencies.
## thus the true frequencies are, with 89% probability, in these ranges:
probs$quantiles[, , c('5.5%', '94.5%')]
#>            Q
#> species          5.5%     94.5%
#>   Adelie    0.3993814 0.4865208
#>   Chinstrap 0.1622504 0.2375967
#>   Gentoo    0.3162802 0.4034252

## plot the probabilities and the ranges for the frequencies as bands:
plot(probs)


## plot the probability distributions for the full-population frequencies
hist(probs)



## ## Example 3:
## Calculate the probability that a new penguin is of species 'Adelie'
## *among those with bill length = 43 mm* (subpopulation)
probs <- Pr(data.frame(species = 'Adelie'), data.frame(bill_len = 43), Kexample)
## alternatively: Pr(Y = ..., X = ..., K = Kexample)

## display the probability value
probs$value
#>         |bill_len
#> species         43
#>   Adelie 0.4581298

## The frequency of 'Adelie' penguins, in the subpopulation of
## penguins with a bill length of 43 mm, is unknown.
## Display the 5.5%- and 94.5%-probability values for such frequency:
## thus the true frequency is, with 89% probability, in this range:
probs$quantiles[, , c('5.5%', '94.5%')]
#>      5.5%     94.5% 
#> 0.3529635 0.5550730 


## ## Example 4:
## Calculate the probability that a new penguin
## is of species 'Adelie' AND its bill length is 43 mm
probs <- Pr(Y = data.frame(species = 'Adelie', bill_len = 43), K = Kexample)

## display the probability value
probs$value
#>                 
#> species,bill_len            
#>        Adelie,43 0.001802924

## display the 89%-probability range for the frequency
## of 'Adelie' penguins with 43 mm bills
probs$quantiles[, , c('5.5%', '94.5%')]
#>        5.5%       94.5% 
#> 0.001292764 0.002354646 


## ## Example 5:
## Calculate the 3 x 2 probabilities for the 3 species
## *among penguins having bill-lengths of 43 mm and of 44 mm*
Y <- data.frame(species = c('Adelie', 'Chinstrap', 'Gentoo'))
X <- data.frame(bill_len = c(43, 44))
probs <- Pr(Y, X, Kexample)

## display the 3 x 2 probability values
probs$value
#>            |bill_len
#> species            43        44
#>   Adelie    0.4581298 0.2170985
#>   Chinstrap 0.1517864 0.2165196
#>   Gentoo    0.3900838 0.5663819

## display the 89%-probability range for the six joint population frequencies
probs$quantiles[, , c('5.5%', '94.5%')]
#> , , Q = 5.5%
#> 
#>            |bill_len
#> species             43        44
#>   Adelie    0.35296349 0.1429274
#>   Chinstrap 0.09101603 0.1404368
#>   Gentoo    0.28845903 0.4539498
#> 
#> , , Q = 94.5%
#> 
#>            |bill_len
#> species            43        44
#>   Adelie    0.5550730 0.2948054
#>   Chinstrap 0.2205274 0.3053230
#>   Gentoo    0.5009354 0.6728543
#> 

## plot the probabilities and frequency ranges
plot(probs)



## ## Example 6:
## Calculate the 3 x 2 joint probabilities for the 3 species
## AND bill-lengths of 43 mm and 44 mm
## Create a grid of values
Y <- expand.grid(
  species = c('Adelie', 'Chinstrap', 'Gentoo'),
  bill_len = c(43, 44)
)
## X is not given
probs <- Pr(Y, NA, Kexample)

## display the 6 joint-probability values
probs$value
#>                 
#> species,bill_len             
#>     Adelie,43    0.0018029242
#>     Chinstrap,43 0.0005961597
#>     Gentoo,43    0.0015347297
#>     Adelie,44    0.0009426087
#>     Chinstrap,44 0.0009367631
#>     Gentoo,44    0.0024665069

## display the 89%-probability ranges the full-population joint frequencies
probs$quantiles[, , c('5.5%', '94.5%')]
#>                 Q
#> species,bill_len         5.5%        94.5%
#>     Adelie,43    0.0012927641 0.0023546458
#>     Chinstrap,43 0.0003372492 0.0008502564
#>     Gentoo,43    0.0011132888 0.0021235183
#>     Adelie,44    0.0005943976 0.0013462921
#>     Chinstrap,44 0.0005728778 0.0013017380
#>     Gentoo,44    0.0018513190 0.0032382762


## ## Example 7:
## To omit 'K =' arguments, set a default 'K =' value with useK()
useK(Kexample)

## Check against Example 1 above
probs <- Pr(data.frame(species = 'Adelie'))
probs$values # same as values from Example 1
#> NULL
probs$quantiles[, , c('5.5%', '94.5%')] # same as range from Example 1
#>      5.5%     94.5% 
#> 0.3993814 0.4865208 

## Check against Example 3 above
probs <- Pr(data.frame(species = 'Adelie'), data.frame(bill_len = 43))
probs$values # same as values from Example 3
#> NULL
probs$quantiles[, , c('5.5%', '94.5%')] # same as range from Example 3
#>      5.5%     94.5% 
#> 0.3529635 0.5550730 

#' ## Remove any default 'K =' argument
useK(NA)