How to Install Statsmodels in Python?
Last Updated :
31 Jan, 2025
Statsmodels is a Python library that enables us to estimate and analyze various statistical models. It is built on numeric and scientific libraries like NumPy and SciPy. It provides classes & functions for the estimation of many different statistical models.
Before installing Statsmodels, ensure that you have:
- Python installed (version 3.x recommended)
- pip (Python package manager) installed
- A virtual environment (optional but recommended for better package management)
Installing Statsmodels in Windows
1. Installing Statsmodels using pip
The easiest way to install Statsmodels is by using pip. Run the following command in your terminal or command prompt:
python -m venv env
.\env\Scripts\activate
pip install statsmodels
This will automatically install Statsmodels with its dependencies including NumPy, SciPy, Pandas, and Patsy. Patsy is used for handling formulas in statistical models.
2. Installing Statsmodels via Conda (Anaconda Users)
If you use Anaconda, you can install Statsmodels using the Conda package manager:
conda install -c conda-forge statsmodels
To continue with installation type 'y':
We have successfully installed statsmodel library.
Verifying the Installation
After installation, you can verify that Statsmodels is working correctly by running the following Python script:
Python
import statsmodels.api as sm
print("Statsmodels version:", sm.__version__)
Output
Statsmodels version: 0.14.4
Installing Statsmodels in Linux
1. Installing Statsmodels using pip
Follow the below steps to install statsmodels in Python on Linux using pip:
$ python3 -m venv StatsM
$ source ./StatsM/bin/activate
$ pip install statsmodels
Hence, installation is successful.
2. Installing Statsmodels using conda
To install Statsmodels in Python on Linux using Conda, follow these steps:
Step 1: Download and Install Miniconda
- Create a directory for Miniconda installation and download the Miniconda installer using the following commands:
mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
- Run the Miniconda installer to install Miniconda and remove the installer script:
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh
Step 2: Activate the Conda Base Environment using following command:
source ~/miniconda3/bin/activate
Note: The (base) in the terminal prompt indicates that you have successfully entered the Conda environment. The base Conda environment is now active. You can install, manage, or run packages within this environment. Any Python commands executed will use the Conda-installed Python and dependencies.
Verify the active Conda environment by running:
conda info --envs
Step 3: Install Statsmodels in the Conda environment using the following command:
conda install -c conda-forge statsmodels
This will display the installed version of Statsmodels, confirming that the installation was successful.
Step 4: Verify the Installation of Statsmodels by running the following command:
import statsmodels.api as sm
print(sm.__version__)
By following these steps, you can successfully install Statsmodels in a Conda environment on Linux.