[R] reconstructing contingency tables in R

Mike Beddo meb at dataventures.com
Wed Apr 30 00:41:41 CEST 2003

Dear R-help,

I need help reconstructing contingency tables from (sort-of) marginals,
so that I can use loglin to model the table. I'm working with category
variables that are binary. However, my data have already been summed
over other variables and I need to un-sum it, i.e. if my variables are A
and B, I would like to have the cell (Nij) frequencies

N total trials
N(A=0, B=0) = N00 times
N(A=0, B=1) = N01
N(A=1, B=0) = N10
N(A=1, B=1) = N11

but instead I have N(A=1) = N_1+ [summed over B], N(B=1) = N_+1 [summed
over B], N(A=1, B=1) = N_11 data available to me. For 2x2 it's
straightforward to compute Nij:

N00 = N - N_1+ - N_+1 + N_11
N01 = N_+1 - N_11
N10 = N_1+ - N_11
N11 = N_11

and then I can use xtabs. But it gets more complicated as the number of
category variables increases. I know how to do it on paper, but I don't
know how to do it in a generic way within R.

- Mike

More information about the R-help mailing list