Multiple regression with OLS in python

时间:2015-06-26 10:16:32

标签: python numpy pandas statsmodels

I have a code for multiple OLS-regression with the Newey-West procedure.

import pandas as pd
import numpy as np
import statsmodels.api as sm

df = pd.DataFrame({'a':[1,3,5,7,4,5,6,4,7,8,9],
                   'b':[3,5,6,2,4,6,7,8,7,8,9]})

results = sm.OLS(df.a, sm.add_constant(df.b)).fit()
new = results.get_robustcov_results(cov_type='HAC',maxlags=1)
print new.summary()

It works, but how should I change the code, if I have more variables like....

df = pd.DataFrame({'a':[1,3,5,7,4,5,6,4,7,8,9],
                   'b':[3,5,6,2,4,6,7,8,7,8,9],
                   'c':[3,5,6,2,4,8,7,8,9,9,9],
                   'd':[3,5,6,2,5,8,8,9,8,10,9]})

... and wanted to analyse their influence on variable a, like the analysis of variable b in the original code?

How should the Code-line results = sm.OLS(df.a, sm.add_constant(df.b)).fit() looks like?

Thanks!!

1 个答案:

答案 0 :(得分:0)

您可以提供多个这样的变量:

results = sm.OLS(df.a, sm.add_constant(df[list('bcd')])).fit()