statsmodels

statsmodels は、多くの異なる統計モデルの推定、統計検定の実行、および 統計データの調査のためのクラスと関数を提供するPythonモジュールです。各推定量について、結果統計の広範なリストが 使用可能です。結果は、既存の統計パッケージに対してテストされ、正しいことが確認されます。パッケージは、オープンソース の修正BSD(3条項)ライセンスの下でリリースされます。オンラインドキュメントは statsmodels.org でホストされています。

イントロダクション

statsmodels は R スタイルの式と pandas DataFramesを使用したモデルの指定をサポートします。通常の最小二乗法を使用した簡単な例を次に示します:

In [1]: import numpy as np

In [2]: import statsmodels.api as sm

In [3]: import statsmodels.formula.api as smf

# Load data
In [4]: dat = sm.datasets.get_rdataset("Guerry", "HistData").data

# Fit regression model (using the natural log of one of the regressors)
In [5]: results = smf.ols('Lottery ~ Literacy + np.log(Pop1831)', data=dat).fit()

# Inspect the results
In [6]: print(results.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                Lottery   R-squared:                       0.348
Model:                            OLS   Adj. R-squared:                  0.333
Method:                 Least Squares   F-statistic:                     22.20
Date:                Tue, 28 Jan 2025   Prob (F-statistic):           1.90e-08
Time:                        00:09:10   Log-Likelihood:                -379.82
No. Observations:                  86   AIC:                             765.6
Df Residuals:                      83   BIC:                             773.0
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
===================================================================================
                      coef    std err          t      P>|t|      [0.025      0.975]
-----------------------------------------------------------------------------------
Intercept         246.4341     35.233      6.995      0.000     176.358     316.510
Literacy           -0.4889      0.128     -3.832      0.000      -0.743      -0.235
np.log(Pop1831)   -31.3114      5.977     -5.239      0.000     -43.199     -19.424
==============================================================================
Omnibus:                        3.713   Durbin-Watson:                   2.019
Prob(Omnibus):                  0.156   Jarque-Bera (JB):                3.394
Skew:                          -0.487   Prob(JB):                        0.183
Kurtosis:                       3.003   Cond. No.                         702.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

式の代わりに numpy 配列を使用することもできます:

In [7]: import numpy as np

In [8]: import statsmodels.api as sm

# Generate artificial data (2 regressors + constant)
In [9]: nobs = 100

In [10]: X = np.random.random((nobs, 2))

In [11]: X = sm.add_constant(X)

In [12]: beta = [1, .1, .5]

In [13]: e = np.random.random(nobs)

In [14]: y = np.dot(X, beta) + e

# Fit regression model
In [15]: results = sm.OLS(y, X).fit()

# Inspect the results
In [16]: print(results.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.247
Model:                            OLS   Adj. R-squared:                  0.231
Method:                 Least Squares   F-statistic:                     15.90
Date:                Tue, 28 Jan 2025   Prob (F-statistic):           1.07e-06
Time:                        00:09:10   Log-Likelihood:                -18.185
No. Observations:                 100   AIC:                             42.37
Df Residuals:                      97   BIC:                             50.18
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const          1.5135      0.073     20.685      0.000       1.368       1.659
x1             0.1958      0.102      1.925      0.057      -0.006       0.398
x2             0.4922      0.104      4.740      0.000       0.286       0.698
==============================================================================
Omnibus:                       23.831   Durbin-Watson:                   1.951
Prob(Omnibus):                  0.000   Jarque-Bera (JB):                6.295
Skew:                          -0.262   Prob(JB):                       0.0430
Kurtosis:                       1.888   Cond. No.                         4.95
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

利用可能な結果を確認するには、 dir(result)を参照してください。属性はresults.__doc__に記述され、result メソッドには独自の docstring があります。

引用

科学出版物のなかでstatsmodelsを引用する際には、次の引用を使用してください:

Seabold, Skipper, and Josef Perktold. " statsmodels: Econometric and statistical modeling with python. " 9th Python in Science Conferenceの議事録。 2010年。

Bibtex 内容

@inproceedings{seabold2010statsmodels,
  title={statsmodels: Econometric and statistical modeling with python},
  author={Seabold, Skipper and Perktold, Josef},
  booktitle={9th Python in Science Conference},
  year={2010},
}

目次

日本語翻訳について

翻訳に関するステートメント は日本語翻訳に関する翻訳者のコメント及びにステートメントを記述しています。

索引

モジュール索引


最終更新日: 2025年01月28日