:hero: examples and tutorials to get started with statsmodels

:orphan:

.. _statsmodels-examples:

Examples
========

This page provides a series of examples, tutorials and recipes to help you get
started with ``statsmodels``. Each of the examples shown here is made available
as an IPython Notebook and as a plain python script on the `statsmodels github
repository <https://github.com/statsmodels/statsmodels/tree/main/examples>`_.

We also encourage users to submit their own examples, tutorials or cool
`statsmodels` trick to the `Examples wiki page
<https://github.com/statsmodels/statsmodels/wiki/Examples>`_

.. toctree::







.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Linear Regression Models
------------------------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/ols
   notebooks/generated/gls
   notebooks/generated/quantile_regression
   notebooks/generated/recursive_ls
   notebooks/generated/rolling_ls
   notebooks/generated/regression_diagnostics
   notebooks/generated/wls
   notebooks/generated/mixed_lm_example
   notebooks/generated/variance_components



.. container:: example

   `Ordinary Least Squares <notebooks/generated/ols.html>`_

   .. image:: ../_static/images/ols.png
      :target: notebooks/generated/ols.html
      :width: 240px


.. container:: example

   `Generalized Least Squares <notebooks/generated/gls.html>`_

   .. image:: ../_static/images/gls.png
      :target: notebooks/generated/gls.html
      :width: 240px


.. container:: example

   `Quantile Regression <notebooks/generated/quantile_regression.html>`_

   .. image:: ../_static/images/quantile_regression.png
      :target: notebooks/generated/quantile_regression.html
      :width: 240px


.. container:: example

   `Recursive Least Squares <notebooks/generated/recursive_ls.html>`_

   .. image:: ../_static/images/recursive_ls.png
      :target: notebooks/generated/recursive_ls.html
      :width: 240px


.. container:: example

   `Rolling Least Squares <notebooks/generated/rolling_ls.html>`_

   .. image:: ../_static/images/rolling_ls.png
      :target: notebooks/generated/rolling_ls.html
      :width: 240px


.. container:: example

   `Regression Diagnostics <notebooks/generated/regression_diagnostics.html>`_

   .. image:: ../_static/images/regression_diagnostics.png
      :target: notebooks/generated/regression_diagnostics.html
      :width: 240px


.. container:: example

   `Weighted Least Squares <notebooks/generated/wls.html>`_

   .. image:: ../_static/images/wls.png
      :target: notebooks/generated/wls.html
      :width: 240px


.. container:: example

   `Linear Mixed-Effects <notebooks/generated/mixed_lm_example.html>`_

   .. image:: ../_static/images/mixed_lm.png
      :target: notebooks/generated/mixed_lm_example.html
      :width: 240px


.. container:: example

   `Variance Component Analysis <notebooks/generated/variance_components.html>`_

   .. image:: ../_static/images/variance_components.png
      :target: notebooks/generated/variance_components.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Plotting
--------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/regression_plots
   notebooks/generated/linear_regression_diagnostics_plots
   notebooks/generated/categorical_interaction_plot
   notebooks/generated/plots_boxplots



.. container:: example

   `Regression Plots <notebooks/generated/regression_plots.html>`_

   .. image:: ../_static/images/regression_plots.png
      :target: notebooks/generated/regression_plots.html
      :width: 240px


.. container:: example

   `Linear Regression Diagnostic <notebooks/generated/linear_regression_diagnostics_plots.html>`_

   .. image:: ../_static/images/linear_regression_diagnostics_plots.png
      :target: notebooks/generated/linear_regression_diagnostics_plots.html
      :width: 240px


.. container:: example

   `Categorical Interactions <notebooks/generated/categorical_interaction_plot.html>`_

   .. image:: ../_static/images/categorical_interaction_plot.png
      :target: notebooks/generated/categorical_interaction_plot.html
      :width: 240px


.. container:: example

   `Box Plots <notebooks/generated/plots_boxplots.html>`_

   .. image:: ../_static/images/plots_boxplots.png
      :target: notebooks/generated/plots_boxplots.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Discrete Choice Models
----------------------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/discrete_choice_overview
   notebooks/generated/discrete_choice_example
   notebooks/generated/ordinal_regression
   notebooks/generated/count_hurdle
   notebooks/generated/postestimation_poisson



.. container:: example

   `Getting Started <notebooks/generated/discrete_choice_overview.html>`_

   .. image:: ../_static/images/discrete_overview.png
      :target: notebooks/generated/discrete_choice_overview.html
      :width: 240px


