Monte Carlo test in a two-way contingency table with the total number of observations fixed, row margin fixed, or both margins fixed.

monte_carlo_chisq_test(x, margin = c("N", "rows", "both"), B = 100000L)

Arguments

x

A matrix representing the contingency table.

margin

A string that determines which margin is fixed: Either "N" for the total number of observations (the default), "rows" for fixed row sums, and "both" for simultaneously fixed row and column sums.

B

The number of simulations used to compute the p-value.

Value

A list of class "htest" giving the simulation results.

Details

Simulation is done by random sampling from the set of all tables with given marginal(s), and works only if the relevant marginal(s) are strictly positive. Continuity correction is never used, and the statistic is quoted without it.

Author

Claus Ekstrom <claus@rprimer.dk>

Examples


m <- matrix(c(12, 4, 8, 6), 2)
chisq.test(m)
#> Warning: Chi-squared approximation may be incorrect
#> 
#> 	Pearson's Chi-squared test with Yates' continuity correction
#> 
#> data:  m
#> X-squared = 0.41853, df = 1, p-value = 0.5177
#> 
chisq.test(m, correct=FALSE)
#> Warning: Chi-squared approximation may be incorrect
#> 
#> 	Pearson's Chi-squared test
#> 
#> data:  m
#> X-squared = 1.0714, df = 1, p-value = 0.3006
#> 
monte_carlo_chisq_test(m)
#> 
#> 	Monte Carlo Pearson's Chi-squared test (based on 100000 replicates with
#> 	fixed N (total number of observations))
#> 
#> data:  m
#> X-squared = 1.0714, B = 100000, p-value = 0.3358
#> 

fisher.test(m)
#> 
#> 	Fisher's Exact Test for Count Data
#> 
#> data:  m
#> p-value = 0.4421
#> alternative hypothesis: true odds ratio is not equal to 1
#> 95 percent confidence interval:
#>   0.3748465 14.3417410
#> sample estimates:
#> odds ratio 
#>   2.188389 
#> 
monte_carlo_chisq_test(m, margin="both")
#> 
#> 	Monte Carlo Pearson's Chi-squared test (based on 100000 replicates with
#> 	fixed row AND column margins)
#> 
#> data:  m
#> X-squared = 1.0714, B = 100000, p-value = 0.4421
#> 

m2 <- matrix(c(9, 3, 3, 7), 2)
monte_carlo_chisq_test(m, margin="N")
#> 
#> 	Monte Carlo Pearson's Chi-squared test (based on 100000 replicates with
#> 	fixed N (total number of observations))
#> 
#> data:  m
#> X-squared = 1.0714, B = 100000, p-value = 0.3328
#> 
monte_carlo_chisq_test(m, margin="both") 
#> 
#> 	Monte Carlo Pearson's Chi-squared test (based on 100000 replicates with
#> 	fixed row AND column margins)
#> 
#> data:  m
#> X-squared = 1.0714, B = 100000, p-value = 0.4408
#>