RandVec: Generate random vectors with a fixed sum

Description Usage Arguments Value Author(s) References Examples

View source: R/RandVec.R

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

1
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

1
2
3
4
5
6
# 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])

Example output

Loading required package: MASS
Loading required package: msm
Warning messages:
1: In rgl.init(initValue, onlyNULL) : RGL: unable to open X11 display
2: 'rgl_init' failed, running with rgl.useNULL = TRUE 
3: .onUnload failed in unloadNamespace() for 'rgl', details:
  call: fun(...)
  error: object 'rgl_quit' not found 
[1] 1
[1] 1

Surrogate documentation built on July 6, 2021, 5:08 p.m.

Related to RandVec in Surrogate...