Moi!
Many thanks to those of you who replied to my question about cunning
ways of sampling without replacement. There seem to be three main
approaches to the problem:
1. Write down all possibilities and use dcat to choose between them
This was suggested by Garnett P. McMillan and Martyn Plummer. Martyn
pointed out that combinations() from the "gregmisc" package of R will do
the job of writing down the possibilites. Of course, for other problems
this might get a bit big.
2. Generate a random number for each integer, rank the integers by the
random number, and take the top n
Suggested by Ben of Texas, Mikel Aickin, George Tomlinson, and Adrian
Barnett. Adrian has already posted his code, but I'll include it here
for completeness, the other code was similar:
model
{
# assign a random uniform probability to each population member
for (j in 1:N){
p[j]~dunif(0,1);
}
for (i in 1:5){
sss[i]<-ranked(p[],i); # find the five lowest probabilities
for (j in 1:N){
# find the ranks of the five lowest probabilities
order[i,j]<-equals(sss[i],p[j])*j;
}
int.sss[i]<-sum(order[i,]); # collapse rank vector into scalar
chosen[i]<-sample[int.sss[i]];
}
}
# data
list(sample=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20),N=20)
# initial values
list(p=c(0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5
,0.5,0.5,0.5))
3. For the ith numnber, Draw an integer, n, at random, and swap the ith
and nth integers
This was suggested by Murray H Smith, and wins my prize for cunningness.
Here's his pseudo-code:
Set x = (1, 2, ..., 20)
For i in 1 to 5
{
draw integer n uniformly from 1 to 20
swap x_i and x_n
}
Return (x_1, x_2, ..., x_5)
I wrote some proper code and it worked, but I can't find the code I
wrote. Which is annoying, because it's not totally straightforward to
do (but can be done!). You have to create a new x for each i, and fill
it with the right values
4. Miscelleneous answers
Steve Fleischman suggested this paper:
Rivot and Prevost. 2002. Hierarchical Bayesian analysis of
capture-mark-recapture data. Canadian J Fisheries Aquatic Sci 59:
1768-1784.
Weikko S. Jaross sent me some VBA code that does the job, but I can't
see how to write it in WinBugs (too many loops!)
Mike Thompson sent me (and you!) some code that's based on using a
utility, but I need to look at it in more detail to see if it can mix well.
Thank you all for your replies, which were certainly a lot quicker than
this response. We managed to get the selection working, although it's
very slow to compile, but that's a totally different problem...
Bob
--
Bob O'Hara
Rolf Nevanlinna Institute
P.O. Box 4 (Yliopistonkatu 5)
FIN-00014 University of Helsinki
Finland
Telephone: +358-9-191 23743
Mobile: +358 50 599 0540
Fax: +358-9-191 22 779
WWW: http://www.RNI.Helsinki.FI/~boh/
-------------------------------------------------------------------
This list is for discussion of modelling issues and the BUGS software.
For help with crashes and error messages, first mail [log in to unmask]
To mail the BUGS list, mail to [log in to unmask]
Before mailing, please check the archive at www.jiscmail.ac.uk/lists/bugs.html
Please do not mail attachments to the list.
To leave the BUGS list, send LEAVE BUGS to [log in to unmask]
If this fails, mail [log in to unmask], NOT the whole list
|