简体   繁体   中英

How to calculate the Power Function of the Chi Square Goodness of Fit Test through Monte Carlo simulations using R?

I need tips of how to calculate the power function of the Chi Square Goodness of Fit test using Monte Carlo Simulations in R. I am familiar with the pwr.chisq function but i need a way to use R to write the code for the Monte Carlo simulation.

I can do it for the T-test as follows:

n <- 100
mean_true <- 17
sd_true <- 2
## Null-Hypothesis (H0: mean_true = mean_0):
mean_0 <- seq(16, 18, len=15)

alpha <- 0.05

B <- 1000

Empirical_Power <- rep(NA, length(mean_0))
for(j in 1:length(Empirical_Power)){

Test_Decisions <- rep(NA, B)

for(i in 1:B){

dat_X <- rnorm(n=n, mean=mean_true, sd = sd_true)

t.Test_result <- t.test(x = dat_X, alternative = "two.sided", mu = mean_0[j])

Test_Decisions[i] <- t.Test_result$p.value < alpha
}
Number_of_Rejections <- length(Test_Decisions[Test_Decisions==TRUE])

Empirical_Power[j] <- Number_of_Rejections/B
}

I need a similar way for Chi-Square and it doesn't seem to work. That's how far i got but clearly it's wrong because no sense can be made from the results:

n <- 100
Frequency_true <- c(50,60,40,47,53)
sd_true <- 2

Frequency_0 <- c(0.2,0.2,0.2,0.2,0.2)

alpha <- 0.05

B <- 1000

Empirical_Power <- rep(NA, length(Frequency_0))
for(j in 1:length(Empirical_Power)){
  
  Test_Decisions <- rep(NA, B)
  
  for(i in 1:B){
    
    dat_X <- rchisq(100000, df=99)
    
    Chisq_result <- chisq.test(x = Frequency_true, p= Frequency_0)
    
    Test_Decisions[i] <- Chisq_result$p.value < alpha
  }
  Number_of_Rejections <- length(Test_Decisions[Test_Decisions==TRUE])
  
  Empirical_Power[j] <- Number_of_Rejections/B
}

The experiment is to draw 250 balls from a urn with 5 different types of balls where all types of balls are equi-probably drawn. The counts of types of balls drawn are given in vector Frequency_true , that follows a multinomial distribution .
So, in order to have the simulated power of the test, simulate B draws with a fixed total count of 250, using rmultinom , run chi-squared tests of Goodness-of-Fit and compute the proportion of p-values below the significance level alpha .

sim_p_value <- function(B, freq, prob){
  Sum <- sum(freq)
  x <- rmultinom(B, size = Sum, prob = prob)
  apply(x, 2, \(y) chisq.test(y, p = prob)$p.value)
}

Frequency_true <- c(50,60,40,47,53)
Frequency_0 <- c(0.2,0.2,0.2,0.2,0.2)
alpha <- 0.05
B <- 1000

set.seed(2022)
pval <- sim_p_value(B, Frequency_true, Frequency_0)
Empirical_Power <- mean(pval < alpha)

Empirical_Power
#> [1] 0.16

Created on 2022-07-09 by the reprex package (v2.0.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM