【发布时间】:2019-09-08 07:41:33
【问题描述】:
我想使用statsmodels OLS 类来创建多元回归模型。考虑以下数据集:
import statsmodels.api as sm
import pandas as pd
import numpy as np
dict = {'industry': ['mining', 'transportation', 'hospitality', 'finance', 'entertainment'],
'debt_ratio':np.random.randn(5), 'cash_flow':np.random.randn(5) + 90}
df = pd.DataFrame.from_dict(dict)
x = data[['debt_ratio', 'industry']]
y = data['cash_flow']
def reg_sm(x, y):
x = np.array(x).T
x = sm.add_constant(x)
results = sm.OLS(endog = y, exog = x).fit()
return results
当我运行以下代码时:
reg_sm(x, y)
我收到以下错误:
TypeError: '>=' not supported between instances of 'float' and 'str'
我尝试将 industry 变量转换为分类变量,但仍然出现错误。我别无选择。
【问题讨论】:
-
这是因为 'industry' 是分类变量,但 OLS 需要数字(这可以从其源代码中看出)。删除行业,或按行业对数据进行分组并将 OLS 应用于每个组。
标签: python pandas statsmodels