[R] question for crosstable

Marc Schwartz marc_schwartz at comcast.net
Sun Mar 9 19:41:08 CET 2008


eugen pircalabelu wrote:
> Good evening R-users!
> 
> I have the following problem: i want to get a weighted crosstable along with the adjusted standardized residuals test
> 
> Example:
>  a<-c(1,1,1,1,1,2,2,2,3,3)
>  b<-c(10,10,23,24,33,45,46,70,21,66)
>  c<-c(3,3,2,3,4,1,1,1,3,3)
>  d<-c("a", "b","b","c","a","a","a", "b", "c","c")
>  z<-data.frame(a, b,c,d)
> library(gmodels) 
> 
> Using the above example i want to get this :
> 
> xtabs(c ~ a+d, z) ,                 where i have 
> sum(xtabs(c ~ a+d, z) )         as my total number of cases, but with the adjusted standardized residuals test. 
> 
> So i want something like the CrossTable function from gmodels package, but taking into account that i have weights  which sum to 24 and not 10, as the CrossTable prints from this: 
> 
>  CrossTable(z$a, z$d, format="SPSS", asresid=T)
> 
> Thank you and have a great day ahead!

Is this what you want?

 > CrossTable(xtabs(c ~ a + d, z), format = "SPSS", asresid = TRUE)

    Cell Contents
|-------------------------|
|                   Count |
| Chi-square contribution |
|             Row Percent |
|          Column Percent |
|           Total Percent |
|           Adj Std Resid |
|-------------------------|

Total Observations in Table:  24

              | d
            a |        a  |        b  |        c  | Row Total |
-------------|-----------|-----------|-----------|-----------|
            1 |        7  |        5  |        3  |       15  |
              |    0.336  |    0.417  |    1.225  |           |
              |   46.667% |   33.333% |   20.000% |   62.500% |
              |   77.778% |   83.333% |   33.333% |           |
              |   29.167% |   20.833% |   12.500% |           |
              |    1.198  |    1.217  |   -2.286  |           |
-------------|-----------|-----------|-----------|-----------|
            2 |        2  |        1  |        0  |        3  |
              |    0.681  |    0.083  |    1.125  |           |
              |   66.667% |   33.333% |    0.000% |   12.500% |
              |   22.222% |   16.667% |    0.000% |           |
              |    8.333% |    4.167% |    0.000% |           |
              |    1.116  |    0.356  |   -1.434  |           |
-------------|-----------|-----------|-----------|-----------|
            3 |        0  |        0  |        6  |        6  |
              |    2.250  |    1.500  |    6.250  |           |
              |    0.000% |    0.000% |  100.000% |   25.000% |
              |    0.000% |    0.000% |   66.667% |           |
              |    0.000% |    0.000% |   25.000% |           |
              |   -2.191  |   -1.633  |    3.651  |           |
-------------|-----------|-----------|-----------|-----------|
Column Total |        9  |        6  |        9  |       24  |
              |   37.500% |   25.000% |   37.500% |           |
-------------|-----------|-----------|-----------|-----------|


Note that the first argument to CrossTable(), 'x', can be either a 
vector or a matrix, the latter if 'y' is unspecified.

HTH,

Marc Schwartz



More information about the R-help mailing list