2
$\begingroup$

I've been investigating exact tests and during that I find monte carlo method very useful.

I can write my own code for randomization and permutation tests but I cannot figure out how R function chisq.test do the randomization.

I see chisq.test uses stats:::C_chisq_sim to do it. But no matter how I try to randomize numbers myself, I cannot get the logic straight.

Anyone can explain a little to me?

Let's say we have a table

  1 5 |  6
  3 3 |  6
 -----+---
  4 8 | 12

What numbers (col sums? total sum?) do we fix and what do we randomize?

Thanks.

$\endgroup$

1 Answer 1

5
$\begingroup$

R's chisq.test uses the same kind of randomization as stats::r2dtable, which conditions on both margins. The calculations are done via Patefield's algorithm[1].

Indeed, the help on chisq.test states it explicitly:

Source:
  The code for Monte Carlo simulation is a C translation of the
  Fortran algorithm of Patefield (1981).

and also gives the full reference.

In the case of 2x2 tables as in your example, if you do that conditioning, you can just pick one cell (say the top left) and do straight simulation from a hypergeometric, since all other cells follow from the conditioning.

[That's not to say that you have to use the conditioning on row and column totals as in Patefield's algorithm, but there are some issues to deal with when you don't.]

[1]: Patefield, W. M. (1981),
"Algorithm AS159. An efficient method of generating r x c tables with given row and column totals."
Applied Statistics 30, 91-97.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.