[R] Fitting Truncated Lognormal to a truncated data set (was: fitting truncated normal distribution)

xpRt.wannabe xprt.wannabe at gmail.com
Fri Aug 18 04:00:00 CEST 2006


Dear List,

I am trying to fit Truncated Lognormal to a data set that is
'truncated' from above a certain value, say, 0.01.  Below is what I
was able to come up with.  I would appreciate it if you could review
and make any necessary changes.

# This is modified off the code for 'dtnorm' of library(msm).
dtlnorm <- function (n, mean = 0, sd = 1, lower = -Inf, upper = Inf)
{
   ret <- numeric()
   if (length(n) > 1)
       n <- length(n)
   while (length(ret) < n) {
       y <- rlnorm(n - length(ret), mean, sd)
       y <- y[y >= lower & y <= upper]
       ret <- c(ret, y)
   }
   stopifnot(length(ret) == n)
   ret
}

# This is modified off the code for 'rtnorm' of the library(msm).
rtlnorm <- function (n, mean = 0, sd = 1, lower = -Inf, upper = Inf)
{
   ret <- numeric()
   if (length(n) > 1)
       n <- length(n)
   while (length(ret) < n) {
       y <- rlnorm(n - length(ret), mean, sd)
       y <- y[y >= lower & y <= upper]
       ret <- c(ret, y)
   }
   stopifnot(length(ret) == n)
   ret
}

x <- rtlnorm(100, mean=-11.64857, sd = 3.422795, 0.01)

fitting truncated normal distribution" on 8/16/2006.
dtlnorm0 <- function(x, mean = 0, sd = 1)
{
dtlnorm(x, mean, sd, 0.01, Inf)
}

fitdistr(x, dtlnorm0, start = list(mean = -11, sd = 1))

Thank you.

platform i386-pc-mingw32
arch     i386
os       mingw32
system   i386, mingw32
status
major    2
minor    2.1
year     2005
month    12
day      20
svn rev  36812
language R



More information about the R-help mailing list