单样本t测试接受或拒绝

时间:2015-10-11 11:21:05

标签: r p-value

我有以下列表:

> str1<-'cor 0.9834559 0.9816176 0.9797794 0.9926471'
> df1 <- read.table(text=scan(text=str1, what='', quiet=TRUE), header=TRUE)
> t.test (df1, mu=0.9816176, alternative = "less", conf.level = 0.95)

    One Sample t-test

data:  df1
t = 0.96491, df = 3, p-value = 0.7971
alternative hypothesis: true mean is less than 0.9816176
95 percent confidence interval:
      -Inf 0.9911001
sample estimates:
mean of x 
 0.984375 

我想根据t检验得到一个决定:AcceptReject假设H0。在这种情况下,因为p-value > 0.05我应该接受H0。 如何自动获得相关结果?

1 个答案:

答案 0 :(得分:2)

这是基于评论的解决方案。它涉及编写一个函数,该函数将从t检验中创建的对象中提取相关的p.value

auto <- function(x) {
  z <- t.test(x, mu=mean(x), alternative = "less", conf.level = 0.95)
  if (z$p.value > 0.05) {
    print("ACCEPT H0") 
    }  else {
    print("REJECT H0")
  }
}
#call the function
auto(df1$cor)
#[1] "ACCEPT H0"