IndexFuncHk <- function(S0,S,Coef0,Coef){ C0 <- diff(Coef0) C <- diff(Coef) L0 <- length(S0) L <- length(S) S0 <- S0[-c(1,L0)] S <- S[-c(1,L)] S.merge <- c(S0,S) S.merge <- unique(sort(S.merge)) C0.rep <- rep(0,length(S.merge)) C.rep <- rep(0,length(S.merge)) for(i in 1:length(S.merge)){ match.S0 <- match(S.merge[i],S0) if (!is.na(match.S0)) C0.rep[i] <- C0[match.S0] else C0.rep[i] <- 0 } for(i in 1:length(S.merge)){ match.S <- match(S.merge[i],S) if (!is.na(match.S)) C.rep[i] <- C[match.S] else C.rep[i] <- 0 } Lambda <- NULL for(i in 1:length(C.rep)){ if(C.rep[i] < 0) Lambda <- c(Lambda,C0.rep[i]/(C0.rep[i]-C.rep[i])) if(C.rep[i] == 0) Lambda <- Lambda if(C.rep[i] > 0) Lambda <- c(Lambda,1) } lambda <- min(Lambda) index <- match(lambda,Lambda) index <- index +1 index }