# Is an implementation of a density function for a logit-normal distribution available in R?

Is an implementation of a density function for a logit-normal distribution available in R?

I have not found one in a package or in the CRAN task view of probability distributions.

This is for a MLE fitting of a function to data.

-

Bare-bones scaled logit-normal distribution code, use at your own risk:

logit <- function(x, a = 0, b = 1)
{
stopifnot(a < b)
x <- (x - a)/(b - a)
log(x) - log(1 - x)
}

invlogit <- function(x, a = 0, b = 1)
{
stopifnot(a < b)
((b - a)/(1 + exp(-x))) + a
}

dlogitnorm <- function(x, ..., a = 0, b = 1, log = FALSE)
{
out <- dnorm(x = logit(x, a, b), ..., log = TRUE) + log(b - a) - log(x - a) - log(b - x)
out[x == a | x == b] <- -Inf
if(log) out else exp(out)
}

plogitnorm <- function(q, ..., a = 0, b = 1, lower.tail = TRUE, log.p = FALSE)
pnorm(logit(q, a, b)), ..., lower.tail = lower.tail, log.p = log.p)

qlogitnorm <- function(p, ..., a = 0, b = 1, lower.tail = TRUE, log.p = FALSE)
invlogit(qnorm(p, ..., lower.tail = lower.tail, log.p = log.p), a, b)

rlogitnorm <- function(n, ..., a = 0, b = 1) invlogit(rnorm(n, ....), a, b)

-
note that logit and invlogit are availabe in base R as plogis, qlogis ... – Ben Bolker Apr 1 '12 at 17:48
@Cyan, I understand this is provided as is, but just to note that the results from the function you provide are not the same as those provide by the package logitnorm. Any suggestions as to the reason? – Etienne Low-Décarie Apr 3 '12 at 19:19
@EtienneLow-Décarie, I can't seem to get my hands on the logitnorm package code. The R-Forge download page doesn't have download links. What am I doing wrong? – Cyan Apr 4 '12 at 2:29

The logitnorm package on R-Forge appears to do what you want.

-
Thank you. Sadly, this package does not build for the current version of R: Build status: Failed to build. I am guessing I will have to wait for this to be resolved. – Etienne Low-Décarie Mar 31 '12 at 21:45
You can always calculate the logitnormal PDF analytically. The formula is on the Wikipedia page. – Martin O'Leary Mar 31 '12 at 22:06
Clicking through the "more information" content on R-forge, it looks like the problem may be a fairly trivial formatting issue in the examples/vignette, so you might want to contact the maintainer -- they may able to fix it quickly. – Ben Bolker Apr 1 '12 at 17:47
I have downloaded the source and worked from there. However, the values creates by the density function kindly provided by @Cyan do not match those kindly provided by Thomas Wutzler in his logitnorm package. Any suggestions? – Etienne Low-Décarie Apr 3 '12 at 19:22
It appears to now build. Moreover, it is now available from CRAN. – Russell S. Pierce Mar 9 at 16:06