Home > Enterprise >  How to count the number of results in simulation?
How to count the number of results in simulation?

Time:11-27

How to count the result of the simulation t-test that have rejected?

For example:

set.seed(5)
> simulation <- function(n, mu, sigma, mu0, alpha) {
  rejects <- 0; S <- 100
  for(i in 1:S) {
  sample <- rnorm(n, mean=mu, sd=sigma)
  t.test(sample,mu=mu0)
  update(rejects)
  }
  return(rejects)
  }

In my code rejects is always 0, how can I update(rejects)? If the p-value of t.test < alpha, I want the number of rejects 1. How can I do that?

CodePudding user response:

The p value is stored in the p.value parameter of the T test. And to update rejects, you just do rejects = rejects 1.

set.seed(5)
simulation <- function(n, mu, sigma, mu0, alpha) {
 rejects <- 0; S <- 100
 for(i in 1:S) {
 sample <- rnorm(n, mean=mu, sd=sigma)
 T=t.test(sample,mu=mu0)
 if (T$p.value < alpha) rejects = rejects 1
 }
 return(rejects)
 }
  •  Tags:  
  • r
  • Related