# Different confidence interval results in R, why?

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.

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.

• 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.) Commented Dec 6, 2012 at 21:17
• Oh, interesting. Thanks for the correction. I'll change it to length(). Commented Dec 6, 2012 at 21:18
• +1. In other words, change the manual calculations to mean(sample) + qt(c(.025, .975), length(sample) - 1) * se.
– whuber
Commented Dec 6, 2012 at 21:18
• 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.)
– chl
Commented Dec 6, 2012 at 22:12
• @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).
– chl
Commented Dec 6, 2012 at 22:41