1. Saisir les données

tab <- matrix(c(592,544,119,97,849,677,504,451,36,14),ncol=5)
rownames(tab) <- c("Garçon","Fille")
colnames(tab) <- c("Blond","Roux","Châtain","Brun","Noir de jais")

2. Visualiser les données

par(mfrow=c(2,1))
couleur <- c("Gold","OrangeRed","Goldenrod","Brown","Black")
barplot(tab[1,],main="Garçon",col=couleur)
barplot(tab[2,],main="Fille",col=couleur)

3. Calculer les profils lignes et les profils colonnes

round(100 * prop.table (tab, margin = 1), 1)
       Blond Roux Châtain Brun Noir de jais
Garçon  28.2  5.7    40.4 24.0          1.7
Fille   30.5  5.4    38.0 25.3          0.8
round(100 * prop.table (tab, margin = 2), 1)
       Blond Roux Châtain Brun Noir de jais
Garçon  52.1 55.1    55.6 52.8           72
Fille   47.9 44.9    44.4 47.2           28

4. Construire le test du chi2

resultat <- chisq.test(tab)
resultat

    Pearson's Chi-squared test

data:  tab
X-squared = 10.467, df = 4, p-value = 0.03325

5. Calculer les contributions au chi2

names(resultat)
[1] "statistic" "parameter" "p.value"   "method"    "data.name" "observed"  "expected"  "residuals" "stdres"   
resultat$statistic
X-squared 
 10.46745 
round(resultat$expected,1)
       Blond  Roux Châtain  Brun Noir de jais
Garçon 614.4 116.8   825.3 516.5           27
Fille  521.6  99.2   700.7 438.5           23
round(100 * resultat$residuals^2 / resultat$statistic, 1)
       Blond Roux Châtain Brun Noir de jais
Garçon   7.8  0.4     6.5  2.9         28.4
Fille    9.2  0.5     7.7  3.4         33.4
round(resultat$residuals, 3)
        Blond   Roux Châtain   Brun Noir de jais
Garçon -0.903  0.202   0.825 -0.549        1.723
Fille   0.979 -0.219  -0.896  0.596       -1.870

Pour aller plus loin

tab.cont <- xtabs(~cheveux+sexe, data=donnees)
chisq.test(tab.cont)
LS0tDQp0aXRsZTogIlRlc3QgZHUgY2hpMiINCmF1dGhvcjogIkh1c3NvbiBldCBhbC4iDQpkYXRlOiAiMDUvMDkvMjAxOCINCm91dHB1dDoNCiAgaHRtbF9ub3RlYm9vazoN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