【发布时间】:2020-07-24 06:12:57
【问题描述】:
我有一些计数数据。我想用计数数据绘制直方图并添加负二项式、正态和泊松密度函数,但将函数拟合到计数数据。
我尝试关注this example,但是 (a) 我无法拟合负二项式和泊松函数 (b) 无法将其缩放到计数数据级别 (c) 不知道如何在同一张图上拟合所有三个每行都有图例 (d) 另外,我怎样才能获得每个拟合的基本统计数据?例如,负二项式拟合将生成参数 k。我怎样才能让它出现在情节上
set.seed(111)
counts <- rbinom(500,100,0.1)
df <- data.frame(counts)
ggplot(df, aes(x = counts)) +
geom_histogram(aes(y=..density..),colour = "black", fill = "white") +
stat_function(fun=dnorm,args=fitdistr(df$counts,"normal")$estimate)
ggplot(df, aes(x = counts)) +
geom_histogram(aes(y=..density..),colour = "black", fill = "white") +
stat_function(fun=poisson,args=fitdistr(df$counts,"poisson")$estimate)
ggplot(df, aes(x = counts)) +
geom_histogram(aes(y=..density..),colour = "black", fill = "white") +
stat_function(fun=dnbinom,args=fitdistr(df$counts,"dnbinom")$estimate)
【问题讨论】:
标签: r ggplot2 distribution data-fitting