#### Fitting a binomial(logit) model by a bivariate poisson(log) surrogate:
## Toy data: Let us say we observe a color polymorphism of irises in 10 populations...
set.seed(123)
ssize <- 10L
shape <- 0.35
toydata <- data.frame(
yellow=rbinom(ssize, 16, prob=rbeta(ssize,shape,shape)),
purple=rbinom(ssize, 16, prob=rbeta(ssize,shape,shape)), # (purple ignored below)
blue=rbinom(ssize, 16, prob=rbeta(ssize,shape,shape)),
phenotype=rnorm(ssize)
)
## Standard binomial fit (purple flowers are ignored here)
(byB <- fitme(cbind(yellow,blue) ~ phenotype, family = binomial(),
data=toydata))
## Surrogate fit
(byP2 <- pois4mlogit(submodels = list(
list(yellow ~ offset(.dynoffset) + phenotype, family = poisson()),
list(blue ~ offset(.dynoffset) + 0, family = poisson())),
data = toydata, types=c("yellow","blue")))
# => Note the different standard errors: the surrogate Poisson fit does not
# provide the "correct" standard errors of the binomial fit.
# However, numInfo(), from which standard errors can be deduced,
# returns correct results:
if (spaMM.getOption("example_maxtime")>0.65) {
(infoP <- numInfo(byP2)) # practically identical to numInfo(byB)
sqrt(diag(solve(infoP))) # SEs as in byB
}
# The 'over-parametrized model' below gives standard errors
# closer to the ones from the standard binomial fit.
# Add a trivial gaussian submodel just to show that this can be done:
(just4fun <- pois4mlogit(submodels = list(
list(yellow ~ offset(.dynoffset) + phenotype, family = poisson()),
list(blue ~ offset(.dynoffset) + 0, family = poisson()),
list(phenotype ~1)),
data = toydata, types=c("yellow","blue")))
#### Over-parametrized model:
(byP2over <- pois4mlogit(submodels = list(
list(yellow ~ offset(.dynoffset) + phenotype, family = poisson()),
list(blue ~ offset(.dynoffset) + phenotype, family = poisson())),
data = toydata, types=c("yellow","blue")))
## Coefficients of binomial model recovered as:
fixef(byP2over)[1:2]-fixef(byP2over)[3:4]
## SEs of coefficients of binomial model recovered as:
{
P2B <- rbind(c(1,0,-1,0),c(0,1,0,-1))
(vcovP2B <- P2B %*% vcov(byP2over) %*% t(P2B))
}
# practically equivalent to
vcov(byB)
# and the original SEs:
sqrt(diag(vcovP2B))
## Probabilities of each type:
matrix(predict(byP2),ncol=2,
dimnames=list(NULL, c("yellow","blue")))
## logLiks
# The likelihood values contained in 'pois4mlogit' objects are now those for
# the multinomial model rather than for an underlying surrogate Poisson model,
# so examples previous given here interpreting differences between these
# likelihoods are no longer relevant.
# Have a look at the 'See also' section for useful features
# not illustrated in the present examples.
Run the code above in your browser using DataLab