Skip to contents

Generates datapoints of chosen joint variates, according to posterior probabilities and posterior conditional probabilities.

Usage

rPr(
  n,
  Ynames,
  X = NA,
  K = NULL,
  tails = NULL,
  mcsamples = NULL,
  mcrownames = NULL
)

Arguments

n

Positive integer: number of samples to draw.

Ynames

Character vector: names of variates to draw jointly

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 X. For variates in this list, the probability conditional is understood in a semi-open interval sense: \(X \le x\) or \(X \ge x\), an so on. See analogous argument in Pr().

mcsamples

Either NULL (default, equivalently 'all' or TRUE), or a vector of integers: which Monte Carlo samples calculated by the learn() function should be used to draw the variate values. The default NULL chooses a random subset if n is smaller than their number, otherwise to recycle them as necessary.

mcrownames

Logical: whether the row names of the created data frame should report underlying information about the Monte Carlo samples used to generate the datapoints; see Value section. Default is FALSE if argument mcsamples is NULL, and TRUE otherwise.

Value

A data frame of joint draws of the variates Ynames from the posterior distribution, conditional on what is specified in the X (and tails) argument.

With argument mcrownames = TRUE or argument mcsamples = not NULL, the row names of the data frame report the Monte Carlo sample (from learn()) from which that datapoint was drawn from, and the grand-total of datapoints drawn from that Monte Carlo sample, according to the scheme "sample_total".

Details

This function generates datapoints according to the posterior probability \(\mathrm{Pr}(Y = y \vert X = x, K)\) or \(\mathrm{Pr}(Y = y \vert X \le x, K)\) or combinations thereof, for the variates specified in the argument Y, and conditional on the variate values specified in the argument X. It is somewhat analogous to the r...-variants of R distribution functions. If X is NA, then the posterior probability \(\mathrm{Pr}(Y | K)\) is used.

Each variate in the argument X can be specified either as a point-value \(X = x\) or as a left-open interval \(X \le x\) or as a right-open interval \(X \ge x\), through the argument tails (see Tail probabilities in the Pr() documentation).

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 rPr() is called with two unnamed arguments, rPr(..., ...), then it is interpreted as rPr(n = ..., Ynames = ...), with X = NA and the default K = argument set by useK(). If it is called with three unnamed arguments, then it is interpreted as rPr(n = ..., Ynames = ..., X = ...), with the default K = argument set by useK(). In order to specify n, Ynames, and K without naming the arguments, the form rPr(..., ..., NA, ...) must be used, explicitly giving X as third argument.

See also

learn(), to generate "K" objects required by qPr().

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

Pr() to calculate joint and conditional probabilities.

qPr() to calculate quantiles.

Kexample an example "K" object included with Prova.

base::set.seed() to generate samples reproducibly.

Examples


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

## ## Example 1:
## Generate 10 values of the 'species' variate,
## with the same probability calculated from the data
datapoints <- rPr(n = 10, Ynames = 'species', K = Kexample)
## alternatively: rPr(10, 'species', NA, Kexample)
datapoints
#>      species
#> 1     Adelie
#> 2     Gentoo
#> 3     Adelie
#> 4     Gentoo
#> 5  Chinstrap
#> 6     Adelie
#> 7  Chinstrap
#> 8     Adelie
#> 9     Gentoo
#> 10    Adelie


## ## Example 2:
## Generate 5 joint values of 'species' and 'bill_len' variates.
datapoints <- rPr(5, c('species', 'bill_len'), NA, Kexample)
datapoints
#>     species bill_len
#> 1    Adelie     38.3
#> 2    Gentoo     46.0
#> 3    Adelie     41.5
#> 4 Chinstrap     54.7
#> 5    Adelie     42.0


## ## Example 3:
## Generate 5 values of the 'species' variate,
## among penguins having bill length shorter than or equal to 40 mm

datapoints <- rPr(5, 'species', data.frame(bill_len = 40), Kexample, tails = list(bill_len = '<='))
datapoints
#>   species
#> 1  Adelie
#> 2  Adelie
#> 3  Adelie
#> 4  Adelie
#> 5  Adelie


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

## Generate 10 values of the 'species' variate.
## The datapoints are different from before,
## since we did not set a random seed with 'set.seed()'
datapoints <- rPr(10, 'species')
datapoints
#>      species
#> 1     Gentoo
#> 2     Gentoo
#> 3     Gentoo
#> 4     Adelie
#> 5     Adelie
#> 6     Adelie
#> 7     Gentoo
#> 8     Adelie
#> 9  Chinstrap
#> 10    Gentoo

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