-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNormality Test
430 lines (298 loc) · 7.95 KB
/
Normality Test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
Sub LimparCampoTela3()
'
Range("B6:B100").Select
Selection.ClearContents
Range("I6:Q100").Select
Selection.ClearContents
Sheets("KolmogorovSmirnov").TB_EstatisticaKS.Text = ""
Sheets("KolmogorovSmirnov").TB_pvalue.Text = ""
Sheets("KolmogorovSmirnov").TB_Conclusao.Text = ""
End Sub
Private Sub VoltarFrmEstatisticaBasica()
With Planilha1
.Visible = xlSheetVisible
End With
With Planilha4
.Visible = xlSheetVeryHidden
End With
Frm_EstatisticaBasica.Show
End Sub
Private Sub TesteNormalidade()
Range("I6:Q100").Select
Selection.ClearContents
Dim cont As Long
Dim n As Long
Dim i As Long
cont = WorksheetFunction.CountA(Range("$B$6:$B$100"))
n = cont
If n < 4 Then
Cells(5, 2).Select
MsgBox "Para o Teste de Normalidade, recomenda-se no mínimo 4 valores"
Exit Sub
Else
End If
'Sort-Order ascending- input data
Range("B6:B100").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=-3
Range("I6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("KolmogorovSmirnov").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("KolmogorovSmirnov").Sort.SortFields.Add2 Key:=Range("I6:I100") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("KolmogorovSmirnov").Sort
.SetRange Range("I6:I100")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Empirical Fn
Dim Fn As Double
i = 6
Do
Fn = (i - 5) / n
Cells(i, 10).Value = Fn
i = i + 1
Loop While Cells(i, 9) <> ""
' F(x)calculation
Dim Soma As Double
Dim Média As Double
Dim Desvio As Double
Dim X, Z As Double
Dim Fx As Double
Soma = WorksheetFunction.Sum(Range("$I$6:$I$100"))
Desvio = WorksheetFunction.StDev(Range("$I$6:$I$100"))
If Desvio = 0 Then
Range("I6:Q100").Select
Selection.ClearContents
MsgBox ("Todos os valores são iguais! Não é possível realizar o teste estatístistico")
Cells(5, 2).Select
Exit Sub
Else
i = 6
Do
Média = (Soma / n)
X = Cells(i, 9).Value
Z = (X - Média) / (Desvio)
DistNormal = WorksheetFunction.Norm_S_Dist(Z, True)
Fx = DistNormal
Cells(i, 11).Value = Fx
i = i + 1
Loop While Cells(i, 9) <> ""
End If
'
'D+ e D-calculation :
Dim Dplus As Double
i = 6
Do
Fn = Cells(i, 10).Value
Fx = Cells(i, 11).Value
Dplus = Abs(Fx - Fn)
Cells(i, 12).Value = Dplus
i = i + 1
Loop While Cells(i, 9) <> ""
Dim Dminus As Double
i = 6
Do
If i = 6 Then
Cells(i, 13).Value = Abs(Cells(i, 11).Value - 0)
ElseIf i > 6 Then
Cells(i, 13).Value = Abs(Cells(i, 11).Value - Cells(i - 1, 10).Value)
End If
i = i + 1
Loop While Cells(i, 9) <> ""
'Determination of Dn=Máx(D+,D-)
Dim Dplusmáximo As Double
Dim Dminusmáximo As Double
Dim Dn As Double
Dplusmáximo = WorksheetFunction.Max(Range("$L$6:$L$100"))
Dminusmáximo = WorksheetFunction.Max(Range("$M$6:$M$100"))
Dn = WorksheetFunction.Max(Dplusmáximo, Dminusmáximo)
Dn = Format(Dn, "0.00000")
Cells(6, 14).Value = Dn
'Dcrit value
Dim Dcrit As Double
Dim pvalue As Double
'Para alfa =0,01
If Sheets("KolmogorovSmirnov").OptionButton1 = True Then
If n = 4 Then
Dcrit = 0.417
ElseIf n = 5 Then
Dcrit = 0.405
ElseIf n = 6 Then
Dcrit = 0.364
ElseIf n = 7 Then
Dcrit = 0.348
ElseIf n = 8 Then
Dcrit = 0.331
ElseIf n = 9 Then
Dcrit = 0.311
ElseIf n = 10 Then
Dcrit = 0.294
ElseIf n = 11 Then
Dcrit = 0.284
ElseIf n = 12 Then
Dcrit = 0.275
ElseIf n = 13 Then
Dcrit = 0.268
ElseIf n = 14 Then
Dcrit = 0.261
ElseIf n = 15 Then
Dcrit = 0.257
ElseIf n = 16 Then
Dcrit = 0.25
ElseIf n = 17 Then
Dcrit = 0.245
ElseIf n = 18 Then
Dcrit = 0.239
ElseIf n = 19 Then
Dcrit = 0.235
ElseIf n = 20 Then
Dcrit = 0.231
ElseIf n = 25 Then
Dcrit = 0.2
ElseIf n = 30 Then
Dcrit = 0.187
Else
Dcrit = 1.031 / Sqr(n)
End If
'Para alfa =0,05
ElseIf Sheets("KolmogorovSmirnov").OptionButton2 = True Then
If n = 4 Then
Dcrit = 0.381
ElseIf n = 5 Then
Dcrit = 0.337
ElseIf n = 6 Then
Dcrit = 0.319
ElseIf n = 7 Then
Dcrit = 0.3
ElseIf n = 8 Then
Dcrit = 0.285
ElseIf n = 9 Then
Dcrit = 0.271
ElseIf n = 10 Then
Dcrit = 0.258
ElseIf n = 11 Then
Dcrit = 0.249
ElseIf n = 12 Then
Dcrit = 0.242
ElseIf n = 13 Then
Dcrit = 0.234
ElseIf n = 14 Then
Dcrit = 0.227
ElseIf n = 15 Then
Dcrit = 0.22
ElseIf n = 16 Then
Dcrit = 0.213
ElseIf n = 17 Then
Dcrit = 0.206
ElseIf n = 18 Then
Dcrit = 0.2
ElseIf n = 19 Then
Dcrit = 0.195
ElseIf n = 20 Then
Dcrit = 0.19
ElseIf n = 25 Then
Dcrit = 0.173
ElseIf n = 30 Then
Dcrit = 0.161
Else
Dcrit = 0.886 / Sqr(n)
End If
'Para alfa =0,10
ElseIf Sheets("KolmogorovSmirnov").OptionButton3 = True Then
If n = 4 Then
Dcrit = 0.352
ElseIf n = 5 Then
Dcrit = 0.315
ElseIf n = 6 Then
Dcrit = 0.294
ElseIf n = 7 Then
Dcrit = 0.276
ElseIf n = 8 Then
Dcrit = 0.261
ElseIf n = 9 Then
Dcrit = 0.249
ElseIf n = 10 Then
Dcrit = 0.239
ElseIf n = 11 Then
Dcrit = 0.23
ElseIf n = 12 Then
Dcrit = 0.223
ElseIf n = 13 Then
Dcrit = 0.214
ElseIf n = 14 Then
Dcrit = 0.207
ElseIf n = 15 Then
Dcrit = 0.201
ElseIf n = 16 Then
Dcrit = 0.195
ElseIf n = 17 Then
Dcrit = 0.189
ElseIf n = 18 Then
Dcrit = 0.184
ElseIf n = 19 Then
Dcrit = 0.179
ElseIf n = 20 Then
Dcrit = 0.174
ElseIf n = 25 Then
Dcrit = 0.158
ElseIf n = 30 Then
Dcrit = 0.144
Else
Dcrit = 0.805 / Sqr(n)
End If
Else
MsgBox "Selecione o nível de significância"
End If
Dcrit = Format(Dcrit, "0.0000")
Cells(6, 15).Value = Dcrit
'pvalue
Dim Dmodif As Double
Dim A, B, C, E, F, G As Double
Dmodif = Dn * (Sqr(n) - 0.01 + (0.85 / Sqr(n)))
If Dn < 0.1 Then
A = -7.01256
B = 2.99587
C = -0.122119
G = (n + 2.78019)
E = 0.974598
F = 1.67997
pvalue = (1 - (Exp(A * G * ((Dn) ^ 2) + B * ((G) ^ (1 / 2) * Dn) + C + (E / ((n) ^ (1 / 2))) + (F / n)) / 100))
Else
If Dmodif < 0.47 Then
pvalue = (1 - (Exp(A * G * ((Dn) ^ 2) + B * ((G) ^ (1 / 2) * Dn) + C + (E / ((n) ^ (1 / 2))) + (F / n)) / 100))
ElseIf Dmodif < 1.035 And Dmodif > 0.47 Then
pvalue = (-3.1909 * ((Dmodif) ^ 3)) + (10.874 * ((Dmodif) ^ 2)) - (12.361 * Dmodif) + 4.6913
ElseIf Dmodif > 1.035 Then
pvalue = 379523 * Exp(-17.82 * Dmodif)
End If
End If
Sheets("KolmogorovSmirnov").TB_EstatisticaKS.Value = Dn
If pvalue < 0.01 Then
Sheets("KolmogorovSmirnov").TB_pvalue.Value = "< 0.01"
ElseIf pvalue > 0.2 Then
Sheets("KolmogorovSmirnov").TB_pvalue.Value = "> 0.20"
Else
Sheets("KolmogorovSmirnov").TB_pvalue.Value = pvalue
End If
If Cells(2, 2).Value = "Normality Test" Then
If Dn < Dcrit Then
Sheets("KolmogorovSmirnov").TB_Conclusao.Value = "NORMAL"
Else
Sheets("KolmogorovSmirnov").TB_Conclusao.Value = "Non-NORMAL"
End If
Cells(6, 15).Select
ElseIf Cells(2, 2).Value = "Teste de Normalidade" Then
If Dn < Dcrit Then
Sheets("KolmogorovSmirnov").TB_Conclusao.Value = "NORMAL"
Else
Sheets("KolmogorovSmirnov").TB_Conclusao.Value = "NÃO-NORMAL"
End If
Cells(6, 15).Select
End If
End Sub