.. container:: example

   `Fair's Affairs Data <notebooks/generated/discrete_choice_example.html>`_

   .. image:: ../_static/images/fairs_data.png
      :target: notebooks/generated/discrete_choice_example.html
      :width: 240px


.. container:: example

   `Ordinal Regression <notebooks/generated/ordinal_regression.html>`_

   .. image:: ../_static/images/ordered_model.png
      :target: notebooks/generated/ordinal_regression.html
      :width: 240px


.. container:: example

   `Hurdel count Models <notebooks/generated/count_hurdle.html>`_

   .. image:: ../_static/images/count_hurdle.png
      :target: notebooks/generated/count_hurdle.html
      :width: 240px


.. container:: example

   `Post-estimation for Poisson Regression <notebooks/generated/postestimation_poisson.html>`_

   .. image:: ../_static/images/post_estimation.png
      :target: notebooks/generated/postestimation_poisson.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Nonparametric Statistics
------------------------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/kernel_density
   notebooks/generated/lowess



.. container:: example

   `Univariate Kernel Density Estimator <notebooks/generated/kernel_density.html>`_

   .. image:: ../_static/images/kde_overview.png
      :target: notebooks/generated/kernel_density.html
      :width: 240px


.. container:: example

   `Lowess Regression <notebooks/generated/lowess.html>`_

   .. image:: ../_static/images/lowess.png
      :target: notebooks/generated/lowess.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Generalized Linear Models
-------------------------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/glm
   notebooks/generated/glm_formula
   notebooks/generated/glm_weights
   notebooks/generated/influence_glm_logit
   notebooks/generated/quasibinomial



.. container:: example

   `Generalized Linear Models Overview <notebooks/generated/glm.html>`_

   .. image:: ../_static/images/glm_overview.png
      :target: notebooks/generated/glm.html
      :width: 240px


.. container:: example

   `Using Formulas with GLMs <notebooks/generated/glm_formula.html>`_

   .. image:: ../_static/images/glm_formulas.png
      :target: notebooks/generated/glm_formula.html
      :width: 240px


.. container:: example

   `Weighting Observations with GLMs <notebooks/generated/glm_weights.html>`_

   .. image:: ../_static/images/glm_weights.png
      :target: notebooks/generated/glm_weights.html
      :width: 240px


.. container:: example

   `Influence Measures for GLMs <notebooks/generated/influence_glm_logit.html>`_

   .. image:: ../_static/images/influence_glm_logit.png
      :target: notebooks/generated/influence_glm_logit.html
      :width: 240px


.. container:: example

   `Quasi-binomial regression <notebooks/generated/quasibinomial.html>`_

   .. image:: ../_static/images/quasibinomial.png
      :target: notebooks/generated/quasibinomial.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Robust Regression
-----------------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/robust_models_1
   notebooks/generated/robust_models_0



.. container:: example

   `M-estimators for Robust Regression <notebooks/generated/robust_models_1.html>`_

   .. image:: ../_static/images/rlm_overview.png
      :target: notebooks/generated/robust_models_1.html
      :width: 240px


.. container:: example

   `Comparing OLS and RLM <notebooks/generated/robust_models_0.html>`_

   .. image:: ../_static/images/rlm.png
      :target: notebooks/generated/robust_models_0.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Generalized Estimating Equations
--------------------------------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/gee_nested_simulation
   notebooks/generated/gee_score_test_simulation



.. container:: example

   `GEE Nested Covariance Structure <notebooks/generated/gee_nested_simulation.html>`_

   .. image:: ../_static/images/gee-cov-struct.png
      :target: notebooks/generated/gee_nested_simulation.html
      :width: 240px


.. container:: example

   `GEE Score Tests <notebooks/generated/gee_score_test_simulation.html>`_

   .. image:: ../_static/images/gee-score-tests.png
      :target: notebooks/generated/gee_score_test_simulation.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Statistics
----------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/interactions_anova
   notebooks/generated/stats_poisson
   notebooks/generated/stats_rankcompare
   notebooks/generated/metaanalysis1
   notebooks/generated/mediation_survival
   notebooks/generated/treatment_effect
   notebooks/generated/copula



.. container:: example

   `ANOVA <notebooks/generated/interactions_anova.html>`_

   .. image:: ../_static/images/anova.png
      :target: notebooks/generated/interactions_anova.html
      :width: 240px


