Calculate P-Value. Range v Array

Bernieg

Board Regular
Joined
Jan 1, 2009
Messages
147
Office Version
  1. 365
Platform
  1. Windows
Hi The code below calculates the P-Value using the Range format.
From articles i have read using an Array would be far faster....But i don't know how to do it.

Your challenge should you accept it is to beat 0.054
The AD & P-value are correct, used Minitab to check.

Thanks in advance
Bernie


Range v Array P Value.xlsm
ABCDEFGHIJKLM
1Sorted Small to LargeRankF(Yi): NormalF(Yn+1-i)AD(S)Source data
21288.242010.044268900.96141405-0.21241133No. Parts(n)30.0001288.3460
31288.250020.066741830.93814026-0.54898092StDev0.039481288.2500
41288.253030.077149640.92112611-0.85031890Mean1288.311288.3650
51288.258040.097169760.89165215-1.06253094Anderson Darling(AD)0.5201288.2990
61288.267050.142341350.83696023-1.12898651Ajusted AD0.5339391571288.2720
71288.270060.160144810.83064295-1.32272172P-Value0.1721288.2720
81288.271070.166389680.82417071-1.530387951288.3250
91288.272080.172789650.78212334-1.63975326Range Time0.05468751288.2530
101288.272090.172789650.74303285-1.76487617Array Time1288.3240
111288.2750100.192915570.70931869-1.824652601288.3040
121288.2890110.304132050.70057791-1.677336011288.3050
131288.2980120.387989110.70057791-1.650387251288.2890
141288.2990130.397728170.65520180-1.655652041288.3240
151288.3040140.447266200.64582415-1.658306781288.2710
161288.3050150.457299590.64582415-1.759699011288.3580
171288.3240160.645824150.45729959-1.083373401288.2670
181288.3240170.645824150.44726620-1.133117461288.2420
191288.3250180.655201800.39772817-1.084834801288.3480
201288.3300190.700577910.38798911-1.044454381288.3350
211288.3300200.700577910.30413205-0.933978591288.2750
221288.3310210.709318690.19291557-0.762295711288.3300
231288.3350220.743032850.17278965-0.697619511288.2580
241288.3400230.782123340.17278965-0.653158621288.3300
251288.3460240.824170710.16638968-0.588074691288.2700
261288.3470250.830642950.16014481-0.588132361288.3790
271288.3480260.836960230.14234135-0.563597301288.3470
281288.3580270.891652150.09716976-0.383189891288.2980
291288.3650280.921126110.07714964-0.297818601288.3400
301288.3700290.938140260.06674183-0.252565501288.3700
311288.3790300.961414050.04426890-0.166436621288.3310
Sheet1
Cell Formulas
RangeFormula
C2:C31C2=NORM.DIST(A2,1288.30923333333,0.0394755999131703,TRUE)
D2:D31D2=VLOOKUP(H$2+1-B2,VLdata,2,FALSE)
E2:E31E2=(2*B2-1)*(LN(C2)+LN(1-D2))/ 30
H2H2=COUNT(A:A)
H3H3=STDEV.S(A:A)
H4H4=AVERAGE(A:A)
H5H5=-30-SUM(E:E)
H6H6=0.519648814547281*(1+0.75/30+2.25/30 ^2)
H7H7=EXP(0.9177-4.279*0.533939156947331-1.38*0.533939156947331^2)
Named Ranges
NameRefers ToCells
Imrdata=Sheet1!$A$2:$A$31H2:H4, C2
VLdata=Sheet1!$B$2:$C$31D3:D31, D2:E2
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B24Cell ValueduplicatestextNO
B24Cell ValueduplicatestextNO
B24Cell ValueduplicatestextNO
B24Cell ValueduplicatestextNO


