私は持っている
replicate(1000, t.test(rnorm(10)))
t.test
正規分布からサイズ 10 のサンプルを抽出し、その上で a を実行し、これを 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