简体   繁体   中英

Sample from an unknown probability distribution

I have a vector of ~100k length, with values between 0 and 1 representing habitat suitability at geographic locations. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM