[rkward-cvs] SF.net SVN: rkward:[3528] trunk/rkward

tfry at users.sourceforge.net tfry at users.sourceforge.net
Mon Apr 25 13:35:14 UTC 2011


Revision: 3528
          http://rkward.svn.sourceforge.net/rkward/?rev=3528&view=rev
Author:   tfry
Date:     2011-04-25 13:35:13 +0000 (Mon, 25 Apr 2011)

Log Message:
-----------
Implement most of the changes to the crosstabs plugin suggested by Andres Necochea

Modified Paths:
--------------
    trunk/rkward/ChangeLog
    trunk/rkward/rkward/plugins/analysis/crosstab.js
    trunk/rkward/rkward/plugins/analysis/crosstab.rkh
    trunk/rkward/rkward/plugins/analysis/crosstab.xml

Modified: trunk/rkward/ChangeLog
===================================================================
--- trunk/rkward/ChangeLog	2011-04-25 11:40:08 UTC (rev 3527)
+++ trunk/rkward/ChangeLog	2011-04-25 13:35:13 UTC (rev 3528)
@@ -1,3 +1,4 @@
+- Crosstabs N to 1 plugin gains options to compute proportions and margins (thanks to Andrés Necochea)		TODO: update / add automated test(s)
 - Added convenience R function rk.list() to allow simplification of plugin code		# TODO: ideally, this should be used in all applicable plugins
 - Added stack-based window switching using Ctrl+(Shift)+Tab; this replaces the old "Next Window" and "Previous Window" actions
 - Fixed: Graphics device windows would disappear when trying to attach them to the main window with some versions of Qt

