【问题标题】:Compute linear regression standardized coefficient (beta) with Python使用 Python 计算线性回归标准化系数 (beta)
【发布时间】:2016-02-28 02:05:35
【问题描述】:

我想使用 Python 中的标准工具(numpy、pandas、scipy.stats 等)计算线性回归模型的 beta or standardized coefficient

我的一个朋友告诉我,这是在 R 中使用以下命令完成的:

lm(scale(y) ~ scale(x))

目前,我在 Python 中这样计算它:

from scipy.stats import linregress
from scipy.stats.mstats import zscore

(beta_coeff, intercept, rvalue, pvalue, stderr) = linregress(zscore(x), zscore(y))
print('The Beta Coeff is: %f' % beta_coeff)

有没有更直接的函数可以在 Python 中计算这个数字?

【问题讨论】:

  • 如果您有多个自变量。请看这个post

标签: python numpy scipy statistics linear-regression


【解决方案1】:

Python 是一种通用语言,但 R 是专门为统计而设计的。几乎总是需要多几行代码才能在 python 中实现相同的(统计)目标,这纯粹是因为 R 在你启动它时就准备好拟合回归模型(使用 lm)。

您的问题的简短回答是否 - 您的 python 代码已经非常简单了

也就是说,我认为更接近于您的 R 代码的是

import statsmodels.api as sm
from scipy.stats.mstats import zscore

print sm.OLS(zscore(y), zscore(x)).fit().summary()

【讨论】:

    猜你喜欢
    • 2018-11-23
    • 2015-11-13
    • 1970-01-01
    • 2021-06-22
    • 2022-10-25
    • 2018-03-16
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    相关资源
    最近更新 更多