简体   繁体   English

在Python中使用OLS进行多元回归

[英]Multiple regression with OLS in python

I have a code for multiple OLS-regression with the Newey-West procedure. 我有一个使用Newey-West过程进行多个OLS回归的代码。

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? ...并且想要分析它们对变量a的影响,就像分析原始代码中的变量b一样?

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

Thanks!! 谢谢!!

您可以提供多个变量,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM