# How to construct confidence intervals for log normal using boot function in R

So, the sample mean for the log-normal is defined as $$exp(\mu + \sigma^2/2)$$. So, I made the boot statistic function as follows:

boot_mean <- function(data, ind){
exp(base::mean(data[ind])+var(data[ind])/2)
}


Then the simulation function:

bs_sim <- function(n, bs_N, mju){
bs <-  boot(rlnorm(n, meanlog = 1, sdlog = 0.5), statistic = boot_mean, R = bs_N)
prod(boot.ci(bs)$perc[4:5] - mju) }  Here is the function that computes the precision of bootstrap intervals: prec_bs <- function(n, N, bs_N, mju){ rez <- replicate(N, bs_sim(n, bs_N, mju)) length(rez[rez<0])/N }  When I apply the prec_bs to data prec_bs(n = 50, N = 500, bs_N = 1000, mju = 1))  I get value zero. When I just apply the boot.ci(boot()) function to generated rlnorm() data, I get unreasonable confidence intervals: Intervals : Level Normal Basic 95% ( -0.38, 107.85 ) (-43.13, 83.99 ) Level Percentile BCa 95% ( 32.05, 159.17 ) ( 34.87, 173.55 )  What I am doing wrong? • It will be helpful if your code is a mwe. Eg, mju does not appear to be specified, nor do I see the purpose of bs_sim Oct 26, 2021 at 13:14 • @ChristophHanck Ok, I edited the code. I created the bs_sim function in order to compute the coverage precision of bootstrap intervals. But the main issue is the boot_mean function, because when function boot.ci(boot()) is applied to generated data, the outputted confidence intervals are completely off. – user Oct 26, 2021 at 13:47 • A nit: the "sample mean" is, as usual, the sum of the data divided by their count. The formula$\exp(\mu + \sigma^2/2)$is the expectation of the assumed Lognormal distribution. It is a parameter having nothing to do with the sample. This is an important distinction to know, because it makes additional--perhaps better--estimators available to you. For instance, why not bootstrap the sample mean? One advantage is that it doesn't require the Lognormal assumption. – whuber Oct 9, 2022 at 13:07 • @whuber Frank Harrell shows here the problem with bootstrapping to get reliable CI for the mean of a highly skewed distribution like Lognormal. – EdM Oct 9, 2022 at 14:27 • @EdM Right -- I'm not commenting on that. BTW, this problem is equivalent to bootstrapping$\mu+\sigma^2/2,\$ whose difficulty is comparable to that of bootstrapping a variance. In other words, the Lognormal assumption (when it can be justified!) helps overcome that difficulty of extreme skew.
– whuber
Oct 9, 2022 at 14:29

Do you maybe simply want

boot.ci(boot(rnorm(5000, mean = 1, sd = 0.5), statistic = boot_mean, R = 500), type = c("norm", "perc", "basic"))

boot.ci(boot(rlnorm(5000, meanlog = 1, sdlog = 0.5), statistic = boot_mean, R = 500), type = c("norm", "perc", "basic"))?

You are after constructing a confidence interval of the log-normal distribution, i.e., $$\exp(\mu+\sigma^2/2)$$. You construct it by producing bootstrap estimates of $$\mu$$ and $$\sigma^2$$ and pass these into the function that turns the parameters into estimates of $$\exp(\mu+\sigma^2/2)$$ in boot_mean. So, I would say, the input to data ought to be normal, not log-normal, for the construction to work.

I do then get reasonable CIs for a true value of exp(1+0.5^2/2).

If you, instead, feed a log-normal sample into boot_mean, the expected value and variance, from https://en.wikipedia.org/wiki/Log-normal_distribution, will be

mu <- 1
sig <- 1/2
eln <- exp(mu+sig^2/2)
vln <- exp(2*mu+sig^2)*(exp(sig^2)-1)


so that the function value is exp(eln+vln/2), which equals roughly 84, for which (for $$n$$ sufficiently large - note this is a rather nonlinear problem, so that even a "correct" bootstrap may not perform well in small samples), the bootstrap seems to work again.