The issue is not specific to a GLM. It's an issue of treatment contrasts.
You should also look at the model with intercept:
set.seed(42)
y <- as.factor(sample(rep(1:2), 30, T))
x <- as.factor(sample(rep(1:2), 30, T))
z <- as.factor(sample(rep(1:2), 30, T))
fit0 <- glm(y ~ z + x, binomial)
predict(fit0, newdata=data.frame(z=factor(2), x=factor(1)))
coef(fit0)
#(Intercept) z2 x2
# -0.1151303 0.3228803 1.0588217
predict(fit0, newdata=data.frame(z=factor(2), x=factor(1)))
# 1
#0.20775
Here the intercept represents the group x1/z1
and the other group means are calculated by adding the coefficients of z2
and/or x2
.
fit1 <- glm(y ~ z + x - 1, binomial)
coef(fit1)
# z1 z2 x2
#-0.1151303 0.2077500 1.0588217
predict(fit1, newdata=data.frame(z=factor(2), x=factor(1)))
# 1
#0.20775
Here the coefficient of z1
represents the group x1/z1
which is the same as the intercept in fit0
. However, the coefficient of z2
represents the group x1/z2
instead of the difference between the group means. Note that 0.208 = -0.115 + 0.323. The x2/*
group means are calculated by adding the x2
coefficient to the x1/*
group means.
It should now be easy to understand why order matters here.