2014-04-28 29 views
0

我一直無法使我的手動t檢驗腳本輸出與t.test相同的結果。我的錯誤的任何想法?R:獲取相同的CI輸出以進行手動t檢驗和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 

回答

3

你忘了添加樣本的意思。

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

這是因爲它是單方面的t檢驗。如果您比較't.test(x,mu = 0)'和'mean(x) - qt(0.975,df = length(x)-1)*(sd(x)/ sqrt(length(x))) ;平均(x)+ qt(0.975,df =長度(x)-1)*(sd(x)/ sqrt(長度(x))''你會看到間隔將會匹配 –

+0

@DavidArenburg-我錯過了'另類=「少」'部分的電話。我今天早上很清楚, – thelatemail