rcon: Constrain random samples

Description Usage Arguments Value Examples

View source: R/original/Additional file 2.R View source: R/simulation.R

Description

Resamples supplied distribution to ensure generated values fall within given constraints.

Usage

1
rcon(n, rdist, con, max.iter = 100)

Arguments

n

Number of samples to draw.

rdist

User-defined function for generating random samples (must take n as only argument).

con

Vector of lower and upper constraints on generated samples.

max.iter

Maximum number of iterations before aborting.

Value

A vector of n values sampled from rdist such that the minimum sampled value is greater than or equal to con[1] and the the maximum is smaller than or equal to con[2].

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Using default distribution parameters
out <- rcon(10000, rnorm, c(-1, 1))
print(summary(out))
hist(out, 20, xlim=c(-2, 2), probability=TRUE,
     main='', xlab='Random variable value')

# Modifying mean and standard deviation
out <- rcon(10000, function(n) {rnorm(n, 0.5, 0.5)}, c(-1, 1))
print(summary(out))
hist(out, 20, xlim = c(-2, 2), probability = TRUE,
     main = '', xlab = 'Random variable value')

ssokolen/metcourse documentation built on May 30, 2019, 8:43 a.m.