Attribute VB_Name = "test_student_t_os" 'Created by Peter Stikker 'Companion website: https://PeterStatistics.com 'YouTube channel: https://www.youtube.com/stikpet 'Donations welcome at Patreon: https://www.patreon.com/bePatron?u=19398076 Public Sub ts_student_t_os_addHelp() Application.MacroOptions _ Macro:="ts_student_t_os", _ Description:="One-sample Student t-test", _ category:=14, _ ArgumentDescriptions:=Array( _ "range with the data as numbers", _ "optional hypothesized mean, otherwise the midrange will be used", _ "output to show, either 'pvalue' (default), 'mu', 'statistic', or 'df'") Application.MacroOptions _ Macro:="ts_student_t_os_arr", _ Description:="One-sample Student t-test" & vbNewLine & "array function, requires 2 rows and 6 columns as output", _ category:=14, _ ArgumentDescriptions:=Array( _ "range with the data as numbers", _ "optional hypothesized mean, otherwise the midrange will be used") End Sub Function ts_student_t_os(data As Range, Optional mu = "none", Optional out = "pvalue") Attribute ts_student_t_os.VB_Description = "perform a one-sample Student t test" Attribute ts_student_t_os.VB_ProcData.VB_Invoke_Func = " \n14" 'perform a one-sample Student t test Dim n, df As Integer Dim avg, s, se, t, p As Double If mu = "none" Then mu = (WorksheetFunction.Min(data) + WorksheetFunction.Max(data)) / 2 End If If out = "mu" Then res = mu Else 'sample size (n), mean (avg) and standard deviation (s) n = WorksheetFunction.Count(data) avg = WorksheetFunction.Average(data) s = WorksheetFunction.StDev(data) 'the standard error (se) and degrees of freedom (df) df = n - 1 If out = "df" Then res = df Else se = s / Sqr(n) 'the t-value t = (avg - mu) / se If out = "statistic" Then res = t Else 'the p-value p = WorksheetFunction.TDist(Abs(t), df, 2) res = p End If End If End If 'return results ts_student_t_os = res End Function Function ts_student_t_os_arr(data As Range, Optional mu = "none") 'perform a one-sample Student t test Dim n, df As Integer Dim avg, s, se, t, p As Double If mu = "none" Then mu = (WorksheetFunction.Min(data) + WorksheetFunction.Max(data)) / 2 End If 'sample size (n), mean (avg) and standard deviation (s) n = WorksheetFunction.Count(data) avg = WorksheetFunction.Average(data) s = WorksheetFunction.StDev(data) 'the standard error (se) and degrees of freedom (df) df = n - 1 se = s / Sqr(n) 'the t-value t = (avg - mu) / se 'the p-value p = WorksheetFunction.TDist(Abs(t), df, 2) 'Results Dim res(1 To 6, 1 To 6) res(1, 1) = "H0 mean" res(1, 2) = "sample mean" res(1, 3) = "t-value" res(1, 4) = "df" res(1, 5) = "p-value" res(1, 6) = "test" res(2, 1) = mu res(2, 2) = avg res(2, 3) = t res(2, 4) = df res(2, 5) = p res(2, 6) = "one-sample Student t" ts_student_t_os_arr = res End Function