Question

I have been having trouble getting my manual t-test script to output the same results as t.test. Any ideas of my mistake?

set.seed(100)
x <- rnorm(10, 1, 1)
t.test(x, mu=0, alternative="less")
##  One Sample t-test
## 
## data:  x
## t = 5.5345, df = 9, p-value = 0.9998
## alternative hypothesis: true mean is less than 0
## 95 percent confidence interval:
##      -Inf 1.307313
## sample estimates:
## mean of x 
## 0.9820428 

# Manual CI calculation
qt(0.95, df=length(x)-1) * sd(x) / sqrt(length(x))
## [1] 0.3252699
Was it helpful?

Solution

You forgot to add the sample mean.

qt(0.95, df=length(x)-1) * sd(x) / sqrt(length(x)) + mean(x)
#[1] 1.307313
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top