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 + ,2 + ,1 + ,4 + ,1 + ,4 + ,0 + ,4 + ,0 + ,3 + ,0 + ,2 + ,0 + ,3 + ,1 + ,4 + ,1 + ,2 + ,NA + ,3 + ,0 + ,3 + ,1 + ,3 + ,1 + ,4 + ,1 + ,3 + ,1 + ,2 + ,0 + ,5 + ,0 + ,2 + ,1 + ,3 + ,0 + ,3 + ,1 + ,3 + ,0 + ,3 + ,1 + ,4 + ,0 + ,3 + ,0 + ,1 + ,1 + ,3 + ,0 + ,2 + ,1 + ,4 + ,1 + ,4 + ,1 + ,4 + ,0 + ,1 + ,0 + ,3 + ,0 + ,1 + ,1 + ,4 + ,0 + ,2 + ,1 + ,3 + ,1 + ,3 + ,0 + ,2 + ,1 + ,4 + ,0 + ,1 + ,1 + ,2 + ,0 + ,3 + ,1 + ,3 + ,0 + ,5 + ,1 + ,3 + ,1 + ,4 + ,0 + ,3 + ,1 + ,4 + ,1 + ,2 + ,1 + ,3 + ,0 + ,2 + ,1 + ,3 + ,0 + ,2 + ,0 + ,3 + ,1 + ,4 + ,1 + ,1 + ,1 + ,1 + ,0 + ,1 + ,1 + ,4 + ,0 + ,2 + ,0 + ,2 + ,1 + ,3 + ,1 + ,5 + ,0 + ,3 + ,1 + ,3 + ,0 + ,3 + ,0 + ,3 + ,1 + ,3 + ,1 + ,4 + ,1 + ,4 + ,1 + ,4 + ,0 + ,3 + ,1 + ,4 + ,0 + ,1 + ,1 + ,3 + ,1 + ,3 + ,1 + ,3 + ,0 + ,5 + ,0 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1 + ,4 + ,1 + ,1 + ,1 + ,4 + ,1 + ,4 + ,1 + ,4 + ,0 + ,3 + ,1 + ,3 + ,0 + ,3 + ,1 + ,5 + ,1 + ,5 + ,1 + ,4 + ,1 + ,4 + ,1 + ,3 + ,1 + ,4 + ,1 + ,4 + ,0 + ,3 + ,0 + ,1 + ,1 + ,4) + ,dim=c(2 + ,300) + ,dimnames=list(c('Gender' + ,'beer.wine') + ,1:300)) > y <- array(NA,dim=c(2,300),dimnames=list(c('Gender','beer.wine'),1:300)) > for (i in 1:dim(x)[1]) + { + for (j in 1:dim(x)[2]) + { + y[i,j] <- as.numeric(x[i,j]) + } + } > 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 2 [2,] 1 4 [3,] 1 4 [4,] 0 4 [5,] 0 3 [6,] 0 2 [7,] 0 3 [8,] 1 4 [9,] 1 2 [10,] NA 3 [11,] 0 3 [12,] 1 3 [13,] 1 4 [14,] 1 3 [15,] 1 2 [16,] 0 5 [17,] 0 2 [18,] 1 3 [19,] 0 3 [20,] 1 3 [21,] 0 3 [22,] 1 4 [23,] 0 3 [24,] 0 1 [25,] 1 3 [26,] 0 2 [27,] 1 4 [28,] 1 4 [29,] 1 4 [30,] 0 1 [31,] 0 3 [32,] 0 1 [33,] 1 4 [34,] 0 2 [35,] 1 3 [36,] 1 3 [37,] 0 2 [38,] 1 4 [39,] 0 1 [40,] 1 2 [41,] 0 3 [42,] 1 3 [43,] 0 5 [44,] 1 3 [45,] 1 4 [46,] 0 3 [47,] 1 4 [48,] 1 2 [49,] 1 3 [50,] 0 2 [51,] 1 3 [52,] 0 2 [53,] 0 3 [54,] 1 4 [55,] 1 1 [56,] 1 1 [57,] 0 1 [58,] 1 4 [59,] 0 2 [60,] 0 2 [61,] 1 3 [62,] 1 5 [63,] 0 3 [64,] 1 3 [65,] 0 3 [66,] 0 3 [67,] 1 3 [68,] 1 4 [69,] 1 4 [70,] 1 4 [71,] 0 3 [72,] 1 4 [73,] 0 1 [74,] 1 3 [75,] 1 3 [76,] 1 3 [77,] 0 5 [78,] 0 4 [79,] 1 3 [80,] 1 5 [81,] 1 4 [82,] 1 4 [83,] 1 4 [84,] 1 4 [85,] 0 3 [86,] 1 3 [87,] 1 4 [88,] 1 3 [89,] 0 1 [90,] 1 4 [91,] 1 4 [92,] 0 3 [93,] 0 3 [94,] 0 2 [95,] 1 4 [96,] 1 1 [97,] 0 3 [98,] 1 5 [99,] 1 4 [100,] 1 4 [101,] 0 3 [102,] 1 4 [103,] 1 4 [104,] 1 4 [105,] 0 3 [106,] 0 3 [107,] 1 3 [108,] 1 5 [109,] 1 4 [110,] 1 3 [111,] 0 1 [112,] 1 4 [113,] 0 3 [114,] 1 2 [115,] 0 4 [116,] 1 4 [117,] 1 5 [118,] 1 4 [119,] 1 4 [120,] 1 3 [121,] 1 4 [122,] 1 4 [123,] 1 4 [124,] 1 5 [125,] 1 3 [126,] 0 4 [127,] 1 4 [128,] 0 3 [129,] 0 3 [130,] 0 4 [131,] 0 4 [132,] 0 4 [133,] 1 5 [134,] 1 4 [135,] 1 4 [136,] 0 1 [137,] 1 4 [138,] 1 4 [139,] 1 4 [140,] 0 4 [141,] 1 4 [142,] 1 4 [143,] 0 3 [144,] 0 3 [145,] 1 4 [146,] 1 4 [147,] 1 5 [148,] 0 1 [149,] 0 4 [150,] 0 3 [151,] 1 4 [152,] 1 4 [153,] 1 4 [154,] 1 3 [155,] 1 4 [156,] 0 3 [157,] 0 4 [158,] 1 4 [159,] 1 4 [160,] 1 4 [161,] 0 4 [162,] 0 3 [163,] 0 5 [164,] 1 4 [165,] 0 2 [166,] 0 2 [167,] 1 2 [168,] 0 1 [169,] 0 2 [170,] 0 4 [171,] 1 2 [172,] 0 4 [173,] 0 3 [174,] 1 1 [175,] 0 3 [176,] 1 3 [177,] 1 4 [178,] 1 3 [179,] 0 3 [180,] 0 4 [181,] 0 3 [182,] 1 4 [183,] 0 5 [184,] 1 4 [185,] 0 3 [186,] 1 4 [187,] 1 2 [188,] 1 4 [189,] 1 3 [190,] 1 4 [191,] 0 3 [192,] 1 4 [193,] 0 3 [194,] 0 2 [195,] 0 4 [196,] 0 3 [197,] 0 3 [198,] 1 2 [199,] 1 3 [200,] 0 2 [201,] 0 2 [202,] 1 2 [203,] 1 4 [204,] 0 3 [205,] 0 4 [206,] 0 3 [207,] 1 5 [208,] 0 4 [209,] 1 4 [210,] 0 1 [211,] 1 3 [212,] 1 4 [213,] 0 3 [214,] 1 4 [215,] 0 3 [216,] 1 3 [217,] 0 3 [218,] 1 5 [219,] 0 3 [220,] 0 1 [221,] 0 4 [222,] 0 1 [223,] 0 1 [224,] 1 3 [225,] 0 2 [226,] 0 1 [227,] 1 4 [228,] 0 2 [229,] 1 4 [230,] 0 3 [231,] 0 4 [232,] 0 3 [233,] 0 2 [234,] 0 4 [235,] 0 2 [236,] 1 4 [237,] 0 2 [238,] 0 1 [239,] 1 4 [240,] 0 3 [241,] 1 4 [242,] 0 4 [243,] 1 4 [244,] 1 4 [245,] 1 3 [246,] 0 3 [247,] 0 3 [248,] 1 4 [249,] 1 3 [250,] 0 4 [251,] 0 4 [252,] 1 4 [253,] 1 4 [254,] 0 1 [255,] 0 3 [256,] 0 4 [257,] 0 3 [258,] 0 1 [259,] 0 1 [260,] 0 3 [261,] 1 4 [262,] 0 2 [263,] 1 3 [264,] 0 1 [265,] 1 4 [266,] 1 1 [267,] 0 3 [268,] 0 3 [269,] 1 4 [270,] 1 4 [271,] 0 3 [272,] 1 4 [273,] 1 4 [274,] 0 3 [275,] 0 1 [276,] 1 3 [277,] 0 3 [278,] 1 4 [279,] 0 2 [280,] 1 5 [281,] 0 3 [282,] 1 4 [283,] 1 4 [284,] 1 1 [285,] 1 4 [286,] 1 4 [287,] 1 4 [288,] 0 3 [289,] 1 3 [290,] 0 3 [291,] 1 5 [292,] 1 5 [293,] 1 4 [294,] 1 4 [295,] 1 3 [296,] 1 4 [297,] 1 4 [298,] 0 3 [299,] 0 1 [300,] 1 4 > (table1 <- table(z[,cat1],z[,cat2])) 1 2 3 4 5 0 23 23 61 24 5 1 6 11 38 95 13 > (V1<-dimnames(y)[[1]][cat1]) [1] "Gender" > (V2<-dimnames(y)[[1]][cat2]) [1] "beer.wine" > postscript(file="/var/wessaorg/rcomp/tmp/1k2r81387274727.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/2rd7c1387274727.tab") > (cst<-chisq.test(table1, simulate.p.value=simulate.p.value) ) Pearson's Chi-squared test data: table1 X-squared = 63.5412, df = 4, p-value = 5.22e-13 > 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/3ftxd1387274727.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/4bbjb1387274727.tab") > > try(system("convert tmp/1k2r81387274727.ps tmp/1k2r81387274727.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 3.309 0.970 4.293