# 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])/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)
[1] 3.019101
> qgamma(.975, shape=1.7*5, scale=1/5, lower.tail=F)
[1] 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))
[1] 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))
[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
Thanks @whuber. Not much escapes you ;-) – gui11aume Jun 14 '12 at 21:53
+1 For a convincing simulation, try a shape parameter of 0.5 and scale of 2 :-). – whuber Jun 14 '12 at 22:01
Aha, thanks @Macro for the edit :D – gui11aume Jun 15 '12 at 20:27

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.

-
@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 show 5 more comments 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\$.