VBA Code:
Sub GetPvalue() 'Get P Value *****************************
Application.ScreenUpdating = False
'Timer***************************************************
Dim secs1 As Single
Dim secs2 As Single
secs1 = Timer()
'********************************************************
Worksheets("Sheet1").Select
Range("A:J").Select
Selection.ClearContents
Range("B1").Select
'Copy Source Data
Range("M2:M33").Select
Selection.Copy
Range("A2").Select
ActiveSheet.Paste
Range("A1").Select
'Sort Data Smallist to Largist
Range("A2:A100").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("A2:A100" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A2:A100")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
'********************************************************
'Stop
Dim Pvalue, AD, imrav, imrstdev, AjustedAD As Double
Dim Nparts As Integer
Dim Imrdata As Range
'Titles
Range("A1") = "Sorted Small to Large"
Range("B1") = "Rank"
Range("C1") = "F(Yi): Normal"
Range("D1") = "F(Yn+1-i)"
Range("E1") = "AD(S)"
Range("g2") = "No. Parts(n)"
Range("g3") = "StDev"
Range("g4") = "Mean"
Range("G5") = "Anderson Darling(AD)"
Range("G6") = "Ajusted AD"
Range("G7") = "P-Value"
Range("G9") = "Range Time"
Range("G10") = "Array Time"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=COUNT(C[-7])"
Range("H3").Select
ActiveCell.FormulaR1C1 = "=STDEV.S(C[-7])"
Range("H4").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(C[-7])"
Nparts = Range("h2")
imrstdev = Range("h3")
imrav = Range("h4")
'Imrdata Dynamic Range
Worksheets("Sheet1").Select
Set sht = Worksheets("Sheet1")
Range("A2").Select
Set StartCell = Range("A2")
LastRow = Nparts + 1
lastColumn = 1
ActiveWorkbook.Names.Add Name:="Imrdata", RefersToR1C1:= _
Range(StartCell, sht.Cells(LastRow, lastColumn))
Range("A2").Select
'Stop
'Rank Sorted Data
Digby = 0
Lr = Nparts + 1
For r = 2 To Lr '
If Not IsEmpty(Cells(r, 1)) Then
Digby = Digby + 1
Cells(r, 2).Value = Digby
End If
Next r
'Stop
'F(Yi): Normal
LastRow = Range("a2").End(xlDown).Row
If Not IsEmpty(Range("a2")) Then
Range("c2").Select
Range("c2:c" & LastRow).FormulaR1C1 = "=NORM.DIST(RC[-2]," & imrav & "," & imrstdev & ",TRUE)"
Columns("c:c").EntireColumn.AutoFit
End If
'Stop
'Create VlookUp range
Set sht = Worksheets("Sheet1")
Range("b2").Select
Set StartCell = Range("b2")
LastRow = Nparts + 1
lastColumn = 3
ActiveWorkbook.Names.Add Name:="VLdata", RefersToR1C1:= _
Range(StartCell, sht.Cells(LastRow, lastColumn))
Range("a2").Select
'Stop
'F(Yn+1-i)
LastRow = Range("a2").End(xlDown).Row
If Not IsEmpty(Range("a2")) Then
Range("d2").Select
Range("d2:d" & LastRow).FormulaR1C1 = "=VLOOKUP(R2C[4]+1-RC[-2],VLdata,2,FALSE)"
Columns("d:d").EntireColumn.AutoFit
End If
'Stop
'AD(S)
LastRow = Range("a2").End(xlDown).Row
If Not IsEmpty(Range("a2")) Then
Range("e2").Select
Range("e2:e" & LastRow).FormulaR1C1 = "=(2*RC[-3]-1)*(LN(RC[-2])+LN(1-RC[-1]))/ " & Nparts & ""
Columns("e:e").EntireColumn.AutoFit
'Stop
'Anderson Darling(AD)
Range("h5") = "=-" & Nparts & "-SUM(e:e)"
AD = Range("h5").Value
'Stop
'If AD Error
If Not IsNumeric(Range("h2")) Then
Pval = "<0.005"
Range("H7") = Pval
GoTo 101
End If
'Ajusted AD
Range("h6") = "=" & AD & "*(1+0.75/" & Nparts & "+2.25/" & Nparts & " ^2)" '
AjustedAD = Range("h6")
End If
'Stop
'P Value
If AjustedAD > 0.6 Then
Range("h7") = "=Exp(1.2937 - 5.709 * " & AjustedAD & " + 0.0186 * " & AjustedAD & " ^ 2)"
Pval = Range("h7")
ElseIf AjustedAD > 0.34 And AjustedAD < 0.6 Then
Range("h7") = "=exp(0.9177-4.279*" & AjustedAD & "-1.38*" & AjustedAD & "^2)"
Pval = Range("h7")
ElseIf AjustedAD > 0.2 And AjustedAD < 0.34 Then
Range("h7") = "= 1- exp(-8.318 + 42.796*" & AjustedAD & "-59.938*" & AjustedAD & "^2)"
Pval = Range("h7")
ElseIf AjustedAD >= 0.2 And AjustedAD < 0.34 Then
Range("h7") = "= 1- exp(-13.436 + 101.14*" & AjustedAD & "-223.73*" & AjustedAD & "^2"
Pval = Range("h7")
End If
If Range("h7").Value <= 0.005 Then
Pval = "<0.005"
Range("H7") = Pval
End If
101 ' jumps here if Ley Numbers
'*******************************************************************
'Stop
secs2 = Timer()
Worksheets("Sheet1").Range("h9").Value = secs2 - secs1
Application.CutCopyMode = True
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,224,817
Messages
6,181,148
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top