Debian Codegraph @

man/u.log.Rd

raw
1 \name{u.log}
2 \alias{u.log}
3 \title{(Anti)Symmetric Log High-Transform}
4 \description{
5 Compute \eqn{log()} only for high values and keep low ones --
6 antisymmetrically such that \code{u.log(x)} is (once) continuously
7 differentiable, it computes
8 %Fails \deqn{f(x) = \left\{\begin{array}{ll}
9 %Fails x & for |x| \leq c \\
10 %Fails sign(x) c (1 + \log(|x|/c)) & for |x| > c \end{array}}{%
11 %Fails f(x) = x for |x| <= c and sign(x)*c*(1 + log(|x|/c)) for |x| >= c.}
12 \eqn{f(x) = x} for \eqn{|x| \le c}{|x| <= c} and
13 \eqn{sign(x) c\cdot(1 + log(|x|/c))}{sign(x)*c*(1 + log(|x|/c))}
14 for \eqn{|x| \ge c}{|x| >= c}.
15 }
16 \usage{
17 u.log(x, c = 1)
18 }
19 \arguments{
20 \item{x}{numeric vector to be transformed.}
21 \item{c}{scalar, > 0}
22 }
23 \details{
24 Alternatively, the \sQuote{IHS} (inverse hyperbolic sine)
25 transform has been proposed, first in more generality as \eqn{S_U()}
26 curves by Johnson(1949); in its simplest form,
27 \eqn{f(x) = arsinh(x) = \log(x + \sqrt{x^2 + 1})},
28 which is also antisymmetric, continous and once differentiable as our \code{u.log(.)}.
29 }
30 \seealso{
31 Werner Stahel's sophisticated version of John Tukey's \dQuote{started log} (which was \eqn{\log(x
32 + c)}), with concave extension to negative values and adaptive default choice of \eqn{c};
33 \code{\link[relevance]{logst}} in his CRAN package \CRANpkg{relevance}, or
34 \code{\link[DescTools]{LogSt}} in package \CRANpkg{DescTools}.
35 }
36 \value{
37 numeric vector of same length as \code{x}.
38 }
39 \author{Martin Maechler, 24 Jan 1995}
40 \references{
41 N. L. Johnson (1949).
42 Systems of Frequency Curves Generated by Methods of Translation,
43 \emph{Biometrika} \bold{36}, pp 149--176.
44 \doi{https://doi.org/10.2307/2332539}
45 }
46 \examples{
47 curve(u.log, -3, 10); abline(h=0, v=0, col = "gray20", lty = 3)
48 curve(1 + log(x), .01, add = TRUE, col= "brown") # simple log
49 curve(u.log(x, 2), add = TRUE, col=2)
50 curve(u.log(x, c= 0.4), add = TRUE, col=4)
51
52 ## Compare with IHS = inverse hyperbolic sine == asinh
53 ihs <- function(x) log(x+sqrt(x^2+1)) # == asinh(x) {aka "arsinh(x)" or "sinh^{-1} (x)"}
54 xI <- c(-Inf, Inf, NA, NaN)
55 stopifnot(all.equal(xI, asinh(xI))) # but not for ihs():
56 cbind(xI, asinh=asinh(xI), ihs=ihs(xI)) # differs for -Inf
57 x <- runif(500, 0, 4); x[100+0:3] <- xI
58 all.equal(ihs(x), asinh(x)) #==> is.NA value mismatch: asinh() is correct {i.e. better!}
59
60 curve(u.log, -2, 20, n=1000); abline(h=0, v=0, col = "gray20", lty = 3)
61 curve(ihs(x)+1-log(2), add=TRUE, col=adjustcolor(2, 1/2), lwd=2)
62 curve(ihs(x), add=TRUE, col=adjustcolor(4, 1/2), lwd=2)
63 ## for x >= 0, u.log(x) is nicely between IHS(x) and shifted IHS
64
65 ## a log10-scale version of asinh() {aka "IHS" }: ihs10(x) := asinh(x/2) / ln(10)
66 ihs10 <- function(x) asinh(x/2)/log(10)
67 xyaxis <- function() abline(h=0, v=0, col = "gray20", lty = 3)
68 leg3 <- function(x = "right")
69 legend(x, legend = c(quote(ihs10(x) == asinh(x/2)/log(10)),
70 quote(log[10](1+x)), quote(log[10](x))),
71 col=c(1,2,5), bty="n", lwd=2)
72 curve(asinh(x/2)/log(10), -5, 20, n=1000, lwd=2); xyaxis()
73 curve(log10(1+x), col=2, lwd=2, add=TRUE)
74 curve(log10( x ), col=5, lwd=2, add=TRUE); leg3()
75
76 ## zoom out and x-log-scale
77 curve(asinh(x/2)/log(10), .1, 100, log="x", n=1000); xyaxis()
78 curve(log10(1+x), col=2, add=TRUE)
79 curve(log10( x ), col=5, add=TRUE) ; leg3("center")
80
81 curve(log10(1+x) - ihs10(x), .1, 1000, col=2, n=1000, log="x", ylim = c(-1,1)*0.10,
82 main = "absolute difference", xaxt="n"); xyaxis(); eaxis(1, sub10=1)
83 curve(log10( x ) - ihs10(x), col=4, n=1000, add = TRUE)
84
85 curve(abs(1 - ihs10(x) / log10(1+x)), .1, 5000, col=2, log = "xy", ylim = c(6e-9, 2),
86 main="|relative error| of approx. ihs10(x) := asinh(x/2)/log(10)", n=1000, axes=FALSE)
87 eaxis(1, sub10=1); eaxis(2, sub10=TRUE)
88 curve(abs(1 - ihs10(x) / log10(x)), col=4, n=1000, add = TRUE)
89 legend("left", legend = c(quote(log[10](1+x)), quote(log[10](x))),
90 col=c(2,4), bty="n", lwd=1)
91
92 ## Compare with Stahel's version of "started log"
93 ## (here, for *vectors* only, and 'base', as Desctools::LogSt();
94
95 ## by MM: "modularized" by providing a threshold-computer function separately:
96 logst_thrWS <- function(x, mult = 1) {
97 lq <- quantile(x[x > 0], probs = c(0.25, 0.75), na.rm = TRUE, names = FALSE)
98 if (lq[1] == lq[2])
99 lq[1] <- lq[2]/2
100 lq[1]^(1 + mult)/lq[2]^mult
101 }
102 logst0L <- function(x, calib = x, threshold = thrFUN(calib, mult=mult),
103 thrFUN = logst_thrWS, mult = 1, base = 10)
104 {
105 ## logical index sub-assignment instead of ifelse(): { already in DescTools::LogSt }
106 res <- x # incl NA's
107 notNA <- !is.na(sml <- (x < (th <- threshold)))
108 i1 <- sml & notNA; res[i1] <- log(th, base) + ((x[i1] - th)/(th * log(base)))
109 i2 <- !sml & notNA; res[i2] <- log(x[i2], base)
110 attr(res, "threshold") <- th
111 attr(res, "base") <- base
112 res
113 }
114 logst0 <- function(x, calib = x, threshold = thrFUN(calib, mult=mult),
115 thrFUN = logst_thrWS, mult = 1, base = 10)
116 {
117 ## Using pmax.int() instead of logical indexing -- NA's work automatically - even faster
118 xm <- pmax.int(threshold, x)
119 res <- log(xm, base) + (x - xm)/(threshold * log(base))
120 attr(res, "threshold") <- threshold
121 attr(res, "base") <- base
122 res
123 }
124
125 ## u.log() is really using natural log() -- whereas logst() defaults to base=10
126 % ==> forget about this {or rather add it to a help(logst) page } <<<<<<<<<<<<<< !!!! %
127 curve(u.log, -4, 10, n=1000); abline(h=0, v=0, col = "gray20", lty = 3); points(-1:1, -1:1, pch=3)
128 curve(log10(x) + 1, add=TRUE, col=adjustcolor("midnightblue", 1/2), lwd=4, lty=6)
129 curve(log10(x), add=TRUE, col=adjustcolor("skyblue3", 1/2), lwd=4, lty=7)
130 curve(logst0(x, threshold= 2 ), add=TRUE, col=adjustcolor("orange",1/2), lwd=2)
131 curve(logst0(x, threshold= 1 ), add=TRUE, col=adjustcolor(2, 1/2), lwd=2)
132 curve(logst0(x, threshold= 1/4), add=TRUE, col=adjustcolor(3, 1/2), lwd=2, lty=2)
133 curve(logst0(x, threshold= 1/8), add=TRUE, col=adjustcolor(4, 1/2), lwd=2, lty=2)
134 }
135
136 \keyword{arith}