[Paper Review] pystacked: Stacking generalization and machine learning in Stata
pystacked is a Stata command that implements stacking generalization—combining multiple machine learning models—using Python's scikit-learn. It supports regression and binary classification by training diverse base learners (e.g., random forests, gradient boosting, neural networks), using cross-validation to generate meta-features, and fitting a final weighted average model, which achieves superior predictive performance, as shown by AUC of 0.997 in a spam classification task.
The pystacked command implements stacked generalization (Wolpert, 1992, Neural Networks 5: 241–259) for regression and binary classification via Python’s scikit-learn. Stacking combines multiple supervised machine learners— the “base” or “level-0” learners—into one learner. The currently supported base learners include regularized regression, random forest, gradient boosted trees, support vector machines, and feed-forward neural nets (multilayer perceptron). pystacked can also be used as a “regular” machine learning program to fit one base learner and thus provides an easy-to-use application programming interface for scikit-learn’s machine learning algorithms.
Motivation & Objective
- To provide Stata users with a seamless interface to stacking generalization, a powerful ensemble method for improving predictive accuracy.
- To extend Stata’s machine learning capabilities by integrating scikit-learn’s diverse algorithms, including regularized regression, random forests, gradient boosting, SVMs, and neural networks.
- To support both stacked generalization and standalone training of individual learners through a unified, user-friendly API.
- To facilitate causal inference applications by enabling integration with DDML (Double-Debiased Machine Learning) via cross-fitting and robust prediction models.
- To enhance reproducibility and usability by supporting sparse matrices, custom pipelines, and parallelization in a Stata-native environment.
Proposed method
- Uses cross-validation to generate out-of-sample predictions ("meta-features") from multiple base learners to prevent overfitting in the stacking process.
- Employs a final-level learner (typically constrained least squares) to predict the true outcome using the cross-validated predictions from base models as input features.
- Applies non-negative, sum-to-one constraints on stacking weights to ensure interpretability as a weighted average of base models.
- Supports model-specific preprocessing via scikit-learn pipelines (e.g., polynomial expansion for logistic regression).
- Enables parallelization via the njobs() option to speed up training of computationally intensive models like gradient boosting and neural networks.
- Provides two syntax variants: a flexible, modular syntax and a simplified command-line format for ease of use.
Experimental results
Research questions
- RQ1Can stacking generalization in Stata significantly improve predictive performance over individual machine learning models in regression and classification tasks?
- RQ2How does pystacked’s integration with scikit-learn enhance Stata’s machine learning ecosystem compared to existing tools?
- RQ3To what extent does stacking reduce prediction error compared to individual base learners in real-world datasets like the spam classification task?
- RQ4How does pystacked support advanced use cases such as causal inference through compatibility with DDML and cross-fitting?
- RQ5What are the practical trade-offs between model diversity, computational cost, and predictive accuracy when combining multiple learners?
Key findings
- In the spam classification task, stacking achieved an out-of-sample AUC of 0.9969, significantly outperforming logistic regression (AUC 0.9133) and individual gradient boosting models (AUC 0.9973 and 0.9562).
- Gradient boosting with 600 trees received the highest stacking weight (0.482), indicating it contributed most to the final prediction, followed by another gradient boosting model with 1000 trees (weight 0.345).
- The final stacking model achieved a test accuracy of 94.9% (678 true positives and 397 true negatives out of 1133 holdout observations), compared to 71.2% for logistic regression.
- The confusion matrix showed that stacking had only 29 false positives and 29 false negatives in the holdout set, compared to 294 and 32 for logistic regression, respectively.
- pystacked successfully integrated diverse models—including neural networks with different architectures—into a single, high-performing ensemble without requiring manual model combination.
- The program’s support for sparse matrices, custom pipelines, and parallelization (via njobs) enabled efficient training and deployment on large datasets within Stata.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.