嗨 JMP 社区,
我需要为简单的时间序列数据计算曲线下的有符号区域(参见下面的模拟数据)。

具体来说,我需要计算零以上的面积(如上图所示)为正,零以下的面积为负,包括从正到负的局部偏移和倒数。
例子:
- 曲线 A 的 AUC = 阳性区域 1 + 阴性区域 2 + 阳性区域 3
- 曲线 C 的 AUC = 正面积 1 + 负面积 2
- 曲线 E 的 AUC = 正区域 1
Polygon Area () 函数似乎无法正确处理更复杂的时间序列,这可能是由于交叉段。 此外,该函数不返回面积的有符号值。 您知道我可以用来实现目标的插件或功能吗?
谢谢。
最好的,
TS
梯形规则?
Names default to here(1);
dt = current data table();
x_col = Column(dt, "Time (Week)");
y_col = Column(dt, "Y");
by_col = Column(dt, "Curve");
auc = function({x_col, y_col, by_col},
{DEFAULT LOCAL},
Summarize(dt, by_values=By(by_col));
areas = [=>];
for(i=1, i<=nitems(by_values), i++,
by_value = by_values[i];
rows = dt << Get Rows Where(by_col[]==by_value); // get he rows for the curve
x_values = x_col[rows];
r = rank(x_values); // make sure it's ordered right
x_values = x_values[r];
y_values = y_col[rows][r];
n = nitems(x_values);
//trapezoid rule
x_1 = x_values[1::n-1];
x_2 = x_values[2::n];
y_1 = y_values[1::n-1];
y_2 = y_values[2::n];
x_intervals = abs(x_2 - x_1);
y_avg = (y_2 + y_1)/2;
area = sum(x_intervals :*</img> y_avg);
areas[by_value] = area
);
return(areas);
);
areas = auc(x_col, y_col, by_col);
show(areas); // areas = ["A" => 10.5, "B" => -7.5, "C" => -16.5, "D" => 6, "E" => 24];
此外,您正在做的不是多边形(如果您在其他地方使用它来做类似的事情)。多边形的面积永远不能为负,否则它就不会存在。
脚本索引中的示例向您展示了它在做什么。
Names Default To Here( 1 );
area = Polygon Area( {0, 0}, {0, 10}, {10, 10}, {10, 0} );
它关闭 {10, 0} 和 {0, 0} 之间的循环(在本例中为正方形)。所以多边形面积为 100。你把它放在 x/y 平面的什么位置并不重要。10x10 正方形的面积应为 100。
Names Default To Here( 1 );
area = Polygon Area( {-100, -100}, {-100, -90}, {-90, -90}, {-90, -100} );
show(area); //100