You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gnuplot拟合与Excel趋势线、PowerShell线性回归差异原因咨询

线性回归结果差异:Gnuplot vs Excel/PowerShell

我对一组时间序列数据做线性回归时,发现Gnuplot的拟合结果和Excel趋势线、PowerShell脚本计算结果差异明显,想搞清楚三个问题:

  • 哪种结果是正确的?
  • 更适合线性回归的方案是什么?
  • 二者斜率差异显著的原因是什么?

原始数据点

Timestamp   Value
1709289398  3.89
1711018216  3.9
1711450016  3.93
1711460288  3.91
1711462458  3.92
1711618830  3.95
1711620395  3.94
1711630819  3.95
1711635811  3.93
1711638487  3.91
1712057838  3.96
1712578056  3.93
1712590907  3.89
1712662297  3.95
1712918775  3.9
1712924128  3.94
1713170534  3.98
1713191398  3.94
1713773713  3.94
1713776247  3.97
1713777374  3.96
1713790153  3.97
1713860582  3.96
1713881074  3.86
1713882272  3.89
1714032046  3.94
1714033372  3.86
1714045066  3.8
1714055405  3.81
1714382965  3.82
1714478636  3.78
1714494189  3.96
1714496044  3.95
1714496681  3.95
1714652333  3.97
1714654135  4.01
1714732802  4.02
1714735452  4.01
1714736266  4.07
1714737131  3.99
1714748973  4.02
1714984178  4.24
1715006630  4
1715181736  3.91
1715599718  3.88
1715675239  3.87
1717430326  3.91
1717432138  3.88
1717750408  3.98

Excel与PowerShell计算结果

Excel趋势线结果:

y = 0.000000005343803x - 5.22104266524509

PowerShell实现的线性回归函数:

function Find-LinearRegession($dataPoints) {
    $n = $dataPoints.Length
    $sumX = 0
    $sumY = 0
    $sumXY = 0
    $sumXX = 0

    foreach ($point in $dataPoints) {
        $x = $point[0]
        $y = $point[1]

        $sumX += $x
        $sumY += $y
        $sumXY += $x * $y
        $sumXX += $x * $x
    }

    $meanX = $sumX / $n
    $meanY = $sumY / $n

    $slope = (($n * $sumXY) - ($sumX * $sumY)) / (($n * $sumXX) - ($sumX * $sumX))
    $intercept = $meanY - ($slope * $meanX)

    return @{
        Slope     = $slope
        Intercept = $intercept
    }
}

PowerShell计算结果:

y = 0.000000005343795x - 5.2210293420428

二者结果几乎一致,误差仅源于浮点运算精度差异。


Gnuplot拟合情况

Gnuplot脚本:

data = 'GnuPlotValues.txt'
set ticslevel 0
set tics nomirror
set key noautotitle
set grid
set key font ',14'
set xdata time
set timefmt "%s"
set format x "%d/%m/%y"
set xtics rotate by 45 right
a = 1
b = 1
g(x) = a*x + b
fit g(x) data using 1:2 via a, b
f(x) = 5.34379555506111E-09*x + -5.2210293420428    
plot data using 1:2 with points pt 7 ps 0.6 lc rgb 'red' title 'Datapoints', \
    g(x) with lines, \
    f(x) with lines title 'Linear regression: f(x) = 5.3437E-09x + -5.221'

实际拟合的g(x)与PowerShell/Excel的f(x)斜率差异明显,图像中红色为Gnuplot拟合线,蓝色为PowerShell回归线。


问题解答

1. 哪种结果正确?

Excel和PowerShell的结果是正确的。它们严格按照**普通最小二乘法(OLS)**的标准公式计算,直接使用原始的Unix时间戳大整数进行回归,计算逻辑无错误。

Gnuplot的结果是“适配时间轴缩放后的结果”,并非原始数据的真实OLS回归参数——因为启用set xdata time后,Gnuplot会将原始时间戳转换为内部的时间单位(如天),改变了x变量的数量级,导致拟合参数数值偏离真实结果。

2. 更适合线性回归的方案是什么?

根据需求选择:

  • 快速验证:用Excel趋势线,操作简单,结果可靠。
  • 自动化处理:用PowerShell脚本,完全自定义OLS计算,可批量处理数据,计算过程可追溯。
  • 可视化+准确拟合:修正Gnuplot脚本,先基于原始数值拟合,再设置时间轴格式。核心代码示例:
    # 先基于原始Timestamp拟合
    a = 1
    b = 1
    g(x) = a*x + b
    fit g(x) data using 1:2 via a, b
    # 再设置时间轴显示格式
    set xdata time
    set timefmt "%s"
    set format x "%d/%m/%y"
    set xtics rotate by 45 right
    # 绘制图像
    plot data using 1:2 with points pt 7 ps 0.6 lc rgb 'red' title 'Datapoints', \
        g(x) with lines title 'Gnuplot OLS Fit'
    

3. 斜率差异显著的原因是什么?

核心原因是Gnuplot的时间轴转换改变了x变量的数量级:

  • 原始Timestamp是10^9级的大整数,启用set xdata time后,Gnuplot会将其转换为104级的天数(如从1970-01-01到当前时间约19000天),x值被缩小了约105倍。
  • 根据OLS公式,斜率与x的数量级成反比:x缩小k倍,拟合出的斜率会放大k倍。这就导致Gnuplot的斜率数值和Excel/PowerShell结果差异巨大,本质是变量缩放后的结果,而非计算逻辑错误。

内容的提问来源于stack exchange,提问作者Frank D.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 18:44:55