R/randomPermutation.R

Defines functions randomPermutation

Documented in randomPermutation

randomPermutation <- function(x) {
  if (!(inherits(x, "JAGSrun") && inherits(x$model, "BMMmodel"))) 
    stop("Use only with 'JAGSrun' objects with model of class 'BMMmodel'.")
  k <- x$model$data$k
  n <- dim(x$results)
  permutedIndex <- as.vector(t(apply(matrix(seq_len(n[1]*k), ncol = k), 1, sample, size = k)))
  variables <- x$variables
  dropIndex <- NULL
  for (i in seq_along(variables)) {
    name <- variables[i]
    ii <- grep(name, colnames(x$results))
    if (length(ii) == k) {
      dummy <- x$results[,ii]
      dummy <- dummy[permutedIndex]
      x$results[,ii] <- dummy
    }
    else if (length(ii) > 1) {
      x$results <- x$results[,-ii]
      dropIndex <- c(dropIndex, i)
    }
  }
  if (length(dropIndex)) {
    x$variables <- x$variables[-dropIndex]
    warning("Variables have been dropped.")
  }
  x
}

Try the bayesmix package in your browser

Any scripts or data that you put into this service are public.

bayesmix documentation built on April 14, 2023, 12:27 a.m.