4

我做了泊松回归,然后将模型可视化:

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

在此处输入图像描述

现在我想将这些模拟泊松分布添加到图中:

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

情节应该是这样的(红点被photoshop过):

在此处输入图像描述

4

1 回答 1

4

您可以将模拟值转换为向量,然后使用它们来制作新的数据框,其中一列包含每 100 次重复的年份,第二列是模拟值。

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)

然后添加模拟点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")

在此处输入图像描述

于 2014-05-18T18:56:54.040 回答