forked from bcgov/ssdtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlgumbel.R
69 lines (63 loc) · 2.03 KB
/
lgumbel.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
# Copyright 2015 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#' Log-Gumbel Distribution
#'
#' Density, distribution function, quantile function, random generation
#' and starting values for the
#' Log-Gumbel distribution
#' with \code{lscale} and \code{llocation} parameters.
#'
#' @param x A numeric vector of values.
#' @inheritParams params
#' @return A numeric vector.
#' @name lgumbel
NULL
#' @rdname lgumbel
#' @export
dlgumbel <- function(x, llocation = 0, lscale = 0, log = FALSE) {
fx <- VGAM::dgumbel(log(x), location = exp(llocation), scale = exp(lscale), log = FALSE) / x
if (log) fx <- log(fx)
fx
}
#' @rdname lgumbel
#' @export
qlgumbel <- function(p, llocation = 0, lscale = 0, lower.tail = TRUE, log.p = FALSE) {
if (log.p) p <- exp(p)
if (!lower.tail) p <- 1 - p
exp(VGAM::qgumbel(p, location = exp(llocation), scale = exp(lscale)))
}
#' @rdname lgumbel
#' @export
plgumbel <- function(q, llocation = 0, lscale = 0, lower.tail = TRUE, log.p = FALSE) {
if (!length(q)) {
return(numeric(0))
}
Fq <- VGAM::pgumbel(log(q), location = exp(llocation), scale = exp(lscale))
if (!lower.tail) Fq <- 1 - Fq
if (log.p) Fq <- log(Fq)
Fq
}
#' @rdname lgumbel
#' @export
rlgumbel <- function(n, llocation = 0, lscale = 0) {
exp(VGAM::rgumbel(n, location = exp(llocation), scale = exp(lscale)))
}
#' @rdname lgumbel
#' @export
slgumbel <- function(x) {
list(start = list(
llocation = mean(log(x), na.rm = TRUE),
lscale = pi * sd(log(x), na.rm = TRUE) / sqrt(6)
))
}