R version 3.0.2 (2013-09-25) -- "Frisbee Sailing" Copyright (C) 2013 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > x <- array(list('Man',3,'Man',3,'Vrouw',2,'Vrouw',5,'Vrouw',3,'Vrouw',4,'Man',5,'Vrouw',5,'Vrouw',1,'Vrouw',3,'Man',5,'Vrouw',5,'Vrouw',3,'Man',5,'Vrouw',5,'Man',4,'Man',5,'Man',3,'Man',4,'Vrouw',4,'Vrouw',5,'Vrouw',3,'Vrouw',4,'Man',5,'Vrouw',3,'Vrouw',1,'Vrouw',5,'Vrouw',3,'Vrouw',3,'Vrouw',2,'Man',2,'Vrouw',3,'Vrouw',2,'Vrouw',2,'Man',5,'Vrouw',4,'Man',4,'Vrouw',5,'Man',4,'Man',4,'Man',3,'Man',2,'Vrouw',5,'Man',2),dim=c(2,44),dimnames=list(c('geslacht','fluo'),1:44)) > y <- array(NA,dim=c(2,44),dimnames=list(c('geslacht','fluo'),1:44)) > for (i in 1:dim(x)[1]) + { + for (j in 1:dim(x)[2]) + { + y[i,j] <- as.numeric(x[i,j]) + } + } There were 44 warnings (use warnings() to see them) > par3 = 'Exact Pearson Chi-Squared by Simulation' > par2 = '2' > par1 = '1' > main = 'Association Plot' > par3 <- 'Exact Pearson Chi-Squared by Simulation' > par2 <- '2' > par1 <- '1' > #'GNU S' R Code compiled by R2WASP v. 1.2.327 () > #Author: root > #To cite this work: Wessa P., 2014, Chi-Squared Test, McNemar Test, and Fisher Exact Test (v1.0.8) in Free Statistics Software (v$_version), Office for Research Development and Education, URL http://www.wessa.net/rwasp_chi_squared_tests.wasp/ > #Source of accompanying publication: > # > library(vcd) Loading required package: grid > cat1 <- as.numeric(par1) # > cat2<- as.numeric(par2) # > simulate.p.value=FALSE > if (par3 == 'Exact Pearson Chi-Squared by Simulation') simulate.p.value=TRUE > x <- t(x) > (z <- array(unlist(x),dim=c(length(x[,1]),length(x[1,])))) [,1] [,2] [1,] "Man" "3" [2,] "Man" "3" [3,] "Vrouw" "2" [4,] "Vrouw" "5" [5,] "Vrouw" "3" [6,] "Vrouw" "4" [7,] "Man" "5" [8,] "Vrouw" "5" [9,] "Vrouw" "1" [10,] "Vrouw" "3" [11,] "Man" "5" [12,] "Vrouw" "5" [13,] "Vrouw" "3" [14,] "Man" "5" [15,] "Vrouw" "5" [16,] "Man" "4" [17,] "Man" "5" [18,] "Man" "3" [19,] "Man" "4" [20,] "Vrouw" "4" [21,] "Vrouw" "5" [22,] "Vrouw" "3" [23,] "Vrouw" "4" [24,] "Man" "5" [25,] "Vrouw" "3" [26,] "Vrouw" "1" [27,] "Vrouw" "5" [28,] "Vrouw" "3" [29,] "Vrouw" "3" [30,] "Vrouw" "2" [31,] "Man" "2" [32,] "Vrouw" "3" [33,] "Vrouw" "2" [34,] "Vrouw" "2" [35,] "Man" "5" [36,] "Vrouw" "4" [37,] "Man" "4" [38,] "Vrouw" "5" [39,] "Man" "4" [40,] "Man" "4" [41,] "Man" "3" [42,] "Man" "2" [43,] "Vrouw" "5" [44,] "Man" "2" > (table1 <- table(z[,cat1],z[,cat2])) 1 2 3 4 5 Man 0 3 4 5 6 Vrouw 2 4 8 4 8 > (V1<-dimnames(y)[[1]][cat1]) [1] "geslacht" > (V2<-dimnames(y)[[1]][cat2]) [1] "fluo" > postscript(file="/var/wessaorg/rcomp/tmp/1ts3q1410951689.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > assoc(ftable(z[,cat1],z[,cat2],row.vars=1,dnn=c(V1,V2)),shade=T) > dev.off() null device 1 > > #Note: the /var/wessaorg/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/wessaorg/rcomp/createtable") > > a<-table.start() > a<-table.row.start(a) > a<-table.element(a,'Tabulation of Results',ncol(table1)+1,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,paste(V1,' x ', V2),ncol(table1)+1,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a, ' ', 1,TRUE) > for(nc in 1:ncol(table1)){ + a<-table.element(a, colnames(table1)[nc], 1, TRUE) + } > a<-table.row.end(a) > for(nr in 1:nrow(table1) ){ + a<-table.element(a, rownames(table1)[nr], 1, TRUE) + for(nc in 1:ncol(table1) ){ + a<-table.element(a, table1[nr, nc], 1, FALSE) + } + a<-table.row.end(a) + } > a<-table.end(a) > table.save(a,file="/var/wessaorg/rcomp/tmp/2x8vu1410951689.tab") > (cst<-chisq.test(table1, simulate.p.value=simulate.p.value) ) Pearson's Chi-squared test with simulated p-value (based on 2000 replicates) data: table1 X-squared = 2.5012, df = NA, p-value = 0.6937 > if (par3 == 'McNemar Chi-Squared') { + (cst <- mcnemar.test(table1)) + } > if (par3=='Fisher Exact Test') { + (cst <- fisher.test(table1)) + } > if ((par3 != 'McNemar Chi-Squared') & (par3 != 'Fisher Exact Test')) { + a<-table.start() + a<-table.row.start(a) + a<-table.element(a,'Tabulation of Expected Results',ncol(table1)+1,TRUE) + a<-table.row.end(a) + a<-table.row.start(a) + a<-table.element(a,paste(V1,' x ', V2),ncol(table1)+1,TRUE) + a<-table.row.end(a) + a<-table.row.start(a) + a<-table.element(a, ' ', 1,TRUE) + for(nc in 1:ncol(table1)){ + a<-table.element(a, colnames(table1)[nc], 1, TRUE) + } + a<-table.row.end(a) + for(nr in 1:nrow(table1) ){ + a<-table.element(a, rownames(table1)[nr], 1, TRUE) + for(nc in 1:ncol(table1) ){ + a<-table.element(a, round(cst$expected[nr, nc], digits=2), 1, FALSE) + } + a<-table.row.end(a) + } + a<-table.end(a) + table.save(a,file="/var/wessaorg/rcomp/tmp/3q6511410951689.tab") + } > a<-table.start() > a<-table.row.start(a) > a<-table.element(a,'Statistical Results',2,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a, cst$method, 2,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > if (par3=='Pearson Chi-Squared') a<-table.element(a, 'Pearson Chi Square Statistic', 1, TRUE) > if (par3=='Exact Pearson Chi-Squared by Simulation') a<-table.element(a, 'Exact Pearson Chi Square Statistic', 1, TRUE) > if (par3=='McNemar Chi-Squared') a<-table.element(a, 'McNemar Chi Square Statistic', 1, TRUE) > if (par3=='Fisher Exact Test') a<-table.element(a, 'Odds Ratio', 1, TRUE) > if (par3=='Fisher Exact Test') { + if ((ncol(table1) == 2) & (nrow(table1) == 2)) { + a<-table.element(a, round(cst$estimate, digits=2), 1,FALSE) + } else { + a<-table.element(a, '--', 1,FALSE) + } + } else { + a<-table.element(a, round(cst$statistic, digits=2), 1,FALSE) + } > a<-table.row.end(a) > if(!simulate.p.value){ + if(par3!='Fisher Exact Test') { + a<-table.row.start(a) + a<-table.element(a, 'Degrees of Freedom', 1, TRUE) + a<-table.element(a, cst$parameter, 1,FALSE) + a<-table.row.end(a) + } + } > a<-table.row.start(a) > a<-table.element(a, 'P value', 1, TRUE) > a<-table.element(a, round(cst$p.value, digits=2), 1,FALSE) > a<-table.row.end(a) > a<-table.end(a) > table.save(a,file="/var/wessaorg/rcomp/tmp/4zetw1410951689.tab") > > try(system("convert tmp/1ts3q1410951689.ps tmp/1ts3q1410951689.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 0.889 0.112 1.003