##2kX2k Crosstable
You can see the simple pattern for design f exp factor expected by gl function
gl(2,3,4)
## [1] 1 1 1 2
## Levels: 1 2
gl(4,1,length=4,ordered = TRUE)
## [1] 1 2 3 4
## Levels: 1 < 2 < 3 < 4
gl(2,3,12,labels=c("oui","non"))
## [1] oui oui oui non non non oui oui oui non non non
## Levels: oui non
#2 levels factors repeated 3 times with 12 reps sequencing labels not mandatory
gl(3,4,12,labels=letters[1:3])#2 levels factors repeated 3 times with 12 reps sequencing labels not mandatory
## [1] a a a a b b b b c c c c
## Levels: a b c
#Altenating a pairs in factor
gl(3,2,12,labels=letters[1:3])#2 levels factors repeated 3 times with 12 reps sequencing labels not mandatory#second position of gl function gives you the prepeated pattern
## [1] a a b b c c a a b b c c
## Levels: a b c
table 2X2
mytab=c(5,1,11,24)
#for a table 2X2k of (de gauche a droite en descendant 5,11,oui non, 1,24 oui non )
#problem here the repat pattern isnt equal in sec position of the function
A=gl(2,1,length=4,labels=c("oui","non"))#sequence de oui non 1x dans un tableau
avant=factor(rep(A,mytab))#je repete le vecteur autant de fois dans sa joint distribution puisque il est a la fois ds les lignes et le colommnes
avant
## [1] oui oui oui oui oui non oui oui oui oui oui oui oui oui oui oui oui non non
## [20] non non non non non non non non non non non non non non non non non non non
## [39] non non non
## Levels: oui non
##checks 5,oui 1 non 11 oui 24 non
##je fais de meme ds les colomnes:
C=gl(2,2,length=4,labels=c("oui","non"))#sequence de oui non 2x dans un
C#cette fois les paires oui sont a la suites ds la colomne de 1 a column 2
## [1] oui oui non non
## Levels: oui non
apres=rep(C,mytab)
apres
## [1] oui oui oui oui oui oui non non non non non non non non non non non non non
## [20] non non non non non non non non non non non non non non non non non non non
## [39] non non non
## Levels: oui non
table(avant,apres)#meme si j'ai oublié factor ds après table le mets pour moi
## apres
## avant oui non
## oui 5 11
## non 1 24
apres=factor(apres)
chisq.test(table(avant,apres)#meme si j'ai oublié factor ds après table le mets pour moi
)
## Warning in chisq.test(table(avant, apres)): L’approximation du Chi-2 est
## peut-être incorrecte
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: table(avant, apres)
## X-squared = 3.8229, df = 1, p-value = 0.05056
avv=c(rep("oui",5),rep("oui",11),rep("non",1),rep("non",24))
avv
## [1] "oui" "oui" "oui" "oui" "oui" "oui" "oui" "oui" "oui" "oui" "oui" "oui"
## [13] "oui" "oui" "oui" "oui" "non" "non" "non" "non" "non" "non" "non" "non"
## [25] "non" "non" "non" "non" "non" "non" "non" "non" "non" "non" "non" "non"
## [37] "non" "non" "non" "non" "non"
length(avv)
## [1] 41
appp=c(rep("oui",5),rep("non",11),rep("oui",1),rep("non",24))
TAB=table(avv,appp)
addmargins(t(TAB))
## avv
## appp non oui Sum
## non 24 11 35
## oui 1 5 6
## Sum 25 16 41
chisq.test(TAB)
## Warning in chisq.test(TAB): L’approximation du Chi-2 est peut-être incorrecte
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: TAB
## X-squared = 3.8229, df = 1, p-value = 0.05056
prop.test(TAB)
## Warning in prop.test(TAB): L’approximation du Chi-2 est peut-être incorrecte
##
## 2-sample test for equality of proportions with continuity correction
##
## data: TAB
## X-squared = 3.8229, df = 1, p-value = 0.05056
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.01850523 0.56350523
## sample estimates:
## prop 1 prop 2
## 0.9600 0.6875
prop.test(t(TAB))
## Warning in prop.test(t(TAB)): L’approximation du Chi-2 est peut-être incorrecte
##
## 2-sample test for equality of proportions with continuity correction
##
## data: t(TAB)
## X-squared = 3.8229, df = 1, p-value = 0.05056
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## 0.08590482 0.95219042
## sample estimates:
## prop 1 prop 2
## 0.6857143 0.1666667
# some testing equally equivalent on 2k2ktable