Skip to contents

Create a data frame of values for one variate, or a combination of values for several variates.

Usage

vrtgrid(vrt, K = NULL, length.out = NA)

Arguments

vrt

Character vector: names of the variates; they must match variate names in the metadata file provided to the learn() function.

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.

length.out

Vector or list of positive integers or NA values, possibly named: number of values to be created for each variate. Elements with names are used for the homonymous variates in vrt. Unnamed elements are used for the remaining variates, recycled as necessary. See "Details" for the meaning of NA values. Default NA.

Value

A data frame with columns corresponding to the vrt argument, and one row for each combination of the variate values.

Details

The value ranges are based on the information from data and metadata stored in the Knowledge object (see learn()) provided in the K = argument; they include, and extend slightly beyond, the ranges observed in the data used in the learn() function. Variate domains are always respected.

The set of chosen values, for each variate, depends on the type of variate (nominal or continuous, rounded, and so on, see metadata):

  • For a discrete (nominal or ordinal) variate, all possible values are chosen.

  • For a continuous, non-rounded variate, a number of values as specified in the length.out argument; or 257 values if length.out is missing or NA.

  • For a continuous, rounded variate, a number of values as specified in the length.out argument; or, if length.out is missing or NA, the output values are separated by the variates's rounding interval (field datastep in the metadata).

The output is a data frame that can be used directly in functions like Pr().

See also

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

Pr() to calculate probabilities and their revisabilities.

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

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

base::expand.grid(), used internally by vrtgrid(), for a more general way to create a data frame with combination of specified values of several variates.

Kexample an example "K" object included with Prova.

Examples

## Use the "K" object 'Kexample',
## calculated from the "penguins" dataset;
## variates: 'species' and 'bill_len'

## set of values for the variate "species";
## since this variate is of a nominal kind, all values are included
valuesSpecies <- vrtgrid('species', Kexample)

print(valuesSpecies)
#>     species
#> 1    Adelie
#> 2 Chinstrap
#> 3    Gentoo

## create a small set of values for the variate "bill length";
## this variate is continuous and rounded
valuesBill <- vrtgrid('bill_len', Kexample, length.out = 4)

print(valuesBill)
#>   bill_len
#> 1 27.50000
#> 2 39.73333
#> 3 51.96667
#> 4 64.20000

## calculate the conditional probabilities for the 'bill_len' values above,
## given the values of 'species'
probs <- Pr(valuesBill, valuesSpecies, Kexample)


## Create a data frame with all possible combinations of the values above;
## the 'length.out' argument does not apply to the discrete variate 'species'
valuesAll <- vrtgrid(c('species', 'bill_len'), Kexample, length.out = 4)

print(valuesAll)
#>      species bill_len
#> 1     Adelie 27.50000
#> 2  Chinstrap 27.50000
#> 3     Gentoo 27.50000
#> 4     Adelie 39.73333
#> 5  Chinstrap 39.73333
#> 6     Gentoo 39.73333
#> 7     Adelie 51.96667
#> 8  Chinstrap 51.96667
#> 9     Gentoo 51.96667
#> 10    Adelie 64.20000
#> 11 Chinstrap 64.20000
#> 12    Gentoo 64.20000

## base::expand.grid() would give a similar result
valuesAll2 <- expand.grid(
  species = unlist(valuesSpecies), bill_len = unlist(valuesBill)
)

print(valuesAll2)
#>      species bill_len
#> 1     Adelie 27.50000
#> 2  Chinstrap 27.50000
#> 3     Gentoo 27.50000
#> 4     Adelie 39.73333
#> 5  Chinstrap 39.73333
#> 6     Gentoo 39.73333
#> 7     Adelie 51.96667
#> 8  Chinstrap 51.96667
#> 9     Gentoo 51.96667
#> 10    Adelie 64.20000
#> 11 Chinstrap 64.20000
#> 12    Gentoo 64.20000


## To omit 'K =' arguments, we can set a default one with useK()
useK(Kexample)

## set of values for the variate "species";
## since this variate is of a nominal kind, all values are included
valuesSpecies <- vrtgrid('species')

print(valuesSpecies)
#>     species
#> 1    Adelie
#> 2 Chinstrap
#> 3    Gentoo

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