Regroup histories for CMR analysis

If you have daily CMR data, it can be too huge information for CMR analysis using Rmark. A solution is to group data by time period. Here is a solution without using any for loop.
(With a modification by Alexandre Girard)

regrouphistory <- function(history, group=7) {
  if (any(sapply(history, nchar)!=nchar(history[1]))) stop("All histories must be of the same length")
  h1 <- strsplit(history, "")
  factor <- as.character(1:((length(h1[[1]]) %/% group)+1))
  f <- as.character(sapply(factor, FUN=function(x) rep(x, group)))[seq_along(h1[[1]])]
  r <- sapply(h1, function(h) {
  h3 <- vapply(split(h, f), function(x) ifelse(any(x=="1"), "1", "0"), FUN.VALUE = "0")
  h3 <- h3[order(as.numeric(names(h3)))]
  return(paste0(h3, collapse = ""))
  })
  return(r)
}

histoire <- "000000001000010100000100000100001000000000000"
regrouphistory(histoire, group=10)
[1] "11110"
histoire <- c("000000001000010100000100000100001000000000000", "000000001000010100000100000100001000000000000")
regrouphistory(histoire, group=7)
[1] "0111100" "0111100"


Note that the last history has generally fewer information than the previous ones. It can generate problem. It is better to remove it.

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04