Statistical programming, matrix languages, and more

The IML equivalent of the R "sample" function

Reply
SAS Employee
Posts: 21

The IML equivalent of the R "sample" function

Hello,

Does anyone know what is the SAS/IML equivalent of the R "sample" function?

Thanks,
Eyal
SAS Super FREQ
Posts: 3,232

Re: The IML equivalent of the R "sample" function

Are you sampling with replacement or without? Equal probability or unequal?

For equal-prob sampling without replacement, use permutations. I just did a series of blog posts on generating permutations in IML:
http://blogs.sas.com/iml/index.php?/archives/16-Generating-Random-Permutations.html

For sampling with replacement, I present a a complete module (equal or unequal probabilities) in Chapter 12 of my book Statistial Programming with SAS/IML Stoftware. Within the next few weeks, the programs for my book should be available for download at http://support.sas.com/publishing/authors/wicklin.html

Since the code isn't posted there yet, here's some (less general) code for the case of random sampling with replacement under uniform probability:

proc iml;
/** Module for random sampling with replacement and uniform probability.
Assume A is row vector with k elements. **/
start SampleReplaceUni(A, nSamples, nRep);
k = ncol(A); /** number of elements **/
results = j(nSamples, nRep); /** allocate result matrix **/
call randgen(results, "Uniform"); /** fill with random U(0,1) **/
results = ceil(k*results); /** convert to integers 1,2,...k **/
return (shape(A[results], nSamples)); /** reshape and return from A **/
finish;

call randseed(1234);
/** Draw 5 samples from {1,2,...,8}. Do it 10 times. **/
s = SampleReplaceUni(1:8, 10, 5);
print s;
SAS Employee
Posts: 21

Re: The IML equivalent of the R "sample" function

Hello Rick,

I am sampling with replacement and with unequal probabilities.
Specifically, this is the R statement:

mu <- sample(c(4,12),size=1000,replace=T,prob=c(0.75,0.25))

Is the code you wrote doing this?
Is there no built-in IML (or IMLPlus) function for this?

Thanks,
Eyal
SAS Super FREQ
Posts: 3,232

Re: The IML equivalent of the R "sample" function

The previous module is for equal probability. For unequal prob, use the following:

proc iml;
/** Module for random sampling with replacement and unequal probability.
* Assume A is row vector with k elements and
* PROB is vector of probabilities {p1, p2, ..., pk}. **/
start SampleReplaceUneq(A, nSamples, nRep, prob);
results = j(nSamples, nRep); /** allocate result matrix **/
call randgen(results, "Table", prob); /** fill with 1,2,...k **/
return (shape(A[results], nSamples)); /** reshape and return from A **/
finish;

call randseed(1234);
set = {4 12};
k = ncol(set);
/** Resample from the set with given probabilities. Do it 1000 times. **/
mu = SampleReplaceUneq(set, 1000, k, {0.75 0.25});
SAS Super FREQ
Posts: 3,232

Re: The IML equivalent of the R "sample" function

Update: SAS/IML 12.1 (SAS 9.3m2) supports the SAMPLE function, which supports drawing samples with or without replacement from finite sets, with equal or unequal probabilities.

Post a Question
Discussion Stats
  • 4 replies
  • 1006 views
  • 0 likes
  • 2 in conversation