📄 双因素m2.bas
字号:
Attribute VB_Name = "modMethod"
Option Explicit
'双因素方差分析
'x:试验数据,已知
'FA:列因素的F检验值,计算结果
'FB:行因素的F检验值,计算结果
Public Sub DoubleE(x() As Double, FA As Double, FB As Double)
Dim T As Double, T2 As Double, Xij2 As Double
Dim Ti As Double, Tj As Double, Ti2 As Double, Tj2 As Double
Dim QA As Double, QB As Double, Qe As Double, Q As Double
Dim UA As Integer, UB As Integer, Ue As Integer, U As Integer
Dim SA2 As Double, SB2 As Double, Se2 As Double
Dim p As Integer, m As Integer, n As Integer
Dim I As Integer, J As Integer
'm:行因素水平数。p:列因素水平数
m = UBound(x, 1): p = UBound(x, 2): n = m * p
'求总和数T
For I = 1 To p
For J = 1 To m
T = T + (x(J, I) - 32)
Next J
Next I
'求QA
T2 = T * T
For I = 1 To p
Ti = 0
For J = 1 To m
Ti = Ti + (x(J, I) - 32)
Next J
Ti2 = Ti2 + Ti * Ti
Next I
QA = Ti2 / m - T2 / n
'求QB
For I = 1 To m
Tj = 0
For J = 1 To p
Tj = Tj + (x(I, J) - 32)
Next J
Tj2 = Tj2 + Tj * Tj
Next I
QB = Tj2 / p - T2 / n
'求Q
For I = 1 To p
For J = 1 To m
Xij2 = Xij2 + (x(J, I) - 32) ^ 2
Next J
Next I
Q = Xij2 - T2 / n
Qe = Q - QA - QB
UA = p - 1: UB = m - 1: Ue = UA * UB: U = p * m - 1
'求方差
SA2 = QA / UA: SB2 = QB / UB: Se2 = Qe / Ue
'求F检验值,FA是列因素,FB是行因素
FA = SA2 / Se2: FB = SB2 / Se2
End Sub
'求Gamma函数的对数LogGamma(x)
'x:自变量
'G:Gamma函数的对数
Public Sub lnGamma(x As Double, G As Double)
Dim y As Double, z As Double, A As Double
Dim B As Double, B1 As Double, n As Integer
Dim I As Integer
If x < 8 Then
y = x + 8: n = -1
Else
y = x: n = 1
End If
z = 1 / (y * y)
A = (y - 0.5) * Log(y) - y + 0.9189385
B1 = (0.0007663452 * z - 0.0005940956) * z
B1 = (B1 + 0.0007936431) * z
B1 = (B1 - 0.002777778) * z
B = (B1 + 0.0833333) / y
G = A + B
If n >= 0 Then Exit Sub
y = y - 1: A = y
For I = 1 To 7
A = A * (y - I)
Next I
G = G - Log(A)
End Sub
'求正态分布的分位数
'Q:上侧概率
'x:分位数
Public Sub PNorm(Q, x)
Dim p As Double, y As Double, z As Double
Dim B0 As Double, B1 As Double, B2 As Double
Dim B3 As Double, B4 As Double, B5 As Double
Dim B6 As Double, B7 As Double, B8 As Double
Dim B9 As Double, B10 As Double, B As Double
B0 = 1.570796288: B1 = 0.03706987906
B2 = -0.0008364353589: B3 = -0.0002250947176
B4 = 0.000006841218299: B5 = 0.000005824238515
B6 = -0.00000104527497: B7 = 8.360937017E-08
B8 = -3.231081277E-09: B9 = 3.657763036E-11
B10 = 6.936233982E-13
If Q = 0.5 Then
x = 0: GoTo PN01
End If
If Q > 0.5 Then p = 1 - Q Else p = Q
y = -Log(4 * p * (1 - p))
B = y * (B9 + y * B10)
B = y * (B8 + B): B = y * (B7 + B)
B = y * (B6 + B): B = y * (B5 + B)
B = y * (B4 + B): B = y * (B3 + B)
B = y * (B2 + B): B = y * (B1 + B)
z = y * (B0 + B): x = Sqr(z)
If Q > 0.5 Then x = -x
PN01:
End Sub
'计算F分布的分布函数
'n1:自由度,已知
'n2:自由度,已知
'F:F值,已知
'p:下侧概率,所求
'd:概率密度,所求
Public Sub F_DIST(n1 As Integer, n2 As Integer, F As Double, _
p As Double, d As Double)
Dim x As Double, U As Double, Lu As Double
Dim IAI As Integer, IBI As Integer, nn1 As Integer, nn2 As Integer
Dim I As Integer
Const PI As Double = 3.14159265359
If F = 0 Then
p = 0: d = 0: Exit Sub
End If
x = n1 * F / (n2 + n1 * F)
If (n1 \ 2) * 2 = n1 Then
If (n2 \ 2) * 2 = n2 Then
U = x * (1 - x): p = x: IAI = 2: IBI = 2
Else
U = x * Sqr(1 - x) / 2: p = 1 - Sqr(1 - x): IAI = 2: IBI = 1
End If
Else
If (n2 \ 2) * 2 = n2 Then
p = Sqr(x): U = p * (1 - x) / 2: IAI = 1: IBI = 2
Else
U = Sqr(x * (1 - x)) / PI
p = 1 - 2 * Atn(Sqr((1 - x) / x)) / PI: IAI = 1: IBI = 1
End If
End If
nn1 = n1 - 2: nn2 = n2 - 2
If U = 0 Then
d = U / F
Exit Sub
Else
Lu = Log(U)
End If
If IAI = n1 Then GoTo LL1
For I = IAI To nn1 Step 2
p = p - 2 * U / I
Lu = Lu + Log((1 + IBI / I) * x)
U = Exp(Lu)
Next I
LL1:
If IBI = n2 Then
d = U / F: Exit Sub
End If
For I = IBI To nn2 Step 2
p = p + 2 * U / I
Lu = Lu + Log((1 + n1 / I) * (1 - x))
U = Exp(Lu)
Next I
d = U / F
End Sub
'计算F分布的分位数
'n1:自由度,已知
'n2:自由度,已知
'Q:上侧概率,已知
'F:分位数,所求
Public Sub PF_DIST(n1 As Integer, n2 As Integer, _
Q As Double, F As Double)
Dim DF12 As Double, DF22 As Double, A As Double, B As Double
Dim A1 As Double, B1 As Double, p As Double, YQ As Double
Dim E As Double, FO As Double, pp As Double, d As Double
Dim GA1 As Double, GA2 As Double, GA3 As Double
Dim K As Integer
DF12 = n1 / 2: DF22 = n2 / 2
A = 2 / (9 * n1): A1 = 1 - A
B = 2 / (9 * n2): B1 = 1 - B
p = 1 - Q: PNorm Q, YQ
E = B1 * B1 - B * YQ * YQ
If E > 0.8 Then
FO = ((A1 * B1 + YQ * Sqr(A1 * A1 * B + A * E)) / E) ^ 3
Else
lnGamma DF12 + DF22, GA1
lnGamma DF12, GA2
lnGamma DF22, GA3
FO = (2 / n2) * (GA1 - GA2 - GA3 + 0.69315 + (DF22 - 1) * Log(n2) _
- DF22 * Log(n1) - Log(Q))
FO = Exp(FO)
End If
For K = 1 To 30
F_DIST n1, n2, FO, pp, d
If d = 0 Then
F = FO: Exit Sub
End If
F = FO - (pp - p) / d
If Abs(FO - F) < 0.000001 * Abs(F) Then Exit Sub Else FO = F
Next K
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -