model.matrix (via predict) (PR#2206)
Thu, 24 Oct 2002 16:29:59 +1000

I'm not sure what Glenn's "obvious" workarounds are, but if you fit the
model in the equivalent and more conventional form:

gg <- glm(clms ~ logj + i100*j0 + ihalf*jmin6, data=dfr, family=poisson)

The predict(gg, newdata = dfr) step now works fine (for me, Windows 2000, R

I present this in the hope someone can use it as a clue to find out what's
going on.  Sorry, I don't have time to do it myself.

Bill Venables.

-----Original Message-----
From: []
Sent: Thursday, October 24, 2002 2:33 PM
Subject: model.matrix (via predict) (PR#2206)

Full_Name: Glenn Stone
Version: 1.5.1 and 1.6.0
OS: win2000
Submission from: (NULL) (

The following code produces incorrect fitted values in version 1.5.1 and an
error in 1.6.0 

   Error in "contrasts<-"(*tmp*, value = "contr.treatment") : 
           contrasts apply only to factors
   In addition: Warning message: 
   variable ihalf is not a factor in: model.frame.default(object, data, xlev

The problem in 1.5.1 seems to be model.matrix return inconsistently ordered
interaction terms. There are abviously several work arounds.

### Start of example
dfr <- expand.grid(i=0:154,j=0:154)
dfr <- dfr[with(dfr,i+j<=154),]

dfr$logj <- with(dfr,log(j+0.5))
dfr$jmin6 <- with(dfr, pmin(j,6))
dfr$ihalf <- with(dfr, factor(floor(i/6)))
dfr$i100 <- with(dfr, ifelse(i<100,1,0))
dfr$j0 <- with(dfr, ifelse(j==0,1,0))

Ai <- runif(length(levels(dfr$ihalf)))
Ai <- Ai-Ai[1]
Bi <- runif(length(levels(dfr$ihalf)))
Bi <- (Bi-Bi[1])/10

eta <- with(dfr, -5 -2.5*logj - 3*i100*j0
            + 0.01*jmin6+Ai[as.numeric(ihalf)]

dfr$clms <- sapply( eta , function(x) rpois(1,1e6*exp(x)))
gg <- glm(clms~ logj + i100:j0 + jmin6 + ihalf + ihalf:jmin6,

### End of example

r-devel mailing list -- Read
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To:
r-devel mailing list -- Read
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: