Repository Public
1import Foundation2import SheetCore3import Statistics4import AnalysisKit56enum AnovaSupport {7 /// Localized source names of the engine's ANOVA rows.8 static func source(_ s: String, rowName: String? = nil, colName: String? = nil) -> String {9 switch s {10 case "Between Groups": L("Between groups")11 case "Within Groups": L("Within groups")12 case "Rows": rowName ?? L("Rows")13 case "Columns": colName ?? L("Columns")14 case "Interaction": L("Interaction")15 case "Error": L("Error")16 case "Within": L("Within cells")17 case "Total": L("Total")18 default: s19 }20 }2122 static func table(_ r: AnovaResult, rowName: String? = nil, colName: String? = nil) -> ReportTable {23 var rows: [[Value]] = [[.text(L("Source of variation")), .text(L("Sum of squares")), .text(L("df")), .text(L("Mean square")), .text("F"),24 .text(L("p-value")), .text(L("η²")), .text(L("Partial η²"))]]25 for row in r.rows {26 rows.append([.text(source(row.source, rowName: rowName, colName: colName)), .finite(row.sumOfSquares), .finite(row.df), .finite(row.meanSquare),27 .finite(row.f), .finite(row.pValue), .finite(row.etaSquared), .finite(row.partialEtaSquared)])28 }29 return ReportTable(title: L("Analysis of variance"), rows: rows,30 columnFormats: [1: .fixed(4), 2: .integer, 3: .fixed(4), 4: .fixed(4), 5: .pValue, 6: .fixed(4), 7: .fixed(4)],31 note: L("p-values are upper-tail probabilities of the F distribution."))32 }3334 static func groupTable(_ groups: [(label: String, count: Int, mean: Double, variance: Double)], title: String) -> ReportTable {35 var rows: [[Value]] = [[.text(L("Level")), .text(L("n")), .text(L("Mean")), .text(L("Variance")), .text(L("Standard deviation"))]]36 for g in groups { rows.append([.text(g.label), .number(Double(g.count)), .finite(g.mean), .finite(g.variance), .finite(g.variance.squareRoot())]) }37 return ReportTable(title: title, rows: rows, columnFormats: [1: .integer])38 }3940 static func effectBlocks(_ row: AnovaRow, name: String, dfError: Double, alpha: Double, null: String, locale: Locale) -> [ResultBlock] {41 guard let p = row.pValue, let f = row.f else { return [] }42 return [.interpretation(L("%@: F(%@, %@) = %@. ", name, Fmt.df(row.df, locale: locale), Fmt.df(dfError, locale: locale), Fmt.fixed(f, 4, locale: locale))43 + Interpret.decision(p: p, alpha: alpha, null: null, locale: locale))]44 }45}4647/// One-way ANOVA with Tukey HSD.48enum OneWayAnovaTool {49 static let descriptor: ToolDescriptor = {50 var t = ToolDescriptor(51 id: "compare.anova-oneway", title: L("One-Way ANOVA"), group: Common.groupCompare, systemImage: "chart.bar.doc.horizontal",52 summary: L("Compares the means of several independent groups, with Tukey HSD pairwise comparisons."),53 fields: [.range("data", L("Data range"))] + GroupFields.fields() + [54 .toggle("tukey", L("Tukey HSD pairwise comparisons"), true),55 .alpha(L("Significance level α")),56 ],57 info: [58 L("F = mean square between groups / mean square within groups with (k − 1, N − k) degrees of freedom; η² = SS between / SS total."),59 L("Tukey HSD uses the Tukey–Kramer adjustment for unequal group sizes; adjusted p-values and simultaneous intervals come from the studentized range distribution with the family-wise level 1 − α."),60 L("Blank cells, text and error values are treated as missing."),61 ],62 run: run)63 t.demo = { _, v in v.set("data", range: "Groups!A1:C31"); v.set("groupCols", columns: [0, 1, 2]) }64 return t65 }()6667 static func run(_ input: ToolInput) throws -> ToolResult {68 let alpha = try Common.alpha(input.values)69 let frame = try input.frame()70 let (groups, missing) = try GroupFields.read(input)71 let labels = groups.map(\.label)72 let r = try Inference.oneWayANOVA(groups.map(\.values), labels: labels)73 guard let between = r.row("Between Groups"), let within = r.row("Within Groups"), let p = between.pValue, let f = between.f else {74 throw ToolError(L("The analysis of variance could not be computed."))75 }76 var blocks: [ResultBlock] = [77 .metrics([Metric("F", f, .fixed(4)), Metric(L("Degrees of freedom"), text: Fmt.df(between.df, locale: input.locale) + ", " + Fmt.df(within.df, locale: input.locale)),78 Metric(L("p-value"), p, .pValue, detail: L("(upper tail)")), Metric(L("Effect size (η²)"), between.etaSquared ?? .nan, .fixed(4))]),79 .table(AnovaSupport.table(r)),80 .interpretation(Interpret.decision(p: p, alpha: alpha, null: L("all group means are equal"), locale: input.locale)),81 .table(AnovaSupport.groupTable(r.groups.map { ($0.label, $0.count, $0.mean, $0.variance) }, title: L("Groups"))),82 .chart(Common.boxPlot(groups, title: L("Distribution by group"), yTitle: L("Value"), locale: input.locale)),83 ]84 if input.values.bool("tukey"), groups.count >= 2 {85 let t = try Inference.tukeyHSD(groups.map(\.values), labels: labels, level: 1 - alpha)86 var rows: [[Value]] = [[.text(L("Comparison")), .text(L("Mean difference")), .text("q"), .text(L("Adjusted p-value")),87 .text(L("Lower bound")), .text(L("Upper bound")), .text(L("Decision"))]]88 for c in t.comparisons {89 rows.append([.text(c.groupA + " − " + c.groupB), .finite(c.difference), .finite(c.q), .finite(c.pValue), .finite(c.lower), .finite(c.upper),90 .text(c.pValue < alpha ? L("reject equality") : L("fail to reject equality"))])91 }92 blocks.append(.table(ReportTable(title: L("Tukey HSD pairwise comparisons"), rows: rows,93 columnFormats: [1: .fixed(4), 2: .fixed(4), 3: .pValue, 4: .fixed(4), 5: .fixed(4)],94 note: L("Simultaneous %@ confidence intervals; critical q = %@ with %d groups and %@ error degrees of freedom.",95 Fmt.percent(t.level, 1, locale: input.locale), Fmt.fixed(t.criticalQ, 4, locale: input.locale), t.groupCount,96 Fmt.df(t.dfError, locale: input.locale)))))97 }98 blocks.append(.assumptions(r.assumptions, title: L("Assumptions")))99 if let note = GroupFields.missingNote(missing) { blocks.append(.note([note])) }100 return ToolResult(title: L("One-Way ANOVA"), subtitle: L("%@ — %d groups", frame.label, groups.count), blocks: blocks, sheetName: L("ANOVA"))101 }102}103104/// Two-way ANOVA without replication (table) or with replication (balanced, long format).105enum TwoWayAnovaTool {106 static let descriptor: ToolDescriptor = {107 var t = ToolDescriptor(108 id: "compare.anova-twoway", title: L("Two-Way ANOVA"), group: Common.groupCompare, systemImage: "square.grid.2x2",109 summary: L("Tests the effects of two factors, without replication (one value per cell) or with balanced replication."),110 fields: [111 .choice("design", L("Design"), [FieldOption("table", L("Without replication (table of values)")),112 FieldOption("replicated", L("With replication (value and two factor columns)"))]),113 .range("data", L("Data range")),114 .column("value", L("Value column"), visible: { $0.choice("design") == "replicated" }),115 .column("factorA", L("Factor A (rows)"), numeric: false, visible: { $0.choice("design") == "replicated" }),116 .column("factorB", L("Factor B (columns)"), numeric: false, visible: { $0.choice("design") == "replicated" }),117 .alpha(L("Significance level α")),118 ],119 info: [120 L("Without replication: every numeric column is a level of the column factor and every row a level of the row factor; the interaction cannot be separated from the error."),121 L("With replication: fixed-effects F tests against the within-cell mean square; the design must be balanced (the same number, at least 2, of observations in every cell)."),122 L("Unbalanced designs are not supported (they need Type II/III sums of squares): use the regression tool with categorical dummies instead."),123 ],124 run: run)125 t.demo = { _, v in v.set("design", choice: "table"); v.set("data", range: "Groups!H1:K4") }126 return t127 }()128129 static func run(_ input: ToolInput) throws -> ToolResult {130 let v = input.values131 let alpha = try Common.alpha(v)132 let frame = try input.frame()133 let result: AnovaResult134 var rowLevels: [String], colLevels: [String]135 var rowName = L("Rows"), colName = L("Columns")136 var subtitle = frame.label137 if v.choice("design") == "replicated" {138 let value = try input.column("value"), a = try input.column("factorA"), b = try input.column("factorB")139 guard Set([value.index, a.index, b.index]).count == 3 else { throw ToolError(L("Choose three different columns.")) }140 let la = a.labels, lb = b.labels141 rowLevels = []; colLevels = []142 var cells = [[String]: [Double]]()143 var skipped = 0144 for (i, x) in value.numbers.enumerated() {145 guard !x.isNaN, !la[i].isEmpty, !lb[i].isEmpty else { skipped += 1; continue }146 if !rowLevels.contains(la[i]) { rowLevels.append(la[i]) }147 if !colLevels.contains(lb[i]) { colLevels.append(lb[i]) }148 cells[[la[i], lb[i]], default: []].append(x)149 }150 guard rowLevels.count >= 2, colLevels.count >= 2 else { throw ToolError(L("Each factor needs at least two levels.")) }151 let grid = rowLevels.map { r in colLevels.map { cells[[r, $0]] ?? [] } }152 let sizes = Set(grid.flatMap { $0.map(\.count) })153 guard sizes.count == 1, let reps = sizes.first, reps >= 2 else {154 throw ToolError(sizes.count == 1155 ? L("Every cell holds a single observation: use the design without replication.")156 : L("The design is unbalanced (cells hold between %d and %d observations). Unbalanced two-way ANOVA is not supported; use the regression tool with categorical dummies.", sizes.min() ?? 0, sizes.max() ?? 0))157 }158 result = try Inference.twoWayANOVA(replicated: grid)159 rowName = a.name; colName = b.name160 subtitle += " — " + value.name + " ~ " + a.name + " × " + b.name161 if skipped > 0 { subtitle += " · " + L("%d rows skipped", skipped) }162 } else {163 let numeric = frame.columns.filter(\.isNumeric)164 guard numeric.count >= 2 else { throw ToolError(L("The table needs at least two numeric columns.")) }165 let labelCol = frame.columns.first { !$0.isNumeric && $0.textCount > 0 }166 guard numeric.allSatisfy({ $0.missingCount == 0 }) else { throw ToolError(L("The table contains missing values; two-way ANOVA without replication needs one value in every cell.")) }167 colLevels = numeric.map(\.name)168 rowLevels = (0..<frame.rowCount).map { r in labelCol.map { $0.labels[r] }.flatMap { $0.isEmpty ? nil : $0 } ?? L("Row %d", r + 1) }169 result = try Inference.twoWayANOVA((0..<frame.rowCount).map { r in numeric.map { $0.numbers[r] } })170 if let labelCol { rowName = labelCol.name }171 }172 let errorRow = result.row("Within") ?? result.row("Error")173 let dfError = errorRow?.df ?? .nan174 var blocks: [ResultBlock] = [.table(AnovaSupport.table(result, rowName: rowName, colName: colName))]175 if let r = result.row("Rows") {176 blocks += AnovaSupport.effectBlocks(r, name: rowName, dfError: dfError, alpha: alpha, null: L("the means of all levels of %@ are equal", rowName), locale: input.locale)177 }178 if let c = result.row("Columns") {179 blocks += AnovaSupport.effectBlocks(c, name: colName, dfError: dfError, alpha: alpha, null: L("the means of all levels of %@ are equal", colName), locale: input.locale)180 }181 if let i = result.row("Interaction") {182 blocks += AnovaSupport.effectBlocks(i, name: L("Interaction"), dfError: dfError, alpha: alpha, null: L("there is no interaction between the two factors"), locale: input.locale)183 }184 let a = rowLevels.count185 let groups = result.groups.enumerated().map { i, g in (i < a ? rowLevels[i] : colLevels[i - a], g.count, g.mean, g.variance) }186 blocks.append(.table(AnovaSupport.groupTable(Array(groups.prefix(a)), title: L("Levels of %@", rowName))))187 blocks.append(.table(AnovaSupport.groupTable(Array(groups.dropFirst(a)), title: L("Levels of %@", colName))))188 blocks.append(.chart(ChartBlock(title: L("Level means"),189 spec: ChartKit.columns(categories: groups.map(\.0), series: [(L("Mean"), groups.map(\.2))], xTitle: "", yTitle: L("Mean")),190 summary: L("Bars of the mean of each level: %d levels of %@ and %d levels of %@.", a, rowName, colLevels.count, colName), height: 220)))191 blocks.append(.assumptions(result.assumptions, title: L("Assumptions")))192 return ToolResult(title: v.choice("design") == "replicated" ? L("Two-Way ANOVA with Replication") : L("Two-Way ANOVA without Replication"),193 subtitle: subtitle, blocks: blocks, sheetName: L("Two-way ANOVA"))194 }195}196