rweibull_with_mean_sd.Rd
Mean and standard deviation parameterised Weibull sampling function
rweibull_with_mean_sd(n, mean, sd)
n | Numeric, the number of samples to take. |
---|---|
mean | Numeric, the mean of the distribution. |
sd | Numeric the standard deviation of the distribution. |
A vector of samples from the Weibull distribution
#> [1] 2.922596sd(sample)#> [1] 4.976698hist(sample)## Code rweibull_with_mean_sd#> function (n, mean, sd) #> { #> mu <- mean #> sigma <- sd #> loc <- 0 #> cv <- sigma/(mu - loc) #> if (cv < 1e-06) { #> nu <- cv/(sqrt(trigamma(1)) - cv * digamma(1)) #> shape <- 1/nu #> scale <- (mu - loc)/(1 + nu * digamma(1)) #> } #> else { #> aa <- log(cv^2 + 1) #> nu <- 2 * cv/(1 + cv) #> repeat { #> gb <- (lgamma(1 + 2 * nu) - 2 * lgamma(1 + nu) - #> aa)/(2 * (digamma(1 + 2 * nu) - digamma(1 + nu))) #> nu <- nu - gb #> if (abs(gb) < 1e-12) #> break #> } #> shape <- 1/nu #> scale <- exp(log(mu - loc) - lgamma(1 + nu)) #> } #> stats::rweibull(n, shape = shape, scale = scale) #> } #> <bytecode: 0x7ff8fcf626f8> #> <environment: namespace:NCoVUtils>