BradleyTerry2 icon indicating copy to clipboard operation
BradleyTerry2 copied to clipboard

Error in v1.0-8 about the structure of the data for the random factor

Open wcornwell opened this issue 7 years ago • 3 comments

Hello,

I had some code that seemed to be working with v1.0-6, but when I updated to v1.0-8, the model call returns an error.

model1<-BTm(player1=winner,player2=loser,
            formula = ~ prev +   
              (1|ID), id="ID",data=dataset)

which still in v1.0-6 runs (apparently) well, but in v1.0-8 it returns this error

Error in (function (fixed, random = NULL, family = "binomial", data = NULL, : 
`random` should be a matrix object, with 596 rows.

the traceback is pretty cryptic at least to me:

Error in (function (fixed, random = NULL, family = "binomial", data = NULL, : 
`random` should be a matrix object, with 596 rows.
5.
stop("`random` should be a matrix object, with ", nrow(modelData), " rows.")
4.
(function (fixed, random = NULL, family = "binomial", data = NULL, subset = NULL, weights = NULL, offset = NULL, na.action = NULL, start = NULL, etastart = NULL, mustart = NULL, control = glmmPQL.control(...), sigma = 0.1, sigma.fixed = FALSE, model = TRUE, x = FALSE, ...
3.
eval(fit, parent.frame())
2.
eval(fit, parent.frame())
1.
BTm(player1 = winner, player2 = loser, formula = ~prev + (1 | ID), id = "ID", data = dataset)

This is probably my mistake, but I'm having trouble tracking down where it's coming from. Any thoughts?

wcornwell avatar Oct 04 '17 23:10 wcornwell

The error is coming from BradleyTerry2::glmmPQL which is called when the model formula involves random effects. BTm ought to set up the random effects matrix correctly though. It's odd because your example appears to have the same form as the example in ?chameleons, which is running fine.

I would check the data structure of chameleons and see if your dataset has a similar form. For example, are winner$ID and loser$ID factors with the same set of levels?

If you're still stuck, I think I'd need a reproducible example to test - perhaps you can share a subset of your data, there are some tips here on how to share a minimal dataset: https://stackoverflow.com/q/5963269/173755.

hturner avatar Oct 05 '17 08:10 hturner

Thanks so much for the quick response!

Turns out after some more digging it's a missing data handling issue, although trickily the missing data was not in the random effects matrix, but rather the previous contest result column. This should reproduce it with the chameleon data:

 chameleons$winner$prev.wins.2[34] <- NA
 summary(chameleon.model <- BTm(player1 = winner, player2 = loser, formula = ~ prev.wins.2 + ch.res[ID] + prop.main[ID] + (1|ID), id = "ID",  data = chameleons))
Error in (function (fixed, random = NULL, family = "binomial", data = NULL,  : 
  `random` should be a matrix object, with 105 rows.

I think for our use case I'll be able to sort it out. Thanks again!

wcornwell avatar Oct 05 '17 10:10 wcornwell

Glad you were able to find the source of the problem. I'll keep this issue open though, to check if this case could be handled more gracefully - at least giving a more helpful error message.

hturner avatar Oct 05 '17 10:10 hturner