form_trans {pdqr}R Documentation

Transform pdqr-function

Description

Perform a transformation of pdqr-function(s) (which assumed to be independent).

Usage

form_trans(f_list, trans, ..., method = "random", n_sample = 10000,
  args_new = list())

form_trans_self(f, trans, ..., method = "random", args_new = list())

Arguments

f_list

A list consisting from pdqr-function(s) and/or single number(s). Should have at least one pdqr-function (see Details).

trans

Transformation function. Should take as many (vectorized) arguments as there are elements in f_list or a single argument for form_trans_self(). Should return numeric or logical values.

...

Extra arguments to trans.

method

Transformation method. One of "random" or "bruteforce".

n_sample

Number of elements to sample.

args_new

List of extra arguments for new_*() to control density().

f

A pdqr-function.

Details

form_trans_self() is a thin wrapper for form_trans() that accepts a single pdqr-function instead of a list of them.

Class of output is chosen as class of first pdqr-function in f_list. Type of output is chosen to be "discrete" in case all input pdqr-functions have "discrete" type, and "continuous" otherwise.

Method "random" performs transformation using random generation of samples:

Method "bruteforce":

Notes about "bruteforce" method:

Value

A pdqr-function for transformed random variable.

See Also

Pdqr methods for S3 group generic functions for more accurate implementations of most commonly used functions. Some of them are direct (without randomness) and some of them use form_trans() with "random" method.

form_regrid() to increase/decrease granularity of pdqr-functions for method "bruteforce".

Other form functions: form_estimate(), form_mix(), form_regrid(), form_resupport(), form_retype(), form_smooth(), form_tails()

Examples

# Default "random" transformation
d_norm <- as_d(dnorm)
## More accurate result would give use of `+` directly with: d_norm + d_norm
d_norm_2 <- form_trans(list(d_norm, d_norm), trans = `+`)
plot(d_norm_2)
lines(as_d(dnorm, sd = sqrt(2)), col = "red")

## Input list can have single numbers
form_trans(list(d_norm, 100), trans = `+`)

## Output of `trans` can be logical. Next example is random version of
## `d_norm >= 0`.
form_trans(list(d_norm, 0), trans = `>=`)

# Transformation with "bruteforce" method
power <- function(x, n = 1) {
  x^n
}
p_dis <- new_p(
  data.frame(x = 1:3, prob = c(0.1, 0.2, 0.7)),
  type = "discrete"
)

p_dis_sq <- form_trans_self(
  p_dis, trans = power, n = 2, method = "bruteforce"
)
meta_x_tbl(p_dis_sq)
## Compare with "random" method
p_dis_sq_rand <- form_trans_self(p_dis, trans = power, n = 2)
meta_x_tbl(p_dis_sq_rand)

# `form_trans_self()` is a wrapper for `form_trans()`
form_trans_self(d_norm, trans = function(x) {
  2 * x
})

[Package pdqr version 0.3.1 Index]