#' Gamma Function for Calculating the Similarity Score
#'
#' @param seqlist List of sequences separated by time window.
#' @param allsqs Matrix containing all the possible SQS for this sequence's
#' elements.
#' @param sqsindex Index Matrix indicating which sequences have which SQS's.
#' @param catvec Vector of the different categories found in the sequences.
#' @param beta Score parameter. See below for conditions.
#' @param lam Score parameter. See below for conditions.
#'
#' @return Matrix of similarity scores for different sequences based on the
#' final sequence in the list.
#' @export
#'
#' @details
#' Ensure lam(k-1)k/2<beta to comply with the method constraints.
#'
#' @examples
#' df<-data.frame(Time=c("2020-01-01 00:10:09", "2020-01-01 01:12:34" , "2020-01-02 06:38:09",
#' "2020-01-02 07:21:51"),Cat=c('A','B','A','C'))
#' #For daily data:
#' seqlist<-SeqList(df,'%Y-%m-%d')
#' sqslist<-SQSList(seqlist,2)
#' allsqs<-AllSQS(c('A','B','C'),2)
#' sqsindex<-SQSIndex(allsqs,sqslist)
#' GammaFunction(seqlist,allsqs,sqsindex,c('A','B','C'),1,0.5)
GammaFunction <- function(seqlist, allsqs, sqsindex, catvec, beta, lam) {
kcheck<-max(as.numeric(allsqs[,3]))
if(lam*kcheck*(kcheck+1)/2>=beta){
warning('lam and beta dont satisfy the condition descibed in the details of the documention.' )
}
GamFunc <- matrix(0, dim(sqsindex)[1], dim(sqsindex)[2] - 1)
for (i in 1:dim(sqsindex)[1]) {
if (sqsindex[i, dim(sqsindex)[2]] == 1) {
for (j in 1:(dim(sqsindex)[2] - 1)) {
if (sqsindex[i, j] == 1) {
if (allsqs[i, 3] == "0") {
k <- 0
} else {
k <- as.numeric(allsqs[i, 3])
}
lambda1 <- rep(0, length(seqlist[[dim(sqsindex)[2]]][, 2]) - k)
for (y in 1:(length(seqlist[[dim(sqsindex)[2]]][, 2]) - k)) {
if ((seqlist[[dim(sqsindex)[2]]][, 2][y] == allsqs[i, 1]) & (seqlist[[dim(sqsindex)[2]]][, 2][(y + k)] == allsqs[i, 2])) {
test <- seqlist[[dim(sqsindex)[2]]][, 2][y:(y + k)]
lambda <- rep(1, length(seqlist[[j]][, 2]) - k)
nlambda <- 0
for (x in 1:(length(seqlist[[j]][, 2]) - k)) {
if ((test[1] == seqlist[[j]][, 2][x]) & (test[length(test)] == seqlist[[j]][, 2][x + k])) {
if (k <= 1) {
nlambda[2] <- 0
} else {
for (m in 1:(k - 1)) {
if (test[m + 1] == seqlist[[j]][, 2][x + m]) {
nlambda[m + 1] <- nlambda[m] + lam
} else {
nlambda[m + 1] <- nlambda[m]
}
}
}
lambda[x] <- sum(nlambda[-1]) + beta
} else {
lambda[x] <- 0
}
}
lambda1[y] <- sum(lambda)
}
}
GamFunc[i, j] <- sum(lambda1)
} else {
GamFunc[i, j] <- 0
}
}
} else {
GamFunc[i, ] <- rep(NA, dim(sqsindex)[2] - 1)
}
}
return(GamFunc)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.