我有一些正数,并且我使用对数正态分布进行绘制并显示CNT的概率在1到50之间。我想对曲线下方的区域进行着色并计算该概率。我已经成功绘制了图表,并试图计算概率,但是返回的结果看起来不正确,我在哪里出错?如何绘制lb和ub之间的曲线下方的区域?
df <-structure(list(Year_Month = structure(1:35, .Label = c("2015-05",
"2015-10", "2015-11", "2015-12", "2016-01", "2016-02", "2016-03",
"2016-04", "2016-05", "2016-06", "2016-07", "2016-08", "2016-09",
"2016-10", "2016-11", "2016-12", "2017-01", "2017-02", "2017-03",
"2017-04", "2017-05", "2017-06", "2017-07", "2017-08", "2017-09",
"2017-10", "2017-11", "2017-12", "2018-01", "2018-02", "2018-03",
"2018-04", "2018-05", "2018-06", "2018-07"), class = "factor"),
CNT = c(1, 1, 1, 5, 6, 5, 21, 10, 11, 16,
14, 19, 11, 9, 15, 6, 7, 33, 24, 47, 76, 92,
72, 92, 63, 60, 69, 66, 65, 89, 91, 76, 84, 71,
40)), .Names = c("Year_Month", "CNT"), row.names = c(NA,
35), class = "data.frame")
std=sd(df$CNT)
m=mean(df$CNT)
lb=1
ub=50
ggplot(df, aes(x=CNT)) + stat_function(fun=dlnorm, args=list(mean=m, sd=std))
i <- CNT >= lb & CNT <= ub
area <- plnorm(ub, m, std) - plnorm(lb, m, std)
area
答案 0 :(得分:1)
对数范数的参数化要求您传递平均对数值,而不是原始值。试试
std <- sd(log(df$CNT))
m <- mean(log(df$CNT))
lb <- 1
ub <- 50
ggplot(df, aes(x=CNT)) +
stat_function(fun=dlnorm, args=list(mean=m, sd=std)) +
stat_function(fun=dlnorm, args=list(mean=m, sd=std), xlim=c(lb, ub), geom="area")
plnorm(ub, m, std) - plnorm(lb, m, std)
# [1] 0.7230461