Dec-13-2017, 05:25 PM
Hello:
I had some code to do multiple variable linear regression using statsmodels, the following is my code:
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 1.000
Model: OLS Adj. R-squared: 1.000
Method: Least Squares F-statistic: 1.204e+32
Date: Wed, 13 Dec 2017 Prob (F-statistic): 6.91e-279
Time: 18:20:25 Log-Likelihood: 646.36
No. Observations: 20 AIC: -1289.
Df Residuals: 18 BIC: -1287.
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
Intercept -1.0000 4.74e-16 -2.11e+15 0.000 -1.000 -1.000
x1 -0.6667 4.46e-16 -1.5e+15 0.000 -0.667 -0.667
x2 0.3333 3.04e-17 1.1e+16 0.000 0.333 0.333
x3 1.3333 5.02e-16 2.65e+15 0.000 1.333 1.333
==============================================================================
Omnibus: 1.008 Durbin-Watson: 0.381
Prob(Omnibus): 0.604 Jarque-Bera (JB): 0.784
Skew: 0.452 Prob(JB): 0.676
Kurtosis: 2.645 Cond. No. 6.56e+16
==============================================================================
But I want to use the coefficient for each variable, for example, the coef for x1 (-0.6667), coef for x2 (0.3333),
coef for x3 (1.3333) and Intercept (-1.0)
But I can't find any useful document on how to extract each coefficient and the intercept for the linear regression model.
Please advise,
Thanks,
I had some code to do multiple variable linear regression using statsmodels, the following is my code:
import numpy as np import statsmodels.api as sm import statsmodels.formula.api as smf import pandas as pd x0 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25] y = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21] def genList1(x, n, offset): list1 = [] if (n + offset) <= len(x): list1 = x[offset:(offset + n)] return(list1) x1 = genList1(x0, 20, 5) x2 = genList1(x0, 20, 4) x3 = genList1(x0, 20, 3) xy = [('Y', y), ('x1', x1), ('x2', x2), ('x3', x3)] df = pd.DataFrame.from_items(xy) model = smf.ols('y ~ x1 + x2 +x3', df).fit() print(model.summary()) print('Done')I can see the following results:
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 1.000
Model: OLS Adj. R-squared: 1.000
Method: Least Squares F-statistic: 1.204e+32
Date: Wed, 13 Dec 2017 Prob (F-statistic): 6.91e-279
Time: 18:20:25 Log-Likelihood: 646.36
No. Observations: 20 AIC: -1289.
Df Residuals: 18 BIC: -1287.
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
Intercept -1.0000 4.74e-16 -2.11e+15 0.000 -1.000 -1.000
x1 -0.6667 4.46e-16 -1.5e+15 0.000 -0.667 -0.667
x2 0.3333 3.04e-17 1.1e+16 0.000 0.333 0.333
x3 1.3333 5.02e-16 2.65e+15 0.000 1.333 1.333
==============================================================================
Omnibus: 1.008 Durbin-Watson: 0.381
Prob(Omnibus): 0.604 Jarque-Bera (JB): 0.784
Skew: 0.452 Prob(JB): 0.676
Kurtosis: 2.645 Cond. No. 6.56e+16
==============================================================================
But I want to use the coefficient for each variable, for example, the coef for x1 (-0.6667), coef for x2 (0.3333),
coef for x3 (1.3333) and Intercept (-1.0)
But I can't find any useful document on how to extract each coefficient and the intercept for the linear regression model.
Please advise,
Thanks,