R:为手动t检验和t.test获得相同的CI输出

时间:2014-04-28 22:13:51

标签: r statistics

我一直无法使用手动t-test脚本输出与t.test相同的结果。我错误的任何想法?

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

1 个答案:

答案 0 :(得分:3)

您忘记添加样本均值。

qt(0.95, df=length(x)-1) * sd(x) / sqrt(length(x)) + mean(x)
#[1] 1.307313