Repository Public
1import Foundation2import SheetCore3import Statistics4import AnalysisKit56/// Chi-square goodness-of-fit test.7enum ChiSquareGoodnessOfFitTool {8 static let descriptor: ToolDescriptor = {9 var t = ToolDescriptor(10 id: "compare.chisq-gof", title: L("Chi-Square Goodness of Fit"), group: Common.groupCompare, systemImage: "checklist",11 summary: L("Tests whether observed category counts follow hypothesised proportions."),12 fields: [13 .range("data", L("Data range")),14 .column("observed", L("Observed counts")),15 .column("labels", L("Category labels"), numeric: false, optional: true),16 .choice("expectedKind", L("Expected distribution"), [FieldOption("equal", L("Equal proportions")), FieldOption("counts", L("Expected counts column")),17 FieldOption("probabilities", L("Probabilities column"))]),18 .column("expected", L("Expected column"), optional: true, visible: { $0.choice("expectedKind") != "equal" }),19 .integer("estimated", L("Parameters estimated from the data"), 0, min: 0, max: 50,20 help: L("Reduces the degrees of freedom: df = categories − 1 − estimated parameters.")),21 .alpha(L("Significance level α")),22 ],23 info: [24 L("Pearson statistic Σ(O − E)²/E; the p-value is the upper tail of χ² with k − 1 − m degrees of freedom."),25 L("Expected counts must have the same total as the observed counts; probabilities are normalised to sum to 1."),26 L("The χ² approximation needs expected counts that are large enough (rule of thumb: all ≥ 5)."),27 ],28 run: run)29 t.demo = { _, v in v.set("data", range: "Groups!H1:K4"); v.set("observed", column: 1); v.set("labels", column: 0) }30 return t31 }()3233 static func run(_ input: ToolInput) throws -> ToolResult {34 let v = input.values35 let alpha = try Common.alpha(v)36 let frame = try input.frame()37 let obsCol = try input.column("observed")38 let kind = v.choice("expectedKind")39 let expCol = kind == "equal" ? nil : input.optionalColumn("expected")40 if kind != "equal", expCol == nil { throw ToolError(L("Choose the column holding the expected counts or probabilities.")) }41 let labelCol = input.optionalColumn("labels")42 var series = [obsCol.numbers]43 if let expCol { series.append(expCol.numbers) }44 let (rows, kept) = DataFrame.listwise(series)45 guard rows.count >= 2 else { throw ToolError(L("At least two categories with numeric counts are required.")) }46 let observed = kept[0]47 let test: HypothesisTest48 let total = observed.reduce(0, +)49 let expected: [Double]50 switch kind {51 case "counts":52 expected = kept[1]53 test = try Inference.chiSquareGoodnessOfFit(observed: observed, expected: expected, estimatedParameters: v.int("estimated"))54 case "probabilities":55 let sum = kept[1].reduce(0, +)56 expected = kept[1].map { $0 / sum * total }57 test = try Inference.chiSquareGoodnessOfFit(observed: observed, probabilities: kept[1], estimatedParameters: v.int("estimated"))58 default:59 expected = Array(repeating: total / Double(observed.count), count: observed.count)60 test = try Inference.chiSquareGoodnessOfFit(observed: observed, estimatedParameters: v.int("estimated"))61 }62 let labels = rows.map { r in labelCol.map { $0.labels[r] }.flatMap { $0.isEmpty ? nil : $0 } ?? L("Category %d", r + 1) }63 var table: [[Value]] = [[.text(L("Category")), .text(L("Observed")), .text(L("Expected")), .text(L("Residual (O − E)/√E")), .text(L("Contribution to χ²"))]]64 for i in observed.indices {65 let d = observed[i] - expected[i]66 table.append([.text(labels[i]), .number(observed[i]), .finite(expected[i]), .finite(d / expected[i].squareRoot()), .finite(d * d / expected[i])])67 }68 table.append([.text(L("Total")), .number(total), .finite(expected.reduce(0, +)), .blank, .finite(test.statistic)])69 var extra: [ResultBlock] = [70 .table(ReportTable(title: L("Observed and expected counts"), rows: table, columnFormats: [2: .fixed(2), 3: .fixed(4), 4: .fixed(4)])),71 .chart(ChartBlock(title: L("Observed and expected counts"),72 spec: ChartKit.columns(categories: labels, series: [(L("Observed"), observed), (L("Expected"), expected)], xTitle: L("Category"), yTitle: L("Count")),73 summary: L("Grouped bars of observed and expected counts for %d categories.", observed.count), height: 220)),74 ]75 if let m = expected.min(), m < 5 {76 extra.insert(.warning(L("The smallest expected count is %@ (< 5): the χ² approximation may be unreliable.", Fmt.number(m, locale: input.locale))), at: 0)77 }78 return Common.testResult(test, title: L("Chi-Square Goodness of Fit"), subtitle: frame.label + " — " + obsCol.name, alpha: alpha,79 null: L("the observed counts follow the expected distribution"), locale: input.locale, extra: extra, sheetName: L("Goodness of fit"))80 }81}8283/// Chi-square test of independence from a contingency table or from two categorical columns.84enum ChiSquareIndependenceTool {85 static let descriptor: ToolDescriptor = {86 var t = ToolDescriptor(87 id: "compare.chisq-independence", title: L("Chi-Square Independence"), group: Common.groupCompare, systemImage: "tablecells",88 summary: L("Tests whether two categorical variables are independent, from a table of counts or from raw observations."),89 fields: [90 .choice("source", L("Input"), [FieldOption("table", L("Contingency table of counts")), FieldOption("columns", L("Two categorical columns"))]),91 .range("data", L("Data range"), help: nil),92 .column("rowFactor", L("Row variable"), numeric: false, visible: { $0.choice("source") == "columns" }),93 .column("colFactor", L("Column variable"), numeric: false, visible: { $0.choice("source") == "columns" }),94 .toggle("yates", L("Yates continuity correction (2 × 2 tables only)"), false),95 .alpha(L("Significance level α")),96 ],97 info: [98 L("Contingency table: the header row gives the column labels; a first column of text gives the row labels; every numeric column is a column of counts."),99 L("Two categorical columns: the observations are cross-tabulated; rows with a blank value in either column are dropped."),100 L("Expected counts are row total × column total / grand total; df = (r − 1)(c − 1). Cramér's V is computed from the uncorrected χ²."),101 L("Fisher's exact test is not available; with small expected counts interpret the result with caution."),102 ],103 run: run)104 t.demo = { _, v in v.set("data", range: "Groups!H1:K4") }105 return t106 }()107108 /// Cross-tabulates two label columns (levels in order of first appearance).109 static func crossTab(_ a: [String], _ b: [String]) -> (rows: [String], cols: [String], counts: [[Double]], dropped: Int) {110 var rows = [String](), cols = [String](), rowIndex = [String: Int](), colIndex = [String: Int]()111 var cells = [[Int]: Double]()112 var dropped = 0113 for (x, y) in zip(a, b) {114 guard !x.isEmpty, !y.isEmpty else { dropped += 1; continue }115 if rowIndex[x] == nil { rowIndex[x] = rows.count; rows.append(x) }116 if colIndex[y] == nil { colIndex[y] = cols.count; cols.append(y) }117 cells[[rowIndex[x]!, colIndex[y]!], default: 0] += 1118 }119 let counts = rows.indices.map { i in cols.indices.map { cells[[i, $0]] ?? 0 } }120 return (rows, cols, counts, dropped)121 }122123 static func run(_ input: ToolInput) throws -> ToolResult {124 let v = input.values125 let alpha = try Common.alpha(v)126 let frame = try input.frame()127 var rowLabels: [String], colLabels: [String], counts: [[Double]]128 var notes: [String?] = []129 var subtitle = frame.label130 if v.choice("source") == "columns" {131 let a = try input.column("rowFactor"), b = try input.column("colFactor")132 guard a.index != b.index else { throw ToolError(L("Choose two different columns.")) }133 let tab = crossTab(a.labels, b.labels)134 (rowLabels, colLabels, counts) = (tab.rows, tab.cols, tab.counts)135 if tab.dropped > 0 { notes.append(L("%d rows with a blank value were dropped.", tab.dropped)) }136 subtitle += " — " + a.name + " × " + b.name137 } else {138 let numeric = frame.columns.filter(\.isNumeric)139 guard numeric.count >= 2 else { throw ToolError(L("The contingency table needs at least two numeric columns of counts.")) }140 let labelCol = frame.columns.first { !$0.isNumeric && $0.textCount > 0 }141 let (rows, cols) = DataFrame.listwise(numeric.map(\.numbers))142 if rows.count < frame.rowCount { notes.append(L("%d rows with a missing count were dropped.", frame.rowCount - rows.count)) }143 colLabels = numeric.map(\.name)144 rowLabels = rows.map { r in labelCol.map { $0.labels[r] }.flatMap { $0.isEmpty ? nil : $0 } ?? L("Row %d", r + 1) }145 counts = rows.indices.map { i in cols.map { $0[i] } }146 }147 guard rowLabels.count >= 2, colLabels.count >= 2 else { throw ToolError(L("The table needs at least two rows and two columns.")) }148 let result = try Inference.chiSquareIndependence(counts, yatesCorrection: v.bool("yates"))149 func grid(_ title: String, _ m: Matrix, totals: Bool, format: CellFormat) -> ReportTable {150 var rows: [[Value]] = [[.text("")] + colLabels.map { .text($0) } + (totals ? [.text(L("Total"))] : [])]151 for i in rowLabels.indices {152 rows.append([.text(rowLabels[i])] + colLabels.indices.map { .finite(m[i, $0]) } + (totals ? [.finite(result.rowTotals[i])] : []))153 }154 if totals { rows.append([.text(L("Total"))] + result.columnTotals.map { .finite($0) } + [.finite(result.total)]) }155 var t = ReportTable(title: title, rows: rows)156 for c in 1...(colLabels.count + 1) { t.columnFormats[c] = format }157 return t158 }159 var extra: [ResultBlock] = []160 if result.minimumExpected < 5 {161 extra.append(.warning(L("The smallest expected count is %@ (< 5): the χ² approximation may be unreliable.", Fmt.number(result.minimumExpected, locale: input.locale))))162 }163 if v.bool("yates"), !(rowLabels.count == 2 && colLabels.count == 2) { notes.append(L("The Yates correction applies to 2 × 2 tables only and was not used.")) }164 extra += [165 .table(grid(L("Observed counts"), result.observed, totals: true, format: .integer)),166 .table(grid(L("Expected counts under independence"), result.expected, totals: false, format: .fixed(2))),167 .table(grid(L("Pearson residuals (O − E)/√E"), result.residuals, totals: false, format: .fixed(3))),168 .chart(ChartBlock(title: L("Observed counts"),169 spec: ChartKit.columns(categories: rowLabels, series: colLabels.indices.map { j in (colLabels[j], rowLabels.indices.map { result.observed[$0, j] }) },170 xTitle: "", yTitle: L("Count")),171 summary: L("Grouped bars of the observed counts: %d rows by %d columns, %@ observations.", rowLabels.count, colLabels.count,172 Fmt.number(result.total, locale: input.locale)), height: 220)),173 ]174 return Common.testResult(result.test, title: L("Chi-Square Test of Independence"), subtitle: subtitle, alpha: alpha,175 null: L("the row and column variables are independent"), locale: input.locale, notes: notes, extra: extra,176 sheetName: L("Independence"))177 }178}179