[英]Displaying only the p-value of multiple t.tests
I have 我有
replicate(1000, t.test(rnorm(10)))
What it does that it draws a sample of size ten from a normal distribution, performs a t.test
on it, and does this a 1000 times. 它的作用是从正态分布中抽取10号样本,对其执行t.test
,并执行1000次。 But for my assignment I'm only interested in the p-value (the question is: how many times is the null hypothesis rejected). 但对于我的任务,我只对p值感兴趣(问题是:零假设被拒绝了多少次)。 How do I get only the p-values, or can I add something that already says how many times the null hypothesis is rejected(how many times the p-value is smaller than 0.05) 我如何仅获得p值,或者我可以添加已经说明零假设被拒绝多少次的内容(p值小于0.05的次数)
t.test
returns a object of class htest
which is a list containing a number of components including p.value
(which is what you want). t.test
返回类htest
的对象,这是一个包含许多组件的列表,包括p.value
(这是你想要的)。
You have a couple of options. 你有几个选择。
You can save the t.test
results in a list and then extract the p.value
component 您可以将t.test
结果保存在列表中,然后提取p.value
组件
# simplify = FALSE to avoid coercion to array
ttestlist <- replicate(1000, t.test(rnorm(10)), simplify = FALSE)
ttest.pval <- sapply(ttestlist, '[[', 'p.value')
Or you could simply only save that component of the t.test
object 或者你只能保存t.test
对象的那个组件
pvals <- replicate(1000, t.test(rnorm(10))$p.value)
Here are the steps I'd use to solve your problem. 以下是我用来解决问题的步骤。 Pay attention to how I broke it down into the smallest component parts and built it up step by step: 注意我如何将其分解为最小的组件并逐步构建它:
#Let's look at the structure of one t.test to see where the p-value is stored
str(t.test(rnorm(10)))
#It is named "p.value, so let's see if we can extract it
t.test(rnorm(10))[["p.value"]]
#Now let's test if its less than your 0.05 value
ifelse(t.test(rnorm(10))[["p.value"]]< 0.05,1,0)
#That worked. Now let's replace the code above in your replicate function:
replicate(1000, ifelse(t.test(rnorm(10))[["p.value"]]< 0.05,1,0))
#That worked too, now we can just take the sum of that:
#Make it reproducible this time
set.seed(42)
sum(replicate(1000, ifelse(t.test(rnorm(10))[["p.value"]]< 0.05,1,0)))
Should yield this: 应该得到这个:
[1] 54
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.