[R] Fitting loglinear model with glm() and loglm()
Christofer Bogaso
bogaso.christofer at gmail.com
Tue Mar 20 11:03:55 CET 2012
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:
library(MASS)
Model_1 <- loglm(~1 + 2 + 3, dat)
fitted(Model_1)
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] +
Dummy_Variable_Matrix[,3],
poisson(link = log));
fitted(model_2)
### 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!
More information about the R-help
mailing list