【发布时间】:2020-07-28 13:34:23
【问题描述】:
我正在尝试将负二项式分布拟合到计数数据,但缩小到像 this example 中的计数在我的数据中,我必须分离出两个物种的二项式函数绘图。但是,在函数中指定这一点并在两个物种的键中获取带有参数值的线图例并不容易。
set.seed(111)
count <- rbinom(500,100,0.1)
species <- rep(c("A","B"),time = 250)
df <- data.frame(count,species)
#Specifying negative binomial function
negbinom.params <- fitdistr(df$count,"negative binomial", method = "SANN")$estimate
dist.params <- map(list(`Negative Binomial` = negbinom.params),~ map2(names(.),.,~ paste0(.x," = ",round(.y,2))) %>% unlist %>% paste0(.,collapse = ", ")) %>% map2_chr(names(.),., ~ paste(.x,.y,sep=":\n"))
#Plotting
mybinwidth = 2
ggplot(df, aes(x = count, colour = species, fill = species)) +
facet_grid(.~species) +
geom_histogram(aes(y=..count..),alpha = 0.5, binwidth = mybinwidth) +
stat_function(aes(color = "orange"),
fun = function(x,size, mu) {
mybinwidth * nrow(df) * dnbinom(x,size = size, mu = mu)
},
args=fitdistr(df$count, "negative binomial", method="SANN")$estimate,
xlim=c(0,50),n=20)
【问题讨论】:
-
是我在做傻事还是它也给你+- 37个警告?
-
我也收到 37 条警告..
标签: r ggplot2 distribution data-fitting