Skip to content

Making a 'joint distribution'?

3 messages · Ajay Shah, John Fox, Thomas Lumley

#
Suppose I make two discrete variables --
I know I can do:
0 1 2 3 4
  0 5 5 5 5 4
  1 4 2 6 7 3
  2 5 3 5 3 6
  3 3 1 3 1 2
  4 6 4 3 3 6
0  1  2  3  4 
24 22 22 10 22
0  1  2  3  4 
23 15 22 19 21 

which is all great. But how do I produce the typical presentation of
the "joint distribution" where we put the marginal distributions in
the margins? E.g. I'd like to get some object "joint" where one would
get :
0  1  2  3  4  f1
  0  5  5  5  5  4  24
  1  4  2  6  7  3  22
  2  5  3  5  3  6  22
  3  3  1  3  1  2  10
  4  6  4  3  3  6  22
 f2 23 15 22 19 21 100

So that one could then say "joint/nrow(D)" and get nice probabilities
out of it. It would great to be able to say "xtable(joint/nrow(D))" :-)

I'm sure R has a lovely way to do this, but I'm not able to figure it out.
2 days later
#
Dear Ajay,

There may be a function that does this already, but if not, it not hard to
do, at least for two-way tables:

Table <- function(x, y) {
      tab <- table(x, y)
      tab <- cbind(tab, rowSums(tab))
      tab <- rbind(tab, colSums(tab))
      rownames(tab)[nrow(tab)] <- deparse(substitute(y))
      colnames(tab)[ncol(tab)] <- deparse(substitute(x))
      tab
      }

I hope this helps,
 John
#
On Sun, 3 Oct 2004, Ajay Shah wrote:

            
<sniP>
?addmargins.

 	-thomas