简体   繁体   English

在 R 中模拟一个奇怪的分布

[英]Simulate a strange distribution in R

Hi all I'm attempting to simulate data that looks something like this:大家好,我正在尝试模拟如下所示的数据: 在此处输入图片说明

Anyone know how I'd go about doing this?有人知道我会怎么做吗?

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:楼梯显示累积分布,红色箭头显示如何转换均匀随机数:

逆变换

you could also use two beta distributions.你也可以使用两个 beta 发行版。

Beta is a very useful distribution. Beta 版是一个非常有用的发行版。

beta<-c(rbeta(600, 0.1, 5, ncp = 0),rbeta(1200, 3, 4, ncp = 1))

hist(beta,breaks=30,probability = T)

测试版

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=100pz=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.

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