【问题标题】:Predict out of sample R2 with stats model使用统计模型预测样本 R2
【发布时间】:2019-10-20 20:48:02
【问题描述】:

这是我训练模型并检查样本统计数据的代码

  #OLS statsmodels: Train the model 
    model = sm.OLS(Ytrain, Xtrain)
    results = model.fit()

    #In of Sample Statistics + residuals
    InY_Prediction = results.predict(Xtrain)
    InR2 = results.rsquared
    InAdjR2 = results.rsquared_adj
    InResiduals = Ytrain-InY_Prediction
    #print(results.summary())

我试图在我的第一个代码中使用 R2 来获取样本外数据

#Out of Sample Statistics + residuals
OutY_Predicted = results.predict(Xtest)

如何获得用于样本外回归的 R2?

【问题讨论】:

  • 问题是什么?
  • 如何让 R2 用于样本外回归

标签: python statsmodels


【解决方案1】:

根据定义,rsquared 是因变量(例如 ytest)和拟合值(OutY_Predicted)的平方相关系数。因此,获得通常的 rsquared 的一种方法是:

OutY_Predicted = results.predict(Xtest)
rsquared = ytest.corr(OutY_Predicted)**2

【讨论】:

    猜你喜欢
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2021-07-07
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多