Log-Normal calculations using R

对数正太分布

R
packages
作者

Reddy Lee

发布日期

2023年11月6日星期一

修改的

2023年12月22日星期五


代码
library(tidyverse)

对数正态分布数据是指对数据取对数后,数据呈正态分布的数据模型。和正态分布一样,对数正态分布也有两个基本的参数:\(\sigma\)\(\mu\)。用R计算对数正态分布有4个基本的函数。

1 随机生成对数正态分布数据: rlnorm(n,meanlg,sdlg)

代码
x <- rlnorm(120,6.1,1.0)

2 计算累积概率: plnorm(q,meanlg,sdlg)

代码
plnorm(717,6.1,1.0)
[1] 0.6826335
代码
plnorm(831,6.1,1.0)
[1] 0.7332361
代码
plnorm(c(717,744,831),6.1,1.0)
[1] 0.6826335 0.6956889 0.7332361

3 计算临界值: qlnorm(p,meanlg,sdlg)

代码
qlnorm(0.6956889,6.1,1.0)
[1] 744.0001
代码
qlnorm(c(0.6826335, 0.6956889, 0.7332361),6.1,1.0)
[1] 716.9999 744.0001 831.0000

4 pdf值: dlnorm(x,meanlg,sdlg)

代码
dlnorm(0.6956889,6.1,1)
[1] 4.881891e-10
代码
ggplot()+
  geom_function(fun=dlnorm,
                args=list(mena=6.1,
                          sd=1.0))+
  xlim(0,1500)+
  labs(y="Density")

5 练习

Example. Freshman undergraduate enrollments at U.S. colleges have an approximate log-normal distribution with parameters mean = 6.1 and sd = 1.0. ### What is the probability that a randomly-selected college enrolls between 500 and 1000 freshmen in a year?

代码
plnorm(1000,6.1,1)-plnorm(500,6.1,1)
[1] 0.2447621

5.1 What is the 99th percentile for U.S. college enrollment?

代码
qlnorm(0.99,6.1,1)
[1] 4565.795

5.2 Simulate selecting 100 colleges at random. Plot a histogram of freshman enrollments at those colleges.

代码
freshmen <- rlnorm(100,6.1,1)

qplot(freshmen,geom="histogram")

代码
qplot(freshmen,geom="density")

回到顶部