我真的很想看到一个数据样本以及一个代码 sn-p 来重现您的错误。
否则,我的建议将无法解决您的特定错误消息。但是,它可以让您对存储在 pandas 数据框中的一组时间序列进行多元回归分析。假设您在时间序列中使用的是连续值而不是分类值,下面是我使用 pandas 和 statsmodels 的方法:
具有随机值的数据框:
# Imports
import pandas as pd
import numpy as np
import itertools
np.random.seed(1)
rows = 12
listVars= ['y','x1', 'x2', 'x3']
rng = pd.date_range('1/1/2017', periods=rows, freq='D')
df_1 = pd.DataFrame(np.random.randint(100,150,size=(rows, len(listVars))), columns=listVars)
df_1 = df_1.set_index(rng)
print(df_1)
输出 - 一些可以使用的数据:
y x1 x2 x3
2017-01-01 137 143 112 108
2017-01-02 109 111 105 115
2017-01-03 100 116 101 112
2017-01-04 107 145 106 125
2017-01-05 120 137 118 120
2017-01-06 111 142 128 129
2017-01-07 114 104 123 123
2017-01-08 141 149 130 132
2017-01-09 122 113 141 109
2017-01-10 107 122 101 100
2017-01-11 117 108 124 113
2017-01-12 147 142 108 130
下面的函数将让您指定源数据框以及因变量 y 和自变量选择 x1, x2。使用 statsmodels,一些期望的结果将存储在数据框中。在那里,R2 将是数值类型,而回归系数和 p 值将是列表,因为这些估计值的数量会随着您希望包含在分析中的自变量数量而变化。
def LinReg(df, y, x, const):
betas = x.copy()
# Model with out without a constant
if const == True:
x = sm.add_constant(df[x])
model = sm.OLS(df[y], x).fit()
else:
model = sm.OLS(df[y], df[x]).fit()
# Estimates of R2 and p
res1 = {'Y': [y],
'R2': [format(model.rsquared, '.4f')],
'p': [model.pvalues.tolist()],
'start': [df.index[0]],
'stop': [df.index[-1]],
'obs' : [df.shape[0]],
'X': [betas]}
df_res1 = pd.DataFrame(data = res1)
# Regression Coefficients
theParams = model.params[0:]
coefs = theParams.to_frame()
df_coefs = pd.DataFrame(coefs.T)
xNames = list(df_coefs)
xValues = list(df_coefs.loc[0].values)
xValues2 = [ '%.2f' % elem for elem in xValues ]
res2 = {'Independent': [xNames],
'beta': [xValues2]}
df_res2 = pd.DataFrame(data = res2)
# All results
df_res = pd.concat([df_res1, df_res2], axis = 1)
df_res = df_res.T
df_res.columns = ['results']
return(df_res)
这是一个测试运行:
df_regression = LinReg(df = df, y = 'y', x = ['x1', 'x2'], const = True)
print(df_regression)
输出:
results
R2 0.3650
X [x1, x2]
Y y
obs 12
p [0.7417691742514285, 0.07989515781898897, 0.25...
start 2017-01-01 00:00:00
stop 2017-01-12 00:00:00
Independent [const, x1, x2]
coefficients [16.29, 0.47, 0.37]
这是一个简单的复制粘贴的全部内容:
# Imports
import pandas as pd
import numpy as np
import statsmodels.api as sm
np.random.seed(1)
rows = 12
listVars= ['y','x1', 'x2', 'x3']
rng = pd.date_range('1/1/2017', periods=rows, freq='D')
df = pd.DataFrame(np.random.randint(100,150,size=(rows, len(listVars))), columns=listVars)
df = df.set_index(rng)
def LinReg(df, y, x, const):
betas = x.copy()
# Model with out without a constant
if const == True:
x = sm.add_constant(df[x])
model = sm.OLS(df[y], x).fit()
else:
model = sm.OLS(df[y], df[x]).fit()
# Estimates of R2 and p
res1 = {'Y': [y],
'R2': [format(model.rsquared, '.4f')],
'p': [model.pvalues.tolist()],
'start': [df.index[0]],
'stop': [df.index[-1]],
'obs' : [df.shape[0]],
'X': [betas]}
df_res1 = pd.DataFrame(data = res1)
# Regression Coefficients
theParams = model.params[0:]
coefs = theParams.to_frame()
df_coefs = pd.DataFrame(coefs.T)
xNames = list(df_coefs)
xValues = list(df_coefs.loc[0].values)
xValues2 = [ '%.2f' % elem for elem in xValues ]
res2 = {'Independent': [xNames],
'beta': [xValues2]}
df_res2 = pd.DataFrame(data = res2)
# All results
df_res = pd.concat([df_res1, df_res2], axis = 1)
df_res = df_res.T
df_res.columns = ['results']
return(df_res)
df_regression = LinReg(df = df, y = 'y', x = ['x1', 'x2'], const = True)
print(df_regression)