turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-22-2015 04:22 AM

I need to create a matrix having 734 rows and 17 columns the row and column total are fixed how do i create such matrix having random values obeying the restriction i mentioned...I am using proc iml and this code but it is not working properly

proc iml;

use outd;

read all var {outd};

use ctrstat1;

read all var {count_sum};

a= j(734,17);

do i=1 to 12478;

call randseed(123);

call randgen(a, "Uniform");

end;

a[+, ] = count_sum ;

a[ ,+] = outd ;

print a;

quit;

outd is the vector having total of each row and count_sum is the vector having sum of each column..Please Please help

Accepted Solutions

Solution

09-25-2015
06:23 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-24-2015 06:13 AM - edited 09-24-2015 06:14 AM

Ahh! Now I see what you want. I apologize for not understanding earlier. I thought you were trying to do something more complicated.

The null model for a table is usually assumed to be the model of independence. You can easily generate an independent table by forming the (outer) product of the side marginal (sum across columns) with the bottom marginal (sum down the rows). For example, here is program that produces a 7x3 table from the 7-element and 3-element marginal sums:

```
proc iml;
side = {34, 23, 21, 16, 16, 21, 12};
bottom = {60 52 31};
total = sum(side); /* = sum(bottom); */
table = side*bottom/side[+]; /* null model of independence */
print table;
```

You can learn about other null models by looking at the documentation for the IPF function.

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-22-2015 10:01 AM

Hi Parul,

Thanks for your question. I've moved your question to the SAS/IML forum as more experts will be able to help here.

Best,

Anna

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-22-2015 10:46 AM

Are you trying to simulate a frequency table? That is, are the entries of the matrix integers with specified row and column sums?

For example, if you tell me you want a 2x3 table with row sums {4 5 4} and column sums {6, 7}, a valid table would be

m = {1 2 3,

3 3 1};

Is that what you want? Otherwise, please provide an small example that shows what you want.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-22-2015 11:54 PM

Thanks Rick and Anna for replying me...

Rick i want to create a matrix of 734 rows and 17 columns i have the total of each row and each column in the form of dataset as in and out degree. Now what i want is i want to fill in this 734X 17 matrix with random numbers not integers such that the row and column total will be my in and out degree. Hope i clarified i question

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-22-2015 11:55 PM

Rick yes you correctly understood. I want this only could you pls help me for coding this.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-23-2015 05:57 AM

I've never simulated a contingency table with two fixed marginals, but I don't think it is easy. Looking at the statistical literature reveals a handful of paper with complicated algorithms.

Do you need only one of these matrices, or do you need thousands of them? The second case is more difficult, since it requires creating a sampling algorithm that draws uniformly from the set of all contingency tables with fixed marginals, which is a hard problem.

The first case is simpler because it allows you to contruct an approximation to the matrix, and then "fix it up" by examing places where the row/col sums are not corerct and applying an iterative refinement.

Since this is a complicated problem, I have to ask what you are trying to accomplish? What is your ultimate goal?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-23-2015 11:06 PM

I just want one such matrix which will be my null model. I know there will be many different solution of this for same set on in and out degree. what i want to do is to take average of all those simulated values as my null model.If not in SAS can you suggest where can i do this

Solution

09-25-2015
06:23 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-24-2015 06:13 AM - edited 09-24-2015 06:14 AM

Ahh! Now I see what you want. I apologize for not understanding earlier. I thought you were trying to do something more complicated.

The null model for a table is usually assumed to be the model of independence. You can easily generate an independent table by forming the (outer) product of the side marginal (sum across columns) with the bottom marginal (sum down the rows). For example, here is program that produces a 7x3 table from the 7-element and 3-element marginal sums:

```
proc iml;
side = {34, 23, 21, 16, 16, 21, 12};
bottom = {60 52 31};
total = sum(side); /* = sum(bottom); */
table = side*bottom/side[+]; /* null model of independence */
print table;
```

You can learn about other null models by looking at the documentation for the IPF function.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-25-2015 03:41 AM

Thank you so much Rick the solution is what I want.....just little more if u could help actually i want the random integers ..I tried ceil and floor function with you code but it is not giving exact sum for some of the rows

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-25-2015 07:49 AM

May be you could try selective use of either ceil() or floor() on each element of the matrix. You could force either the row sums or the column sums to be correct and it is likely the other will be very close. For example:

```
proc iml;
start rowround( x );
s = x;
do i = 1 to nrow(x);
d = x[i, ] - floor(x[i, ]);
s[i, ] = rank(d) > round(ncol(x) - sum(d));
end;
do i = 1 to nrow(x); do j = 1 to ncol(x);
if s[i,j] then s[i,j] = ceil(x[i,j]);
else s[i,j] = floor(x[i,j]);
end; end;
return(s);
finish;
side = {34, 23, 21, 16, 16, 21, 12};
bottom = {60 52 31};
total = sum(side); /* = sum(bottom); */
table = side*bottom/side[+]; /* null model of independence */
print table;
table = rowround(table);
print table ,, bottom, (table[+,]), side (table[,+]);
quit;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-25-2015 08:08 AM - edited 09-25-2015 08:08 AM

@Parul There is no randomness to the solution that I posted, which is why I was confused earlier. This thread's title is "Random matrix..." so I initially assumed you wanted to draw a table "uniformly at random" from the set of all tables that have the given marginals. It is possible to sample uniformly at random, but it requires more effort, so I didn't want to pursue it until I understood why you need it and what you intend to do with it.

It sounds like you don't need a random matrix, but you only require ANY matrix that satisfies the marginal constraints (There's a difference!). If true, then use the IPF function. The doc for the IPF function also has an example of a "greedy algorithm" that will satisfy your requirement.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-23-2015 08:37 AM

After giving this issue A LOT of thought, I wrote a series of blog posts that culminated in the article "Simulate contingency tables with fixed row and column sums in SAS." This turned out to be a very interesting topic, so thanks to @Parul for asking the question.