Search code examples
pythonpython-3.xregressionlinear-regressionstatsmodels

AttributeError: 'function' object has no attribute 'summary'


I am running linear regression and I get the attributeError for 'summary'.

I am working on windows OS, python 3.7

y=dataset
X=dataset [['A'] + ['B'] + ['C'] + ['D'] + ['E']]
X1 = sm.add_constant(X)
model = sm.OLS(endog = y, exog = X)
results = model.fit
results.summary()

AttributeError: 'function' object has no attribute 'summary'


Solution

  • From statsmodels OLS example:

    import numpy as np
    
    import statsmodels.api as sm
    
    # Artificial data:
    nsample = 100
    x = np.linspace(0, 10, 100)
    X = np.column_stack((x, x**2))
    beta = np.array([1, 0.1, 10])
    e = np.random.normal(size=nsample)
    
    # Our model needs an intercept so we add a column of 1s:
    X = sm.add_constant(X)
    y = np.dot(X, beta) + e
    
    # Fit and summary:
    model = sm.OLS(y, X)
    results = model.fit()
    
    print(results.summary())
    
    print('Parameters: ', results.params)
    print('R2: ', results.rsquared)
    

    In your case, results = model.fit has to be results = model.fit().