2016-04-21 111 views
2

我想在给定间隔内生成一些威布尔随机数。例如,来自威布尔分布的具有形状2和在区间(0,10)中的比例30的20个随机数。以特定间隔生成随机数

rweibull R中的函数产生具有给定形状和比例值的威布尔分布的随机数。有人可以提出一种方法吗?先谢谢你。

+0

检查了这一点:http://stats.stackexchange.com/questions/88773/getting-rweibull-to-output-n-observations-in-1-52-given-specific-shape-scale – chinsoon12

回答

3

使用distr包。它允许很容易地做这种事情。

require(distr) 
#we create the distribution 
d<-Truncate(Weibull(shape=2,scale=30),lower=0,upper=10) 
#The d object has four slots: d,r,p,q that correspond to the [drpq] prefix of standard R distributions 
#This extracts 10 random numbers 
[email protected](10) 
#get an histogram 
hist([email protected](10000)) 
+0

尼古拉谢谢你。我知道了 – Nayomi

1

使用基础R可以生成随机数,滤波器,其拖放到目标间隔,并产生一些更多的,如果它们的量似乎是小于需要。

rweibull_interval <- function(n, shape, scale = 1, min = 0, max = 10) { 
    weib_rnd <- rweibull(10*n, shape, scale) 
    weib_rnd <- weib_rnd[weib_rnd > min & weib_rnd < max] 
    if (length(weib_rnd) < n) 
    return(c(weib_rnd, rweibull_interval(n - length(weib_rnd), shape, scale, min, max))) else 
     return(weib_rnd[1:n]) 
} 

set.seed(1) 
rweibull_interval(20, 2, 30, 0, 10) 

[1] 9.308806 9.820195 7.156999 2.704469 7.795618 9.057581 6.013369 2.570710 8.430086 4.658973 
[11] 2.715765 8.164236 3.676312 9.987181 9.969484 9.578524 7.220014 8.241863 5.951382 6.934886