python - 在 Python 中引导多个回归参数

标签 python statistics linear-regression statsmodels statistics-bootstrap

我正在尝试使用 bootstraping 来估计 Python 中的多重回归系数,但我不知道如何实现它。

我使用 statsmodels.ols(formula = 'Y ~ A * B * C, ... ) 来运行单个模型。如何实现一个 Bootstrap ,返回此普通最小二乘模型返回的所有参数的估计值和置信区间?

我发现 statsmodels 中可能存在引导方法,但我不知道如何导入它以及它是否具有我想要的功能。 scikits 中还有另一个(或几个),但同样,我不知道如何使用它们来估计许多返回的回归参数。

感谢您的帮助。我完全被难住了——而且对 Python 还很陌生。

最佳答案

您可以使用resample可以通过pip下载安装包。这是 Github 页面:https://github.com/dsaxton/resample .

doc文件夹中有一个笔记本,其中包含此类问题的示例(这里我们使用 sklearn 但它也可以适用于 statsmodels)。本质上,您将建模过程定义为完整数据集(包括预测变量和响应变量)的函数,该函数以您喜欢的任何格式返回模型参数(这里我们返回带有系数和截距的字典),然后重新计算使用bootstrap引导样本上的函数来自resample.bootstrap模块( df 是包含预测变量的 pandas DataFrame ,而 y 是包含响应变量的 Series ):

from resample.bootstrap import bootstrap
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression

def fitreg(A):
    scale = StandardScaler()
    reg = LinearRegression(fit_intercept=True)
    X_scale = scale.fit_transform(A[:, :A.shape[1]-2])
    y = A[:, A.shape[1]-1]
    reg.fit(X_scale, y)
    return {"coef": reg.coef_, "intercept": reg.intercept_}

boot_coef = bootstrap(a=df.join(y).values, f=fitreg, b=5000)

关于python - 在 Python 中引导多个回归参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151401/

相关文章:

r - R 中的置信区间

python - 网页抓取 : getting KeyError when parsing JSON in Python

python - 使用pandas hub_table()将属性值对转换为表

Python套接字: Server side not responding after the input of list

r - 如何绘制一个箱线图,其订书钉看起来像 R 中的方括号

python - 如何从代码中删除 RunTimeWarning 错误?

Python 将输出转换为句子

ruby - 如何从 Ruby 中的指数分布中提取?

r - 近似 R 中二项式随机变量之和的分布

python - Numpy/matplotlib - 绘制线性回归会产生错误的斜率