[R] Fitting loglinear model with glm() and loglm()

Søren Højsgaard sorenh at math.aau.dk
Tue Mar 20 11:22:00 CET 2012

Dear Christofer,

loglm uses an iterative proportional scaling (IPS) algorithm for fitting a log-linear model to a contingency table. glm uses an iteratively reweighted least squares algorithm. The result from IPS is exact.


-----Oprindelig meddelelse-----
Fra: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] På vegne af Christofer Bogaso
Sendt: 20. marts 2012 11:04
Til: r-help at r-project.org
Emne: [R] Fitting loglinear model with glm() and loglm()

Dear all, I have small difficulty in comprehending the loglinear model with R. Assume, we have following data

dat <- array(c(911, 44, 538, 456, 3, 2, 43, 279), c(2, 2, 2))

Now I fit a loglinear model with this and get the fitted values:

Model_1 <- loglm(~1 + 2 + 3, dat)

I could do this same task using glm() function as well because loglinear model is just 1 kind of glm

### Create dummy variables manually
Dummy_Variable_Matrix <- rbind(c(1, 1, 1),
							   c(0, 1, 1),
							   c(1, 0, 1),
							   c(0, 0, 1),
							   c(1, 1, 0),
							   c(0, 1, 0),
							   c(1, 0, 0),
							   c(0, 0, 0))

### Fit glm

model_2 <- glm(as.vector(dat) ~
						   Dummy_Variable_Matrix[,1] +
						   Dummy_Variable_Matrix[,2] +
						   poisson(link = log));

### However................

fitted(model_2) == as.vector(fitted(Model_1)) ### do not match

However it is true that the difference is very small, still I am wondering whether should I just ingore that small difference? Or I have done something fundamentally wrong?

Thanks for your help!

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list