【问题标题】:how to use for loop on glm如何在 glm 上使用 for 循环
【发布时间】:2020-09-26 23:40:07
【问题描述】:

我想使用情绪分数来预测每只股票的回报(股票 1、股票 2 和股票 3)。请参阅下面的示例数据集。

data={"sentiment":[0.9, 0.75, 0.88, 0.23] , "stock1":[0.0015, 0.034, -0.065, 0.015], "stock2":[0.023, -0.001, 0.0098, 0.072], "stock3":[-0.0052, 0.0083, 0.012, 0.094]}
sample=pd.DataFrame(data, columns=['sentiment', 'stock1', 'stock2', 'stock3'])
print(sample)

我想使用 for 循环来迭代 3 种不同的股票回报,而不是运行 3 次回归,这里是我的尝试:

diff_stock=['stock1','stock2','stock3']
for i in diff_stock:
    model=glm(formula='i ~ sentiment', data=sample, family=sm.families.Gaussian()).fit()
    print(model.summary())

但是,我不断收到此错误消息:

PatsyError: Number of rows mismatch between data argument and i (3377 versus 1) i ~ favorite_count

i(股票列)中似乎只有 1 个值,但我不明白为什么...

【问题讨论】:

    标签: python for-loop statsmodels glm


    【解决方案1】:

    需要将公式构造成字符串,例如:

    import statsmodels.formula.api as smf
    
    for i in diff_stock:
        model=smf.glm(formula= i + ' ~ sentiment', data=sample,
        family=sm.families.Gaussian()).fit()
        print(model.summary())
    

    【讨论】:

      猜你喜欢
      • 2021-11-24
      • 2021-06-26
      • 1970-01-01
      • 2011-05-12
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      相关资源
      最近更新 更多