Learn R Programming

posterior (version 1.6.0)

mutate_variables: Mutate variables in draws objects

Description

Mutate variables in a draws object.

Usage

mutate_variables(.x, ...)

# S3 method for draws_matrix mutate_variables(.x, ...)

# S3 method for draws_array mutate_variables(.x, ...)

# S3 method for draws_df mutate_variables(.x, ...)

# S3 method for draws_list mutate_variables(.x, ...)

# S3 method for draws_rvars mutate_variables(.x, ...)

Value

Returns a draws object of the same format as .x, with variables mutated according to the expressions provided in ....

Arguments

.x

(draws) A draws object.

...

Name-value pairs of expressions, each with either length 1 or the same length as in the entire input (i.e., number of iterations or draws). The name of each argument will be the name of a new variable, and the value will be its corresponding value. Use a NULL value in mutate_variables to drop a variable. New variables overwrite existing variables of the same name.

Details

In order to mutate variables in draws_matrix and draws_array objects, they are transformed to draws_df objects first and then transformed back after mutation. As those transformations are quite expensive for larger number of draws, we recommend using mutate_variables on draws_df and draws_list objects if speed is an issue.

In draws_rvars objects, the output of each expression in ... is coerced to an rvar object if it is not already one using as_rvar().

See Also

variables, rename_variables

Examples

Run this code
x <- as_draws_df(example_draws())
x <- subset(x, variable = c("mu", "tau"))

mutate_variables(x, tau2 = tau^2)
mutate_variables(x, scale = 1.96 * tau, lower = mu - scale)

Run the code above in your browser using DataLab