【发布时间】:2018-06-14 06:29:01
【问题描述】:
拒绝抽样
我正在使用截断正态分布的拒绝抽样,请参阅下面的 r 代码。如何使采样停止在特定的 n?例如 1000 个观察值。 IE。我想在接受的样本数达到 n (1000) 时停止采样。
有什么建议吗?非常感谢任何帮助:)
#Truncated normal curve
curve(dnorm(x, mean=2, sd=2)/(1-pnorm(1, mean=2, sd=2)),1,9)
#create a data.frame with 100000 random values between 1 and 9
sampled <- data.frame(proposal = runif(100000,1,9))
sampled$targetDensity <- dnorm(sampled$proposal, mean=2, sd=2)/(1-pnorm(1, mean=2, sd=2))
#accept proportional to the targetDensity
maxDens = max(sampled$targetDensity, na.rm = T)
sampled$accepted = ifelse(runif(100000,0,1) < sampled$targetDensity / maxDens, TRUE, FALSE)
hist(sampled$proposal[sampled$accepted], freq = F, col = "grey", breaks = 100, xlim = c(1,9), ylim = c(0,0.35),main="Random draws from skewed normal, truncated at 1")
curve(dnorm(x, mean=2, sd=2)/(1-pnorm(1, mean=2, sd=2)),1,9, add =TRUE, col = "red", xlim = c(1,9), ylim = c(0,0.35))
X <- sampled$proposal[sampled$accepted]
如何在采样时将 X 的长度设置为特定的数字?
【问题讨论】:
-
您是要执行所有计算,然后选择前 1,000 个通过的,还是只计算直到 1,000 个通过?前者很简单(sampled$proposal[sampled$accepted][1:1000]),后者需要额外的步骤,因为您必须在每次采样后检查您是否已达到 1,000 次通过。除非数据库非常大,否则我认为这不会更有效。
-
我想做后者。你知道它可以如何执行吗? @AodhanOL
-
我会写一些方法来做到这一点。我认为它们不是很好的 R 代码。
-
是否有必要使用拒绝抽样?有一些相当简单的方法可以做到这一点而不会被拒绝。
标签: r statistics normal-distribution resampling statistical-sampling