Skip to contents

This function generates random values from the expression. Random values or closed form expression will share the same value as long as they have the same name.

Arguments

n

Integer. Number of observations.

rhs_val

Boolean. Whether or not to keep the right hand side values of the expression. Default is FALSE.

computed

List. Default is NULL. If it is provided, random variables or random closed form expression will use the values from the list, which makes the expression potentially deterministic.

Value

Numeric values.

Examples


# Constant variable
a <- 1

# Random uniform variable
b <- rand_uniform()

# Define a closed form expression
cf <- closed_form(~3 * (exp(a) + b))

# Generate 5 values
cf$gen(5)
#> [1] 9.025952 9.006486 8.686820 9.064382 9.624908

# Generate 5 values, and keep RHS
cf$gen(5, rhs_val = TRUE)
#> $lhs
#> [1]  9.689161  8.161639 10.021806 10.296185  8.792739
#> 
#> $rhs
#> $rhs$b
#> [1] 0.51143860 0.00226467 0.62232006 0.71377975 0.21263121
#> 
#> 

d <- rand_normal()

# Define a closed form expression with another closed form expression
cf2 <- closed_form(~cf + 3 * d)

# Generate 5 values
cf2$gen(5)
#> [1]  7.289884 10.847694 11.801906  7.806618 10.038594

# Generate 5 values, and keep RHS
cf2$gen(5, rhs_val = TRUE)
#> $lhs
#> [1]  1.888998 11.707233  8.190652  8.005405 11.628381
#> 
#> $rhs
#> $rhs$b
#> [1] 0.31838138 0.84764663 0.07206554 0.69952794 0.14146297
#> 
#> $rhs$cf
#> [1]  9.109990 10.697785  8.371042 10.253429  8.579234
#> 
#> $rhs$d
#> [1] -2.40699727  0.33648257 -0.06013018 -0.74934151  1.01638213
#> 
#> 

# Define a closed form expression with two random variables of the same name
cf3 <- closed_form(~d + d)

# Both `d` will share the same values
cf3$gen(5, rhs_val = TRUE)
#> $lhs
#> [1] -3.71423355  2.45187191 -0.46534391 -1.69692250 -0.02546444
#> 
#> $rhs
#> $rhs$d
#> [1] -1.85711677  1.22593595 -0.23267196 -0.84846125 -0.01273222
#> 
#> 

# Define a closed form expression with two closed form expressions of the same name
cf4 <- closed_form(~cf3 + cf3)

# Both `cf3` will share the same values, both `d` will share the same values as well
cf4$gen(5, rhs_val = TRUE)
#> $lhs
#> [1]  9.1466890 -3.8513348 -0.8938031  1.5021936  1.5214328
#> 
#> $rhs
#> $rhs$d
#> [1]  2.2866722 -0.9628337 -0.2234508  0.3755484  0.3803582
#> 
#> $rhs$cf3
#> [1]  4.5733445 -1.9256674 -0.4469016  0.7510968  0.7607164
#> 
#> 

# Define a closed form expression with two different closed form expressions,
# but contains same random variables
cf5 <- closed_form(~cf3 + cf4)

# Both `d` in `cf3` and `cf4` will share the same value
cf5$gen(5, rhs_val = TRUE)
#> $lhs
#> [1]  3.130342  7.451691  9.046918  7.158485 -7.172244
#> 
#> $rhs
#> $rhs$d
#> [1]  0.5217237  1.2419485  1.5078196  1.1930808 -1.1953740
#> 
#> $rhs$cf3
#> [1]  1.043447  2.483897  3.015639  2.386162 -2.390748
#> 
#> $rhs$cf4
#> [1]  2.086895  4.967794  6.031278  4.772323 -4.781496
#> 
#> 

# Control the value of `d`
cf5$gen(5, rhs_val = TRUE, computed = list(d = 1))
#> $lhs
#> [1] 6
#> 
#> $rhs
#> $rhs$d
#> [1] 1
#> 
#> $rhs$cf3
#> [1] 2
#> 
#> $rhs$cf4
#> [1] 4
#> 
#>