R/find_offset.R

Defines functions find_offset

Documented in find_offset

#' @title Find possible offset terms in a model
#' @name find_offset
#'
#' @description Returns a character vector with the name(s) of offset terms.
#'
#' @inheritParams find_predictors
#'
#' @return A character vector with the name(s) of offset terms.
#'
#' @examplesIf require("pscl")
#' # Generate some zero-inflated data
#' set.seed(123)
#' N <- 100 # Samples
#' x <- runif(N, 0, 10) # Predictor
#' off <- rgamma(N, 3, 2) # Offset variable
#' yhat <- -1 + x * 0.5 + log(off) # Prediction on log scale
#' dat <- data.frame(y = NA, x, logOff = log(off))
#' dat$y <- rpois(N, exp(yhat)) # Poisson process
#' dat$y <- ifelse(rbinom(N, 1, 0.3), 0, dat$y) # Zero-inflation process
#'
#' m1 <- zeroinfl(y ~ offset(logOff) + x | 1, data = dat, dist = "poisson")
#' find_offset(m1)
#'
#' m2 <- zeroinfl(y ~ x | 1, data = dat, offset = logOff, dist = "poisson")
#' find_offset(m2)
#' @export
find_offset <- function(x) {
  terms <- .safe(
    as.character(attributes(stats::terms(find_formula(x)[[1]]))$variables),
    find_terms(x)
  )
  offset <- NULL

  offcol <- grep("offset(", terms, fixed = TRUE)
  if (length(offcol)) {
    offset <- terms[offcol]
  }

  model_call <- get_call(x)
  if (is.null(offset) && object_has_names(model_call, "offset")) {
    offset <- safe_deparse(model_call$offset)
  }

  # fixest sometimes returns a weird macro syntax instead of the real offset
  # if we have to implement too many model-specific workarounds, it may eventually be worth it to do S3
  # VAB: no test because I can only replicate in a weird {etwfe} example
  if (inherits(x, "fixest")) {
    if (is.null(offset) || startsWith(offset, "..")) {
      offset <- x[["model_info"]][["offset"]]
    }
    offset <- sub("^~", "", offset)
  }

  offset <- clean_names(offset)

  # sometimes we get an empty list (e.g., fixest with iris dataset)
  if (length(offset)) {
    return(offset)
  } else {
    return(NULL)
  }
}

Try the insight package in your browser

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

insight documentation built on Nov. 26, 2023, 5:08 p.m.