R/cnms2formula.R

Defines functions cnms2formula

cnms2formula <-
function(cnms) {
  # A function that builds a random effects formula from the ?component names?, 
  # a list that can be extracted from an lmerMod object by .@cnms or 
  # getME(., "cnms").
  #
  # Args: 
  #   cnms     = List from an lmerMod object by .@cnms or getME(., "cnms").
  #
  # Returns:
  #   reFormula = random effects part of a lmerMod formula
  #
  len      <- unlist(lapply(cnms, length))
  cnms     <- cnms[which(len != 0)]
  charForm <- character(length(cnms))
  
  for(i in 1:length(cnms)) {
    if (cnms[[i]][1] == "(Intercept)") {
      cnms[[i]][1] <- "1"
    } else {
      tpv <- cnms[[i]]
      cnms[[i]] <- append("",tpv)
      cnms[[i]][1] <- "-1"
    }
    charForm[i] <- paste("(", paste(cnms[[i]], collapse = " + "), 
                         " | ",names(cnms)[i], ")", sep = "")
  }
  
  reFormula <- paste(charForm, collapse = " + ")

  return(reFormula)
}

Try the cAIC4 package in your browser

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

cAIC4 documentation built on Sept. 22, 2021, 5:07 p.m.