The command netivreg
implements the Generalized Three-Stage Least Squares (G3SLS) estimator developed in
Estrada (2022), 'Causal Inference in Multilayered Networks,' Ph.D. Dissertation and the Generalized Method of Moments (GMM) estimator in Chan, T. J., J. Estrada, K. P. Huynh, D. T. Jacho-Chávez, C. T. Lam, and L. Sánchez-Aragón (2024) 'On the Estimation of Social Effects with Observational Network Data and Random Assignment,' Journal of Econometric Methods for the endogenous linear-in-means model.
The two procedures utilize full observability of a two-layered multiplex network data structure using Stata's new multiframes capabilities
and Python integration (version 16 or above).
Authors:
Pablo Estrada
Emory University, Atlanta, GA, USAJuan Estrada
Analysis Group Economic Consulting, Washington, DC, USAKim Huynh
Bank of Canada, Ottawa, Ontario, CanadaDavid Jacho-Chávez
Emory University, Atlanta, GA, USALeonardo Sánchez-Aragón
ESPOL University, Guayaquil, Guayas, Ecuador
Stata 16.0 or above, and Python 3.9 or above are needed as well as the following Python packages,
Python Package | Version |
---|---|
networkX | 3.2.x |
numpy | 1.26.x |
pandas | 2.2.x |
scikit-learn | 1.5.x |
scipy | 1.13.x |
-
Install Python via Anaconda:
Download and install Anaconda on your computer, which comes with Python 3.9 or higher. The Anaconda distribution is highly recommended to ensure compatibility with
netivreg
.Note: On Windows, Anaconda is typically installed in the
C:\Users\<username>\anaconda3
folder. -
Create a Python environment for netivreg:
To ensure backward compatibility and avoid version conflicts, we recommend you create a dedicated environment for
netivreg
with the required Python packages. Do this by first, openning the Anaconda prompt and run the following command:conda create -n netivreg_env python=3.9 pip networkx=3.2 numpy=1.26 pandas=2.2 scikit-learn=1.5 scipy=1.13
This command creates a conda environment named
netivreg_env
with Python 3.9 and the necessary packages with the compatible versions.Note: On Windows, this environment is typically created within the
C:\Users\<username>\anaconda3\envs
folder. Insideenvs
, a subfolder namednetivreg_env
will hold all files for this specific environment. -
Activate the Python environment for netivreg:
Run the following command on the Anaconda prompt:
conda activate netivreg_env
-
Directing Stata to use the crated Python environment:
After completing steps 1–3 above, link Python to your local Stata installation. Open Stata and use the following command in the Stata command window to set the Python version:
Note: On Windows, one would write
python set exec "C:\Users\<username>\anaconda3\envs\netivreg_env\python.exe" , permanently
Replace
<username>
with your actual username in the path. This command tells Stata to use the specified Python executable within the Anaconda environmentnetivreg_env
. Setting it permanently means Stata will remember this configuration for future sessions. Otherwise, you will need to set this address every time you would like to use thenetivreg
command.
-
Copy
netivreg
files to the Stata ado folder:- Locate the
netivreg.ado
andnetivreg.sthlp
files in theFiles
folder within this repository. - Copy these files to the
/ado/plus/n
folder in your local user installation directory. For example, on Windows, this path would beC:/Users/<username>/ado/plus/n
. This folder is automatically created when you install any user-written Stata package.
- Locate the
-
Copy Python Files to the Stata ado/plus/py Folder:
- Locate the
ado/plus/py
folder in your local user installation directory. For example,C:/Users/<username>/ado/plus/py
in Windows. If this folder does not exist, you can create it. - Inside the
ado/plus/py
folder, create a new folder namednetivreg
. - Locate the
__init__.py
,g3sls.py
andgmm.py
files in theFiles
folder within this repository. - Copy these files to the
ado/plus/py/netivreg
folder.
Optional Setup: Directly Adding Python Files to
ado/plus/py
(We thank Stata for making this suggestion.)- Change the content of
__init__.py
to the following:
__version__ = "1.0.0" from g3sls import * from gmm import *
-
Previously, we used
from .g3sls import *
andfrom .gmm import *
, with a.
prefix. This update removes the.
to simplify the import statements. -
Rename the new
__init__.py
tonetivreg.py
. -
Copy the
netivreg.py
,g3sls.py
, andgmm.py
into the folderado/plus/py
. Now, you do not need to create the folder namednetivreg
.
- Locate the
For Windows users, please refer to the following instructional video
-
To recover the location path of the environment
netivreg_env
, the user can run the following command in the command prompt,conda env list
-
To desactive an environment, in your Aanconda Prompt run
conda desactivate
In Files
folder within this repository, there is a do-file named manuscript_output.do
that replicates the tables from the manuscript 'netivreg
: Estimation of Peer Effects in Endogenous Social Networks' using the datasets found in the same Files
folder in this repository.