Hi,
Is this what you want?
#
mat <- matrix(c(65,4,22,24,6,81,5,8,0,11,85,19,4,7,3,90),4,4)
rowmarg <- rep(100, nrow(mat)) # the row margin totals that you want
colmarg <- c(90, 120, 80, 110) # the column margin totals that you want
newmat <- loglin( outer(rowmarg, colmarg) / sum(rowmarg), margin=list(1,2),
start=mat, fit=TRUE, eps=1.e-05, iter=100)$fit
newmat
apply(newmat, 1, sum)
apply(newmat, 2, sum)
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Koen Hufkens
Sent: Monday, March 23, 2009 9:33 AM
To: Gerard M. Keogh
Cc: r-help; r-help-bounces at r-project.org
Subject: Re: [R] Iterative Proportional Fitting, use
The data I used was just an example to work upon.
My real dataset is a confusion matrix of 24x24 (and 17x17), so coding it
into a model with all different kinds of combinations seems tedious.
That's why I hoped to use the ipf() function as it accepts a matrix as
input.
Thanks for the suggestion but I hope I can get around this without recoding
the original data.
Kind regards,
Koen
-----Original Message-----
From: Gerard M. Keogh <GMKeogh at justice.ie>
To: Koen Hufkens <koen.hufkens at ua.ac.be>
Cc: r-help <r-help at r-project.org>, r-help-bounces at r-project.org
Subject: Re: [R] Iterative Proportional Fitting, use
Date: Mon, 23 Mar 2009 13:11:15 +0000
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
No No No 279"),header=TRUE)
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
PLEASE do read the posting guide
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.
****************************************************************************
*******
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.