Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the regression coefficient from statsmodels.api?

 result = sm.OLS(gold_lookback, silver_lookback ).fit() 

After I get the result, how can I get the coefficient and the constant?

In other words, if y = ax + c how to get the values a and c?

like image 914
JOHN Avatar asked Nov 20 '17 09:11

JOHN


People also ask

How do you find the coefficient in Python?

The Pearson Correlation coefficient can be computed in Python using corrcoef() method from Numpy. The input for this function is typically a matrix, say of size mxn , where: Each column represents the values of a random variable. Each row represents a single sample of n random variables.

What is Statsmodels formula API?

statsmodels. formula. api : A convenience interface for specifying models using formula strings and DataFrames. This API directly exposes the from_formula class method of models that support the formula API.

What is REGR Coef_?

Regression coefficients are estimates of the unknown population parameters and describe the relationship between a predictor variable and the response. In linear regression, coefficients are the values that multiply the predictor values. Suppose you have the following regression equation: y = 3X + 5.


1 Answers

You can use the params property of a fitted model to get the coefficients.

For example, the following code:

import statsmodels.api as sm import numpy as np np.random.seed(1) X = sm.add_constant(np.arange(100)) y = np.dot(X, [1,2]) + np.random.normal(size=100) result = sm.OLS(y, X).fit() print(result.params) 

will print you a numpy array [ 0.89516052 2.00334187] - estimates of intercept and slope respectively.

If you want more information, you can use the object result.summary() that contains 3 detailed tables with model description.

like image 114
David Dale Avatar answered Sep 28 '22 19:09

David Dale