JUICE 1.0000000000 WINE 1.0000000000 COFFEE 0.7265552461 Warning messages:
1: In chisq.test(.x) : Chi-squared approximation may be incorrect ...
(Anapply() allows us not to write the code for the test 13 times. You may omit cbind()since it used only to make output prettier. There were multiple warnings, and we will return to them soon.)
The result is that two foods exhibit significant associations with illness—Caesar salad and tomatoes. The culprit is identified! Almost. After all, it is unlikely that both dishes were contaminated. Now we must try to determine what was the main cause of the food poisoning. We will return to this subject later.
Let us discuss one more detail. Above, we applied chi-squared test simultane- ously several times. To account for multiple comparisons, we must adjust p- values, magnify them in accordance with the particular rule, for example, with widely known Bonferroni correction rule, or with (more reliable) Benjamini and Hochberg correction rule like in the following example:
> p.adjust(c(0.005, 0.05, 0.1), method="BH") [1] 0.015 0.075 0.100
Now you know how to apply p-value corrections for multiple comparisons. Try to do this for our toxicity data. Maybe, it will help to identify the culprit?
* * *
The special case of chi-squared test is thegoodness-of-fit test, orG-test. We will apply it to the famous data, results of Gregor Mendel first experiment. In this experiment, he crossed pea plants which grew out of round and angled seeds. When he counted seeds from the first generation of hybrids, he found that among 7,324 seeds, 5,474 were round and 1850 were angled. Mendel guessed that true ratio in this and six other experiments is 3:15:
5Mendel G. 1866. Versuche über Pflanzen-Hybriden. Verhandlungen des naturforschenden Vereines in Brünn. Bd. 4, Abhandlungen: 12.http://biodiversitylibrary.org/page/40164750
> chisq.test(c(5474, 1850), p=c(3/4, 1/4)) Chi-squared test for given probabilities data: c(5474, 1850)
X-squared = 0.26288, df = 1, p-value = 0.6081
Goodness-of-fit test uses the null that frequencies in the first argument (interpreted as one-dimensional contingency table) arenot different from probabilities in the second argument. Therefore, 3:1 ratio is statistically supported. As you might note, it is not radically different from the proportion test explained in the previous chap- ter.
Withoutp parameter, G-test simply checks if probabilities are equal. Let us check, for example, if numbers of species in supergroups of living organisms on Earth are equal:
> sp <- read.table("data/species.txt", sep="\t")
> species <- sp[, 2]
> names(species) <- sp[, 1]
> dotchart(rev(sort(log10(species))),
+ xlab="Decimal logarithm of species number", pch=19, pt.cex=1.2)
> chisq.test(species)
Chi-squared test for given probabilities data: species
X-squared = 4771700, df = 7, p-value < 2.2e-16
Naturally, numbers of species are not equal between supergroups. Some of them like bacteria (supergroup Monera) have surprisingly low number of species, others like insects (supergroup Ecdysozoa)—really large number (Fig.5.14).
* * *
Chi-squared test works well when the number of cases per cell is more then 5. If there are less cases,Rgives at least three workarounds.
First, instead of p-valueestimatedfrom the theoretical distribution, there is a way to calculate it directly, withFisher exact test. Tea drinker table contains less then 5 cases per cell so it is a good example:
> fisher.test(tea.t)
Fisher's Exact Test for Count Data data: tea.t
p-value = 0.4857
alternative hypothesis: true odds ratio is not equal to 1 95 percent confidence interval:
Ecdysozoa Vegetabilia Protista Spiralia Deuterostomia Non−Bilateria Monera Viri
●
●
●
●
●
●
●
●
3.5 4.0 4.5 5.0 5.5 6.0
Decimal logarithm of species number
Figure 5.14: Numbers of species in supergroups of living organisms.
0.2117329 621.9337505 sample estimates:
odds ratio 6.408309
Fisher test checks the null if odds ratio is just one. Although in this case, calculation gives odds ratio (3 : 1)/(1 : 3) = 9, there are only 8 observations, and confidence interval still includes one. Therefore, contrary to the first impression, the test does not support the idea that aforementioned woman is a good guesser.
Fourfold plot (pleasecheckit yourself) gives the similar result:
> fourfoldplot(tea.t)
While there is apparent difference between diagonals, confidence rings significantly intersect.
Fisher test is computationally intensive so it is not recommended to use it for large number of cases.
The second workaround is theYates continuity correction which in Ris default for chi-squared test on 2×2 tables. We use now data from the original Yates (1934)6 publication, data is taken from study of the influence of breast and artificial feeding on teeth formation:
> ee <- read.table("data/teeth.txt", h=T)
> chisq.test(table(ee))
Pearson's Chi-squared test with Yates' continuity correction data: table(ee)
X-squared = 1.1398, df = 1, p-value = 0.2857 Warning message:
In chisq.test(table(ee)) :
Chi-squared approximation may be incorrect (Note the warning in the end.)
Yates correction isnota default for thesummary.table()function:
> summary(table(ee)) # No correction in summary.table() Number of cases in table: 42
Number of factors: 2
Test for independence of all factors:
Chisq = 2.3858, df = 1, p-value = 0.1224 Chi-squared approximation may be incorrect
(Note different p-value: this is an effect of no correction. For all other kind of tables (e.g., non 2×2), results ofchisq.test()andsummary.table()should be similar.) The third way is tosimulatechi-squared test p-value with replication:
> chisq.test(table(ee), simulate.p.value=T)
Pearson's Chi-squared test with simulated p-value (based on 2000 replicates)
data: table(ee)
X-squared = 2.3858, df = NA, p-value = 0.1754
(Note that since this algorithm is based on random procedure, p-values might differ.)
6Yates F. 1934. Contingency tables involving small numbers and theχ2test. Journal of the Royal Statistical Society. 1(2): 217–235.
* * *
How to calculate aneffect size for the association of categorical variables? One of them isodds ratiofrom the Fisher test (see above). There are also several different effect size measures changing from 0 (no association) to (theoretically) 1 (which is an extremely strong association). If you do not want to use external packages, one of them,ϕcoefficient is easy to calculate from theχ-squared statistic.
> sqrt(chisq.test(tea.t, correct=FALSE)$statistic/sum(tea.t)) ...
0.5
Φcoefficient works only for two binary variables. If variables are not binary, there areTschuprow’s T andCramer’s V coefficients. Now it is better to use the external code from theshipunovpackage:
> (x <- margin.table(Titanic, 1:2)) Sex
Class Male Female
1st 180 145
2nd 179 106
3rd 510 196
Crew 862 23
> VTcoeffs(x) # shipunov
coefficients values comments
1 Cramer's V 0.3987227 medium
2 Cramer's V (corrected) 0.3970098 medium
3 Tschuprow's T 0.3029637
4 Tschuprow's T (corrected) 0.3016622
Rpackagevcdhas functionassocstats()which calculates odds ratio,ϕ, Cramer’s V and several other effect measures.
In the open repository, filecochlearia.txtcontains measurements of morpho- logical characters in several populations (locations) of scurvy-grass,Cochlearia.
One of characters, binary IS.CREEPINGreflects the plant life form: creeping or upright stem. Please check if numbers of creeping plants are different between locations, provide effect sizes and p-values.
* * *
There are many table tests. For example,test of proportionsfrom the previous chap- ter could be easily extended for two samples and therefore could be used as a table
test. There is alsomcnemar.test()which is used to compare proportions when they belong to same objects (paired proportions). You might want to check the help (and especially examples) in order to understand how they work.
In thebetula(see above) data, there are two binary characters: LOBES(position of lobes on the flower bract) andWINGS(the relative size of fruit wings). Please find if proportions of plants with 0 and 1 values of LOBESare different between location 1 and location 2.
Are proportions ofLOBESandWINGvalues different in the whole dataset?
* * *
The typical sequence of procedures related with analysis of tables is listed below:
• Check the phenomenon of association:table(),xtabs()
• Plot it first:mosaicplot(),spineplot(),assocplot()
• Decide is association is statistically significant:chisq.test(),fisher.test()
• Measure how strong is an association:VTCoeffs()
• Optionally, if there are more then two groups per case involved, runpost hoc pairise tests with the appropriate correction: pairwise.Table2.test()
* * *
To conclude this “differences” chapter, here is the Table5.5 which will guide the reader throughmost frequently used types of analysis. Please note also the much more detailed Table6.1in the appendix.
5.5 Answers to exercises
5.5.1 Two sample tests, effect sizes
Answerto the sign test question. It is enough to write:
> aa <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
> bb <- c(5, 5, 5, 5, 5, 5, 5, 5, 5)
> dif <- aa - bb