R/helpers.R

Defines functions .onUnload temp_var partial_mu_eta check_factor

### Helper functions (not exported)


# Checks if variable is a factor and transforms if necessary
check_factor <- function(x) {
  if (is.factor(x)) {
    droplevels(x)
  } else {
    factor(x)
  }
}


# Higher order partial derivatives of \mu with respect to \eta
partial_mu_eta <- function(eta, family, order) {
  f <- family[["mu.eta"]](eta)
  if (order == 2L) {
    if (family[["link"]] == "logit") {
      f * (1.0 - 2.0 * family[["linkinv"]](eta))
    } else {
      - eta * f
    }
  } else {
    if (family[["link"]] == "logit") {
      f * ((1.0 - 2.0 * family[["linkinv"]](eta))^2 - 2.0 * f)
    } else {
      (eta^2 - 1.0) * f
    }
  }
}


# Returns suitable name for a temporary variable
temp_var <- function(data) {
  repeat {
    tmpvar <- paste0(sample(letters, 5L, replace = TRUE), collapse = "")
    if (!(tmpvar %in% colnames(data))) {
      break
    }
  }
  tmpvar
}


# Unload
.onUnload <- function(libpath) {
  library.dynam.unload("bife", libpath)
}

Try the bife package in your browser

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

bife documentation built on Aug. 11, 2022, 5:11 p.m.