Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
##==============================================================================
## Function to draw values from Gamma (Normal shape) ==
##==============================================================================
#' Draw values from a Custom Gamma Distribution to force a Normal shape (symmetrical bell curve)
#'
#' @param n number of value to draw.
#' @param mu mean
#' @param sd standard deviation
#' @param r scaling factor. Used to ensure a Normal shape for small mean values.
#'
#' @return a vector of drawn values.
#' @export
#'
#' @examples
#' sample_gamma(n= 5, mu = 4, sd = 1.5)
#'
sample_gamma <- function(n, mu, sd, r=3) {
if(mu <= 0){
xx <- rep(0,n)
}else{
if(sd <= 0){
xx <- rep(mu,n)
}else{
if(mu < 100){
mu = mu*10^r
sd = sd*10^r
shape = (mu/sd)^2 ; shape
scale = sd^2/mu ; scale
xx = rgamma(n, shape = shape, scale = scale) / 10^r
} else {
shape = (mu/sd)^2
scale = sd^2/mu
xx = rgamma(n, shape = shape, scale = scale)
}
}
}
return(xx)
} # End function
################################################################################