Repository Public
1import Foundation2import SheetCore3import Statistics4import AnalysisKit56/// One-sample, two-sample (pooled / Welch) and paired t-tests.7enum TTestTool {8 static let descriptor: ToolDescriptor = {9 var t = ToolDescriptor(10 id: "compare.ttest", title: L("t-Tests"), group: Common.groupCompare, systemImage: "t.square",11 summary: L("Student t-tests of a mean, of two independent means or of paired differences."),12 fields: [13 .choice("kind", L("Test"), [FieldOption("one", L("One sample")), FieldOption("two", L("Two independent samples")), FieldOption("paired", L("Paired samples"))]),14 .range("data", L("Data range")),15 .column("x", L("Sample 1")),16 .column("y", L("Sample 2"), visible: { $0.choice("kind") != "one" }),17 .number("mu", L("Hypothesised mean"), 0, visible: { $0.choice("kind") == "one" }),18 .number("difference", L("Hypothesised difference"), 0, visible: { $0.choice("kind") != "one" }),19 .choice("variances", L("Variances"), [FieldOption("welch", L("Unequal (Welch)")), FieldOption("pooled", L("Equal (pooled)"))],20 visible: { $0.choice("kind") == "two" }),21 Common.alternativeField(),22 Common.levelField(),23 .alpha(L("Significance level α")),24 ],25 info: [26 L("Two independent samples: Welch's test (unequal variances, Satterthwaite degrees of freedom) is the default; the pooled test assumes equal variances."),27 L("Paired test: a one-sample test on the differences Sample 1 − Sample 2; rows with a missing value in either column are dropped."),28 L("Effect size: Cohen's d (d_z for paired samples)."),29 ],30 run: run)31 t.demo = { _, v in32 v.set("kind", choice: "two"); v.set("data", range: "Groups!A1:C31"); v.set("x", column: 0); v.set("y", column: 1)33 }34 return t35 }()3637 static func run(_ input: ToolInput) throws -> ToolResult {38 let v = input.values39 let alpha = try Common.alpha(v), level = try Common.level(v)40 let alt = Common.alternative(v)41 let frame = try input.frame()42 let a = try input.column("x")43 let test: HypothesisTest44 var subtitle = frame.label + " — " + a.name45 var null: String46 var samples: [(label: String, values: [Double])]47 var notes: [String?] = []48 switch v.choice("kind") {49 case "two":50 let b = try input.column("y")51 guard a.index != b.index else { throw ToolError(L("Choose two different columns.")) }52 test = try Inference.tTest(a.present, b.present, equalVariances: v.choice("variances") == "pooled", difference: v.number("difference"),53 alternative: alt, level: level)54 subtitle += " / " + b.name55 null = L("the difference of the two population means equals %@", Fmt.number(v.number("difference"), locale: input.locale))56 samples = [(a.name, a.present), (b.name, b.present)]57 notes.append(Common.missingNote([a, b]))58 case "paired":59 let b = try input.column("y")60 guard a.index != b.index else { throw ToolError(L("Choose two different columns.")) }61 let (x, y) = Common.pairs(a, b)62 test = try Inference.pairedTTest(x, y, difference: v.number("difference"), alternative: alt, level: level)63 subtitle += " − " + b.name64 null = L("the mean of the paired differences equals %@", Fmt.number(v.number("difference"), locale: input.locale))65 samples = [(a.name, x), (b.name, y), (L("Difference"), zip(x, y).map { $0 - $1 })]66 notes.append(Common.pairedNote(a, b, kept: x.count))67 default:68 test = try Inference.tTest(a.present, mu: v.number("mu"), alternative: alt, level: level)69 null = L("the population mean equals %@", Fmt.number(v.number("mu"), locale: input.locale))70 samples = [(a.name, a.present)]71 notes.append(Common.missingNote([a]))72 }73 var extra: [ResultBlock] = [.table(Common.samplesTable(samples))]74 if samples.count >= 2 {75 extra.append(.chart(Common.boxPlot(Array(samples.prefix(2)), title: L("Distribution of the samples"), yTitle: L("Value"), locale: input.locale)))76 }77 return Common.testResult(test, title: Common.testName(test.name), subtitle: subtitle, alpha: alpha, null: null, locale: input.locale, notes: notes, extra: extra,78 sheetName: L("t-test"))79 }80}81