/
dMOEIW.R
148 lines (139 loc) · 4.23 KB
/
dMOEIW.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#' The Marshall-Olkin Extended Inverse Weibull distribution
#'
#' @author Amylkar Urrea Montoya, \email{amylkar.urrea@@udea.edu.co}
#'
#' @description
#' Density, distribution function, quantile function,
#' random generation and hazard function for the Marshall-Olkin Extended Inverse Weibull distribution
#' with parameters \code{mu}, \code{sigma} and \code{nu}.
#'
#' @param x,q vector of quantiles.
#' @param p vector of probabilities.
#' @param n number of observations.
#' @param mu parameter.
#' @param sigma parameter.
#' @param nu parameter.
#' @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 Marshall-Olkin Extended Inverse Weibull distribution \code{mu},
#' \code{sigma} and \code{nu} has density given by
#'
#' \eqn{f(x) = \frac{\mu \sigma \nu x^{-(\sigma + 1)} exp\{{-\mu x^{-\sigma}}\}}{\{\nu -(\nu-1) exp\{{-\mu x ^{-\sigma}}\} \}^{2}},}
#'
#' for x > 0.
#'
#' @return
#' \code{dMOEIW} gives the density, \code{pMOEIW} gives the distribution
#' function, \code{qMOEIW} gives the quantile function, \code{rMOEIW}
#' generates random deviates and \code{hMOEIW} gives the hazard function.
#'
#' @example examples/examples_dMOEIW.R
#'
#' @references
#' \insertRef{okasha2017}{RelDists}
#'
#' @importFrom Rdpack reprompt
#'
#' @export
dMOEIW <- function(x, mu, sigma, nu, 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", ""))
if (any(nu <= 0))
stop(paste("nu must be positive", "\n", ""))
A <- log(mu) + log(sigma) + log(nu) - (sigma + 1) * log(x) - mu * x^(-sigma)
B <- 2 * log(nu - (nu - 1) * exp(-mu * x^(-sigma)))
loglik <- A - B
if (log == FALSE)
density <- exp(loglik)
else
density <- loglik
return(density)
}
#' @export
#' @rdname dMOEIW
pMOEIW <- function(q, mu, sigma, nu,
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 (any(nu <= 0))
stop(paste("nu must be positive", "\n", ""))
A <- exp(-mu * q^(-sigma))
B <- nu - (nu - 1) * A
cdf <- A / B
if (lower.tail == TRUE)
cdf <- cdf
else cdf <- 1 - cdf
if (log.p == FALSE)
cdf <- cdf
else cdf <- log(cdf)
cdf
}
#' @export
#' @rdname dMOEIW
qMOEIW <- function(p, mu, sigma, nu,
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 (any(nu <= 0))
stop(paste("nu 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", ""))
fda <- function(x, mu, sigma, nu){
exp(-mu * x^(-sigma)) / (nu - (nu - 1) * exp(-mu * x^(-sigma)))
}
fda1 <- function(x, mu, sigma, nu, p) {
fda(x, mu, sigma, nu) - p
}
r_de_la_funcion <- function(mu, sigma, nu, p) {
uniroot(fda1, interval=c(0, 1e+06), mu, sigma, nu, p)$root
}
r_de_la_funcion <- Vectorize(r_de_la_funcion)
q <- r_de_la_funcion(mu, sigma, nu, p)
q
}
#' @importFrom stats runif
#' @export
#' @rdname dMOEIW
rMOEIW <- function(n, mu, sigma, nu){
if (any(mu <= 0))
stop(paste("mu must be positive", "\n", ""))
if (any(sigma <= 0))
stop(paste("sigma must be positive", "\n", ""))
if (any(nu <= 0))
stop(paste("nu must be positive", "\n", ""))
n <- ceiling(n)
p <- runif(n)
r <- qMOEIW(p, mu, sigma, nu)
r
}
#' @export
#' @rdname dMOEIW
hMOEIW<-function(x, mu, sigma, nu){
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", ""))
if (any(nu <= 0))
stop(paste("nu must be positive", "\n", ""))
h <- dMOEIW(x, mu, sigma, nu, log=FALSE) /
pMOEIW(q=x, mu, sigma, nu, lower.tail=FALSE, log.p=FALSE)
h
}