【问题标题】:sum, min, max & mean of R-values above a regression line in PythonPython中回归线上方R值的总和,最小值,最大值和平均值
【发布时间】:2019-11-23 07:09:34
【问题描述】:

我有一个时间序列数据框,如下图所示(下图中将 2 个数据框一起绘制)

线图线是橙色和蓝色线。每个数据集的回归线或趋势线将分别类似于橙色和蓝色线。

如何分别计算回归线上方和下方的 R 值(与回归线的距离)的总和、最小值、最大值和平均值?即python中正R值和负R值的总和,最小值,最大值和平均值。 我正在尝试做的事情可能有一个术语,但我是统计新手并且不知道这一点。谁能指导我?

更新 我拥有的数据如下所示(实际数据要长得多)。总体趋势下降,但中间有小幅上涨。

Time	Values
101	20.402
102	20.302
103	20.202
104	20.102
105	20.002
106	19.902
107	19.802
108	19.702
109	19.602
110	19.502
111	19.402
112	19.302
113	19.202
114	20.337
115	20.437
116	20.537
117	18.802
118	18.702
119	18.602
120	18.502
121	18.402
122	18.302
123	18.202
124	18.102
125	18.002
126	17.902
127	17.802
128	17.702
129	17.602
130	17.502
131	18.502
132	18.402
133	18.302
134	17.702
135	17.602
136	17.502
137	17.402
138	17.302
139	17.202
140	17.102
141	17.002

【问题讨论】:

  • 你能提供你目前使用过的任何代码吗?
  • 到目前为止,我还没有使用任何代码来计算上述几点,因为我是统计新手,不知道该怎么做。我有下面的代码从下面的函数中分割数据来绘制数据sns.lineplot(x="Time", y="Values", data=grouped_bel1800, ax=ba) sns.lineplot(x="Time", y="Values", data=grouped_bel1800_2, ax=ba)
  • 下面的代码似乎很好用,它计算 wole 数据框的回归线并给出距离。但是,由于我想要每个组的回归线和距离,这对我不起作用。谁能知道如何调整这个?可能是一个函数 & apply 可以工作,但我也无法做到这一点。下面评论中的代码
  • model = LinearRegression() x = data_train_bel1800.groupby(['Cycle','Type'])['Time'].head(2000)[:,np.newaxis] y = data_train_bel1800.groupby(['Cycle','Type'])['Values'].head(2000) model.fit(x, y) data_train_bel1800["Distance"] = (data_train_bel1800.groupby(['Cycle','Type'])['Values'].head(2000) - model.predict(data_train_bel1800.groupby(['Cycle','Type'])['Time'].head(2000)[:,np.newaxis]))

标签: python statistics linear-regression


【解决方案1】:

此示例使用您发布的数据并分别计算正误差和负误差,不包括恰好为零的误差。

import numpy


xData = numpy.array([101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0, 110.0, 111.0, 112.0, 113.0, 114.0, 115.0, 116.0, 117.0, 118.0, 119.0, 120.0, 121.0, 122.0, 123.0, 124.0, 125.0, 126.0, 127.0, 128.0, 129.0, 130.0, 131.0, 132.0, 133.0, 134.0, 135.0, 136.0, 137.0, 138.0, 139.0, 140.0, 141.0])
yData = numpy.array([20.402, 20.302, 20.202, 20.102, 20.002, 19.902, 19.802, 19.702, 19.602, 19.502, 19.402, 19.302, 19.202, 20.337, 20.437, 20.537, 18.802, 18.702, 18.602, 18.502, 18.402, 18.302, 18.202, 18.102, 18.002, 17.902, 17.802, 17.702, 17.602, 17.502, 18.502, 18.402, 18.302, 17.702, 17.602, 17.502, 17.402, 17.302, 17.202, 17.102, 17.002])


polynomialOrder = 1 # example straight line

# curve fit the test data
fittedParameters = numpy.polyfit(xData, yData, polynomialOrder)
print('Fitted Parameters:', fittedParameters)

modelPredictions = numpy.polyval(fittedParameters, xData)
fitErrors = modelPredictions - yData

positiveErrors = []
negativeErrors = []

# this logic excludes errors of exactly zero
for error in fitErrors:
    if error < 0.0:
       negativeErrors.append(error)
    if error > 0.0:
       positiveErrors.append(error)

print('Positive error statistics:')
print('    sum =',  numpy.sum(positiveErrors))
print('    min =',  numpy.min(positiveErrors))
print('    max =',  numpy.max(positiveErrors))
print('    mean =', numpy.mean(positiveErrors))

print()

print('Negative error statistics:')
print('    sum =',  numpy.sum(negativeErrors))
print('    min =',  numpy.min(negativeErrors))
print('    max =',  numpy.max(negativeErrors))
print('    mean =', numpy.mean(negativeErrors))

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    相关资源
    最近更新 更多