R version 2.14.1 (2011-12-22)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

R est un logiciel libre livré sans AUCUNE GARANTIE.
Vous pouvez le redistribuer sous certaines conditions.
Tapez 'license()' ou 'licence()' pour plus de détails.

R est un projet collaboratif avec de nombreux contributeurs.
Tapez 'contributors()' pour plus d'information et
'citation()' pour la façon de le citer dans les publications.

Tapez 'demo()' pour des démonstrations, 'help()' pour l'aide
en ligne ou 'help.start()' pour obtenir l'aide au format HTML.
Tapez 'q()' pour quitter R.

CWD = /Users/chl/Documents/Tutors/R_biomed_fr/pub 
> ## blood.txt
> ## R --no-save < blood.r > blood.log
> ##
> ## Number of children by mother ABO-blood type, father ABO-blood type,
> ## and the sex of the child.
> ##
> ## +-------------+-----+-----+-----+-----+
> ## |father/mother|  A  |  B  | AB  |  O  |
> ## +-------------+-----+-----+-----+-----+
> ## |   male/A    |  114|  218|   24|  267|
> ## +-------------+-----+-----+-----+-----+
> ## |   male/B    |  231|  685|   63|  843|
> ## +-------------+-----+-----+-----+-----+
> ## |   male/AB   |   33|   71|    8|   94|
> ## +-------------+-----+-----+-----+-----+
> ## |   male/O    |  308|  877|   84| 1014|
> ## +-------------+-----+-----+-----+-----+
> ## |  female/A   |   93|  206|   30|  293|
> ## +-------------+-----+-----+-----+-----+
> ## |  female/B   |  237|  640|   51|  803|
> ## +-------------+-----+-----+-----+-----+
> ## |  female/AB  |   37|   72|    9|  101|
> ## +-------------+-----+-----+-----+-----+
> ## |  female/O   |  279|  800|   79|  989|
> ## +-------------+-----+-----+-----+-----+
> ##
> ## Table 6.4 of Selvin's Modern Applied Biostatistical Methods Using
> ## S-PLUS (Oxford University Press, 1998). 
> 
> ## On importe les données
> blood <- read.table("blood.txt", header=TRUE)
> 
> ## À quoi ressemble les données ? (6 premières lignes)
> head(blood)
  mother father sex count
1      1      1   1   114
2      1      2   1   231
3      1      3   1    33
4      1      4   1   308
5      1      1   0    93
6      1      2   0   237
> 
> ## Quel est le nom des variables
> names(blood)
[1] "mother" "father" "sex"    "count" 
> 
> ## Quelles sont les variables, comment sont-elles stockées ?
> str(blood)
'data.frame':	32 obs. of  4 variables:
 $ mother: int  1 1 1 1 1 1 1 1 2 2 ...
 $ father: int  1 2 3 4 1 2 3 4 1 2 ...
 $ sex   : int  1 1 1 1 0 0 0 0 1 1 ...
 $ count : int  114 231 33 308 93 237 37 279 218 685 ...
> 
> ## Recodage des labels
> ##   father/mother: A=1, B=2, AB=3, O=4
> ##   sex: Male=1, Female=0
> bg <- c("A","B","AB","O")
> blood$mother <- factor(blood$mother, labels=bg)
> blood$father <- factor(blood$father, labels=bg)
> blood$sex <- factor(blood$sex, labels=c("female","male"))
> 
> ## On affiche un résumé des données après recodage
> summary(blood)
 mother father     sex         count       
 A :8   A :8   female:16   Min.   :   8.0  
 B :8   B :8   male  :16   1st Qu.:  69.0  
 AB:8   AB:8               Median : 160.0  
 O :8   O :8               Mean   : 301.7  
                           3rd Qu.: 391.0  
                           Max.   :1014.0  
> 
> ## On vérifie que l'on obtient le tableau d'effectifs initial
> xtabs(count ~ mother + father + sex, data=blood)
, , sex = female

      father