.. container:: example

   `Poisson rates <notebooks/generated/stats_poisson.html>`_

   .. image:: ../_static/images/stats_poisson.png
      :target: notebooks/generated/stats_poisson.html
      :width: 240px


.. container:: example

   `Rank Comparison <notebooks/generated/stats_rankcompare.html>`_

   .. image:: ../_static/images/rank_compare.png
      :target: notebooks/generated/stats_rankcompare.html
      :width: 240px


.. container:: example

   `Meta-Analysis in statsmodels <notebooks/generated/metaanalysis1.html>`_

   .. image:: ../_static/images/metaanalysis1.png
      :target: notebooks/generated/metaanalysis1.html
      :width: 240px


.. container:: example

   `Mediation analysis with duration data <notebooks/generated/mediation_survival.html>`_

   .. image:: ../_static/images/mediation_survival.png
      :target: notebooks/generated/mediation_survival.html
      :width: 240px


.. container:: example

   `Treatment Effect Estimation <notebooks/generated/treatment_effect.html>`_

   .. image:: ../_static/images/treatment_effect.png
      :target: notebooks/generated/treatment_effect.html
      :width: 240px


.. container:: example

   `Copulas <notebooks/generated/copula.html>`_

   .. image:: ../_static/images/copula.png
      :target: notebooks/generated/copula.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Time Series Analysis
--------------------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/autoregressions
   notebooks/generated/autoregressive_distributed_lag
   notebooks/generated/deterministics
   notebooks/generated/tsa_arma_0
   notebooks/generated/tsa_arma_1
   notebooks/generated/tsa_filters
   notebooks/generated/markov_regression
   notebooks/generated/markov_autoregression
   notebooks/generated/exponential_smoothing
   notebooks/generated/stl_decomposition
   notebooks/generated/mstl_decomposition
   notebooks/generated/stationarity_detrending_adf_kpss



.. container:: example

   `Autoregressions <notebooks/generated/autoregressions.html>`_

   .. image:: ../_static/images/autoregressions.png
      :target: notebooks/generated/autoregressions.html
      :width: 240px


.. container:: example

   `Autoregressive Distributed Lag Models <notebooks/generated/autoregressive_distributed_lag.html>`_

   .. image:: ../_static/images/autoregressive_distributed_lag.png
      :target: notebooks/generated/autoregressive_distributed_lag.html
      :width: 240px


.. container:: example

   `Deterministic Terms <notebooks/generated/deterministics.html>`_

   .. image:: ../_static/images/deterministics.png
      :target: notebooks/generated/deterministics.html
      :width: 240px


.. container:: example

   `ARMA: Sunspots Data <notebooks/generated/tsa_arma_0.html>`_

   .. image:: ../_static/images/tsa_arma0.png
      :target: notebooks/generated/tsa_arma_0.html
      :width: 240px


.. container:: example

   `ARMA: Artificial Data <notebooks/generated/tsa_arma_1.html>`_

   .. image:: ../_static/images/tsa_arma1.png
      :target: notebooks/generated/tsa_arma_1.html
      :width: 240px


.. container:: example

   `Time Series Filters <notebooks/generated/tsa_filters.html>`_

   .. image:: ../_static/images/tsa_filters.png
      :target: notebooks/generated/tsa_filters.html
      :width: 240px


.. container:: example

   `Markov switching dynamic regression <notebooks/generated/markov_regression.html>`_

   .. image:: ../_static/images/markov_regression.png
      :target: notebooks/generated/markov_regression.html
      :width: 240px


.. container:: example

   `Markov switching autoregression <notebooks/generated/markov_autoregression.html>`_

   .. image:: ../_static/images/markov_autoregression.png
      :target: notebooks/generated/markov_autoregression.html
      :width: 240px


.. container:: example

   `Exponential Smoothing <notebooks/generated/exponential_smoothing.html>`_

   .. image:: ../_static/images/exponential_smoothing.png
      :target: notebooks/generated/exponential_smoothing.html
      :width: 240px


.. container:: example

   `Seasonal Decomposition <notebooks/generated/stl_decomposition.html>`_

   .. image:: ../_static/images/stl_decomposition.png
      :target: notebooks/generated/stl_decomposition.html
      :width: 240px


.. container:: example

   `Multi-seasonal Decomposition <notebooks/generated/mstl_decomposition.html>`_

   .. image:: ../_static/images/mstl_decomposition.png
      :target: notebooks/generated/mstl_decomposition.html
      :width: 240px


