Functional Labeled Optimal Partitioning - A peak detection algorithm that allows for three types of labels.
install.packages("FLOPART")
##OR
if(require("remotes"))install.packages("remotes")
remotes::install_github("alyssajs/FLOPART)The main driver function is FLOPART, which takes
data_vecAn integer vector of dataweight_vecA double vector of weights for each data pointdata_countThe number of data points (an integer)penaltyThe penalty value (a double)cost_matA numeric vector used for output - after execution, the first half of this vector contains the cost of being in the peak state for each data point, and the second half of this vector contains the cost of being in the background state for each data pointend_vecAn integer vector of segment ends - finite positive values represent segment endspoints, infinite or negative values are used as placeholdersmean_vecA double vector of segment means in reverse orderintervals_matAn integer vector that stores the number of cost function pieces - that is, the number of potential changepoints. Used to analyze time complexity.label_startsAn integer vector of indices where labels start (0-based)label_endsAn integer vector of indices where labels end (0-based)label_typesAn integer vector of label types where -1 corresponds to a peakEnd label, 0 corresponds to a noPeaks label, and 1 corresponds to a peakStart labellabel_countThe number of labels (should match the length of the label_starts, label_ends, and label_types vector) - an integer
FLOPART has an interface function to use FLOPART in R by calling the underlying C++ code.
library(FLOPART)
data_count = 6L
data_vec <- as.integer(c(1,10,10,10,10,1))
weight_vec <- as.double(rep(1, data_count))
penalty <- as.double(0)
cost_mat <- numeric(data_count * 2)
end_vec <- integer(data_count)
mean_vec <- numeric(data_count)
intervals_mat <- integer(data_count*2)
label_starts <- 1L
label_ends <- 3L
label_types <- 0L
label_count <- 1L
result <-
.C("FLOPART_interface", data_vec = data_vec, weight_vec = weight_vec,
data_count = data_count, penalty = penalty, cost_mat = cost_mat,
end_vec = end_vec, mean_vec = mean_vec, intervals_mat = intervals_mat,
label_starts = label_starts, label_ends = label_ends,
label_types = label_types, label_count = label_count,
PACKAGE="FLOPART")
costMatrix <- matrix(result[["cost_mat"]], nrow=2, ncol=data_count, byrow=TRUE)
> costMatrix
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 Inf Inf Inf -9.100866 -6.621371
[2,] 1 -3.876115 -6.621371 -8.11962 -9.053900 -7.417388Creating the cost matrix from the cost vector in the above way results in a matrix with the entries at [1,i] corresponding to the costs of being in the peak state for data point i and the entries at [2,i] corresponding to the costs of being in the background