Skip to contents

Allows specification of the random component and link function for a response variable. In principle this could be used to specify any GLM family, but it is usually easier to use the predefined families, such as gaussian() and binomial().

Usage

custom_family(distribution, inverse_link)

Arguments

distribution

The distribution of the random component. This should be in the form of a function taking one argument, the vector of values on the inverse link scale, and returning a vector of draws from the distribution.

inverse_link

The inverse link function.

Value

A family object representing this family

Details

A GLM is specified by a combination of:

  • Random component, i.e. the distribution that Y is drawn from

  • Link function relating the mean of the random component to the linear predictor

  • Linear predictor

Using custom_family() we can specify the random component and link function, while the linear predictor is set in population() when setting up the population relationships. A family specified this way can be used to specify a population (via population()), but can't be used to estimate a model (such as with glm()).

See also

ols_with_error() for the special case of linear regression with custom error distribution

Examples

# A zero-inflated Poisson family
rzeroinfpois <- function(ys) {
  n <- length(ys)
  rpois(n, lambda = ys * rbinom(n, 1, prob = 0.4))
}

custom_family(rzeroinfpois, exp)
#> 
#> Family: custom_family 
#> Link function: inverse of exp 
#>