如何在Excel中实现单样本t检验
发布网友
发布时间:2022-05-05 00:58
我来回答
共1个回答
热心网友
时间:2022-06-28 00:51
自己写了一个单样本t检验的自定义函数
Public Function 单样本t检验(RowData As Range, StandardValue As Double, _
Optional symbol As Variant = "=", Optional Probability As Double = 0.05) As String
Dim Ave As Double, Stdev As Double, t As Double, tinv As Double
Dim Numb As Integer
Numb = RowData.Count
Ave = Application.WorksheetFunction.Average(RowData)
Stdev = Application.WorksheetFunction.Stdev(RowData)
t = (Ave - StandardValue) / (Stdev / Sqr(Numb))
If (symbol = "=") Then ''''单样本双侧t检验
tinv = Application.WorksheetFunction.tinv(Probability, Numb - 1)
If (Abs(t) <= tinv) Then
单样本t检验 = "接受" & "“" & "等于" & StandardValue & "”" & "的假设。"
Else: 单样本t检验 = "不接受" & "“" & "等于" & StandardValue & "”" & "的假设。"
End If
ElseIf (symbol Like "<") Then ''''单样本右侧t检验
Probability = 0.025
tinv = Application.WorksheetFunction.tinv(2 * Probability, Numb - 1)
If (t <= tinv) Then
单样本t检验 = "接受" & "“" & "小于" & StandardValue & "”" & "的假设。"
Else: 单样本t检验 = "不接受" & "“" & "小于" & StandardValue & "”" & "的假设。"
End If
ElseIf (symbol Like ">") Then ''''单样本左侧t检验
tinv = Application.WorksheetFunction.tinv(2 * Probability, Numb - 1)
If (t >= tinv) Then
单样本t检验 = "接受" & "“" & "大于" & StandardValue & "”" & "的假设。"
Else: 单样本t检验 = "不接受" & "“" & "大于" & StandardValue & "”" & "的假设。"
End If
End If
End Function