统计模型回归的输出
Output of a statsmodels regression
我想使用 statsmodels 执行简单的线性回归,我现在已经尝试了几种不同的方法,但我就是没有让它工作。我现在构建的代码没有给我任何错误,但也没有显示结果
我正在尝试为变量 "Direction" 创建一个模型,如果相应日期的 return 为负,则取值为 0,如果为正,则取值为 1。解释变量是 return 的 (5) 个滞后。 df13 包含滞后以及每个观察日期的方向。我试过这段代码,正如我所提到的,它没有给出错误,而是说“优化已成功终止。
当前函数值:0.682314
迭代 5
但是,我希望看到典型的 table 以及所有 beta 值、它们的重要性等。
此外,您怎么看,既然 Direction 是一个二元变量,使用 logit 模型而不是线性模型会更好吗?但是,在作业中它显示为线性模型。
最后,很抱歉它没有在此处正确显示,但我不知道如何编写代码或插入我的数据框
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import os
import itertools
from sklearn import preprocessing
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
import statsmodels.api as sm
import matplotlib.pyplot as plt
from statsmodels.sandbox.regression.predstd import wls_prediction_std
...
X = df13[['Lag1', 'Lag2', 'Lag3', 'Lag4', 'Lag5']]
Y = df13['Direction']
X = sm.add_constant(X)
model = sm.Logit(Y.astype(float), X.astype(float)).fit()
predictions = model.predict(X)
print_model = model.summary
print(print_model)
编辑:我确定它必须是逻辑回归,所以我更新了那部分
我不知道这是不是无意的,但看起来你需要分别定义 X 和 Y:
X = df13[['Lag1', 'Lag2', 'Lag3', 'Lag4', 'Lag5']]
Y = df13['Direction']
其次,我不熟悉 statsmodel,但我会尝试将您的数据帧转换为 numpy 数组。您可以使用
Xnum = X.to_numpy()
ynum = y.to_numpy()
并尝试将它们传递给回归器。
我想使用 statsmodels 执行简单的线性回归,我现在已经尝试了几种不同的方法,但我就是没有让它工作。我现在构建的代码没有给我任何错误,但也没有显示结果
我正在尝试为变量 "Direction" 创建一个模型,如果相应日期的 return 为负,则取值为 0,如果为正,则取值为 1。解释变量是 return 的 (5) 个滞后。 df13 包含滞后以及每个观察日期的方向。我试过这段代码,正如我所提到的,它没有给出错误,而是说“优化已成功终止。 当前函数值:0.682314 迭代 5
但是,我希望看到典型的 table 以及所有 beta 值、它们的重要性等。
此外,您怎么看,既然 Direction 是一个二元变量,使用 logit 模型而不是线性模型会更好吗?但是,在作业中它显示为线性模型。
最后,很抱歉它没有在此处正确显示,但我不知道如何编写代码或插入我的数据框
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import os
import itertools
from sklearn import preprocessing
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
import statsmodels.api as sm
import matplotlib.pyplot as plt
from statsmodels.sandbox.regression.predstd import wls_prediction_std
...
X = df13[['Lag1', 'Lag2', 'Lag3', 'Lag4', 'Lag5']]
Y = df13['Direction']
X = sm.add_constant(X)
model = sm.Logit(Y.astype(float), X.astype(float)).fit()
predictions = model.predict(X)
print_model = model.summary
print(print_model)
编辑:我确定它必须是逻辑回归,所以我更新了那部分
我不知道这是不是无意的,但看起来你需要分别定义 X 和 Y:
X = df13[['Lag1', 'Lag2', 'Lag3', 'Lag4', 'Lag5']]
Y = df13['Direction']
其次,我不熟悉 statsmodel,但我会尝试将您的数据帧转换为 numpy 数组。您可以使用
Xnum = X.to_numpy()
ynum = y.to_numpy()
并尝试将它们传递给回归器。