.. container:: example

   `Stationarity and detrending (ADF/KPSS) <notebooks/generated/stationarity_detrending_adf_kpss.html>`_

   .. image:: ../_static/images/stationarity_detrending_adf_kpss.png
      :target: notebooks/generated/stationarity_detrending_adf_kpss.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

State space models
------------------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/statespace_sarimax_stata
   notebooks/generated/statespace_sarimax_internet
   notebooks/generated/statespace_sarimax_faq
   notebooks/generated/statespace_varmax
   notebooks/generated/statespace_dfm_coincident
   notebooks/generated/statespace_structural_harvey_jaeger
   notebooks/generated/statespace_cycles
   notebooks/generated/statespace_local_linear_trend
   notebooks/generated/statespace_arma_0
   notebooks/generated/statespace_seasonal
   notebooks/generated/statespace_fixed_params
   notebooks/generated/statespace_tvpvar_mcmc_cfa
   notebooks/generated/statespace_sarimax_pymc3
   notebooks/generated/statespace_news
   notebooks/generated/statespace_custom_models
   notebooks/generated/ets



.. container:: example

   `SARIMAX: Introduction <notebooks/generated/statespace_sarimax_stata.html>`_

   .. image:: ../_static/images/statespace_sarimax_stata.png
      :target: notebooks/generated/statespace_sarimax_stata.html
      :width: 240px


.. container:: example

   `SARIMAX: Model selection, missing data <notebooks/generated/statespace_sarimax_internet.html>`_

   .. image:: ../_static/images/statespace_sarimax_internet.png
      :target: notebooks/generated/statespace_sarimax_internet.html
      :width: 240px


.. container:: example

   `SARIMAX: Frequently Asked Questions (FAQ) <notebooks/generated/statespace_sarimax_faq.html>`_

   .. image:: ../_static/images/statespace_sarimax_faq.png
      :target: notebooks/generated/statespace_sarimax_faq.html
      :width: 240px


.. container:: example

   `VARMAX: Introduction <notebooks/generated/statespace_varmax.html>`_

   .. image:: ../_static/images/statespace_varmax.png
      :target: notebooks/generated/statespace_varmax.html
      :width: 240px


.. container:: example

   `Dynamic Factor Models: Application <notebooks/generated/statespace_dfm_coincident.html>`_

   .. image:: ../_static/images/statespace_dfm_coincident.png
      :target: notebooks/generated/statespace_dfm_coincident.html
      :width: 240px


.. container:: example

   `Unobserved Components: Application <notebooks/generated/statespace_structural_harvey_jaeger.html>`_

   .. image:: ../_static/images/statespace_structural_harvey_jaeger.png
      :target: notebooks/generated/statespace_structural_harvey_jaeger.html
      :width: 240px


.. container:: example

   `Trends and cycles in unemployment <notebooks/generated/statespace_cycles.html>`_

   .. image:: ../_static/images/statespace_cycles.png
      :target: notebooks/generated/statespace_cycles.html
      :width: 240px


.. container:: example

   `State space modeling: Local Linear Trends <notebooks/generated/statespace_local_linear_trend.html>`_

   .. image:: ../_static/images/statespace_local_linear_trend.png
      :target: notebooks/generated/statespace_local_linear_trend.html
      :width: 240px


.. container:: example

   `Statespace ARMA: Sunspots Data <notebooks/generated/statespace_arma_0.html>`_

   .. image:: ../_static/images/statespace_arma0.png
      :target: notebooks/generated/statespace_arma_0.html
      :width: 240px


.. container:: example

   `Seasonality in Time Series Data <notebooks/generated/statespace_seasonal.html>`_

   .. image:: ../_static/images/statespace_seasonal.png
      :target: notebooks/generated/statespace_seasonal.html
      :width: 240px


.. container:: example

   `Fixed / constrained parameters in state space models <notebooks/generated/statespace_fixed_params.html>`_

   .. image:: ../_static/images/statespace_fixed_params.png
      :target: notebooks/generated/statespace_fixed_params.html
      :width: 240px


.. container:: example

   `TVP-VAR, MCMC, and sparse simulation smoothing <notebooks/generated/statespace_tvpvar_mcmc_cfa.html>`_

   .. image:: ../_static/images/statespace_tvpvar_mcmc_cfa.png
      :target: notebooks/generated/statespace_tvpvar_mcmc_cfa.html
      :width: 240px


