我只是試圖使用該rbinom函式重新創建一個示例,但是由于概率低,“成功”的數量比我預期的要高得多。
numSamples <- 10000 #number of samples to be drawn from population
numTrials <- 100 #this is the sample size (size of each sample)
probs <- seq(0.001, 0.9999, 0.01)
for (i in 1:length(probs)) {
x <- rbinom(n = numSamples, size = numTrials, prob = probs[i])
}
一切似乎都很簡單,除了我讓所有樣本的成功次數在 97 - 100 之間。當我使用較小的概率(例如 0.001)手動執行一些測驗用例時,我得到預期的成功次數:0。因此,我的for回圈讀取內容的方式存在問題。出了什么問題?
uj5u.com熱心網友回復:
這是獲取所有樣本的一種方法:
res <- list()
for (i in 1:length(probs)) {
res <- c(res,
list(rbinom(n = numSamples, size = numTrials, prob = probs[i]))
}
然后你可以unlist(res)得到一個長向量,或者do.call(rbind, res)折疊成一個矩陣。以這種方式增長串列然后折疊它不會產生與增長向量相同的性能損失。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/467847.html
下一篇:在for回圈中替換多行字串
