[R] Question On CrossTable function in gmodels package

Marc Schwartz marc_schwartz at comcast.net
Thu Jan 29 15:57:24 CET 2009


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)))

> TAB

           0  1  2
  0-5yrs   4  2  6
  6-11yrs 78 27 15
  12+ yrs 61 39 16

> ASR

                   0          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




More information about the R-help mailing list