r - Add simulated poisson distributions to a ggplot -


i have made poisson regression , visualised model:

library(ggplot2) year <- 1990:2010 count <- c(29, 8, 13, 3, 20, 14, 18, 15, 10, 19, 17, 18, 24, 47, 52, 24, 25, 24, 31, 56, 48) df <- data.frame(year, count) my_glm <- glm(count ~ year, family = "poisson", data = df) my_glm$model$fitted <- predict(my_glm, type = "response") ggplot(my_glm$model) + geom_point(aes(year, count)) + geom_line(aes(year, fitted)) 

enter image description here

now want add these simulated poisson distributions plot:

library(plyr) poisson_sim <- llply(my_glm$model$fitted, function(x) rpois(100, x)) 

the plot should (red points photoshopped):

enter image description here

you can convert simulated values vector , use them make new data frame 1 column contains years repeated each 100 times , second column simulated values.

poisson_sim <- unlist(llply(my_glm$model$fitted, function(x) rpois(100, x))) df.new<-data.frame(year=rep(year,each=100),sim=poisson_sim) 

then add simulated points geom_jitter().

ggplot(my_glm$model) + geom_point(aes(year, count)) + geom_line(aes(year, fitted))+       geom_jitter(data=df.new,aes(year,sim),color="red") 

enter image description here


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -