0

I'm hoping to do the same as in this question but this time add a negative binomial distribution to the plot.

This is my code:

library(ggplot2); library(MASS)
year <- 1990:2009
set.seed(1)
counts <- sample(1:1000, 20)
df <- data.frame(year, counts)
my_nb_reg <- glm.nb(counts ~ year, data = df)
my_nb_reg$model$fitted <- predict(my_nb_reg, type = "response")


library(plyr)
# nb_sim <- unlist(llply(my_nb_reg$model$fitted, function(x) rnbinom(n = ?, size = ?, prob = ?, mu = x)))
df.new <- data.frame(year, nb_sim)
ggplot(my_nb_reg$model) + geom_point(aes(year, counts)) + geom_jitter(data= nb_sim, aes(year, nb_sim), color = "red")

The line that is commented out requires arguments n, size and prob. Does anyone know how to add negative binomial distributions to the plot?

Community
  • 1
  • 1
luciano
  • 13,158
  • 36
  • 90
  • 130

1 Answers1

2

I would use rnegbin from MASS.

Here is use: n as the number of simulated points.

mu as the predicted values from the model and

theta as the estimated theta from the model.

library(ggplot2); library(MASS)
year <- 1990:2009
set.seed(1)
counts <- sample(1:1000, 20)
df <- data.frame(year, counts)
my_nb_reg <- glm.nb(counts ~ year, data = df)
my_nb_reg$model$fitted <- predict(my_nb_reg, type = "response")


nb_sim <- unlist(lapply(my_nb_reg$model$fitted, function(x) rnegbin(n = 1000, mu = x, theta = my_nb_reg$theta)))
df.new <- data.frame(year, nb_sim)
ggplot() + 
  geom_jitter(data = df.new, aes(year, nb_sim), color = "red", alpha = 0.2) +
  geom_point(data = my_nb_reg$model, aes(year, counts)) +
  geom_point(data = my_nb_reg$model, aes(year, fitted), shape = 'x', size = 4)

enter image description here

EDi
  • 13,160
  • 2
  • 48
  • 57