Python Forum
Newbie question how to find the coefficient for each variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie question how to find the coefficient for each variable
#1
Hello:
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,
Reply


Messages In This Thread
Newbie question how to find the coefficient for each variable - by zydjohn - Dec-13-2017, 05:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get coefficient of determination R2 after scipy.curve_fit? AlekseyPython 0 1,893 Feb-12-2021, 09:03 AM
Last Post: AlekseyPython
  how to calculate overlaping coefficient between two probablity functions Staph 3 3,775 Aug-11-2019, 08:10 AM
Last Post: Staph
  Help with correlation coefficient mattjb84 7 4,969 Jun-29-2018, 09:56 PM
Last Post: Larz60+
  Newbie question to return only the index of a dataframe zydjohn 0 2,584 Jan-22-2018, 03:40 PM
Last Post: zydjohn
  Newbie question: how to generate dataframe and use multiple regression zydjohn 0 2,301 Dec-10-2017, 09:49 AM
Last Post: zydjohn
  Newbie question on how to use pandas.rolling_mean zydjohn 5 14,307 Dec-09-2017, 08:42 PM
Last Post: j.crater
  Newbie question for using map, lambda zydjohn 2 3,434 Dec-09-2017, 07:18 PM
Last Post: zydjohn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020