嗨 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