# How to calculate mean and standard deviation in R given confidence interval and a normal or gamma distribution?

Suppose you are given a $95 \%$ CI $(1,6)$ based on the normal distribution. Is there any easy way to find $\mu$ and $\sigma$? What if it came from a gamma distribution? Can we do this in R?

• Toby, be careful about how you interpret answers to this question, because "based on the normal distribution" can mean various things. For instance, some people might interpret a CI which uses the Student T distribution to be "based on the normal distribution" (because it is, indirectly). Moreover, there are many kinds of CIs: they are often "symmetric" in some sense, but not always (in particular, a CI related to a gamma distribution might not be). It all comes down to what formula was used to compute the CI. Do you have any information about that? – whuber Jun 13 '12 at 19:03

First note that there is not enough information to solve this problem. In both cases, $n$ the sample size is missing. In the case of the Gaussian distribution, assuming you know $n$, you can easily do it by following @Michael Chernick's instructions. In R that would give something like this (with $n=43$ for the sake of the example).

n <- 43
ci <- c(1,6)
# Take the middle of the CI to get x_bar (3.5).
x_bar <- mean(ci)
# Use 1 = x_bar - 1.96 * sd/sqrt(n)
S2 <- n^2 * (x_bar - ci)/1.96


For the case of the Gamma distribution, things are a bit more complicated because it is not symmetric. So the mean is not in the center of the CI.

For example, say you sample from a Gamma population $\Gamma(\alpha,1)$ where $\alpha$ is unknown. The sample mean is the sum of $n$ variables distributed as $\Gamma(\alpha,1)$ divided by $n$, so it is a variable distributed as $\Gamma(n\alpha,1/n)$. Say that we observed a mean of $1.7$ for a sample size of $n=5$. There are several CI that contain this value as we can check.

> qgamma(.975, shape=1.7*5, scale=1/5)
 3.019101
> qgamma(.975, shape=1.7*5, scale=1/5, lower.tail=F)
 0.7564186


A 95% CI for $\alpha$ is $(.756, 3.019)$, the middle of which is $1.89$, not $1.70$. In short, finding the $\alpha$ and $\theta$ that produce a 95% CI is possible because the solution is unique, but it is a hack.

Fortunately, as $n$ increases, the distribution becomes more and more Gaussian and symmetric, so the CI will be symmetric around the mean. The mean and variance of a $\Gamma(n\alpha,\theta/n)$ are $\alpha\theta$ and $\alpha\theta/n$, so you can use the results of the Gaussian case and solve this very simple equation to get $\alpha$ and $\theta$.

Erratum: Following @whuber's comment I realized that the proposed way to get a confidence interval for $\alpha$ is not good.

The example given above was meant to demonstrate that getting CI with Gamma variables is much more tedious than with Gaussian variables. My mistake proves the point even better. At @whuber's prompt I will show that the CI I proposed is incorrect.

set.seed(123)
# Simulate 100,000 means of 5 Gaussian(0,1) variables (positive control).
means <- rnorm(100000, sd=1/sqrt(5))
upper <- means + qnorm(.975)/sqrt(5)
lower <- means - qnorm(.975)/sqrt(5)
mean((upper > 0) & (lower < 0))
 0.95007 # OK.
# Simulate 100,000 means of 5 Gamma(1,1) variables.
means <- rgamma(100000, shape=5, scale=1/5)
upper <- qgamma(.975, shape=5*means, scale=1/5)
lower <- qgamma(.975, shape=5*means, scale=1/5, lower.tail=FALSE)
mean((upper > 1) & (lower < 1))
 0.94666 # Almost, but not quite.

• This reply starts off really well. The second part, though, does not give a correct CI for a gamma distribution. (Simulate it for small values of shape and small sample sizes.) – whuber Jun 14 '12 at 14:06
• +1 For a convincing simulation, try a shape parameter of 0.5 and scale of 2 :-). – whuber Jun 14 '12 at 22:01

If you mean the true parameters of course the answer is no. But if you mean that you want to recover the sample estimates from the confidence interval the answer is yes for the normal distribution if the sample size $n$ is also given.

If the confidence interval was $(1,6)$, then $1= \overline{X}-1.96 \cdot S/\sqrt{n}$ and $6=\overline{X}+1.96 \cdot S/\sqrt{n}$. So $\overline{X}= (6+1)/2=3.5$ and then $6=3.5 +1.96 \cdot S/\sqrt{n}$ or $S=\sqrt{n} 2.5/(1.96)$.

