# [R] Iterative Proportional Fitting, use

Gerard M. Keogh GMKeogh at justice.ie
Mon Mar 23 14:11:15 CET 2009

```Keon,

why not fit a loglinear independence model which as far as I know is the
same.

Gerard

Here's an example from Agresti - Intro to Cat Data analysis
Example: Alcohol, cigarette, marijuana use
|------------------+------------------+------------------------------------|
|      Alcohol     |     Cigarette    |            Marijuana Use           |
|                  |                  |                                    |
|        use       |        use       |                                    |
|------------------+------------------+------------------------------------|
|                  |                  |               Yes No               |
|------------------+------------------+------------------------------------|
|        Yes       |        Yes       |               911 538              |
|------------------+------------------+------------------------------------|
|                  |        No        |               44 456               |
|------------------+------------------+------------------------------------|
|        No        |        Yes       |                3 43                |
|------------------+------------------+------------------------------------|
|                  |        No        |                2 279               |
|------------------+------------------+------------------------------------|

Coding and Models

table8.3 = read.table(textConnection("alc cig mar count
Yes Yes Yes 911
Yes Yes No  538
Yes No  Yes 44
Yes No  No  456
No  Yes Yes 3
No  Yes No  43
No  No  Yes 2
closeAllConnections()
# independence model (A,C,M)
fit1.a.c.m = glm(count ~ mar+cig+alc, family=poisson, data=table8.3)
fit1.glm\$fitted.values
# intermediate model
fit2.m.ca = glm(count ~ mar+cig:alc, family=poisson, data=table8.3)
fit2.m.ca\$fitted.values
# homogeneous association model
fit3.m.c.a  =  glm(count  ~  mar:cig+mar:alc+cig:alc, family=poisson,
data=table8.3)
fit3.m.c.a\$fitted.values
# saturated model
fits = glm(count ~ mar*cig*alc, family=poisson, data=table8.3)
fits\$fitted.values

The  coding  for  variables  in the above program and the fitted values are

given  below – they show that the homogeneous association model is the only

model that fits these data well.

|---------+------------+------------+--------+------------+---------+-----------|
| Alcohol |  Cigarette |  Marijuana | Actual |   (A,C,M)  |  (AC,M) | (AC:AM:CM)|
|   use   |     Use    |     Use    |  (ACM) | Independenc|         | homogeneou|
|         |            |            |        |      e     |         |     s     |
|---------+------------+------------+--------+------------+---------+-----------|
|   Yes   |     Yes    |     Yes    |   911  |    540.0   |  611.2  |   910.4   |
|---------+------------+------------+--------+------------+---------+-----------|
|         |            |     No     |   538  |    740.2   |  837.8  |   538.6   |
|---------+------------+------------+--------+------------+---------+-----------|
|         |     No     |     Yes    |   44   |    282.1   |  210.9  |    44.6   |
|---------+------------+------------+--------+------------+---------+-----------|
|         |            |     No     |   456  |    386.7   |  289.1  |   455.4   |
|---------+------------+------------+--------+------------+---------+-----------|
|    No   |     Yes    |     Yes    |    3   |    90.6    |   19.4  |    3.6    |
|---------+------------+------------+--------+------------+---------+-----------|
|         |            |     No     |   43   |    124.2   |   26.6  |    42.4   |
|---------+------------+------------+--------+------------+---------+-----------|
|         |     No     |     Yes    |    2   |    47.3    |  118.5  |    1.4    |
|---------+------------+------------+--------+------------+---------+-----------|
|         |            |     No     |   279  |    64.9    |  162.5  |   279.6   |
|---------+------------+------------+--------+------------+---------+-----------|

Koen Hufkens
<koen.hufkens at ua.
ac.be>                                                     To
Sent by:                  r-help <r-help at r-project.org>
r-help-bounces at r-                                          cc
project.org
Subject
[R] Iterative Proportional Fitting,
23/03/2009 12:13          use

Hi list,

I would like to normalize a matrix (two actually for comparison) using
iterative proportional fitting.

Using ipf() would be the easiest way to do this, however I can't get my
head around the use of the function. More specifically, the margins
settings...

for a matrix:

mat <- matrix(c(65,4,22,24,6,81,5,8,0,11,85,19,4,7,3,90),4,4)

using

fit <- ipf(mat,margins=c(1,1,1,1,0,1,1,1,1))

generates a matrix with just 1's.

using

fit <- ipf(mat,margins=c(100,100,100,100,0,100,100,100,100))

gives a segmentation fault and crashes R !

so how do you define the margin values to which to sum the row and
column values in your matrix correctly?

Kind regards,
Koen

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

**********************************************************************************
The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.  It is the policy of the Department of Justice, Equality and Law Reform and the Agencies and Offices using its IT services to disallow the sending of offensive material.
Should you consider that the material contained in this message is offensive you should contact the sender immediately and also mailminder[at]justice.ie.

Is le haghaidh an duine nó an eintitis ar a bhfuil sí dírithe, agus le haghaidh an duine nó an eintitis sin amháin, a bheartaítear an fhaisnéis a tarchuireadh agus féadfaidh sé go bhfuil ábhar faoi rún agus/nó faoi phribhléid inti. Toirmisctear aon athbhreithniú, atarchur nó leathadh a dhéanamh ar an bhfaisnéis seo, aon úsáid eile a bhaint aisti nó aon ghníomh a dhéanamh ar a hiontaoibh, ag daoine nó ag eintitis seachas an faighteoir beartaithe. Má fuair tú é seo trí dhearmad, téigh i dteagmháil leis an seoltóir, le do thoil, agus scrios an t-ábhar as aon ríomhaire. Is é beartas na Roinne Dlí agus Cirt, Comhionannais agus Athchóirithe Dlí, agus na nOifígí agus na nGníomhaireachtaí a úsáideann seirbhísí TF na Roinne, seoladh ábhair cholúil a dhícheadú.
Más rud é go measann tú gur ábhar colúil atá san ábhar atá sa teachtaireacht seo is ceart duit dul i dteagmháil leis an seoltóir láithreach agus le mailminder[ag]justice.ie chomh maith.
***********************************************************************************

```