eugen pircalabelu
2009-Jan-29 14:01 UTC
[R] Question On CrossTable function in gmodels package
Hi R-users, I have the following problem with CrossTable function within ?gmodels? package: the output of the function (format ?spss? and asresid=T) can not be stored within another object. For example:>library(gmodels) >data(infert, package = "datasets") > CrossTable(infert$education, infert$induced)->aa # the function prints everything ok on the screen > aa # works just fine$t y x 0 1 2 0-5yrs 4 2 6 6-11yrs 78 27 15 12+ yrs 61 39 16 ?????. But when I call>CrossTable(infert$education, infert$induced, asresid=TRUE, format="SPSS")->aaa # the function prints everything ok on the screen > aaa # now I have a problemNULL Why is aaa object NULL? Should it be NULL? I suspect it has something to do with the SPSS-format option but I?m not sure The reason I want it stored in an object, is to use the adjusted standardized residuals in a LateX document with xtable. Does anyone have a solution, please? Thank you very much and have a great day ahead! PS: sessionInfo() R version 2.7.1 (2008-06-23) i386-pc-mingw32 locale: LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] gmodels_2.14.1 xtable_1.5-4 loaded via a namespace (and not attached): [1] gdata_2.3.1 gtools_2.4.0 MASS_7.2-42 tools_2.7.1
Marc Schwartz
2009-Jan-29 14:57 UTC
[R] Question On CrossTable function in gmodels package
on 01/29/2009 08:01 AM eugen pircalabelu wrote:> Hi R-users, > I have the following problem with CrossTable function within ?gmodels? package: the output of the function (format ?spss? and asresid=T) can not be stored within another object. > For example: > >> library(gmodels) >> data(infert, package = "datasets") >> CrossTable(infert$education, infert$induced)->aa # the function prints everything ok on the screen >> aa # works just fine > $t > y > x 0 1 2 > 0-5yrs 4 2 6 > 6-11yrs 78 27 15 > 12+ yrs 61 39 16 > ?????. > But when I call >> CrossTable(infert$education, infert$induced, asresid=TRUE, format="SPSS")->aaa # the function prints everything ok on the screen >> aaa # now I have a problem > NULL > > Why is aaa object NULL? Should it be NULL? I suspect it has something to do with the SPSS-format option but I?m not sure > The reason I want it stored in an object, is to use the adjusted standardized residuals in a LateX document with xtable. > Does anyone have a solution, please? > > Thank you very much and have a great day ahead!In reviewing the code, it looks like the internal print function for the SPSS formatted output does not include an invisible() returned object, thus you get the NULL. This is a consequence of there being multiple authors (me being one) who have contributed sections of code to the function over the years. The function really needs to be re-designed from scratch as it has become a monolith. I have not used it myself in quite a few years. If you only need a part of the results generated by the function, the best approach would be to take that part of the code and use it separately: TAB <- table(infert$education, infert$induced) RS <- rowSums(TAB) CS <- colSums(TAB) GT <- sum(TAB) CST <- chisq.test(TAB) ASR <- (CST$observed - CST$expected) / sqrt(CST$expected * ((1 - RS / GT) %*% t(1 - CS / GT)))> TAB0 1 2 0-5yrs 4 2 6 6-11yrs 78 27 15 12+ yrs 61 39 16> ASR0 1 2 0-5yrs -1.7484576 -0.8559305 3.4965329 6-11yrs 2.2647496 -1.6814370 -1.0354298 12+ yrs -1.5163521 2.0521732 -0.4666731 HTH, Marc Schwartz