We can easily help you, if you give as a definition of L(.). Ok, let's believe the answer by Gavin Kelly.
In that case,
L0 <- function(x) exp(-(x^2)/2)/sqrt(2*pi) - x * (1 - pnorm(x))
L <- function(x) dnorm(x) - x * pnorm(x, lower.tail=FALSE)
is a numerically better, namely accurate also for large x
, where L0
suffers from cancellation:
> L(10)
[1] 7.47456e-25
> L0(10)
[1] 7.694599e-23
As the OP further asked for the inverse function as well, @Ben Bolker showed the standard solution via root finding,
Linv <- function(y) uniroot(function(x) L(x) - y, interval=c(-10,10))$root
which works in good cases, if the argument is of length 1. OTOH, if Linv
itself should
vectorize, and if you want it and L(.)
to also work for boundary cases, there are some tricks and speedups one can do.
---> Nice extended answer on rpubs: http://rpubs.com/maechler/16436