Solved – Using fitted GLM model to simulate y’s from new x-values

generalized linear modelpoisson distributionrsimulation

I have a fitted GLM model: m1=glm(y~x,family=poisson,data=data). I would like to use this fitted model to simulate new data but simulate(m1,nsim=1) results only in y's for the original x-values used to fit the model. Can the simulate function be used to generate y's from new x-values?

Best Answer

How about this. First make some fake data to test on

x <- rnorm(100) 
y <- rpois(rep(1,100), exp(x)) ## poisson regression with slope=1
## fit model
m1 <- glm(y ~ x,family=poisson)

Now decide on your new x points

new.data <- data.frame(x=seq(-3,3,.1))

get the predicted expected value of y for these points

mu.y <- predict(m1, newdata=new.data, type='response')

and generate k sets of simulated y at these new points. Your question has k=1 but we may as well be general.

sim.y <- replicate(k, rpois(rep(1, length(mu.y)), mu.y))

Now sim.y is a matrix with as many rows as new.data and k columns, each containing a possible set of y values assuming the model is correct.