用 Python 计算线性回归标准化系数 (beta)
Compute linear regression standardized coefficient (beta) with Python
我想使用 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中是否有更直接的函数来计算这个数字?
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()
我想使用 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中是否有更直接的函数来计算这个数字?
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()