如何使用相同的均值和方差将估算的负二项密度函数叠加在真实数据上的条形图上?
library(data.table)
library(ggplot2)
temp <- data.table(cbind(V1=c(1,2,3,4,5,9), N=c(50,40,30,20,10,2)))
ggplot(temp, aes(x=V1, y= N)) +
geom_histogram(stat="identity", binwidth = 2.5) +
scale_y_continuous(breaks=c(0, 100, 200, max(temp$N))) +
scale_x_continuous(breaks=c(0, 100, 200, max(temp$V1))) +
theme(panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank()
)
我试图添加stat_function(fun = dnbinom, args = list(size=1, mu = mean(temp$V1)), color="red")
,但我看到的只是横坐标上的红线。 dpois
(lambda=mean(temp$V1)
)和dnorm
(mean = mean(temp$V1), sd = sd(temp$V1)
)相同。
也许我的参数化错了?
答案 0 :(得分:1)
@mmk是正确的:规范化是关键。以下是您如何实现自己想要的目标:
#simplest normalization
temp$Nmod <- temp$N / sum(temp$N)
#alternative normalization
#temp$Nmod <- temp$N / sqrt(sum(temp$N * temp$N))
temp$pois <- dpois(temp$V1, lambda = mean(temp$V1))
temp$nbinom <- dnbinom(temp$V1, mu = mean(temp$V1), size = 1)
ggplot(temp, aes(x=V1, y= Nmod)) +
geom_histogram(stat="identity", binwidth = 2.5) +
theme(panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank()) +
geom_line(aes(y = pois), col = "red") +
geom_line(aes(y = nbinom), col = "blue")