[R] length 1 offset in glm

Kenneth Knoblauch ken.knoblauch at inserm.fr
Mon Feb 23 17:05:46 CET 2009


Hi,

I'm trying to use an offset with glm.  According to the glm man
page

offset	... This should be NULL or a numeric vector of length either  
one or equal to the number of cases. ...

but with the following example, I get an error if the offset is of length 1

c1 <- structure(list(Contr = c(0.028, 0.043, 0.064, 0.097, 0.146, 0.219
+ ), Correct = c(34L, 57L, 94L, 152L, 160L, 160L), Incorrect = c(126L,
+ 103L, 66L, 8L, 0L, 0L)), .Names = c("Contr", "Correct", "Incorrect"
+ ), row.names = c(NA, 6L), class = "data.frame")

glm(cbind(Correct, Incorrect) ~ Contr - 1, binomial,
	data = c1, offset = qlogis(0.25))

Error in model.frame.default(formula = cbind(Correct, Incorrect) ~ Contr -  :
   variable lengths differ (found for '(offset)')

but not if it has the length of the number of rows of the data frame

glm(cbind(Correct, Incorrect) ~ Contr - 1, binomial,
	data = c1, offset = rep(qlogis(0.25), nrow(c1)))

...
Coefficients:
Contr
28.34

Degrees of Freedom: 6 Total (i.e. Null);  5 Residual
Null Deviance:	    1342
Residual Deviance: 92.35 	AIC: 114.3

(I'm not pushing this as a good model...)

I traced the error to this line in glm

mf <- eval(mf, parent.frame())

and the error can be "fixed" if the following code from later in glm is put
before the above line with the additional line that I added to
(re)set the offset of mf

if (!is.null(offset)) {
         if (length(offset) == 1)
             offset <- rep(offset, NROW(Y))
         else if (length(offset) != NROW(Y))
             stop(gettextf("number of offsets is %d should equal %d  
(number of observations)",
                 length(offset), NROW(Y)), domain = NA)
     }

  mf$offset <- offset

I don't know if this breaks something important or is a terrible
hack.  Or, am I misreading the way glm should work...

sessionInfo()
R version 2.8.1 Patched (2009-02-22 r47985)
i386-apple-darwin8.11.1

locale:
en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.8.1

Thanks for any enlightenment.

Ken


-- 
Ken Knoblauch
Inserm U846
Stem-cell and Brain Research Institute
Department of Integrative Neurosciences
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.sbri.fr/members/kenneth-knoblauch.html




More information about the R-help mailing list