このようなデータセットから:
_import pandas as pd
import numpy as np
import statsmodels.api as sm
# A dataframe with two variables
np.random.seed(123)
rows = 12
rng = pd.date_range('1/1/2017', periods=rows, freq='D')
df = pd.DataFrame(np.random.randint(100,150,size=(rows, 2)), columns=['y', 'x'])
df = df.set_index(rng)
_
...そしてこのような線形回帰モデル:
_x = sm.add_constant(df['x'])
model = sm.OLS(df['y'], x).fit()
_
...この方法でいくつかのモデル係数を簡単に取得できます。
_print(model.params)
_
しかし、モデルの概要から他のすべてのパラメーターを取得する方法を見つけることができません。
_print(str(model.summary()))
_
質問で述べたように、私は特にR-squaredに興味があります。
投稿から OLSから特定の値を抽出する方法-パンダの概要?print(model.r2)
を使用して同じことを実行できることを学びました。しかし、それはstatsmodelsでは機能しないようです。
助言がありますか?
次のように決定係数を取得できます。
model.rsquared
import pandas as pd
import numpy as np
import statsmodels.api as sm
# A dataframe with two variables
np.random.seed(123)
rows = 12
rng = pd.date_range('1/1/2017', periods=rows, freq='D')
df = pd.DataFrame(np.random.randint(100,150,size=(rows, 2)), columns=['y', 'x'])
df = df.set_index(rng)
x = sm.add_constant(df['x'])
model = sm.OLS(df['y'], x).fit()
print(model.params)
print(model.rsquared)
print(str(model.summary()))
const 176.636417
x -0.357185
dtype: float64
0.338332793094
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.338
Model: OLS Adj. R-squared: 0.272
Method: Least Squares F-statistic: 5.113
Date: Tue, 30 Jan 2018 Prob (F-statistic): 0.0473
Time: 05:36:04 Log-Likelihood: -41.442
No. Observations: 12 AIC: 86.88
Df Residuals: 10 BIC: 87.85
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 176.6364 20.546 8.597 0.000 130.858 222.415
x -0.3572 0.158 -2.261 0.047 -0.709 -0.005
==============================================================================
Omnibus: 1.934 Durbin-Watson: 1.182
Prob(Omnibus): 0.380 Jarque-Bera (JB): 1.010
Skew: -0.331 Prob(JB): 0.603
Kurtosis: 1.742 Cond. No. 1.10e+03
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.1e+03. This might indicate that there are
strong multicollinearity or other numerical problems.
少しのコードで:
for attr in dir(model):
if not attr.startswith('_'):
print(attr)
オブジェクトのすべての属性を確認できます。
HC0_se
HC1_se
HC2_se
HC3_se
aic
bic
bse
centered_tss
compare_f_test
compare_lm_test
compare_lr_test
condition_number
conf_int
conf_int_el
cov_HC0
cov_HC1
cov_HC2
cov_HC3
cov_kwds
cov_params
cov_type
df_model
df_resid
eigenvals
el_test
ess
f_pvalue
f_test
fittedvalues
fvalue
get_influence
get_prediction
get_robustcov_results
initialize
k_constant
llf
load
model
mse_model
mse_resid
mse_total
nobs
normalized_cov_params
outlier_test
params
predict
pvalues
remove_data
resid
resid_pearson
rsquared
rsquared_adj
save
scale
ssr
summary
summary2
t_test
tvalues
uncentered_tss
use_t
wald_test
wald_test_terms
wresid