# NOT RUN {
### example requires gnm
if (require(gnm)) {
### convert to trinomial counts
football.tri <- expandCategorical(football, "result", idvar = "match")
head(football.tri)
### add variable to indicate whether team playing at home
football.tri$at.home <- !logical(nrow(football.tri))
### fit shifted & scaled Davidson model
### - subset to first and last season for illustration
shifScalDav <- gnm(count ~
GenDavidson(result == 1, result == 0, result == -1,
home:season, away:season, home.adv = ~1,
tie.max = ~1, tie.scale = ~1, tie.mode = ~1,
at.home1 = at.home,
at.home2 = !at.home) - 1,
eliminate = match, family = poisson, data = football.tri,
subset = season %in% c("2008-9", "2012-13"))
### look at coefs
coef <- coef(shifScalDav)
## home advantage
exp(coef["home.adv"])
## max p(tie)
plogis(coef["tie.max"])
## mode p(tie)
plogis(coef["tie.mode"])
## scale relative to Davidson of dependence of p(tie) on p(win|not a draw)
exp(coef["tie.scale"])
### check model fit
alpha <- names(coef[-(1:4)])
plotProportions(result == 1, result == 0, result == -1,
home:season, away:season,
abilities = coef[alpha], home.adv = coef["home.adv"],
tie.max = coef["tie.max"], tie.scale = coef["tie.scale"],
tie.mode = coef["tie.mode"],
at.home1 = at.home, at.home2 = !at.home,
data = football.tri, subset = count == 1)
}
### analyse all five seasons
### - takes a little while to run, particularly likelihood ratio tests
# }
# NOT RUN {
### fit Davidson model
Dav <- gnm(count ~ GenDavidson(result == 1, result == 0, result == -1,
home:season, away:season, home.adv = ~1,
tie.max = ~1,
at.home1 = at.home,
at.home2 = !at.home) - 1,
eliminate = match, family = poisson, data = football.tri)
### fit scaled Davidson model
scalDav <- gnm(count ~ GenDavidson(result == 1, result == 0, result == -1,
home:season, away:season, home.adv = ~1,
tie.max = ~1, tie.scale = ~1,
at.home1 = at.home,
at.home2 = !at.home) - 1,
eliminate = match, family = poisson, data = football.tri)
### fit shifted & scaled Davidson model
shifScalDav <- gnm(count ~
GenDavidson(result == 1, result == 0, result == -1,
home:season, away:season, home.adv = ~1,
tie.max = ~1, tie.scale = ~1, tie.mode = ~1,
at.home1 = at.home,
at.home2 = !at.home) - 1,
eliminate = match, family = poisson, data = football.tri)
### compare models
anova(Dav, scalDav, shifScalDav, test = "Chisq")
### diagnostic plots
main <- c("Davidson", "Scaled Davidson", "Shifted & Scaled Davidson")
mod <- list(Dav, scalDav, shifScalDav)
names(mod) <- main
## use football.tri data so that at.home can be found,
## but restrict to actual match results
par(mfrow = c(2,2))
for (i in 1:3) {
coef <- parameters(mod[[i]])
plotProportions(result == 1, result == 0, result == -1,
home:season, away:season,
abilities = coef[alpha],
home.adv = coef["home.adv"],
tie.max = coef["tie.max"],
tie.scale = coef["tie.scale"],
tie.mode = coef["tie.mode"],
at.home1 = at.home,
at.home2 = !at.home,
main = main[i],
data = football.tri, subset = count == 1)
}
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab