This utility function finds the elbow in a curve which is concave relative to a line drawn between the first and last points. The elbow is defined as the point with the greatest orthogonal distance from that line.
findElbow(y, ylab = "y values", plot = FALSE, returnIndex = TRUE)
Numeric vector of y values for the curve.
Y-axis label.
Logical. Should a plot be made?
Logical. Should the return value be the index of the elbow point?
If returnIndex = TRUE
, the index of
the elbow point. If returnIndex = FALSE
,
a data frame containing an index values (x),
the y values passed to the function, and the
the orthogonal distances of the y values from
the line connecting the first and last points.
which.max(data_frame_name$dist)
will give the index of
the elbow point.
This function makes some simple checks that the data is concave as defined above. Even so, it may give answers in some cases that are not valid. Please check on typical data that you encounter to verify that it works in your cases.
The concept of this function is based on the clever idea in the Stackoverflow post at stackoverflow.com/a/2022348/633251 and relies on code posted at paulbourke.net/geometry/pointlineplane/pointline.r (referenced in the SO post). Minor modifications to the code were made to that code in order to vectorize it.