我的数据和代码是这样的:
my_vector <- rnorm(150)
my_factor1 <- gl(3,50)
my_factor2 <- gl(2,75)
tapply(my_vector, my_factor1, function(x)
t.test(my_vector~my_factor2, paired=T))
我想对每个级别的my_factor1进行单独的t检验,以测试my_vector的两个级别的my_vector。
但是,使用我的代码时,t-test不会分割my_factor1的级别,并且每个级别的结果都相同,因为my_vector完全包含在每个t.test中。
这是我的代码的输出:
$`1`
Paired t-test
data: my_vector by my_factor2
t = 0.2448, df = 74, p-value = 0.8073
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2866512 0.3669667
sample estimates:
mean of the differences
0.04015775
$`2`
Paired t-test
data: my_vector by my_factor2
t = 0.2448, df = 74, p-value = 0.8073
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2866512 0.3669667
sample estimates:
mean of the differences
0.04015775
$`3`
Paired t-test
data: my_vector by my_factor2
t = 0.2448, df = 74, p-value = 0.8073
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2866512 0.3669667
sample estimates:
mean of the differences
0.04015775
我错过了什么或做错了什么?
答案 0 :(得分:2)
您的示例稍有问题,因为如果您设置:
df <- data.frame(my_vector = rnorm(150),
my_factor1 = gl(3,50),
my_factor2 = gl(2,75)
)
my_factor2
= 1或3时,my_factor1
只有一个唯一值,因为重复的重叠方式。见?gl
。所以:
df <- data.frame(my_vector = rnorm(150),
my_factor1 = gl(3,1,150),
my_factor2 = gl(2,1,150)
)
with(df,
by(df, my_factor1,
function(x) t.test(my_vector ~ my_factor2, data=x)
)
)
这似乎可以产生您想要的输出。
作为旁注 - 考虑更正多重比较:https://stats.stackexchange.com/questions/16779/when-is-multiple-comparison-correction-necessary