Skip to content

ipf function in R

3 messages · Chandra Shah, (Ted Harding), Charles C. Berry

#
Hi
I have a 3 x 2 contingency table:
10 20
30 40
50 60
I want to update the frequencies to new marginal totals:
100 130
40 80 110
I want to use  the ipf (iterative proportional fitting) function which 
is apparently in the cat package.
Can somebody please advice me how to input this data and invoke ipf in R 
to obtain an updated contingency table?
Thanks.
By the way I am quite new to R.
#
On 05-Mar-08 07:14:28, Chandra Shah wrote:
It is not clear from your description what you really want to do.
Hence, not clear whether you should be using ipf() to do it!

The purpose of ipf() in the 'cat' package is very simple:
You suppply a complete contingency table, and specify the
order of interactions you want to include in the fit (using
the "margins" parameter in ipf()); and then the value of ipf()
is a table of the fitted (expected) values corresponding to
a model of the type you have specified, fitted by maximum
likelihood.

Your task seems to be different. If I understand your statement.
it looks as though you want to find a,b,c,d such that

  10+a  20+c | 40
  30+b  40+d | 80
  50    60   |110
-------------+---
 100   130   |230

or, since there are row and column constraints on a,b,c,d:

  10+a  30-a | 40
  40-a  40+a | 80
  50    60   |110
-------------+---
 100   130   |230

so then the task would be to find a.

But now it depends on what you would mean by "find a".

You could treat 'a' as an unknown parameter, and fit it
by maximum likelihood under various assumptions about the
dependence between rows and columns (e.g. that the log
odds-ratios have particular values).

Or perhaps (and here is where ipf() may come in) for a
given choice of 'a' you could submit the resulting table
to ipf() and compute chisq from the resulting fitted
values and the given "observed" values; then seek the
value of 'a' which minimises chisq.

However, it's not really possible to advise in more detail
without being sure of what you are trying to do. Above,
I am only guessing!

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 05-Mar-08                                       Time: 10:26:57
------------------------------ XFMail ------------------------------
#
On Wed, 5 Mar 2008, Chandra Shah wrote:

            
I'd use loglin()

 	newtab <-
 		loglin(	rowmarg%o%colmarg/sum(colmarg),
 		margin=list(1,2),
 		start=tab, fit=TRUE )$fit


with rowmarg and colmarg set to your updated marginals.

As for inputting the data, if this is all you have, type it in at the 
command line. See

 	?matrix
 	?c

and note this:
HTH,

Chuck
Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901