The most recent version of TMB includes the ability to simulated model, so we have implemented that ability in glmmTMB. These simulations take all random effects into account.
library(glmmTMB)
library(ggplot2); theme_set(theme_bw())
Fit a typical model:
data(Owls)
owls_nb1 <- glmmTMB(SiblingNegotiation ~ FoodTreatment*SexParent +
(1|Nest)+offset(log(BroodSize)),
family = list(family="nbinom1",link="log"),
ziformula = ~1, data=Owls)
Then we can simulate from the fitted model with the simulate.glmmTMB
function. It produces a list of simulated observation vectors, each of which is the same size as the original vector of observations. The default is to only simulate one vector (nsim=1
) but we still return a list for consitency.
simo=simulate(owls_nb1, seed=1)
Simdat=Owls
Simdat$SiblingNegotiation=simo[[1]]
Simdat=transform(Simdat,
NegPerChick = SiblingNegotiation/BroodSize,
type="simulated")
Owls$type = "observed"
Dat=rbind(Owls, Simdat)
Then we can plot the simulated data against the observed data to check if they are similar. Because the simulated data is conditional on the estimated random effects, the nest-specific observations should be similar to the nest-specific simulations.
ggplot(Dat, aes(NegPerChick, colour=type))+geom_density()+facet_grid(FoodTreatment~SexParent)
ggplot(Dat, aes(Nest, SiblingNegotiation, colour=type))+geom_boxplot()+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))