I have some positive numbers and I am using log-normal distribution to plot and show the probability of CNT being between 1 and 50. I want to color the area below the curve and calculate the probability. I have plotted the chart successfully and I am trying to calculate the probability, however the result being returned does not look right, where am I making a mistake? How can I paint the area below the curve between lb and 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
The parameterization of the log-norm expects you to pass in the mean log value, not the raw value. Try
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