#Chapitre 8
require(BioStatR)
## Loading required package: BioStatR
#page 347
fisher.test(matrix(c(5,1,0,14),ncol=2,byrow=TRUE))
## 
##  Fisher's Exact Test for Count Data
## 
## data:  matrix(c(5, 1, 0, 14), ncol = 2, byrow = TRUE)
## p-value = 0.000387
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
##  4.307393      Inf
## sample estimates:
## odds ratio 
##        Inf
#page 356
#Exercice 8.1
#1)
Rhesus<-matrix(c(3620,3805,934,172,631,676,165,30),nrow=2,byrow=TRUE)
rownames(Rhesus)<-c("Rh+","Rh-")
colnames(Rhesus)<-c("O","A","B","AB")
#2)
Rhesus
##        O    A   B  AB
## Rh+ 3620 3805 934 172
## Rh-  631  676 165  30
#3)
class(Rhesus)
## [1] "matrix"
Rhesus<-as.table(Rhesus)
class(Rhesus)
## [1] "table"
#4)
plot(Rhesus,main="Dénombrements")

pdf("figexo81.pdf")
plot(Rhesus,main="Dénombrements")
dev.off()
## quartz_off_screen 
##                 2
#page 357
#5)
margin.table(Rhesus)
## [1] 10033
margin.table(Rhesus,margin=1)
##  Rh+  Rh- 
## 8531 1502
margin.table(Rhesus,margin=2)
##    O    A    B   AB 
## 4251 4481 1099  202
#6)
chisq.test(Rhesus,simulate.p.value=FALSE)$expected
##             O         A        B        AB
## Rh+ 3614.5999 3810.1675 934.4731 171.75939
## Rh-  636.4001  670.8325 164.5269  30.24061
chisq.test(Rhesus,simulate.p.value=FALSE)
## 
##  Pearson's Chi-squared test
## 
## data:  Rhesus
## X-squared = 0.10456, df = 3, p-value = 0.9913
#7)
chisq.test(Rhesus,simulate.p.value=TRUE,B=50000)
## 
##  Pearson's Chi-squared test with simulated p-value (based on 50000
##  replicates)
## 
## data:  Rhesus
## X-squared = 0.10456, df = NA, p-value = 0.9915
#page 358
#8)
fisher.test(Rhesus)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  Rhesus
## p-value = 0.991
## alternative hypothesis: two.sided
#9)
fisher.test(Rhesus,simulate.p.value=TRUE,B=50000)
## 
##  Fisher's Exact Test for Count Data with simulated p-value (based
##  on 50000 replicates)
## 
## data:  Rhesus
## p-value = 0.9912
## alternative hypothesis: two.sided
#Exercice 8.2
#1)
flor<-matrix(c(34,73,63,16,12,12),nrow=2,byrow=T)
rownames(flor)<-c("Fleuri","Pas fleuri")
colnames(flor)<-c("Engrais A","Engrais B","Engrais C")
flor<-as.table(flor)

#page 359
#2)
flor
##            Engrais A Engrais B Engrais C
## Fleuri            34        73        63
## Pas fleuri        16        12        12
#3)
dim(flor)
## [1] 2 3
#4)
plot(flor,main="Dénombrements")

#5)
chisq.test(flor)$expected
##            Engrais A Engrais B Engrais C
## Fleuri      40.47619  68.80952  60.71429
## Pas fleuri   9.52381  16.19048  14.28571
chisq.test(flor)
## 
##  Pearson's Chi-squared test
## 
## data:  flor
## X-squared = 7.2316, df = 2, p-value = 0.0269
#En plus : calcul de la p-valeur par simulation
chisq.test(flor,simulate.p.value=T,B=100000)
## 
##  Pearson's Chi-squared test with simulated p-value (based on 1e+05
##  replicates)
## 
## data:  flor
## X-squared = 7.2316, df = NA, p-value = 0.02779
#page 360
#6)
chisq.test(flor)$residuals
##             Engrais A  Engrais B  Engrais C
## Fleuri     -1.0179344  0.5051718  0.2933435
## Pas fleuri  2.0985256 -1.0414384 -0.6047432
#page 361
#7)
if(!("vcd" %in% rownames(installed.packages()))){install.packages("vcd")}
library(vcd)
## Loading required package: grid
assoc(flor,shade=TRUE)

assoc(t(flor),shade=TRUE)

pdf("figexo82.pdf")
assoc(flor,shade=TRUE)
dev.off()
## quartz_off_screen 
##                 2
pdf("figexo82transpose.pdf")
assoc(t(flor),shade=TRUE)
dev.off()
## quartz_off_screen 
##                 2
#Exercice 8.3
res.test<-chisq.test(c(100,18,24,18),p=c(90,30,30,10),rescale.p=TRUE)
res.test$expected
## [1] 90 30 30 10
res.test
## 
##  Chi-squared test for given probabilities
## 
## data:  c(100, 18, 24, 18)
## X-squared = 13.511, df = 3, p-value = 0.003652
chisq.test(c(100,18,24,18),p=c(90,30,30,10),rescale.p=TRUE,simulate=TRUE)
## 
##  Chi-squared test for given probabilities with simulated p-value
##  (based on 2000 replicates)
## 
## data:  c(100, 18, 24, 18)
## X-squared = 13.511, df = NA, p-value = 0.005997
#page 362
#Exercice 8.4
#1)
radio<-matrix(c(103,12,18,35),nrow=2,byrow=T)
rownames(radio)<-c("Bras cassé","Bras normal")
colnames(radio)<-c("Bras cassé","Bras normal")
radio<-as.table(radio)
#2)
radio
##             Bras cassé Bras normal
## Bras cassé         103          12
## Bras normal         18          35
#4)
mcnemar.test(radio)
## 
##  McNemar's Chi-squared test with continuity correction
## 
## data:  radio
## McNemar's chi-squared = 0.83333, df = 1, p-value = 0.3613
#page 363
#5)
binom.test(radio[2],n=sum(radio[c(2,3)]))
## 
##  Exact binomial test
## 
## data:  radio[2] and sum(radio[c(2, 3)])
## number of successes = 18, number of trials = 30, p-value = 0.3616
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
##  0.4060349 0.7734424
## sample estimates:
## probability of success 
##                    0.6