Repository Public
1import Foundation2import SheetCore3import Statistics4import AnalysisKit56/// Dependent variable and regressor matrix built from the chosen columns after listwise deletion.7struct RegressionDesign {8 var frame: DataFrame9 var yName: String10 var y: [Double]11 var x: Matrix12 var names: [String]13 /// Offsets of the rows used, inside the frame.14 var rows: [Int]15 /// Reference level of each categorical regressor.16 var baselines: [(variable: String, level: String)]17 /// Extra numeric columns restricted to the used rows (weights…), keyed by field id.18 var numericExtras: [String: [Double]] = [:]19 /// Extra label columns restricted to the used rows (cluster / entity / time ids), keyed by field id.20 var labelExtras: [String: [String]] = [:]21 var labelExtraNames: [String: String] = [:]2223 /// - Parameters:24 /// - numericExtras / labelExtras: field ids of further columns that must be present on a row for it to be used.25 static func build(_ input: ToolInput, y yID: String = "y", x xID: String = "x", categorical catID: String? = "cat",26 numericExtras: [String] = [], labelExtras: [String] = []) throws -> RegressionDesign {27 let frame = try input.frame()28 let yCol = try input.column(yID)29 let cats = catID.map { id in input.values.columnIndices(id).filter { $0 >= 0 && $0 < frame.columns.count && $0 != yCol.index }.sorted().map { frame.columns[$0] } } ?? []30 let xAll = input.values.columnIndices(xID).filter { $0 >= 0 && $0 < frame.columns.count }.sorted().map { frame.columns[$0] }31 .filter { c in c.index != yCol.index && !cats.contains { $0.index == c.index } }32 let numeric = numericExtras.compactMap { id in input.optionalColumn(id).map { (id, $0) } }33 let labelled = labelExtras.compactMap { id in input.optionalColumn(id).map { (id, $0) } }34 let reserved = Set(numeric.map { $0.1.index } + labelled.map { $0.1.index })35 let xCols = xAll.filter { !reserved.contains($0.index) }36 guard !xCols.isEmpty || !cats.isEmpty else { throw ToolError(L("Choose at least one regressor other than the dependent variable.")) }37 let catLabels = cats.map(\.labels), extraLabels = labelled.map { $0.1.labels }38 let rows = (0..<frame.rowCount).filter { r in39 !yCol.numbers[r].isNaN && xCols.allSatisfy { !$0.numbers[r].isNaN } && catLabels.allSatisfy { !$0[r].isEmpty }40 && numeric.allSatisfy { !$0.1.numbers[r].isNaN } && extraLabels.allSatisfy { !$0[r].isEmpty }41 }42 guard rows.count >= 3 else { throw ToolError(L("Only %d complete rows: not enough observations.", rows.count)) }43 var builder = DesignBuilder()44 for c in xCols { builder.addNumeric(c.name, rows.map { c.numbers[$0] }) }45 var baselines = [(String, String)]()46 for (c, labels) in zip(cats, catLabels) {47 let values = rows.map { labels[$0] }48 guard Set(values).count >= 2 else { throw ToolError(L("The categorical regressor “%@” has a single level in the rows used.", c.name)) }49 try builder.addCategorical(c.name, values)50 if let b = builder.baselines[c.name] { baselines.append((c.name, b)) }51 }52 var d = RegressionDesign(frame: frame, yName: yCol.name, y: rows.map { yCol.numbers[$0] }, x: try builder.matrix(), names: builder.names,53 rows: rows, baselines: baselines)54 for (id, c) in numeric { d.numericExtras[id] = rows.map { c.numbers[$0] } }55 for ((id, c), labels) in zip(labelled, extraLabels) { d.labelExtras[id] = rows.map { labels[$0] }; d.labelExtraNames[id] = c.name }56 return d57 }5859 var notes: [String] {60 var out = [Sup.listwiseNote(used: rows.count, of: frame.rowCount)]61 for b in baselines { out.append(L("“%@” enters as indicator variables; reference level: %@.", b.variable, b.level)) }62 return out63 }64}65