Skip to contents

The ols_with_error() family can represent any non-Gaussian error, provided random variates can be drawn by an R function. 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()).

Usage

ols_with_error(error, ...)

Arguments

error

Function that can draw random variables from the non-Gaussian distribution, or a string giving the name of the function. For example, rt draws t-distributed random variates. The function must take an argument n indicating how many random variates to draw (as all random generation functions built into R do).

...

Further arguments passed to the error function to draw random variates, such as to specify degrees of freedom, shape parameters, or other parameters of the distribution. These arguments are evaluated with the model data in the environment, so they can be expressions referring to model data, such as values of the predictors.

Value

A family object representing this family.

See also

custom_family() for fully custom families, including for GLMs

Examples

# t-distributed errors with 3 degrees of freedom
ols_with_error(rt, df = 3)
#> 
#> Family: ols_with_error 
#> Link function: identity 
#> 

# A linear regression with t-distributed error, using error_scale to make
# errors large
population(
  x1 = predictor(rnorm, mean = 4, sd = 10),
  x2 = predictor(runif, min = 0, max = 10),
  y = response(0.7 + 2.2 * x1 - 0.2 * x2,
               family = ols_with_error(rt, df = 4),
               error_scale = 2.5)
)
#> Population with variables:
#> x1: rnorm(list(mean = 4, sd = 10))
#> x2: runif(list(min = 0, max = 10))
#> y: ols_with_error(0.7 + 2.2 * x1 - 0.2 * x2, error_scale = 2.5)

# Cauchy-distributed errors
ols_with_error(rcauchy, scale = 3)
#> 
#> Family: ols_with_error 
#> Link function: identity 
#> 

# A contaminated error distribution, where
# 95% of observations are Gaussian and 5% are Cauchy
rcontaminated <- function(n) {
  contaminant <- rbinom(n, 1, prob = 0.05)

  return(ifelse(contaminant == 1,
                rcauchy(n, scale = 20),
                rnorm(n, sd = 1)))
}
ols_with_error(rcontaminated)
#> 
#> Family: ols_with_error 
#> Link function: identity 
#>