将负二项分布添加到散点图

时间:2014-05-22 09:55:40

标签: r ggplot2 scatter-plot

我希望和this question中的做法一样,但这次会给情节添加负二项分布。

这是我的代码:

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")

注释掉的行需要参数n,size和prob。有谁知道如何在图中添加负二项分布?

1 个答案:

答案 0 :(得分:2)

我会使用MASS中的rnegbin

这是用途: n作为模拟点的数量。

mu作为模型的预测值和

theta作为模型的估计θ。

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