扩展现有功能(学生t检验)

时间:2015-03-23 03:13:12

标签: r function

我试图获得学生的t检验,但不仅使用t.test函数,而且还扩展它。

到目前为止,

x<- c(16.6,13.4,14.6,15.1,12.9,15.2,14.0,16.6,15.4,13.0)
y<- c(15.8,17.9,18.2,20.2,18.1,17.8,18.3,18.6,17.0,18.4)

ttest <- function(x,y) {
  n1<- length(y)       #y length
  n2<- length(x)       #x length
  somay <- sum(y)
  somax <- sum(x)
  y1 <- (somay)/n1       #y mean
  y2<- (somax)/n2        #x mean
  dadosy <- na.omit(y)
  dadosx <- na.omit(x)
  disvquad1 <- (dadosy-y1)^2
  disvquad2 <- (dadosx-y2)^2
  s12<- (1/(n1-1))*sum(disvquad1)    #variance of y
  s22<- (1/(n2-1))*sum(disvquad2)    #variance of x
  s2 <- ((n1-1)*s12+(n2-1)*s22)/((n1-1)+(n2-1))
  s<- (s2)^(1/2)
  t<- (y1-y2)/((1/n1)+(1/n2))^(1/s)     #test statistic 
  return(t)
}
> ttest(x,y)
[1] 12.22666

但是,当我使用此参数运行t.test函数时,我会收到不同的值:

> t.test(x,y)

    Welch Two Sample t-test

data:  x and y
t = -6.0257, df = 17.425, p-value = 1.227e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -4.520774 -2.179226
sample estimates:
mean of x mean of y 
    14.68     18.03 

t = -6.0257 != t = 12.22666

任何想法为什么我会收到不同的价值观?谢谢你的帮助。

1 个答案:

答案 0 :(得分:3)

您的测试统计信息的公式是错误的。它应该是

ttest <- function(x,y) {
n1<- length(y)       #y length
n2<- length(x)       #x length
somay <- sum(y)
somax <- sum(x)
y1 <- (somay)/n1       #y mean
y2<- (somax)/n2        #x mean
dadosy <- na.omit(y)
dadosx <- na.omit(x)
disvquad1 <- (dadosy-y1)^2
disvquad2 <- (dadosx-y2)^2
s12<- (1/(n1-1))*sum(disvquad1)    #variance of y
s22<- (1/(n2-1))*sum(disvquad2)    #variance of x
s2 <- ((n1-1)*s12+(n2-1)*s22)/((n1-1)+(n2-1))
s<- (s2)^(1/2)
t<- (y1-y2)/(sqrt(1/n1+1/n2)*s)     #test statistic 
return(t)
}

> ttest(x,y)
[1] 6.025747

> t.test(y,x)

Welch Two Sample t-test

data:  y and x
t = 6.0257, df = 17.425, p-value = 1.227e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
2.179226 4.520774
sample estimates:
mean of x mean of y 
18.03     14.68