JohnK
JohnK

Reputation: 1039

Simulating the significance level of a t-test in R

The following code generates 10000 t-tests on a custom distribution I have created.

> x <- replicate(10000,{  
  t.test(rcn(20,.25,25), mu=0, alternative="greater")
  })

I am constructing an empirical level of significance and so I am interested in the number of test-statistics that are greather than the critical value of the respective t-distribution, which is 1.729 (for a t-distribution with 19 degrees of freedom).

How can I select (and count) these test-statistics here? The ratio of their number over 10000, the total number of simulations, will give me my empirical level of significance.

Upvotes: 0

Views: 337

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

You can directly access the test statistic of each t.test in replicate. For example:

x <- replicate(10000, {
 t.test(rcn(20,.25,25), mu=0, alternative="greater")$statistic
})

This returns a vector of t-values.

You can compare it with your critical value and count the TRUEs:

crit <- 1.729
sum(x > crit)

Upvotes: 1

Related Questions