[英]Displaying only the p-value of multiple t.tests
我有
replicate(1000, t.test(rnorm(10)))
它的作用是從正態分布中抽取10號樣本,對其執行t.test
,並執行1000次。 但對於我的任務,我只對p值感興趣(問題是:零假設被拒絕了多少次)。 我如何僅獲得p值,或者我可以添加已經說明零假設被拒絕多少次的內容(p值小於0.05的次數)
t.test
返回類htest
的對象,這是一個包含許多組件的列表,包括p.value
(這是你想要的)。
你有幾個選擇。
您可以將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')
或者你只能保存t.test
對象的那個組件
pvals <- replicate(1000, t.test(rnorm(10))$p.value)
以下是我用來解決問題的步驟。 注意我如何將其分解為最小的組件並逐步構建它:
#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)))
應該得到這個:
[1] 54
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.