> ##### 2. Poisson Distributions : 포아송분포 #####
> # poisson distribution with lambda=2, X ~ Poisson(lambda=2)
> lambda <- 2
> x <- 0:5
> pois1 <- lambda^x/factorial(x)*exp(-lambda) # (1) Mass Function-1
> pois1
[1] 0.13533528 0.27067057 0.27067057 0.18044704 0.09022352 0.03608941
> pois2 <- dpois(x, lambda) # (2) dpois 함수: 포아송 확률분포함수
> pois2
[1] 0.13533528 0.27067057 0.27067057 0.18044704 0.09022352 0.03608941
> cumsum(pois2) # (3) cumsum 함수 이용 - 확률값 누적
[1] 0.1353353 0.4060058 0.6766764 0.8571235 0.9473470 0.9834364
> ppois(x, lambda) # (4) ppois 함수: 포아송 누적분포함수
[1] 0.1353353 0.4060058 0.6766764 0.8571235 0.9473470 0.9834364
>
> # 난수 발생
> sapply(10^(0:5), FUN=function(x){set.seed(0); mean(rpois(x, lambda))})
[1] 4.00000 2.50000 2.04000 2.01500 2.00700 2.00132
>
> # 확률 계산
> x <- 0:10
> lambda <- c(1, 4, 10)
> mat.pois <- sapply(lambda, FUN=function(lambda) dpois(x, lambda))
> mat.pois
[,1] [,2] [,3]
[1,] 3.678794e-01 0.018315639 4.539993e-05
[2,] 3.678794e-01 0.073262556 4.539993e-04
[3,] 1.839397e-01 0.146525111 2.269996e-03
[4,] 6.131324e-02 0.195366815 7.566655e-03
[5,] 1.532831e-02 0.195366815 1.891664e-02
[6,] 3.065662e-03 0.156293452 3.783327e-02
[7,] 5.109437e-04 0.104195635 6.305546e-02
[8,] 7.299195e-05 0.059540363 9.007923e-02
[9,] 9.123994e-06 0.029770181 1.125990e-01
[10,] 1.013777e-06 0.013231192 1.251100e-01
[11,] 1.013777e-07 0.005292477 1.251100e-01
> format(mat.pois, scientific=F, digit=3) # 출력형태를 '0.xxxx..."로 변경
[,1] [,2] [,3]
[1,] "0.367879441" "0.018315639" "0.000045400"
[2,] "0.367879441" "0.073262556" "0.000453999"
[3,] "0.183939721" "0.146525111" "0.002269996"
[4,] "0.061313240" "0.195366815" "0.007566655"
[5,] "0.015328310" "0.195366815" "0.018916637"
[6,] "0.003065662" "0.156293452" "0.037833275"
[7,] "0.000510944" "0.104195635" "0.063055458"
[8,] "0.000072992" "0.059540363" "0.090079226"
[9,] "0.000009124" "0.029770181" "0.112599032"
[10,] "0.000001014" "0.013231192" "0.125110036"
[11,] "0.000000101" "0.005292477" "0.125110036"
>
> ##### 실습3. lambda = 1:6인 포아송분포의 확률밀도함수 #####
> opar <- par(mfrow=c(2,3))
>
> x <- 0:10
> plot(x, dpois(x, 1), type='h', col='Red', ylab='f(x)', xlab='X', ylim=c(0, 0.4), lwd=5,
+ main = c("X ~ Poisson(1); PDF"))
> plot(x, dpois(x, 2), type='h', col='Green', ylab='f(x)', xlab='X', ylim=c(0, 0.4), lwd=5,
+ main = c("X ~ Poisson(2); PDF"))
> plot(x, dpois(x, 3), type='h', col='Blue', ylab='f(x)', xlab='X', ylim=c(0, 0.4), lwd=5,
+ main = c("X ~ Poisson(3); PDF"))
> plot(x, dpois(x, 4), type='h', col='Orange', ylab='f(x)', xlab='X', ylim=c(0, 0.4), lwd=5,
+ main = c("X ~ Poisson(4); PDF"))
> plot(x, dpois(x, 5), type='h', col='Skyblue', ylab='f(x)', xlab='X', ylim=c(0, 0.4), lwd=5,
+ main = c("X ~ Poisson(5); PDF"))
> plot(x, dpois(x, 6), type='h', col='Purple', ylab='f(x)', xlab='X', ylim=c(0, 0.4), lwd=5,
+ main = c("X ~ Poisson(6); PDF"))
>
> par(opar)
> ##### 실습4. lambda = 1:6인 포아송분포의 누적분포함수 #####
> opar <- par(mfrow=c(2,3))
> n <- 10; x <- 0:10
>
> (pois.cdf.1 <- ppois(0:n, 1))
[1] 0.3678794 0.7357589 0.9196986 0.9810118 0.9963402 0.9994058 0.9999168 0.9999898
[9] 0.9999989 0.9999999 1.0000000
> plot(x, pois.cdf.1, type='S', ylab='F(x)', xlab='x', ylim=c(0,1),
+ main="X ~ Poisson(1); CDF", lwd=5, col="Red")
> points(x-1, pois.cdf.1, type='h', col="white", lwd=5)
> points(x-1, pois.cdf.1, pch=16)
> points(x, pois.cdf.1, pch=1)
>
> (pois.cdf.2 <- ppois(0:n, 2))
[1] 0.1353353 0.4060058 0.6766764 0.8571235 0.9473470 0.9834364 0.9954662 0.9989033
[9] 0.9997626 0.9999535 0.9999917
> plot(x, pois.cdf.2, type='S', ylab='F(x)', xlab='x', ylim=c(0,1),
+ main="X ~ Poisson(2); CDF", lwd=5, col="Green")
> points(x-1, pois.cdf.2, type='h', col="white", lwd=5)
> points(x-1, pois.cdf.2, pch=16)
> points(x, pois.cdf.2, pch=1)
>
> (pois.cdf.3 <- ppois(0:n, 3))
[1] 0.04978707 0.19914827 0.42319008 0.64723189 0.81526324 0.91608206 0.96649146
[8] 0.98809550 0.99619701 0.99889751 0.99970766
> plot(x, pois.cdf.3, type='S', ylab='F(x)', xlab='x', ylim=c(0,1),
+ main="X ~ Poisson(3); CDF", lwd=5, col="Blue")
> points(x-1, pois.cdf.3, type='h', col="white", lwd=5)
> points(x-1, pois.cdf.3, pch=16)
> points(x, pois.cdf.3, pch=1)
>
> (pois.cdf.4 <- ppois(0:n, 4))
[1] 0.01831564 0.09157819 0.23810331 0.43347012 0.62883694 0.78513039 0.88932602
[8] 0.94886638 0.97863657 0.99186776 0.99716023
> plot(x, pois.cdf.4, type='S', ylab='F(x)', xlab='x', ylim=c(0,1),
+ main="X ~ Poisson(4); CDF", lwd=5, col="Orange")
> points(x-1, pois.cdf.4, type='h', col="white", lwd=5)
> points(x-1, pois.cdf.4, pch=16)
> points(x, pois.cdf.4, pch=1)
>
> (pois.cdf.5 <- ppois(0:n, 5))
[1] 0.006737947 0.040427682 0.124652019 0.265025915 0.440493285 0.615960655
[7] 0.762183463 0.866628326 0.931906365 0.968171943 0.986304731
> plot(x, pois.cdf.5, type='S', ylab='F(x)', xlab='x', ylim=c(0,1),
+ main="X ~ Poisson(5); CDF", lwd=5, col="Skyblue")
> points(x-1, pois.cdf.5, type='h', col="white", lwd=5)
> points(x-1, pois.cdf.5, pch=16)
> points(x, pois.cdf.5, pch=1)
>
> (pois.cdf.6 <- ppois(0:n, 6))
[1] 0.002478752 0.017351265 0.061968804 0.151203883 0.285056500 0.445679641
[7] 0.606302782 0.743979760 0.847237494 0.916075983 0.957379076
> plot(x, pois.cdf.6, type='S', ylab='F(x)', xlab='x', ylim=c(0,1),
+ main="X ~ Poisson(6); CDF", lwd=5, col="Purple")
> points(x-1, pois.cdf.6, type='h', col="white", lwd=5)
> points(x-1, pois.cdf.6, pch=16)
> points(x, pois.cdf.6, pch=1)
>
> par(opar)
'데이터 [Data] > R' 카테고리의 다른 글
워드클라우드 자체실습: wordcloud2() (0) | 2021.06.07 |
---|---|
R Distributions: 초기하분포, 초기하분포의 이항근사 (0) | 2021.06.07 |
R Distributions: 이항분포의 누적분포함수 (0) | 2021.06.05 |
R Distributions: 이항분포 (0) | 2021.06.04 |
R Graphics 3: 문자나 점의 크기, 그래픽모수, 플랏영역, 좌표축범위 (0) | 2021.06.03 |
댓글