2016-04-21 103 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