2
$\begingroup$

So I've noticed something extremely confusing about t.test, which is that the confidence interval it provides w.r.t a two-sided alt hypothesis seems to be inconsistent with what it provides for a one-sided test, my own CI function & excel's CONFIDENCE() (which are consistent with each other). In addition it appears to be inconsistent with my own two-tailed z test function, and all of these inconsistencies are pretty small (but bigger than floating point error)!

Here is my example: t.test---

sample = c(38, 30, 41, 28, 31)
t.test(sample) # CI = 26.65334, 40.54666

t.test(sample, alternative = 'less') # CI = -Inf, 38.93388
t.test(sample, alternative = 'greater') # CI = 28.26612,  Inf`

My own CI---

# tested 10/5/19
# xbar = sample mean, s = sample sigma, n = sample size
mean_conf_interval <- function(xbar, s, n, conf_lvl=0.95) {
  z_a2 <- qnorm((1-conf_lvl)/2, lower.tail=F)
  err_margin <- z_a2*s/sqrt(n)
  return(c(xbar-err_margin, xbar+err_margin))
}

mean_conf_interval(mean(sample),sd(sample),length(sample))
# CI = 28.26612,  38.93388

excel--- (rough translation to pseudo code)

E276:E280 = 38, 30, 41, 28, 31 # same sample as before
B277 = CONFIDENCE(0.05,  STDEV(E276:E280), COUNT(E276:E280)) # error margin
B278=AVERAGE(E276:E280) # xbar
# CI = B278-B277, B278+B277 = 28.69617167,  38.50382833

For the sake of brevity I won't go into comparison with my own two-sided-z-test function unless necessary. But what's going on? This is so strange that it actually looks like a bug...

$\endgroup$
3
  • 1
    $\begingroup$ Well, t.test uses the $t$-quantile instead of your function which uses the normal quantile. In large samples, the differences are negligible but in your example, the $t$-quantile is $2.776$ whereas the normal quantile is $1.96$. Add qt((1-conf_lvl)/2, df = n - 1, lower.tail=FALSE) to your function and the results are identical. $\endgroup$ Commented Oct 22, 2019 at 18:22
  • 2
    $\begingroup$ I strongly disagree with the off-topic vote. While this obviously involves R and code, the answer is inherently a statistical one, relating to the differences between the Z and T distributions. $\endgroup$ Commented Oct 22, 2019 at 18:32
  • 1
    $\begingroup$ I agree; code doesn't make it off topic, as long as it requires statistical expertise to understand or answer $\endgroup$
    – Glen_b
    Commented Oct 23, 2019 at 5:56

1 Answer 1

4
$\begingroup$

You are using the standard normal distribution to calculate the 0.975 quantile as 1.96. You should be using the $t_{n-1}$ distribution, which will give a value somewhat larger than 1.96.

Try z_a2 <- qt((1-conf_lvl)/2,n-1,lower.tail=F) in your function and see what you get.

Edit: I have run the following code, getting the same confidence interval that I have in my comment.

mean_conf_interval <- function(xbar, s, n, conf_lvl=0.95) {
  z_a2 <- qnorm((1-conf_lvl)/2, lower.tail=F)
  err_margin <- z_a2*s/sqrt(n)
  return(c(xbar-err_margin, xbar+err_margin))
}

sample <- c(38, 30, 41, 28, 31)
xbar <- mean(sample)
s <- sd(sample)
n <- length(sample)
mean_conf_interval(xbar,s,n)
$\endgroup$
8
  • $\begingroup$ So then is only the t-quantile CI correct? Also could you please address why t.test's finite one-sided CI boundaries appear to also use norm-quantiles instead of t-quantiles? $\endgroup$
    – profPlum
    Commented Oct 22, 2019 at 18:47
  • $\begingroup$ You use the t-test to account for the fact that you're estimating the variance. For the one-sided intervals, I am not getting the numbers that you are getting. My confidence interval is narrower: $( 28.69617 ,38.50383 )$. $\endgroup$
    – Dave
    Commented Oct 22, 2019 at 19:29
  • $\begingroup$ Thanks that is helpful to know. But no those are the numbers that I get for one-sided intervals (please recheck my post)! That's my point; I don't understand why I get those for one-sided intervals (apparently using norm-quantile) but the wider one (using t-quantile) for two-sided. And to reiterate these are both from the t.test() function. $\endgroup$
    – profPlum
    Commented Oct 22, 2019 at 20:12
  • $\begingroup$ @profPlum Please post your verbatim R code. I do not get the confidence interval that you get. $\endgroup$
    – Dave
    Commented Oct 22, 2019 at 20:37
  • $\begingroup$ I did and I just reran it to double check, the verbatim R code is in the first code-box of my post (not counting the inline one). $\endgroup$
    – profPlum
    Commented Oct 22, 2019 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.