Using the following population:

sample <- c(41.5, 56.7, 54.2, 98.9, 56.7, 43.9, 35.8, 28.8)

I get a different result for the upper and lower confidence intervals when calculating them "manually" than what result from the standard library t.test() function.

s <- sd(sample) # standard deviation 
se <- s/sqrt(NROW(sample)) # standard error 

# Using t.test()
lower <- (t.test(sample))$conf.int[1] # yields 34.11755
upper <- (t.test(sample))$conf.int[2] # yields 70.00745

# Calculating manually
lower <- mean(sample)-(1.96*se) # yields 37.18821
upper <- mean(sample)+(1.96*se) # yields 66.93679

Can somebody explain what is going on here?

Update: Thanks for the information everybody! This was really enlightening.


1 Answer 1


You are using 1.96, which is the Normal quantile, rather than the quantile from the t distribution with appropriate degrees of freedom (length(sample)-1). Your manually-calculated confidence interval is too narrow.

  • $\begingroup$ Interestingly, NROW (as in the OP) would be needed here as it will treat a vector as a one-column matrix. length would be another option. (NROW was actually new to me, I had to look it up.) $\endgroup$ Dec 6, 2012 at 21:17
  • $\begingroup$ Oh, interesting. Thanks for the correction. I'll change it to length(). $\endgroup$ Dec 6, 2012 at 21:18
  • 6
    $\begingroup$ +1. In other words, change the manual calculations to mean(sample) + qt(c(.025, .975), length(sample) - 1) * se. $\endgroup$
    – whuber
    Dec 6, 2012 at 21:18
  • 6
    $\begingroup$ And be careful with length(x) (or NROW(x)) since it will return the size of the vector, including missing values if any. In case of doubt, sum(!is.na(x)) or sum(complete.cases(x)) are to be preferred. (BTW, sample is the name of a specific function in R.) $\endgroup$
    – chl
    Dec 6, 2012 at 22:12
  • 1
    $\begingroup$ @shootingstars You're welcome. Re: your preceding comment; if you know the population variance (or SD) you don't need to estimate it from your sample and you can use a z-test (or, equivalently, refer to standard N(0;1) quantiles) -- but this is rarely true (that we know the true SD). $\endgroup$
    – chl
    Dec 6, 2012 at 22:41

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.