TL;DR
This paper introduces salmon, a new Python package designed to make fitting, visualizing, and understanding linear regression models more intuitive and powerful. It bridges the gap between user-friendly statistical software and flexible programming languages by offering an object-oriented approach to model specification and automatic, high-quality visualizations.
What Problem Does This Paper Address?
Linear models are fundamental tools across many scientific disciplines. However, existing solutions in Python often force users to choose between ease of use (like point-and-click software) and flexibility (requiring manual matrix manipulation). While R offers a good balance with its formula syntax, it's not directly usable in Python. Existing Python libraries like statsmodels and scikit-learn have their own limitations: statsmodels can be less intuitive for complex models, and scikit-learn requires manual data transformation and offers limited support for model description and inference. salmon aims to provide a Pythonic, object-oriented solution that combines the expressiveness of R's formulas with the power and extensibility of Python, while also offering superior visualization capabilities.
Methods
salmon introduces an object-oriented approach to specifying linear models. Instead of writing complex formulas as strings, users define model components using composable Python objects. This includes:
- Quantitative Variables: Represented by
Q("variable_name"), allowing direct use of column names from pandas DataFrames. - Categorical Variables: Represented by
C("variable_name"), which automatically handles dummy coding and allows for baseline level specification. - Transformations: Built-in functions like
Log(),Poly(),Sin(),Std(), etc., can be applied directly to variables. - Model Specification: Models are built by combining these objects using standard Python operators (
+for addition,*for interactions). - Automatic Visualizations: The
.plot()method generates publication-quality plots (e.g., scatterplots with fitted lines, confidence bands) tailored to the model type. - Model Building:
salmonincludes astepwisefunction for automated model selection based on metrics like BIC, with options to enforce standard model building practices (e.g., including main effects when interactions are present).
Datasets
The paper uses a sampled version of the Ames housing dataset (De Cock 2011) for demonstration and evaluation. No new benchmarks are introduced; the focus is on showcasing salmon's capabilities with a well-known dataset.
Key Insights and Ideas
- Object-Oriented Model Specification: Moving away from string-based formulas to composable Python objects makes model specification more robust, less error-prone (especially with complex variable names), and more integrated with the Python ecosystem.
- Integrated Visualization: The automatic generation of high-quality plots directly from fitted models significantly simplifies the process of model exploration and diagnostics, a common pain point in purely programmatic approaches.
- Bridging the Gap:
salmoneffectively translates the intuitive formula syntax of R into a Pythonic, object-oriented framework, making advanced statistical modeling more accessible to Python users. - Flexibility in Model Building: The
stepwisefunction, especially with thenaive=Falseoption, offers a principled way to automate model selection while adhering to common statistical modeling conventions.
Key Results
- Ease of Use: The paper demonstrates how
salmonsimplifies the specification of various linear models, from simple linear regression to complex models with polynomial terms, interactions, and transformations, often with more concise code than R or other Python libraries. - Visualization Quality: The
.plot()function produces clear and informative visualizations, including confidence bands, that would require significant manual effort in other packages. - Performance: Timing comparisons show
salmonis consistently faster thanstatsmodelsand often faster than R for larger models, attributed to efficient linear algebra handling. - Numerical Stability:
salmondemonstrates superior numerical stability compared tostatsmodelsand R when dealing with collinearity issues in synthetic data, correctly recovering coefficients where others fail.
Limitations and Future Work
- Current Scope: The paper focuses on linear regression. Future work aims to extend
salmonto other model types, such as generalized linear models. - Additional Tests: The authors plan to incorporate more statistical tests supported in R, like linear contrasts, into
salmon. - Model Building Practices: While
salmon'sstepwisefunction can enforce standard practices, the paper notes that automated model selection can still lead to models that violate some best practices if not carefully managed.
Summary updated