data_list: Data lists for input into Bayesian models

View source: R/compose_data.R

data_listR Documentation

Data lists for input into Bayesian models

Description

Functions used by compose_data() to create lists of data suitable for input into a Bayesian modeling function. These functions typically should not be called directly (instead use compose_data()), but are exposed for the rare cases in which you may need to provide your own conversion routines for a data type not already supported (see Details).

Usage

data_list(...)

as_data_list(object, name = "", ...)

## Default S3 method:
as_data_list(object, name = "", ...)

## S3 method for class 'numeric'
as_data_list(object, name = "", scalar_as_array = FALSE, ...)

## S3 method for class 'logical'
as_data_list(object, name = "", ...)

## S3 method for class 'factor'
as_data_list(object, name = "", .n_name = n_prefix("n"), ...)

## S3 method for class 'character'
as_data_list(object, name = "", ...)

## S3 method for class 'list'
as_data_list(object, name = "", ...)

## S3 method for class 'data.frame'
as_data_list(object, name = "", .n_name = n_prefix("n"), ...)

## S3 method for class 'data_list'
as_data_list(object, name = "", ...)

Arguments

...

Additional arguments passed to other implementations of as_data_list, or for data_list, passed to list().

object

The object to convert (see Details).

name

The name of the element in the returned list corresponding to this object.

scalar_as_array

If TRUE, returns single scalars as an 1-dimensional array with one element. This is used by as_data_list.data.frame to ensure that columns from a data frame with only one row are still returned as arrays instead of scalars.

.n_name

A function that is used to form variables storing the number of rows in data frames or the number of levels in factors in ...). For example, if a factor with name = "foo" (having three levels) is passed in, the list returned will include an element named .n_name("foo"), which by default would be "n_foo", containing the value 3.

Details

data_list creates a list with class c("data_list", "list") instead of c("list"), but largely otherwise acts like the list() function.

as_data_list recursively translates its first argument into list elements, concatenating all resulting lists together. By default this means that:

  • numerics are included as-is.

  • logicals are translated into numeric using as.numeric().

  • factors are translated into numeric using as.numeric(), and an additional element named .n_name(name) is added with the number of levels in the factor.

  • character vectors are converted into factors then translated into numeric in the same manner as factors are.

  • lists are translated by translating all elements of the list (recursively) and adding them to the result.

  • data.frames are translated by translating every column of the data.frame and adding them to the result. A variable named "n" (or .n_name(name) if name is not "") is also added containing the number of rows in the data frame.

  • all other types are dropped (and a warning given)

If you wish to add support for additional types not described above, provide an implementation of as_data_list() for the type. See the implementations of as_data_list.numeric, as_data_list.logical, etc for examples.

Value

An object of class c("data_list", "list"), where each element is a translated variable as described above.

Author(s)

Matthew Kay

See Also

compose_data().

Examples


# Typically these functions should not be used directly.
# See the compose_data function for examples of how to translate
# data in lists for input to Bayesian modeling functions.


tidybayes documentation built on Sept. 15, 2024, 9:08 a.m.