mother    A    B   AB    O
    A    93  237   37  279
    B   206  640   72  800
    AB   30   51    9   79
    O   293  803  101  989

, , sex = male

      father
mother    A    B   AB    O
    A   114  231   33  308
    B   218  685   71  877
    AB   24   63    8   84
    O   267  843   94 1014

> 
> ## Quelle est la fréquence marginale du groupe AB chez les pères ?
> xtabs(~ father, data=blood)
father
 A  B AB  O 
 8  8  8  8 
> table(blood$father)["AB"]
AB 
 8 
> 
> ## Les groupes sanguins sont-ils équilibrés entre les pères et mères ?
> xtabs(~ mother + father, data=blood)
      father
mother A B AB O
    A  2 2  2 2
    B  2 2  2 2
    AB 2 2  2 2
    O  2 2  2 2
> with(blood, table(mother,father))
      father
mother A B AB O
    A  2 2  2 2
    B  2 2  2 2
    AB 2 2  2 2
    O  2 2  2 2
> 
> ## Pour quelle combinaison père/mère/sexe observe-t-on la fréquence
> ## la plus faible ?
> blood[which(blood$count==8),]
   mother father  sex count
19     AB     AB male     8
> blood[order(blood$count),][1,]
   mother father  sex count
19     AB     AB male     8
> 
> ## On aggrège les données sur le sexe
> as.data.frame(xtabs(count ~ mother + father, data=blood))
   mother father Freq
1       A      A  207
2       B      A  424
3      AB      A   54
4       O      A  560
5       A      B  468
6       B      B 1325
7      AB      B  114
8       O      B 1646
9       A     AB   70
10      B     AB  143
11     AB     AB   17
12      O     AB  195
13      A      O  587
14      B      O 1677
15     AB      O  163
16      O      O 2003
> 
> ## On aggrège les groupes A et B en "A ou B" chez les mères
> levels(blood$mother)[1:2] <- "A ou B"
> xtabs(~ mother + father, data=blood)
        father
mother   A B AB O
  A ou B 4 4  4 4
  AB     2 2  2 2
  O      2 2  2 2
> 
> ## On convertit les effectifs en fréquences, en ne considérant que
> ## les garçons
> blood.male <- subset(blood, sex=="male")
> prop.table(xtabs(count ~ mother + father, data=blood.male))
        father
mother             A           B          AB           O
  A ou B 0.067288204 0.185650588 0.021078233 0.240170247
  AB     0.004864208 0.012768545 0.001621403 0.017024726
  O      0.054114309 0.170855290 0.019051480 0.205512769
> 
> ## Même chose avec les fréquences marginales par ligne et colonne
> prop.table(xtabs(count ~ mother + father, data=blood.male), margin=1)
        father
mother            A          B         AB          O
  A ou B 0.13086322 0.36105637 0.04099330 0.46708711
  AB     0.13407821 0.35195531 0.04469274 0.46927374
  O      0.12037872 0.38007214 0.04238052 0.45716862
> prop.table(xtabs(count ~ mother + father, data=blood.male), margin=2)
        father
mother            A          B         AB          O
  A ou B 0.53290530 0.50274424 0.50485437 0.51905388
  AB     0.03852327 0.03457739 0.03883495 0.03679369
  O      0.42857143 0.46267838 0.45631068 0.44415243
> 
> ## On arrondit le dernier résultat en % avec 1 décimale
> round(prop.table(xtabs(count ~ mother + father, data=blood.male), margin=2)*100, 1)
        father
mother      A    B   AB    O
  A ou B 53.3 50.3 50.5 51.9
  AB      3.9  3.5  3.9  3.7
  O      42.9 46.3 45.6 44.4
> 
> ## On vérifier que les colonnes somment bien à 1
> apply(prop.table(xtabs(count ~ mother + father, data=blood.male), margin=2), 2, sum)
 A  B AB  O 
 1  1  1  1 
> 
>