optimLm: Use optimization to estimate generalized linear models with...

Description Usage Arguments Details Examples

View source: R/optimLm.R

Description

Use optimization to estimate generalized linear models with arbitrary loss function

Usage

1
2

Arguments

formula

an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted.

data

an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. If not found in data, the variables are taken from environment(formula), typically the environment from which optimLm is called.

loss

the loss function taking three arguments: target values x, predictions y and weights w (see loss for details).

invlink

inverse link function (see family).

...

additional parameters passed to optim.

subset

an optional vector specifying a subset of observations to be used in the fitting process.

weights

an optional vector of 'prior weights' to be used in the fitting process. Should be NULL or a numeric vector.

na.action

a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is na.fail if that is unset. The 'factory-fresh' default is na.omit. Another possible value is NULL, no action. Value na.exclude can be useful.

start

starting values for the parameters in the linear predictor.

Details

optimLm is a wrapper around optim function that can be used to estimate the parameters of generalized linear model

β = argmin{ loss(Y, invlink(Xβ)) }

where invlink is an inverse link function (see family) and loss is a user-specified loss function.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
myloss <- function(x, y, w) {
  r <- x-y
  out <- numeric(length(r))
  out[r<0] <- r[r<0]^2
  out[r>=0] <- abs(r[r>=0])
  sum(w * out)
}

optimLm(mpg ~ ., data = mtcars, loss = myloss)
optimLm(mpg ~ ., data = mtcars, loss = huberloss(epsilon = 4))
optimLm(mpg ~ ., data = mtcars, loss = thresholdloss(epsilon = 4))

twolodzko/twextras documentation built on May 3, 2019, 1:52 p.m.