I have got a table with known marginal sums:
| | | | 7|
| | | | 7|
| | | | 6|
| | | | 6|
| | | | 5|
| | | | 4|
| | | | 3|
+--+-+-+--+
|26|6|6|38|
I want to estimate the best integer contingency table to fulfil the row and column sums that minimises the error from the expected distribution.
1) I can calculate estimates (float):
|4.79|1.11|1.11| 7|
|4.79|1.11|1.11| 7|
|4.11|0.95|0.95| 6|
|4.11|0.95|0.95| 6|
|3.42|0.79|0.79| 5|
|2.74|0.63|0.63| 4|
|2.05|0.47|0.47| 3|
+----+----+----+--+
| 26| 6| 6|38|
2) then round to the nearest integer:
| 5|1|1| 7|
| 5|1|1| 7|
| 4|1|1| 6|
| 4|1|1| 6|
| 3|1|1| 5|
| 3|1|1| 4|
| 2|0|0| 3|
+--+-+-+--+
|26|6|6|38|
This happens to work quite well but the row sums in the last two rows don't match the target row sum.
Is there an algorithm (javascript) to solve this generally?