# NOT RUN {
library(mvtnorm)
##
## 1. a factor and a numeric
##
PoisReg2 <- data.frame(y=1:6,
x=factor(rep(0:2, 2)), x1=rep(1:2, e=3))
GLMpoisR2 <- glm(y~x+x1, poisson, PoisReg2)
newDat. <- data.frame(
x=factor(rep(c(0, 2), 2), levels=0:2),
x1=3:6)
# NOTE: Force newDat2['x'] to have the same levels
# as PoisReg2['x']
GLMsim2n <- simulate(GLMpoisR2, nsim=3, seed=2,
newdata=newDat.)
##
## 2. One variable: BMA returns
## a mixture of constant & linear models
##
PoisRegDat <- data.frame(x=1:2, y=c(5, 10))
GLMex <- glm(y~x, poisson, PoisRegDat)
# Simulate for the model data
GLMsig <- simulate(GLMex, nsim=2, seed=1)
# Simulate for new data
newDat <- data.frame(x=3:4,
row.names=paste0('f', 3:4))
GLMsio <- simulate(GLMex, nsim=3, seed=2,
newdata=newDat)
##
## 2a. Compute the correct answers manually
##
newMat <- model.matrix(~., newDat)
RNGstate <- structure(2, kind = as.list(RNGkind()))
set.seed(2)
sim <- mvtnorm::rmvnorm(3, coef(GLMex),
vcov(GLMex))
rownames(sim) <- paste0('sim_', 1:3)
simDF <- data.frame(t(sim))
GLMsim.l <- tcrossprod(newMat, sim)
colnames(GLMsim.l) <- paste0('sim_', 1:3)
GLMsim.r <- exp(GLMsim.l)
GLMsim2 <- list(coef=simDF,
link=data.frame(GLMsim.l),
response=data.frame(GLMsim.r) )
attr(GLMsim2, 'seed') <- RNGstate
# }
# NOT RUN {
all.equal(GLMsio, GLMsim2)
# }
Run the code above in your browser using DataLab