RandVec: Generate random vectors with a fixed sum

View source: R/RandVec.R

RandVecR Documentation

Generate random vectors with a fixed sum

Description

This function generates an n by m array x, each of whose m columns contains n random values lying in the interval [a,b], subject to the condition that their sum be equal to s. The distribution of values is uniform in the sense that it has the conditional probability distribution of a uniform distribution over the whole n-cube, given that the sum of the x's is s. The function uses the randfixedsum algorithm, written by Roger Stafford and implemented in MatLab. For details, see http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum/content/randfixedsum.m

Usage

RandVec(a=0, b=1, s=1, n=9, m=1, Seed=sample(1:1000, size = 1))

Arguments

a

The function RandVec generates an n by m matrix x. Each of the m columns contain n random values lying in the interval [a,b]. The argument a specifies the lower limit of the interval. Default 0.

b

The argument b specifies the upper limit of the interval. Default 1.

s

The argument s specifies the value to which each of the m generated columns should sum to. Default 1.

n

The number of requested elements per column. Default 9.

m

The number of requested columns. Default 1.

Seed

The seed that is used. Default sample(1:1000, size = 1).

Value

An object of class RandVec with components,

RandVecOutput

The randomly generated vectors.

Author(s)

Wim Van der Elst, Ariel Alonso, & Geert Molenberghs

References

The function is an R adaptation of a matlab program written by Roger Stafford. For details on the original Matlab algorithm, see: http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum/content/randfixedsum.m

Examples

# generate two vectors with 10 values ranging between 0 and 1
# where each vector sums to 1
# (uniform distribution over the whole n-cube)
Vectors <- RandVec(a=0, b=1, s=1, n=10, m=2)
sum(Vectors$RandVecOutput[,1])
sum(Vectors$RandVecOutput[,2])

Surrogate documentation built on Sept. 25, 2023, 5:07 p.m.