[英]Simulate a strange distribution in R
This can be considered as a classical problem to transform uniform random numbers generated with runif
with an inverse transformation from empirical relative frequencies.这可以被认为是一个经典的问题,将使用runif
生成的均匀随机数从经验相对频率进行逆变换。 Here an approach that uses approx:这是一种使用大约的方法:
freq <- c(0.39, 0.02, 0.15, 0.18, 0.12, 0.09,0.04, 0.01)
sum(freq) # This is a check. The sum must be 1.0.
r_empirical <- function(n, freq) {
approx(c(0, cumsum(freq)), 0:(length(freq)),
runif(n), method="constant", f=0)$y
}
x <- r_empirical(1000, freq)
hist(x, breaks=0:length(freq))
The following figure demonstrates the basic principle.下图展示了基本原理。 The stairs show the cumulative distribution, the red arrows how a uniform random number can be transformed:楼梯显示累积分布,红色箭头显示如何转换均匀随机数:
Zero-inflated log-Normal?零膨胀对数正态? (Spike at zero looks a little too big for a Tobit, ie censored Normal with the negative stuff piled up on zero) (零尖峰对于 Tobit 来说看起来有点太大了,即审查后的 Normal 负值堆积在零上)
zero_prob <- 0.25
meanlog <- log(20)
sdlog <- 0.4 ## SD ~ 40%
n <- 500
rzilnorm <- function(n, pz, meanlog, sdlog) {
ifelse(runif(n) < pz, 0,
rlnorm(n, meanlog, sdlog))
}
set.seed(101)
hist(rzilnorm(n=500, zero_prob, meanlog, sdlog), col = "gray", breaks=25, freq=FALSE)
My first try was with n=100
and pz=0.2
;我的第一次尝试是使用n=100
和pz=0.2
; if I were going to play around with this more I might increase sdlog
a little bit.如果我要更多地玩这个,我可能会增加sdlog
一点。 Otherwise this looks pretty close?否则这看起来很接近?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.