4
$\begingroup$

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?

$\endgroup$
1
  • 1
    $\begingroup$ From the help, it doesn't look like it, but you could do it with rpois. $\endgroup$
    – Glen_b
    Jan 30, 2013 at 6:56

1 Answer 1

7
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.