For the gamma distribution this paper shows various ways to get the approximate and exact confidence intervals for rates. Getting the parameter estimates from these confidence intervals may be complicated.

• Please consider using TeX in your answers. It makes it much easier to read equations! – MånsT Jun 13 '12 at 18:56
• @MånsT, or we can earn the 'Copy Editor' badge by editing his posts for him :) – Macro Jun 13 '12 at 19:03
• @MichaelChernick - that's fine. When I first learned TeX, I found it to be very useful to look at examples. If you look at how I formatted the equations in your answer here, you can see, for example, that you enter "Equation" environment by typing $ (or $$ to make in a centered equation on a new line) and, once you're in equation environment that \overline{X} will type \overline{X}, and \sqrt{n} will type \sqrt{n}, and so on – Macro Jun 13 '12 at 19:13 • That's a great start! Have a look at the edits to your answers and I'm sure that you'll be TeXing in no time. – MånsT Jun 13 '12 at 19:13 • Some tricks, Michael: (1) math.harvard.edu/texman lets you quickly look up some common expressions. (2) Start \TeX expressions with both the enclosing "\$$" characters, so that as you type you can see the expression previewed below. (3) For using$\TeX$in comments, open a separate window and start to ask a new question on this site. It will give you a preview. Cut and paste it into your comment, then abandon the new question. (4) You can right-click on any$\TeX$on a page to see the original markup: learn from it (and use copy-and-paste judiciously). – whuber Jun 13 '12 at 19:16 You can construct a confidence interval around anything that can be estimated, whether it be a mean, standard deviation, even a maximum for any given probability distribution. Assuming you have a CI around the mean estimated from an experiment in which a finite sample of size$n$were taken from independent, identically distributed random normal variables, then you know the exact confidence interval is given by the sample mean plus or minus 1.96 times the standard error, which is the sample standard deviation scaled by the square root of the sample size.$\bar{x} \pm \mathcal{Z}_{\alpha/2} \left( s/\sqrt{n} \right)$. Your estimates of these parameters, conventionally labeled as$\bar{x}$and$s$are the "best guesses" for the "population mean"$\mu$and "standard deviation"$\sigma\$.

These estimators also estimate the same values regardless of the distribution of your independent samples or the corresponding sampling distribution of the mean. Note however, that the confidence interval is asymptotic and these estimates are not necessarily the best anymore.

• The first paragraph is insightful. Concerning the second, your "exact" CI went out of vogue 104 years ago when "Student" showed that it is not exact and, in finding an exact CI, discovered the t distribution. The third paragraph is difficult to make sense of. Overall, though, what is your answer to the question? – whuber Jun 14 '12 at 14:10
• That's correct, I haven't accounted for the degrees of freedom in what I called the exact CI. What I'm getting at is this: assuming the CI is an asymptotic, dist-n free estimate based on the CLT, then yes the sample mean and sample standard deviation can be arithmetically derived, given the sample size. Without knowledge of the construction of the CI, this may be perilous. CIs for the odds ratio in logistic regression models are non-symmetric and you couldn't estimate the "mean" and "standard deviation" of the sampling distribution of the sample odds ratio based on such a CI. – AdamO Jun 14 '12 at 16:39

If what is given are the quantiles from a normal distribution (although not the same as confidence intervals) then a solution to the problem reduces to find the distribution parameters from quantiles.

For the normal distribution we want to find $$\mu$$ and $$\sigma$$ such that the random variable $$X$$ satisfies: $$P(X $$P(X

The random variable $$X$$ has the same distribution as $$\mu Z + \sigma$$ where $$Z$$ is a standard normal random variable $$Z \sim N(0,1)$$ and cdf $$\phi$$ .

This implies that : $$\phi^{-1}(p_i)\sigma+\mu=x_i$$ In R an example given that 20 and 40 are the 0.05 and 0.95 quantiles then.

approx_sd <- function(x1, x2){
(x2-x1) / (qnorm(0.95) - qnorm(0.05) )
}
approx_sd(20, 40)

approx_mean <- function(x1, x2){
(x1*qnorm(0.95) - x2*qnorm(0.05)) / (qnorm(0.95) - qnorm(0.05) )
}
approx_mean(20, 40)


For the gamma, the solution is more involved because the inverse CDF has not a closed-form. However, there exist tools and software that automate this complex problem, for example, ParameterSolver downloadable from the following repository https://biostatistics.mdanderson.org/SoftwareDownload/

Although your question parts from a frequentist confidence interval which is of course, not a quantile, the text might indicate that readers can arrive at this question looking for a quantile parameterised distribution which is a topic of active research.