gpt4 book ai didi

python - Seaborn regplot 拟合线与来自 stats.linregress 或 stats 模型的计算拟合不匹配

转载 作者:行者123 更新时间:2023-12-04 04:05:26 28 4
gpt4 key购买 nike

我正在尝试拟合 xlog 线性回归。我使用 Seaborn regplot 来绘制拟合,看起来很合适(绿线)。然后,因为 regplot 不提供系数。我使用 stats.linregress 来查找系数。但是,绘制的线(紫色)与 Seaborn regplot 的拟合不匹配。我还使用统计模型来获取与线性回归输出匹配的系数。有没有更好的方法来获得与 regplot 线匹配的系数。我无法重现 Seaborn regplot 线。我需要系数来报告模型的拟合。

import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats

sns.regplot(x, y,x_bins=100, logx=True,n_boot=2000, scatter_kws={"color": "black"},
ci=None,label='logfit',line_kws={"color": "green"})

#Find the coefficients slope and intercept
slope, intercept, r_value, pv, se = stats.linregress(y, np.log10(x))

yy= np.linspace(-.01, 0.05, 400)
xx = 10**(slope*yy+intercept)
plt.plot(xx,yy,marker='.',color='purple')

#Label Figure
plt.tick_params(labelsize=18)
plt.xlabel('insitu', fontsize=22)
plt.ylabel('CI', fontsize=22)
output from code shows lines do not overlay
我还使用了 stats 模型进行拟合,并得到了与 stats.linregress 相同的系数结果。我无法重现 Seaborn regplot 行。
 import statsmodels as sm
import statsmodels.formula.api as smf

results = smf.ols('np.log10(x) ~ (y)', data=df_data).fit()
# Inspect the results
print(results.summary())

最佳答案

您尝试重新创建 seaborn 正在做的事情有两个问题:

  • 你有 stats.linregress 的参数向后
  • 这不是 yhat 的计算方式

  • 以下是重新创建 seaborn logx 回归线的方法:
    diamonds = sns.load_dataset("diamonds").sample(500, random_state=0)

    x = diamonds["price"]
    y = diamonds["carat"]

    ax = sns.regplot(x=x, y=y, logx=True, line_kws=dict(color="g", lw=10))

    fit = stats.linregress(np.log(x), y)
    grid = np.linspace(x.min(), x.max())
    ax.plot(grid, fit.intercept + fit.slope * np.log(grid), color="r", lw=5)
    enter image description here

    关于python - Seaborn regplot 拟合线与来自 stats.linregress 或 stats 模型的计算拟合不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62582644/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com