[Rd] [R] length 1 offset in glm (& lm)

Kenneth Knoblauch ken.knoblauch at inserm.fr
Wed Feb 25 14:10:43 CET 2009


Hi,

Thanks for responding/noticing.  I was going to wait a few more days
while contemplating how to get more notice for the post.
It occurred to me later that it should have gone to R-devel as you
have forwarded it.  I'll include my example (with a fix) for refresher

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)')

lm(cbind(Correct, Incorrect) ~ Contr - 1, binomial,
         data = c1, offset = rep(qlogis(0.25), nrow(c1))) ## which works

The line which throws the error is:

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

I must have done something additional of which I did not keep a record,
but if I change the nrow(Y) in the code fragment to nrow(data),
that works, as in

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

Thank you.

Ken



Quoting Heather Turner <Heather.Turner at warwick.ac.uk>:

> This post about length 1 offsets on R help seems to have been ignored
> (sorry deleted original email - is there a way to continue thread in
> this case?)
>
> https://stat.ethz.ch/pipermail/r-help/2009-February/189352.html
>
> It does seem to be a bug, in that glm does not behave as documented. In
> fact the same bug applies to lm as well.
>
> I don't think the suggested fix works though - Y isn't available until
> the model frame is evaluated. Moreover you would want to evaluate the
> offset as part of the model frame, to take care of the search path,
> subset and na.action.
>
> One possibility would be to modify model.frame to recycle variables of
> length one. This would be a potentially useful feature from my point of
> view as offset(constant) could replace Const(constant) in gnm, which is
> basically a work around for this problem in the case of specifying a
> constant in a nonlinear term. However, this solution may have undesired
> side-effects and I don't feel too strongly about it as there are various
>  work-arounds. Perhaps the simplest solution would be to modify the docs
> so that offsets of length one are disallowed - it is easy enough for the
> user to create a vector of the right length as necessary.
>
> Best regards,
>
> Heather
>



-- 
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-devel mailing list