-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logit-normal distribution in alm(), proposed in #13
- Loading branch information
Ivan Svetunkov
committed
Nov 11, 2020
1 parent
c9fe95b
commit cedbd5b
Showing
12 changed files
with
495 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Package: greybox | ||
Type: Package | ||
Title: Toolbox for Model Building and Forecasting | ||
Version: 0.6.4.41004 | ||
Date: 2020-11-10 | ||
Version: 0.6.4.41005 | ||
Date: 2020-11-11 | ||
Authors@R: c(person("Ivan", "Svetunkov", email = "[email protected]", role = c("aut", "cre"), | ||
comment="Lecturer at Centre for Marketing Analytics and Forecasting, Lancaster University, UK"), | ||
person("Yves R.", "Sagaert", role = c("ctb"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#' Logit Normal Distribution | ||
#' | ||
#' Density, cumulative distribution, quantile functions and random number | ||
#' generation for the distribution that becomes normal after the Logit | ||
#' transformation. | ||
#' | ||
#' The distribution has the following density function: | ||
#' | ||
#' f(y) = 1/(sqrt(2 pi) y (1-y)) exp(-(logit(y) -mu)^2 / (2 sigma^2)) | ||
#' | ||
#' where y is in (0, 1) and logit(y) =log(y/(1-y)). | ||
#' | ||
#' Both \code{plogitnorm} and \code{qlogitnorm} are returned for the lower | ||
#' tail of the distribution. | ||
#' | ||
#' All the functions are defined for the values between 0 and 1. | ||
#' | ||
#' @template author | ||
#' @keywords distribution | ||
#' | ||
#' @param q vector of quantiles. | ||
#' @param p vector of probabilities. | ||
#' @param n number of observations. Should be a single number. | ||
#' @param mu vector of location parameters (means). | ||
#' @param sigma vector of scale parameters. | ||
#' @param log if \code{TRUE}, then probabilities are returned in | ||
#' logarithms. | ||
#' | ||
#' @return Depending on the function, various things are returned | ||
#' (usually either vector or scalar): | ||
#' \itemize{ | ||
#' \item \code{dlogitnorm} returns the density function value for the | ||
#' provided parameters. | ||
#' \item \code{plogitnorm} returns the value of the cumulative function | ||
#' for the provided parameters. | ||
#' \item \code{qlogitnorm} returns quantiles of the distribution. Depending | ||
#' on what was provided in \code{p}, \code{mu} and \code{sigma}, this | ||
#' can be either a vector or a matrix, or an array. | ||
#' \item \code{rlogitnorm} returns a vector of random variables | ||
#' generated from the logitnorm distribution. Depending on what was | ||
#' provided in \code{mu} and \code{sigma}, this can be either a vector | ||
#' or a matrix or an array. | ||
#' } | ||
#' | ||
#' @examples | ||
#' x <- dlogitnorm(c(-1000:1000)/200, 0, 1) | ||
#' plot(c(-1000:1000)/200, x, type="l") | ||
#' | ||
#' x <- plogitnorm(c(-1000:1000)/200, 0, 1) | ||
#' plot(c(-1000:1000)/200, x, type="l") | ||
#' | ||
#' qlogitnorm(c(0.025,0.975), 0, c(1,2)) | ||
#' | ||
#' x <- rlogitnorm(1000, 0, 1) | ||
#' hist(x) | ||
#' | ||
#' @references \itemize{ | ||
#' \item Mead, R. (1965). A Generalised Logit-Normal Distribution. | ||
#' Biometrics, 21 (3), 721–732. doi: 10.2307/2528553 | ||
#' } | ||
#' | ||
#' @rdname logitnorm-distribution | ||
#' @importFrom stats dnorm pnorm qnorm rnorm | ||
#' @export dlogitnorm | ||
dlogitnorm <- function(q, mu=0, sigma=1, log=FALSE){ | ||
logitnormReturn <- 1 / (sigma*sqrt(2*pi)*q*(1-q))*exp(-(log(q/(1-q))-mu)^2/(2*sigma^2)); | ||
# logitnormReturn[q<=0] <- -Inf; | ||
# logitnormReturn[q>=1] <- Inf; | ||
|
||
# Return logs if needed | ||
if(log){ | ||
logitnormReturn[] <- log(logitnormReturn); | ||
} | ||
|
||
return(logitnormReturn); | ||
} | ||
|
||
#' @rdname logitnorm-distribution | ||
#' @export plogitnorm | ||
#' @aliases plogitnorm | ||
plogitnorm <- function(q, mu=0, sigma=1){ | ||
logitnormReturn <- vector("numeric", length(q)); | ||
logitnormReturn[q>=0] <- pnorm(q=log(q/(1-q)), mean=mu, sd=sigma); | ||
logitnormReturn[q<0] <- 0; | ||
logitnormReturn[q>=1] <- 1; | ||
return(logitnormReturn); | ||
} | ||
|
||
#' @rdname logitnorm-distribution | ||
#' @export qlogitnorm | ||
#' @aliases qlogitnorm | ||
qlogitnorm <- function(p, mu=0, sigma=1){ | ||
logitnormReturn <- qnorm(p=p, mean=mu, sd=sigma); | ||
logitnormReturn[] <- exp(logitnormReturn)/(1+exp(logitnormReturn)); | ||
logitnormReturn[is.nan(logitnormReturn)] <- 0; | ||
return(logitnormReturn); | ||
} | ||
|
||
#' @rdname logitnorm-distribution | ||
#' @export rlogitnorm | ||
#' @aliases rlogitnorm | ||
rlogitnorm <- function(n=1, mu=0, sigma=1){ | ||
logitnormReturn <- qlogitnorm(runif(n=n, 0, 1), mu=mu, sigma=sigma); | ||
return(logitnormReturn); | ||
} |
Oops, something went wrong.