next up previous
Next: Creative Commons License

$\chi^2$ Tests and Fisher's Exact Tests in R

R is an open source statistical package with versions available for Linux, Mac OS X, and Windows. In addition to being available without charge, it is very powerful and very flexible. It's been my statistical package of choice for the past 4-5 years. Running a $\chi^2$ test or Fisher's exact test, as you need to do for the first question in Problem #1, is very straightforward - once you get the data in. In the example that follows, we'll imagine that you've determined the number $A_1$ and $A_2$ alleles contributed by fathers to offspring of each maternal genotype as follows:

  Paternal gamete
Maternal genotype $A_1$ $A_2$
$A_1A_1$ 12 17
$A_1A_2$ 4 25
$A_2A_2$ 15 4

Let me show you the code first. Then I'll explain it.

> alleles <- matrix(c(12, 4, 15, 17, 25, 4), nr=3,
+ dimnames=list(c("A1A1", "A1A2", "A2A2"), c("A1", "A2")))
> alleles
     A1 A2
A1A1 12 17
A1A2  4 25
A2A2 15  4
> chisq.test(alleles)

        Pearson's Chi-squared test

data:  alleles 
X-squared = 20.2851, df = 2, p-value = 3.937e-05

> fisher.test(alleles)

        Fisher's Exact Test for Count Data

data:  alleles 
p-value = 2.957e-05
alternative hypothesis: two.sided

The first thing to know about R is that it's command-line oriented. If you've ever used Linux, the DOS box in Windows, or the terminal in Mac OS X, you'll be familiar with the idea of a ``prompt''. That's the > that you see at the start of some lines. It indicates that R is waiting for you to type something. Sometimes you'll have a long command to type, as I do on the first line. IF you havent finished your command yet when you hit return, you'll get a different prompt, the + that you see on the second line. This reminds you that you're in the middle of typing a command. So what does that first line do?

next up previous
Next: Creative Commons License
Kent Holsinger 2006-09-02