diff --git a/R/utils.R b/R/utils.R index 14218cb..505fe15 100644 --- a/R/utils.R +++ b/R/utils.R @@ -20,7 +20,10 @@ utils::globalVariables(c("channel")) # Internal functions ----------------------------------------------------------- -logit2 <- function(x) log2(x) - log2(1 - x) +logit2 <- function(x, alpha = 1e-5) { + #Protect against values exactly 0 or 1 by adding some small alpha + log2((x + alpha) / (1 - x + alpha)) +} ilogit2 <- function(x) 2^x / (1 + 2^x) diff --git a/man/logit2.Rd b/man/logit2.Rd index 64430b4..d66bc51 100644 --- a/man/logit2.Rd +++ b/man/logit2.Rd @@ -1,23 +1,24 @@ \name{logit2} \alias{logit2} \alias{ilogit2} -\title{ - logit in base 2. -} +\title{Logit in base 2 with offset} \description{ - Utility functions for computing logit and inverse logit in base 2. + Utility functions for computing the logit and inverse logit in base 2. + \code{logit2} includes an \code{alpha} parameter to add a small offset, + which helps avoid infinite values when \code{x} is exactly 0 or 1. } \usage{ -logit2(x) -ilogit2(x) + logit2(x, alpha = 1e-5) + ilogit2(x) } \arguments{ - \item{x}{A numeric vector.} + \item{x}{A numeric vector of values (typically beta values between 0 and 1).} + \item{alpha}{A small positive numeric offset (default is \code{1e-5}) added to both the numerator and denominator in the logit transform.} } -\value{A numeric vector.} +\value{A numeric vector of transformed values.} \author{ Kasper Daniel Hansen \email{khansen@jhsph.edu}. } \examples{ -logit2(c(0.25, 0.5, 0.75)) + logit2(c(0.25, 0.5, 0.75)) }