Skip to content
Prev 31567 / 63424 Next

length 1 offset in glm (& lm)

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>: