SPB Git

Repository Public

ANOVA.swift 9.6 KB 155 lines swift

1import Foundation2import SheetCore34/// One line of an analysis-of-variance table.5public struct AnovaRow: Sendable, Hashable, Codable {6    public var source: String7    public var sumOfSquares: Double8    public var df: Double9    /// Mean square = SS/df (nil on the Total line).10    public var meanSquare: Double?11    public var f: Double?12    /// Upper-tail p-value of F(df, df_error).13    public var pValue: Double?14    /// η² = SS_effect / SS_total.15    public var etaSquared: Double?16    /// Partial η² = SS_effect / (SS_effect + SS_error).17    public var partialEtaSquared: Double?18}1920/// Analysis-of-variance table with group descriptives.21public struct AnovaResult: Sendable, Hashable, Codable, TableReport {22    public var name: String23    public var rows: [AnovaRow]24    /// (label, n, mean, variance) of each group or factor level.25    public var groups: [GroupSummary]26    public var assumptions: [String]2728    public struct GroupSummary: Sendable, Hashable, Codable {29        public var label: String30        public var count: Int31        public var mean: Double32        public var variance: Double33    }3435    /// The row for a source of variation, e.g. "Between Groups", "Rows", "Columns", "Interaction".36    public func row(_ source: String) -> AnovaRow? { rows.first { $0.source == source } }3738    public func table() -> [[Value]] {39        var out: [[Value]] = [[.text(name)], [.text("Groups"), .text("Count"), .text("Mean"), .text("Variance")]]40        for g in groups { out.append([.text(g.label), Report.int(g.count), Report.num(g.mean), Report.num(g.variance)]) }41        out.append([.blank])42        out.append(["Source of Variation", "SS", "df", "MS", "F", "P-value (upper tail)", "η²"].map { Value.text($0) })43        for r in rows {44            out.append([.text(r.source), Report.num(r.sumOfSquares), Report.num(r.df), Report.num(r.meanSquare), Report.num(r.f),45                        Report.num(r.pValue), Report.num(r.etaSquared)])46        }47        for (i, a) in assumptions.enumerated() { out.append([.text(i == 0 ? "Assumptions" : ""), .text(a)]) }48        return Report.rectangular(out)49    }50}5152extension Inference {53    private static func effect(_ source: String, ss: Double, df: Double, ssError: Double, dfError: Double, ssTotal: Double) -> AnovaRow {54        let ms = ss / df, f = ms / (ssError / dfError)55        return AnovaRow(source: source, sumOfSquares: ss, df: df, meanSquare: ms, f: f, pValue: FisherFDistribution(d1: df, d2: dfError).sf(f),56                        etaSquared: ss / ssTotal, partialEtaSquared: ss / (ss + ssError))57    }5859    private static let anovaAssumptions = ["Observations are independent", "Residuals are normal within each group",60                                           "Groups have equal variances (check with Levene's test)"]6162    /// One-way (single-factor) ANOVA. Rows: "Between Groups", "Within Groups", "Total".63    /// F = MS_between / MS_within with (k−1, N−k) df; p-value upper tail; η² = SS_between / SS_total.64    public static func oneWayANOVA(_ groups: [[Double]], labels: [String]? = nil) throws -> AnovaResult {65        guard groups.count >= 2 else { throw StatError.insufficientData("ANOVA needs at least two groups") }66        let gs = try groups.enumerated().map { try clean($1, min: 1, "group \($0 + 1)") }67        let all = gs.flatMap { $0 }68        let n = Double(all.count), k = Double(gs.count)69        guard n > k else { throw StatError.insufficientData("ANOVA needs more observations than groups") }70        let grand = Descriptive.mean(all)71        var ssb = 0.0, ssw = 0.072        for g in gs {73            let m = Descriptive.mean(g)74            ssb += Double(g.count) * (m - grand) * (m - grand)75            ssw += Descriptive.sumSquaredDeviations(g)76        }77        guard ssw > 0 else { throw StatError.invalidInput("no within-group variation: F is undefined") }78        let sst = ssb + ssw79        let summaries = gs.enumerated().map { i, g in80            AnovaResult.GroupSummary(label: labels?[i] ?? "Group \(i + 1)", count: g.count, mean: Descriptive.mean(g), variance: Descriptive.variance(g))81        }82        return AnovaResult(name: "One-way ANOVA", rows: [83            effect("Between Groups", ss: ssb, df: k - 1, ssError: ssw, dfError: n - k, ssTotal: sst),84            AnovaRow(source: "Within Groups", sumOfSquares: ssw, df: n - k, meanSquare: ssw / (n - k)),85            AnovaRow(source: "Total", sumOfSquares: sst, df: n - 1),86        ], groups: summaries, assumptions: anovaAssumptions)87    }8889    /// Two-way ANOVA without replication (randomised blocks): `data[i][j]` is the single observation90    /// for row level i and column level j. Rows: "Rows", "Columns", "Error", "Total"; each effect is91    /// tested against the error (interaction) mean square.92    public static func twoWayANOVA(_ data: [[Double]]) throws -> AnovaResult {93        let r = data.count, c = data.first?.count ?? 094        guard r >= 2, c >= 2, data.allSatisfy({ $0.count == c }) else { throw StatError.invalidInput("need a rectangular table with at least 2 rows and 2 columns") }95        let m = Matrix(try data.map { try clean($0, min: 1, "row") })96        let grand = Descriptive.mean(m.data)97        let rowMeans = (0..<r).map { Descriptive.mean(m.row($0)) }, colMeans = (0..<c).map { Descriptive.mean(m.column($0)) }98        let ssr = Double(c) * rowMeans.reduce(0) { $0 + ($1 - grand) * ($1 - grand) }99        let ssc = Double(r) * colMeans.reduce(0) { $0 + ($1 - grand) * ($1 - grand) }100        let sst = Descriptive.sumSquaredDeviations(m.data)101        var sse = 0.0102        for i in 0..<r { for j in 0..<c { let e = m[i, j] - rowMeans[i] - colMeans[j] + grand; sse += e * e } }103        guard sse > 0 else { throw StatError.invalidInput("no residual variation: F is undefined") }104        let dfe = Double((r - 1) * (c - 1))105        var groups = (0..<r).map { AnovaResult.GroupSummary(label: "Row \($0 + 1)", count: c, mean: rowMeans[$0], variance: Descriptive.variance(m.row($0))) }106        groups += (0..<c).map { AnovaResult.GroupSummary(label: "Column \($0 + 1)", count: r, mean: colMeans[$0], variance: Descriptive.variance(m.column($0))) }107        return AnovaResult(name: "Two-way ANOVA without replication", rows: [108            effect("Rows", ss: ssr, df: Double(r - 1), ssError: sse, dfError: dfe, ssTotal: sst),109            effect("Columns", ss: ssc, df: Double(c - 1), ssError: sse, dfError: dfe, ssTotal: sst),110            AnovaRow(source: "Error", sumOfSquares: sse, df: dfe, meanSquare: sse / dfe),111            AnovaRow(source: "Total", sumOfSquares: sst, df: Double(r * c - 1)),112        ], groups: groups, assumptions: anovaAssumptions + ["No interaction between the two factors (it cannot be separated from error without replication)"])113    }114115    /// Two-way ANOVA with replication for a BALANCED design: `cells[i][j]` holds the replicates for116    /// row level i and column level j, the same number (≥ 2) in every cell. Rows: "Rows" (factor A),117    /// "Columns" (factor B), "Interaction", "Within", "Total"; fixed-effects F tests against "Within".118    public static func twoWayANOVA(replicated cells: [[[Double]]]) throws -> AnovaResult {119        let a = cells.count, b = cells.first?.count ?? 0120        guard a >= 2, b >= 2, cells.allSatisfy({ $0.count == b }) else { throw StatError.invalidInput("need at least 2 levels of each factor") }121        let reps = cells[0][0].count122        guard reps >= 2, cells.allSatisfy({ $0.allSatisfy { $0.count == reps && !$0.contains(where: \.isNaN) } }) else {123            throw StatError.invalidInput("the design must be balanced: the same number (≥ 2) of non-missing replicates in every cell")124        }125        let all = cells.flatMap { $0.flatMap { $0 } }126        let grand = Descriptive.mean(all)127        let cellMeans = cells.map { $0.map { Descriptive.mean($0) } }128        let rowMeans = (0..<a).map { i in Descriptive.mean(cells[i].flatMap { $0 }) }129        let colMeans = (0..<b).map { j in Descriptive.mean((0..<a).flatMap { cells[$0][j] }) }130        let n = Double(reps)131        let ssa = n * Double(b) * rowMeans.reduce(0) { $0 + ($1 - grand) * ($1 - grand) }132        let ssb = n * Double(a) * colMeans.reduce(0) { $0 + ($1 - grand) * ($1 - grand) }133        var ssab = 0.0, sse = 0.0134        for i in 0..<a {135            for j in 0..<b {136                let d = cellMeans[i][j] - rowMeans[i] - colMeans[j] + grand137                ssab += n * d * d138                sse += Descriptive.sumSquaredDeviations(cells[i][j])139            }140        }141        guard sse > 0 else { throw StatError.invalidInput("no within-cell variation: F is undefined") }142        let sst = Descriptive.sumSquaredDeviations(all)143        let dfe = Double(a * b * (reps - 1))144        var groups = (0..<a).map { i in AnovaResult.GroupSummary(label: "Row \(i + 1)", count: b * reps, mean: rowMeans[i], variance: Descriptive.variance(cells[i].flatMap { $0 })) }145        groups += (0..<b).map { j in AnovaResult.GroupSummary(label: "Column \(j + 1)", count: a * reps, mean: colMeans[j], variance: Descriptive.variance((0..<a).flatMap { cells[$0][j] })) }146        return AnovaResult(name: "Two-way ANOVA with replication", rows: [147            effect("Rows", ss: ssa, df: Double(a - 1), ssError: sse, dfError: dfe, ssTotal: sst),148            effect("Columns", ss: ssb, df: Double(b - 1), ssError: sse, dfError: dfe, ssTotal: sst),149            effect("Interaction", ss: ssab, df: Double((a - 1) * (b - 1)), ssError: sse, dfError: dfe, ssTotal: sst),150            AnovaRow(source: "Within", sumOfSquares: sse, df: dfe, meanSquare: sse / dfe),151            AnovaRow(source: "Total", sumOfSquares: sst, df: Double(a * b * reps - 1)),152        ], groups: groups, assumptions: anovaAssumptions + ["Balanced design (equal replicates per cell)"])153    }154}155
Files main
Menu

Theme