help nnls

Title

nnls -- Non-negative least squares in Stata

Syntax

nnls
depvar
indepvars
[if] [in],
[graph
graph_save(graphname)
standardize]

Inputs

depvar: numerical variable

indepvars: list of numerical variables representing the features. When a feature is categorical,
please generate the categorical dummies related to this feature. As the command does not do it by default,
it is user's responsibility to generate the appropriate dummies.

Description

nnls is a command implementing non-negative least squares (NNLS) in Stata on top of Python using the Python function "nnls()". NNLS is a type of constrained least squares problem where the coefficients are not allowed to become negative. Generally, NNLS provides regularized (or sparse) solution, meaning that only a few parameters of the least squares regression are allowed to be positive and non-zero.

Options

options

graph generates the importance index bar graph

standardize compute NNLS on z-standardized variables (zero mean and unit variance)

Returns: general

Macros
e(num_features) number of features

e(features) names of the features

e(depvar) name of the dependent variable

Matrices
e(Weights) matrix of the weights

e(Std_Weights) matrix of the standardized weights (i.e., summing up to 1)

Remarks

Remark 1: In order to execute this program, it is necessary to have both Stata 16 or newer versions and Python installed, starting from version 2.7 onwards. Detailed instructions for installing Python on your machine can be found at: https://www.python.org/downloads. We highly recommend using the Anaconda distribution for Python, which can be installed from: https://docs.anaconda.com/free/anaconda/install/index.html#. Additionally, prior to running the command, it is essential to ensure that the Python "SciPy" package and its related dependencies, as well as the Stata Function Interface (sfi) APIs, are installed.

Remark 2. Please, consider to keep updated with future versions of this command.

Example

Example: Non-negative least squares applied to the Boston dataset

Load initial dataset from ancillary file

use boston, clear

Set the outcome

global y "medv"

Set the features

global X "crim zn indus age lstat black"

Run "nnls" using unstandardized variables

nnls $y $X , graph graph_save("my_graph")

Generate predictions

predict PRED_ustd

Clear all the Stata environment

clear all

Load again initial dataset from ancillary file

use boston, clear

Run "nnls" using standardized variables

nnls $y $X , graph graph_save("my_graph") standardize

Generate predictions

predict PRED_std

References

Bro, R. and De Jong, S. 1997. A fast non-negativity-constrained least squares algorithm. Journal of Chemometrics, 11, 393-401.

Cerulli, G. 2023. Fundamentals of Supervised Machine Learning: With Applications in Python, R, and Stata, Springer.

Acknowledgment

The development of this software was supported by FOSSR (Fostering Open Science in Social Science Research), a project funded by the European Union - NextGenerationEU under the NPRR Grant agreement n. MURIR0000008.

Author

Giovanni Cerulli

IRCrES-CNR

Research Institute for Sustainable Economic Growth, National Research Council of Italy

E-mail: giovanni.cerulli@ircres.cnr.it

Also see

Online: regress, lasso