R/consistent-term.R

Defines functions consistent_term

Documented in consistent_term

#' Consistent Terms
#'
#' Test whether the number of dimensions of terms in the same parameter are consistent.
#'
#' @inheritParams params
#' @return A logical vector indicating whether the number of dimensions is consistent.
#' @seealso [term-vector()] and [npdims()]
#' @export
#'
#' @examples
#' consistent_term(term("alpha[1]", "alpha[3]", "beta[1,1]", "beta[2,1]"))
#' consistent_term(term("alpha[1]", NA_term_, "beta[1,1]", "beta[2]"))
consistent_term <- function(x) {
  if(!vld_s3_class(x, "term") && !vld_s3_class(x, "term_rcrd")) {
    chkor_vld(vld_s3_class(x, "term"), vld_s3_class(x, "term_rcrd"))
  }
  x <- as_term_rcrd(x, repair = TRUE)
  names <- field(x, "par")
  x <- npdims_terms_impl(x)
  names(x) <- names
  y <- x[!is.na(x)]
  for (par in unique(names(y))) {
    bol <- names(y) == par
    y[bol] <- length(unique(y[bol])) == 1L
  }
  x[!is.na(x)] <- y
  as.logical(x)
}

Try the term package in your browser

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

term documentation built on Sept. 29, 2022, 5:14 p.m.