/
dIW.R
122 lines (115 loc) · 3.33 KB
/
dIW.R
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#' The Inverse Weibull distribution
#'
#' @author Johan David Marin Benjumea, \email{johand.marin@@udea.edu.co}
#'
#' @description
#' Density, distribution function, quantile function,
#' random generation and hazard function for the inverse weibull distribution with
#' parameters \code{mu} and \code{sigma}.
#'
#' @param x,q vector of quantiles.
#' @param p vector of probabilities.
#' @param n number of observations.
#' @param mu scale parameter.
#' @param sigma shape parameters.
#' @param log,log.p logical; if TRUE, probabilities p are given as log(p).
#' @param lower.tail logical; if TRUE (default), probabilities are P[X <= x], otherwise, P[X > x].
#'
#' @details
#' The inverse weibull distribution with parameters \code{mu} and
#' \code{sigma} has density given by
#'
#' \eqn{f(x) = \mu \sigma x^{-\sigma-1} \exp(\mu x^{-\sigma})}
#'
#' for \eqn{x > 0}, \eqn{\mu > 0} and \eqn{\sigma > 0}
#'
#' @return
#' \code{dIW} gives the density, \code{pIW} gives the distribution
#' function, \code{qIW} gives the quantile function, \code{rIW}
#' generates random deviates and \code{hIW} gives the hazard function.
#'
#' @example examples/examples_dIW.R
#'
#'@references
#'\insertRef{almalki2014modifications}{RelDists}
#'
#'\insertRef{drapella1993complementary}{RelDists}
#'
#' @export
dIW <- function(x, mu, sigma, log=FALSE){
if (any(x < 0))
stop(paste("x must be positive", "\n", ""))
if (any(mu <= 0))
stop(paste("mu must be positive", "\n", ""))
if (any(sigma <= 0))
stop(paste("sigma must be positive", "\n", ""))
loglik <- log(mu*sigma) - (sigma+1)*log(x) - mu*(x^-sigma)
if (log == FALSE)
density <- exp(loglik)
else density <- loglik
return(density)
}
#' @export
#' @rdname dIW
pIW <- function(q, mu, sigma, lower.tail=TRUE, log.p=FALSE){
if (any(q < 0))
stop(paste("q must be positive", "\n", ""))
if (any(mu <= 0 ))
stop(paste("mu must be positive", "\n", ""))
if (any(sigma <= 0))
stop(paste("sigma must be positive", "\n", ""))
cdf <- exp((-mu)*(q^(-sigma)))
if (lower.tail == TRUE)
cdf <- cdf
else cdf <- 1 - cdf
if (log.p == FALSE)
cdf <- cdf
else cdf <- log(cdf)
cdf
}
#' @export
#' @rdname dIW
qIW <- function(p, mu, sigma, lower.tail = TRUE, log.p = FALSE){
if (any(mu <= 0 ))
stop(paste("mu must be positive", "\n", ""))
if (any(sigma <= 0))
stop(paste("sigma must be positive", "\n", ""))
if (log.p == TRUE)
p <- exp(p)
else p <- p
if (lower.tail == TRUE)
p <- p
else p <- 1 - p
if (any(p < 0) | any(p > 1))
stop(paste("p must be between 0 and 1", "\n", ""))
q <- ((-1/mu)*log(p))^(-1/sigma)
q
}
#' @importFrom stats runif
#' @export
#' @rdname dIW
rIW <- function(n,mu,sigma){
if(any(n <= 0))
stop(paste("n must be positive","\n",""))
if (any(mu <= 0 ))
stop(paste("mu must be positive", "\n", ""))
if (any(sigma <= 0))
stop(paste("sigma must be positive", "\n", ""))
n <- ceiling(n)
p <- runif(n)
r <- qIW(p, mu,sigma)
r
}
#' @export
#' @rdname dIW
hIW<-function(x, mu, sigma){
if (any(x < 0))
stop(paste("x must be positive", "\n", ""))
if (any(mu <= 0 ))
stop(paste("mu must be positive", "\n", ""))
if (any(sigma <= 0))
stop(paste("sigma must be positive", "\n", ""))
h <- dIW(x, mu, sigma, log=FALSE) /
pIW(q=x, mu, sigma, lower.tail=FALSE, log.p=FALSE)
h
}