如何用轻敲来对因子的每个水平执行t检验 [英] How to perform t-tests for each level of a factor with tapply
问题描述
我的数据和代码如下:
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_factor2的两个级别都测试my_vector.
I want to do a separate t-test for each level of my_factor1, to test my_vector for both levels of my_factor2.
但是,对于我的代码,t检验不会拆分my_factor1的级别,并且每个级别的结果均相等,因为my_vector完全包含在每个t.test中.
However, with my code the t-test is not splitting the levels of my_factor1, and the results are equal for each level because my_vector is entirely included in each 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
我想念什么或做错什么了?
What am I missing or doing wrong?
推荐答案
您的示例存在一些问题,因为如果您进行了设置,
Your example is slightly problematic, since if you set:
df <- data.frame(my_vector = rnorm(150),
my_factor1 = gl(3,50),
my_factor2 = gl(2,75)
)
当my_factor1
= 1或3时,由于重复的重叠方式,对于my_factor2
只有一个唯一值.请参阅?gl
.也是这样:
You will have only one unique value for my_factor2
when my_factor1
= 1 or 3 because of how your repetitions overlap. See ?gl
. So do:
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)
)
)
似乎会产生所需的输出.
Which appears to produce your desired output.
作为旁注-考虑对多个比较进行更正: https://stats.stackexchange.com/questions/16779/when-is-multiple-comparison-correction-necessary
As a side note-- consider correction for multiple comparisons: https://stats.stackexchange.com/questions/16779/when-is-multiple-comparison-correction-necessary
这篇关于如何用轻敲来对因子的每个水平执行t检验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!