.. container:: example

   `SARIMAX estimation with Bayesian methods <notebooks/generated/statespace_sarimax_pymc3.html>`_

   .. image:: ../_static/images/statespace_sarimax_pymc3.png
      :target: notebooks/generated/statespace_sarimax_pymc3.html
      :width: 240px


.. container:: example

   `Forecasting, updating datasets, and the "news" <notebooks/generated/statespace_news.html>`_

   .. image:: ../_static/images/statespace_news.png
      :target: notebooks/generated/statespace_news.html
      :width: 240px


.. container:: example

   `Statespace: Custom Models <notebooks/generated/statespace_custom_models.html>`_

   .. image:: ../_static/images/statespace_custom_models.png
      :target: notebooks/generated/statespace_custom_models.html
      :width: 240px


.. container:: example

   `ETS models <notebooks/generated/ets.html>`_

   .. image:: ../_static/images/ets.png
      :target: notebooks/generated/ets.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

State space models - Technical notes
------------------------------------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/statespace_concentrated_scale
   notebooks/generated/statespace_chandrasekhar



.. container:: example

   `State space models: concentrating out the scale <notebooks/generated/statespace_concentrated_scale.html>`_

   .. image:: ../_static/images/statespace_concentrated_scale.png
      :target: notebooks/generated/statespace_concentrated_scale.html
      :width: 240px


.. container:: example

   `State space models: Chandrasekhar recursions <notebooks/generated/statespace_chandrasekhar.html>`_

   .. image:: ../_static/images/statespace_chandrasekhar.png
      :target: notebooks/generated/statespace_chandrasekhar.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Forecasting
-----------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/theta-model



.. container:: example

   `Forecasting using the Theta Model <notebooks/generated/theta-model.html>`_

   .. image:: ../_static/images/theta-model.png
      :target: notebooks/generated/theta-model.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

Multivariate Methods
--------------------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/pca_fertility_factors



.. container:: example

   `Principal Component Analysis <notebooks/generated/pca_fertility_factors.html>`_

   .. image:: ../_static/images/pca_fertility_factors.png
      :target: notebooks/generated/pca_fertility_factors.html
      :width: 240px




.. raw:: html

   <div class="example-header-clear">&nbsp;</div>

User Notes
----------

.. toctree::
   :maxdepth: 1
   :hidden:

   notebooks/generated/contrasts
   notebooks/generated/formulas
   notebooks/generated/predict
   notebooks/generated/statespace_forecasting
   notebooks/generated/generic_mle
   notebooks/generated/tsa_dates
   notebooks/generated/chi2_fitting
   notebooks/generated/distributed_estimation



.. container:: example

   `Contrasts <notebooks/generated/contrasts.html>`_

   .. image:: ../_static/images/contrasts.png
      :target: notebooks/generated/contrasts.html
      :width: 240px


.. container:: example

   `Formulas <notebooks/generated/formulas.html>`_

   .. image:: ../_static/images/formulas_intro.png
      :target: notebooks/generated/formulas.html
      :width: 240px


.. container:: example

   `Prediction <notebooks/generated/predict.html>`_

   .. image:: ../_static/images/prediction.png
      :target: notebooks/generated/predict.html
      :width: 240px


.. container:: example

   `Forecasting in statsmodels <notebooks/generated/statespace_forecasting.html>`_

   .. image:: ../_static/images/statespace_forecasting.png
      :target: notebooks/generated/statespace_forecasting.html
      :width: 240px


.. container:: example

   `Generic Maximum Likelihood <notebooks/generated/generic_mle.html>`_

   .. image:: ../_static/images/generic_mle.png
      :target: notebooks/generated/generic_mle.html
      :width: 240px


.. container:: example

   `Dates in Time-Series Models <notebooks/generated/tsa_dates.html>`_

   .. image:: ../_static/images/tsa_dates.png
      :target: notebooks/generated/tsa_dates.html
      :width: 240px


.. container:: example

   `Least squares fitting of models to data <notebooks/generated/chi2_fitting.html>`_

   .. image:: ../_static/images/chi2_fitting.png
      :target: notebooks/generated/chi2_fitting.html
      :width: 240px


.. container:: example

   `Distributed Estimations <notebooks/generated/distributed_estimation.html>`_

   .. image:: ../_static/images/distributed_estimation.png
      :target: notebooks/generated/distributed_estimation.html
      :width: 240px