Modified: trunk/rkward/rkward/plugins/analysis/crosstab.js
===================================================================
--- trunk/rkward/rkward/plugins/analysis/crosstab.js	2011-04-25 11:40:08 UTC (rev 3527)
+++ trunk/rkward/rkward/plugins/analysis/crosstab.js	2011-04-25 13:35:13 UTC (rev 3528)
@@ -1,30 +1,68 @@
+var prop_row, prop_column, prop_total, chisq, chisq_expetec, any_table_additions;
+
+function preprocess () {
+	prop_row = getValue ("prop_row") == "TRUE";
+	prop_column = getValue ("prop_column") == "TRUE";
+	prop_total = getValue ("prop_total") == "TRUE";
+	chisq = getValue ("chisq") == "TRUE";
+	chisq_expected = (getValue ("chisq_expected") == "TRUE") && chisq;
+	any_table_additions = (prop_row || prop_column || prop_total || chisq_expected);
+	if (!any_table_additions) return;
+
+	echo ('# convenience function to bind together several two dimensional tables into a single three dimensional table\n');
+	echo ('bind.tables <- function (...) {\n');
+	echo ('	tables <- list (...)\n');
+	echo ('	output <- unlist (tables)\n');
+	echo ('	dim (output) <- c (dim (tables[[1]]), length (tables))\n');
+	echo ('	dimnames (output) <- c (dimnames (tables[[1]]), list (statistic=names(tables)))\n');
+	echo ('	output\n');
+	echo ('}\n');
+}
+
 function calculate () {
 	var x = getValue ("x") ;
 	var y = trim (getValue ("y")).split (/\n/).join (', ');
+	var margins = (getValue ("margins") == "TRUE");
 
 	echo ('x <- rk.list (' + x + ')\n');
 	echo ('yvars <- rk.list (' + y + ')\n');
 	echo ('results <- list()\n');
+	if (chisq) echo ('chisquares <- list ()\n');
 	echo ('\n');
 	echo ('# calculate crosstabs\n');
 	echo ('for (i in 1:length (yvars)) {\n');
-	echo ('	results[[i]] <- table(x[[1]], yvars[[i]])\n');
-	echo ('}\n');
-	if (getValue ("chisq") == "TRUE") {
-		echo ('\n');
-		echo ('# calculate chisquares\n');
-		echo ('chisquares <- list ()\n');
-		echo ('for (i in 1:length (results)) {\n');
-		echo ('	chisquares[[i]] <- chisq.test (results[[i]], simulate.p.value = ' + getValue ("simpv"));
+	echo ('	count <- table(x[[1]], yvars[[i]])\n');
+	if (chisq) {
+		echo ('	chisquares[[i]] <- chisq.test (count, simulate.p.value = ' + getValue ("simpv"));
 		if (getValue ("simpv") == "TRUE") {
 			echo (',B=(');
 			echo (getValue ("B"));
 			echo (') ');
 		}
 		echo (')\n');
-		echo ('}\n');
 	}
-
+	if (!any_table_additions) {
+		if (margins) echo ('	results[[i]] <- addmargins (count)\n');
+		else echo ('	results[[i]] <- count\n');
+	} else {
+		// unfortunately, mixing margins and proportions is a pain, in that they don't make a whole lot of sense for "% of row", and "% of column"
+		if (margins) {
+			echo ('	results[[i]] <- bind.tables ("count"=addmargins (count)');
+			if (prop_row) echo (',\n		"% of row"=addmargins (prop.table(count, 1) * 100, quiet=TRUE, FUN=function(x) NA)');
+			if (prop_column) echo (',\n		"% of column"=addmargins (prop.table(count, 2) * 100, quiet=TRUE, FUN=function(x) NA)');
+			if (prop_total) echo (',\n		"% of total"=addmargins (prop.table(count) * 100)');
+			if (chisq_expected) echo (',\n		"expected"=addmargins (chisquares[[i]]$expected, quiet=TRUE, FUN=function(x) NA)');
+			echo (')\n');
+		} else {
+			echo ('	results[[i]] <- bind.tables ("count"=count');
+			if (prop_row) echo (',\n		"% of row"=prop.table(count, 1) * 100');
+			if (prop_column) echo (',\n		"% of column"=prop.table(count, 2) * 100');
+			if (prop_total) echo (',\n		"% of total"=prop.table(count) * 100');
+			if (chisq_expected) echo (',\n		"expected"=chisquares[[i]]$expected');
+			echo (')\n');
+		}
+	}
+	echo ('}\n');
 }
 
 function printout () {
@@ -41,7 +79,11 @@
 		echo ('rk.header ("Crosstabs (n to 1)", level=1)\n');
 		echo ('for (i in 1:length (results)) {\n');
 		echo ('	rk.header ("Crosstabs (n to 1)", parameters=list ("Dependent", names (x)[1], "Independent", names (yvars)[i]), level=2)\n');
-		echo ('	rk.results (results[[i]], titles=c(names (x)[1], names (yvars)[i]))\n');
+		if (any_table_additions) {
+			echo ('	rk.print (ftable (results[[i]], col.vars=2))\n');
+		} else {
+			echo ('	rk.results (results[[i]], titles=c(names (x)[1], names (yvars)[i]))\n');
+		}
 		if (getValue ("chisq") == "TRUE") {
 			echo ('\n');
 			echo ('	rk.header ("Pearson\'s Chi Square Test for Crosstabs", list ("Dependent", names (x)[1], "Independent", names (yvars)[i], "Method", chisquares[[i]][["method"]]), level=2)\n');
@@ -53,6 +95,11 @@
 			echo ('	rk.header ("Barplot for Crosstabs", list ("Dependent", names (x)[1], "Independent", names (yvars)[i]' + getValue ('barplot_embed.code.preprocess') + '), level=2)\n');
 			echo ('	rk.graph.on ()\n');
 			echo ('	try ({\n');
+			if (any_table_additions) {
+				echo ('		counts <- results[[i]][, , "count"]\n');
+			} else {
+				echo ('		counts <- results[[i]]\n');
+			}
 			printIndented ("\t\t", getValue ('barplot_embed.code.printout'));
 			echo ('	})\n');
 			echo ('	rk.graph.off ()\n');
@@ -60,7 +107,11 @@
 		echo ('}\n');
 	} else {
 		// produce a single barplot of the first result
-		echo ("i <- 1\n");
+		if (any_table_additions) {
+			echo ('counts <- results[[1]][, , "count"]\n');
+		} else {
+			echo ('counts <- results[[1]]\n');
+		}
 		echo (getValue ('barplot_embed.code.printout'));
 	}
 }

Modified: trunk/rkward/rkward/plugins/analysis/crosstab.rkh
===================================================================
--- trunk/rkward/rkward/plugins/analysis/crosstab.rkh	2011-04-25 11:40:08 UTC (rev 3527)
+++ trunk/rkward/rkward/plugins/analysis/crosstab.rkh	2011-04-25 13:35:13 UTC (rev 3528)
@@ -18,9 +18,16 @@
 		<setting id="barplot">Should a barplot be produced (for each pair)?</setting>
 		<setting id="preview">Allows to preview the barplot(s). Only the first of the barplots is shown in the preview, even if more than one table is produced.</setting>
 
+		<caption id="margins_tab"/>
+		<setting id="margins">Calculate and show sums / margins</setting>
+		<setting id="prop_row">Calculate and show the proportion (in %) of each cell's count of the corresponding row's total count.</setting>
+		<setting id="prop_column">Calculate and show the proportion (in %) of each cell's count of the corresponding column's total count.</setting>
+		<setting id="prop_total">Calculate and show the proportion (in %) of each cell's count of overall total count.</setting>
+
 		<caption id="chi_options"/>
 		<setting id="simpv">Should Monte-Carlo simulation be used to compute the p-value(s)?</setting>
 		<setting id="B">Number of replicates used in the Monte-Carlo test (if applicable).</setting>
+		<setting id="chisq_expected">Show the chisquare expected values per cell.</setting>
 
 		<caption id="barplot_options"/>
 		<setting id="barplot_embed">Various options controlling the look of the generated barplot(s). See <link href="rkward://component/barplot_embed"/></setting>

Modified: trunk/rkward/rkward/plugins/analysis/crosstab.xml
===================================================================
--- trunk/rkward/rkward/plugins/analysis/crosstab.xml	2011-04-25 11:40:08 UTC (rev 3527)
+++ trunk/rkward/rkward/plugins/analysis/crosstab.xml	2011-04-25 13:35:13 UTC (rev 3528)
@@ -6,7 +6,7 @@
 		<connect client="chi_options.enabled" governor="chisq.state" />
 		<connect client="barplot_options.enabled" governor="barplot.state" />
 		<connect client="preview.enabled" governor="barplot.state"/>
-		<set id="barplot_embed.xvar" to="results[[i]]"/>
+		<set id="barplot_embed.xvar" to="counts"/>
 
 		<convert id="montecarlo" mode="equals" sources="simpv.string" standard="TRUE" />
 		<connect client="B.enabled" governor="montecarlo" />
@@ -26,6 +26,13 @@
 					</column>
 				</row>
 			</tab>
+			<tab label="Sums and proportions" id="margins_tab">
+				<checkbox id="margins" label="Show sums" value="TRUE" value_unchecked="FALSE" checked="false" />
+				<checkbox id="prop_row" label="Show percent of row" value="TRUE" value_unchecked="FALSE" checked="false" />
+				<checkbox id="prop_column" label="Show percent of column" value="TRUE" value_unchecked="FALSE" checked="false" />
+				<checkbox id="prop_total" label="Show percent of total" value="TRUE" value_unchecked="FALSE" checked="false" />
+				<stretch/>
+			</tab>
 			<tab label="Chisquare Options" id="chi_options">
 				<radio id="simpv" label="Method of computing p-value">
 					<option value="FALSE" label="Asymptotic"  checked="true" />
@@ -34,6 +41,7 @@
 				<frame label="Number of replicates">
 					<spinbox type="integer" min="1" id="B" initial="2000" label="Number of replicates used in the Monte Carlo Method" />
 				</frame>
+				<checkbox id="chisq_expected" label="Show expect values" value="TRUE" value_unchecked="FALSE" checked="false" />
 				<stretch/>
 			</tab>
 			<tab label="Barplot Options" id="barplot_options">


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list