【问题标题】:Add simulated poisson distributions to a ggplot将模拟泊松分布添加到 ggplot
【发布时间】:2014-05-18 18:42:22
【问题描述】:

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

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

情节应该是这样的(红点是经过照片处理的):

【问题讨论】:

    标签: r ggplot2 plyr poisson


    【解决方案1】:

    您可以将模拟值转换为向量,然后使用它们制作新的数据框,其中一列包含重复每 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-11-22
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多