R:根据概率密度分布生成数据 [英] R: Generate data from a probability density distribution

查看:488
本文介绍了R:根据概率密度分布生成数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个简单的数组,具有相应的概率分布.

Say I have a simple array, with a corresponding probability distribution.

library(stats)    
data <- c(0,0.08,0.15,0.28,0.90)
pdf_of_data <- density(data, from= 0, to=1, bw=0.1)

有没有一种方法可以使用相同的分布生成另一组数据.由于该操作是概率性的,因此不再需要与初始分布完全匹配,而只需从中生成初始分布即可.

Is there a way I could generate another set of data using the same distribution. As the operation is probabilistic, it need not exactly match the initial distribution anymore, but will be just generated from it.

我确实成功地找到了一个简单的解决方案.谢谢!

I did have success finding a simple solution on my own. Thanks!

推荐答案

您最好的选择是生成经验累积密度函数,近似反函数,然后转换输入.

Your best bet is to generate the empirical cumulative density function, approximate the inverse, and then transform the input.

复合表达式看起来像

random.points <- approx(
  cumsum(pdf_of_data$y)/sum(pdf_of_data$y),
  pdf_of_data$x,
  runif(10000)
)$y

收益

hist(random.points, 100)

这篇关于R:根据概率密度分布生成数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