Reputation: 13
I am generating two sets of data from normal distributions using rnorm(30, 10, 5)
. I am testing the hypothesis that the means are equal, using t.test
, and repeating this process 1000 times to estimate the type 1 error rate.
My code is as follows:
for(i in 1:1000) {
print(t.test(rnorm(30, 10, 5), rnorm(30, 10, 5),
alternative="two.sided", var.equal=TRUE)$p.value < 0.05)
}
However, this returns countless TRUE
and FALSE
.
I can manually count the TRUE
s, but is there an easier way?
Upvotes: 1
Views: 88
Reputation: 27398
They're not countless. There are 1000 of them ;)
Instead of printing the result to screen, you should keep track of it in a vector.
A simple change will get you what you want:
sig <- logical(length=1000)
for(i in 1:1000) {
sig[i] <- t.test(rnorm(30, 10, 5), rnorm(30, 10, 5),
alternative="two.sided", var.equal=TRUE)$p.value < 0.05
}
Now you can tabulate the results:
table(sig)
## sig
## FALSE TRUE
## 956 44
A simpler approach would be to use replicate
:
table(replicate(1000, t.test(rnorm(30, 10, 5), rnorm(30, 10, 5),
alternative="two.sided", var.equal=TRUE)$p.value < 0.05))
Upvotes: 2