【问题标题】:How to evaluate a statistical model with given (=not optimised) coefficients?如何评估具有给定(=未优化)系数的统计模型?
【发布时间】:2020-10-14 14:03:08
【问题描述】:

我有一个非常简单的统计模型 y = a * b。

我知道所有变量(a、b 和 y),我不想添加任何优化系数(截距应该为 0,a 和 b 上的系数实际上是 1)。

我需要的是解释方差 (R2),但如果不从头计算所有公式,我无法找到一种简单的方法来梳理它。有没有类似于 statsmodel OLS rsquared 但对于具有固定系数的模型?

我主要使用 python,但如果它更简单,我很乐意使用 R。

【问题讨论】:

  • 嗨,欢迎来到 StackOverflow。如果您将代码发布在您尝试过的所有内容以及卡在哪里的代码中,那将非常有帮助。这将使社区能够以更好的方式帮助您。

标签: python statistics statsmodels


【解决方案1】:

你可以使用scikit-learn的r2_score方法,你可以找到here。您所要做的实际上就是使用您的系数和数据计算预测的因变量y

这是一个最小的工作示例:

from sklearn.metrics import r2_score

# here's the truth value of y
y_true = [3, -0.5, 2, 7]

# here's your single featue x
x = [1, 2.3, 3.1, 0.9]

# and here's your coefficient b and your intercept a
i = [0, 0, 0, 0]
b = 0.3

# compute the predicted value of y using your coefficients
y_pred = [n+m for (n, m) in zip(i, [j*b for j in x])]

# display the R2 score using scikit-learn's r2_score method
print(r2_score(y_true, y_pred))

顺便说一句,从头开始计算 R2 不会比这更复杂...

【讨论】:

    猜你喜欢
    • 2022-11-22
    • 1970-01-01
    • 2016-04-07
    • 2014-11-28
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多