R version 3.0.2 (2013-09-25) -- "Frisbee Sailing" Copyright (C) 2013 The R Foundation for Statistical Computing Platform: i686-pc-linux-gnu (32-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(1,'T',1,'T',1,'T',0,'T',1,'T',1,'T',1,'T',1,'T',1,'T',1,'T',0,'T',1,'T',1,'T',1,'T',0,'T',1,'T',1,'T',1,'T',1,'T',0,'T',1,'T',1,'T',0,'T',0,'T',1,'T',0,'T',1,'T',0,'T',0,'T',1,'T',0,'T',1,'T',0,'T',0,'T',0,'T',1,'T',1,'T',1,'E',1,'E',1,'E',1,'E',1,'E',1,'E',1,'E',0,'E',1,'E',1,'E',1,'E',1,'E',0,'E',1,'E',1,'E',1,'E',0,'E',1,'E',1,'E',1,'E',1,'E',0,'E',0,'E',1,'E',1,'E',1,'E',0,'E',0,'E',1,'E',1,'E',0,'E',1,'E',1,'E',1,'S',1,'S',1,'S',1,'S',1,'S',0,'S',0,'S',1,'S',0,'S',1,'S',0,'S',0,'S',0,'S',0,'S',0,'S',0,'S',0,'S',1,'S',0,'S',0,'S',1,'S',1,'S',1,'S',1,'S',1,'S',1,'S',1,'S',1,'S',0,'S',0,'S',1,'S',0,'S',0,'S',0,'S',1,'S'),dim=c(2,105),dimnames=list(c('score','treatment'),1:105)) > y <- array(NA,dim=c(2,105),dimnames=list(c('score','treatment'),1:105)) > for (i in 1:dim(x)[1]) + { + for (j in 1:dim(x)[2]) + { + y[i,j] <- as.numeric(x[i,j]) + } + } There were 50 or more warnings (use warnings() to see the first 50) > par3 = 'Pearson Chi-Squared' > par2 = '2' > par1 = '1' > main = 'Association Plot' > par3 <- 'Pearson Chi-Squared' > par2 <- '2' > par1 <- '1' > #'GNU S' R Code compiled by R2WASP v. 1.2.327 () > #Author: root > #To cite this work: Wessa P., 2013, Chi-Squared Test, McNemar Test, and Fisher Exact Test (v1.0.7) 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: MASS Loading required package: grid Loading required package: colorspace > 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,] "1" "T" [2,] "1" "T" [3,] "1" "T" [4,] "0" "T" [5,] "1" "T" [6,] "1" "T" [7,] "1" "T" [8,] "1" "T" [9,] "1" "T" [10,] "1" "T" [11,] "0" "T" [12,] "1" "T" [13,] "1" "T" [14,] "1" "T" [15,] "0" "T" [16,] "1" "T" [17,] "1" "T" [18,] "1" "T" [19,] "1" "T" [20,] "0" "T" [21,] "1" "T" [22,] "1" "T" [23,] "0" "T" [24,] "0" "T" [25,] "1" "T" [26,] "0" "T" [27,] "1" "T" [28,] "0" "T" [29,] "0" "T" [30,] "1" "T" [31,] "0" "T" [32,] "1" "T" [33,] "0" "T" [34,] "0" "T" [35,] "0" "T" [36,] "1" "T" [37,] "1" "T" [38,] "1" "E" [39,] "1" "E" [40,] "1" "E" [41,] "1" "E" [42,] "1" "E" [43,] "1" "E" [44,] "1" "E" [45,] "0" "E" [46,] "1" "E" [47,] "1" "E" [48,] "1" "E" [49,] "1" "E" [50,] "0" "E" [51,] "1" "E" [52,] "1" "E" [53,] "1" "E" [54,] "0" "E" [55,] "1" "E" [56,] "1" "E" [57,] "1" "E" [58,] "1" "E" [59,] "0" "E" [60,] "0" "E" [61,] "1" "E" [62,] "1" "E" [63,] "1" "E" [64,] "0" "E" [65,] "0" "E" [66,] "1" "E" [67,] "1" "E" [68,] "0" "E" [69,] "1" "E" [70,] "1" "E" [71,] "1" "S" [72,] "1" "S" [73,] "1" "S" [74,] "1" "S" [75,] "1" "S" [76,] "0" "S" [77,] "0" "S" [78,] "1" "S" [79,] "0" "S" [80,] "1" "S" [81,] "0" "S" [82,] "0" "S" [83,] "0" "S" [84,] "0" "S" [85,] "0" "S" [86,] "0" "S" [87,] "0" "S" [88,] "1" "S" [89,] "0" "S" [90,] "0" "S" [91,] "1" "S" [92,] "1" "S" [93,] "1" "S" [94,] "1" "S" [95,] "1" "S" [96,] "1" "S" [97,] "1" "S" [98,] "1" "S" [99,] "0" "S" [100,] "0" "S" [101,] "1" "S" [102,] "0" "S" [103,] "0" "S" [104,] "0" "S" [105,] "1" "S" > (table1 <- table(z[,cat1],z[,cat2])) E S T 0 8 17 13 1 25 18 24 > (V1<-dimnames(y)[[1]][cat1]) [1] "score" > (V2<-dimnames(y)[[1]][cat2]) [1] "treatment" > postscript(file="/var/wessaorg/rcomp/tmp/15drr1381510175.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/2mthz1381510175.tab") > (cst<-chisq.test(table1, simulate.p.value=simulate.p.value) ) Pearson's Chi-squared test data: table1 X-squared = 4.3811, df = 2, p-value = 0.1119 > 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/3gsa71381510175.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/4cwh91381510175.tab") > > try(system("convert tmp/15drr1381510175.ps tmp/15drr1381510175.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.954 0.373 2.296