I have a vector of ~100k length, with values between 0 and 1 representing habitat suitability at geographic loc开发者_运维百科ations. While some of the values are very small, many of them are 0.9 etc, so the sum is much greater than one.
I would like to generate 1000 random samples of locations, each sample having length 6 (without replacement), with the probability that a location is chosen being weighted by the value of the vector at that location.
Dummy data below. Any ideas?
mylocs = letters[1:10]
myprobs = c(0.1,NA,0.01,0.2,0.6,NA,0.001,0.03,0.9,NA)
mydata = data.frame(mylocs,myprobs)
I'm a bit confused with your question, so here are two possible answers.
If you want you want to sample 1000 groups of six values, where groups can share values, then:
locs = letters[1:15]
probs = c(0.1,NA,0.01,0.2,0.6,NA,0.001,0.03,0.9,NA, 0.1, 0.1, 0.1, 0.1, 0.1)
mydata = data.frame(locs,probs)
d = na.omit(mydata)
replicate(1000, sample(d$locs, size=6, prob=d$probs, replace=F))
If groups shouldn't share values, then just do:
## Change the "2" to 1000 in the real data set
s = sample(d$locs, size=6*2, prob=d$probs, replace=F)
matrix(s, ncol=6)
精彩评论