지난 시간에 넬슨 법칙에 대해 알아보았다.
> 넬슨법칙이란? 바로가기 : https://leedakyeong.tistory.com/entry/%EB%84%AC%EC%8A%A8-%EB%B2%95%EC%B9%99%EC%9D%B4%EB%9E%80-What-is-the-Nelson-Rules
이번에는 R에서 Nelson Rules에 해당하는 값들을 산정하는 방법에 대해 알아보겠다.
Nelson Rules in R
1. package of Nelson Rules
> library(Rspc)
2. Functions of Nelson Rules
* Evaluate Nelson's Rules
EvaluateRules(x,
type = "i", whichRules = 1:8,
lcl = NA, cl = NA, ucl = NA,
controlLimitDistance = 3, returnAllSelectedRules = F, parRules = NULL)
x : data
type : "i" or "c"
"i" : ucl = mean(x) + controlLimitDistance * sd(x); cl = mean(x); lcl = mean(x) - controlLimitDistance * sd(x)
"c" : ucl = mean(x) + controlLimitDistance * sqrt(mean(x)); cl = mean(x); lcl = mean(x) - controlLimitDistance * sqrt(mean(x))
* Rule 별 따로 계산 가능.
Rule1(x, lcl, ucl, sides, ...)
Rule2(x, cl, nPoints = 9, ...)
...
* Get/Set Nelson's Rules parameter
SetParameters()
* Calculation ucl/cl/lcl
CalculateLimits(x,
lcl = NA, cl = NA, ucl = NA,
type = "i", controlLimitDistance = 3)
3. Example of Nelson Rules
3-1. default parameter 확인
> pars = SetParameters()
> pars
$Rule1
$Rule1$sides
[1] "two-sided"
$Rule2
$Rule2$nPoints
[1] 9
$Rule3
$Rule3$nPoints
[1] 6
$Rule3$convention
[1] "jmp"
$Rule3$equalBreaksSeries
[1] TRUE
$Rule4
$Rule4$nPoints
[1] 14
$Rule4$convention
[1] "jmp"
$Rule5
$Rule5$minNPoints
[1] 2
$Rule5$nPoints
[1] 3
$Rule6
$Rule6$minNPoints
[1] 4
$Rule6$nPoints
[1] 5
$Rule7
$Rule7$nPoints
[1] 15
$Rule8
$Rule8$nPoints
[1] 8
3-2. parameter 변경
> pars$Rule1$sides <- "upper" # Rule 1 에 대해 양쪽이 아닌 upper에 대해서만.
> pars$Rule2$nPoints <- 6 # 기준 point 9 -> 6
> pars$Rule3$convention <- "minitab" # Rule 3에 6번째 포인트 미포함. 즉, 6개 이상의 점 -> 7개 이상의 점 이어야 조건 만족.
3-3. Nelson Rule 산정
> nr <- EvaluateRules(x = rnorm(100), parRules = pars)
> nr
x Rule1 Rule2 Rule3 Rule4 Rule5 Rule6 Rule7 Rule8
1 0.47641339 0 0 0 0 0 0 0 0
2 -0.12538000 0 0 0 0 0 0 0 0
3 1.09621620 0 0 0 0 0 0 0 0
4 -1.44422904 0 0 0 0 0 0 0 0
5 1.14782930 0 0 0 0 0 0 0 0
6 -0.46841204 0 0 0 0 0 0 0 0
7 -1.00595059 0 0 0 0 0 0 0 0
8 0.06356268 0 0 0 0 0 0 0 0
9 1.02497260 0 0 0 0 0 0 0 0
10 0.57314202 0 0 0 0 0 0 0 0
11 1.84718210 0 0 0 0 0 0 0 0
12 0.11193337 0 0 0 0 0 0 0 0
13 -0.74603732 0 0 0 0 0 0 0 0
14 1.65821366 0 0 0 0 0 0 0 0
15 0.72172057 0 0 0 0 0 0 0 0
16 -1.66308050 0 0 0 0 0 0 0 0
17 0.57590953 0 0 0 0 0 0 0 0
18 0.47276012 0 0 0 0 0 0 0 0
19 -0.54273166 0 0 0 0 0 0 0 0
20 1.12768707 0 0 0 0 0 0 0 0
21 -1.64779762 0 0 0 0 0 0 0 0
22 -0.31417395 0 0 0 0 0 0 0 0
23 -0.18268157 0 0 0 0 0 0 0 0
24 1.47047849 0 0 0 0 0 0 0 0
25 -0.86589878 0 0 0 0 0 0 0 0
26 1.52746698 0 0 0 0 0 0 0 0
27 1.05417806 0 0 0 0 0 0 0 0
28 1.03007101 0 0 0 0 0 0 0 0
29 0.84016086 0 0 0 0 0 0 0 0
30 0.21696470 0 0 0 0 0 0 0 0
31 -0.67252558 0 0 1 0 0 0 0 0
32 0.13259853 0 0 0 0 0 0 0 0
33 -0.07092735 0 0 0 0 0 0 0 0
34 -0.94269547 0 0 0 0 0 0 0 0
35 -1.02203100 0 0 0 0 0 0 0 0
36 0.28055512 0 0 0 0 0 0 0 0
37 0.54478337 0 0 0 0 0 0 0 0
38 0.13086975 0 0 0 0 0 0 0 0
39 0.28184439 0 0 0 0 0 0 0 0
40 -0.29273075 0 0 0 0 0 0 0 0
41 -1.32535312 0 0 0 0 0 0 0 0
42 2.06513565 0 0 0 0 0 0 0 0
43 0.24217304 0 0 0 0 0 0 0 0
44 -0.34909723 0 0 0 0 0 0 0 0
45 -0.63081237 0 0 0 0 0 0 0 0
46 0.28390672 0 0 0 0 0 0 0 0
47 0.12157699 0 0 0 0 0 0 0 0
48 0.56634411 0 0 0 0 0 0 0 0
49 0.56903290 0 0 0 0 0 0 0 0
50 -0.09058676 0 0 0 0 0 0 0 0
51 0.23049649 0 0 0 0 0 0 0 0
52 0.75285378 0 0 0 0 0 0 0 0
53 0.86338541 0 0 0 0 0 0 0 0
54 0.83819864 0 0 0 0 0 0 0 0
55 -1.76585212 0 0 0 0 0 0 0 0
56 0.68349450 0 0 0 0 0 0 0 0
57 1.16301128 0 0 0 0 0 0 0 0
58 -1.20060185 0 0 0 0 0 0 0 0
59 3.24953010 1 0 0 0 0 0 0 0
60 -0.67573573 0 0 0 0 0 0 0 0
61 -1.21292723 0 0 0 0 0 0 0 0
62 -0.41705041 0 0 0 0 0 0 0 0
63 -0.56555300 0 0 0 0 0 0 0 0
64 -1.48973024 0 0 0 0 0 0 0 0
65 -1.18349566 0 0 0 0 0 0 0 0
66 -0.72711212 0 0 0 0 0 0 0 0
67 -0.68434322 0 0 0 0 0 0 0 0
68 -0.70701829 0 1 0 0 0 0 0 0
69 0.87975865 0 0 0 0 0 0 0 0
70 -0.50986863 0 0 0 0 0 0 0 0
71 -1.29412905 0 0 0 0 0 0 0 0
72 -0.11312520 0 0 0 0 0 0 0 0
73 0.88637595 0 0 0 0 0 0 0 0
74 0.01704614 0 0 0 0 0 0 0 0
75 0.77604145 0 0 0 0 0 0 0 0
76 0.07295527 0 0 0 0 0 0 0 0
77 -2.01550579 0 0 0 0 0 0 0 0
78 -1.00770160 0 0 0 0 0 0 0 0
79 -0.15548671 0 0 0 0 0 0 0 0
80 0.93854205 0 0 0 0 0 0 0 0
81 1.56486520 0 0 0 0 0 0 0 0
82 1.08573566 0 0 0 0 0 0 0 0
83 1.21995491 0 0 0 0 0 0 0 0
84 -0.41330512 0 0 0 0 0 0 0 0
85 -0.13044571 0 0 0 0 0 0 0 0
86 -0.14965983 0 0 0 0 0 0 0 0
87 0.82588656 0 0 0 0 0 0 0 0
88 -0.12341789 0 0 0 0 0 0 0 0
89 1.25846817 0 0 0 0 0 0 0 0
90 -2.31845726 0 0 0 0 0 0 0 0
91 -0.63650751 0 0 0 0 0 0 0 0
92 0.30997578 0 0 0 0 0 0 0 0
93 -2.12257378 0 0 0 0 0 0 0 0
94 0.56313374 0 0 0 0 0 0 0 0
95 0.60299436 0 0 0 0 0 0 0 0
96 -1.10696397 0 0 0 0 0 0 0 0
97 0.64944301 0 0 0 0 0 0 0 0
98 0.64450960 0 0 0 0 0 0 0 0
99 -0.70705879 0 0 0 0 0 0 0 0
100 1.46269706 0 0 0 0 0 0 0 0
3-4. UCL/LCL 변경
> nr <- EvaluateRules(x = rnorm(100), controlLimitDistance = 2, parRules = pars) # ucl/lcl 3 시그마 -> 2 시그마
> CalculateLimits(rnorm(100), controlLimitDistance = 2) # ucl/cl/lcl 확인 가능
$ucl
[1] 2.075604
$cl
[1] 0.05958278
$lcl
[1] -1.956439
단, 3 시그마에서 2 시그마로 변경 시 Rule 5 는 기준선 변경 후 재계산. (다른 Rule 은 기준선 변경 없음.)
> data <- rnorm(100)
> limits = CalculateLimits(x = data, controlLimitDistance=3)
> zones = CalculateZoneBorders(limits)
> zones
-3 -2 -1 0 1 2 3
-2.96444932 -1.95643862 -0.94842792 0.05958278 1.06759347 2.07560417 3.08361487
> limits = CalculateLimits(x = data, controlLimitDistance=2)
> zones = CalculateZoneBorders(limits)
> zones
-3 -2 -1 0 1 2 3
-1.95643862 -1.28443149 -0.61242436 0.05958278 0.73158991 1.40359704 2.07560417
> Rule5(data, zoneB = zones)
'R' 카테고리의 다른 글
[R] caret 패키지로 scale 하는 방법 :: scale in R (preProcess in caret) :: 표준화 vs 정규화 (0) | 2020.09.03 |
---|---|
윈도우 작업 스케줄러에 R script 등록하기 (2) | 2020.03.05 |
[R] R에서 eval() 함수로 표현식 실행하기 (eval in R) (0) | 2019.09.18 |
[R] R에서 문자열 합치기 or list의 모든 element 하나로 합치기 (paste in R) (0) | 2019.09.17 |
[R] 수평선, 수직선 in ggplot (add horizontal line, vertical line in ggplot2) (0) | 2019.09.05 |