【发布时间】:2014-10-14 05:32:22
【问题描述】:
我正在尝试将 f-test 和 t-test 组合成一个函数,但 R 一直返回错误。
ds <- structure(list(Gender = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("F", "M"), class = "factor"),
Ratings = c(4L, 1L, 3L, 4L, 5L, 5L, 5L, 3L, 1L, 5L, 4L, 5L
)), .Names = c("Gender", "Ratings"), class = "data.frame", row.names = c(NA,
-12L))
从基本统计来看,如果 var.test 返回的值 > 0.05,则 t.test 应该有参数 var.equal = TRUE。
这样
> var.test(ds$Ratings~ ds$Gender)
F test to compare two variances
data: ds$Ratings by ds$Gender
F = 1.1324, num df = 5, denom df = 5, p-value = 0.8948
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.1584512 8.0922265
sample estimates:
ratio of variances
1.132353
t.test 应该是
> t.test(ds$Ratings~ ds$Gender, var.equal = TRUE)
Two Sample t-test
data: ds$Ratings by ds$Gender
t = 0.1857, df = 10, p-value = 0.8564
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.833149 2.166482
sample estimates:
mean in group F mean in group M
3.833333 3.666667
我试图将这两者组合成一个函数,如果 var.test 的 p 值
super.t <- function(y,x) {
tmp <- var.test(y~x)
if tmp$p.value < 0.05 {
tmp1 <- t.test(y~x, var.equal = FALSE)
} else {
tmp1 <- t.test(y~x, var.equal = TRUE)
}
return(tmp1)
}
super.t(ds$Ratings~ ds$Gender)
谢谢女士们先生们。
【问题讨论】: