Skip to contents

This class represents a generalized linear model with negative binomial distribution. It inherits from GLM and implements its functions that, for example, evaluate the conditional density and distribution functions.

Super classes

gofreg::ParamRegrModel -> gofreg::GLM -> NegBinomGLM

Methods

Inherited methods


Method fit()

Calculates the maximum likelihood estimator for the model parameters based on given data.

Usage

NegBinomGLM$fit(
  data,
  params_init = private$params,
  loglik = loglik_xy,
  inplace = FALSE
)

Arguments

data

tibble containing the data to fit the model to

params_init

initial value of the model parameters to use for the optimization (defaults to the fitted parameter values)

loglik

function(data, model, params) defaults to loglik_xy()

inplace

logical; if TRUE, default model parameters are set accordingly and parameter estimator is not returned

Returns

MLE of the model parameters for the given data, same shape as params_init


Method f_yx()

Evaluates the conditional density function.

Usage

NegBinomGLM$f_yx(t, x, params = private$params)

Arguments

t

value(s) at which the conditional density shall be evaluated

x

matrix of covariates, each row representing one sample

params

model parameters to use (list() with tags beta and shape), defaults to the fitted parameter values

Returns

value(s) of the conditional density function, same shape as t


Method F_yx()

Evaluates the conditional distribution function.

Usage

NegBinomGLM$F_yx(t, x, params = private$params)

Arguments

t

value(s) at which the conditional distribution shall be evaluated

x

matrix of covariates, each row representing one sample

params

model parameters to use (list() with tags beta and shape), defaults to the fitted parameter values

Returns

value(s) of the conditional distribution function, same shape as t


Method F1_yx()

Evaluates the conditional quantile function.

Usage

NegBinomGLM$F1_yx(t, x, params = private$params)

Arguments

t

value(s) at which the conditional quantile function shall be evaluated

x

matrix of covariates, each row representing one sample

params

model parameters to use (list() with tags beta and shape), defaults to the fitted parameter values

Returns

value(s) of the conditional quantile function, same shape as t


Method sample_yx()

Generates a new sample of response variables with the same conditional distribution.

Usage

NegBinomGLM$sample_yx(x, params = private$params)

Arguments

x

matrix of covariates, each row representing one sample

params

model parameters to use (list() with tags beta and shape), defaults to the fitted parameter values

Returns

vector of sampled response variables, same length as nrow(x)


Method clone()

The objects of this class are cloneable with this method.

Usage

NegBinomGLM$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# Use the built-in cars dataset
x <- datasets::cars$speed
y <- datasets::cars$dist
data <- dplyr::tibble(x=x, y=y)

# Create an instance of a NegBinomGLM
model <- NegBinomGLM$new()

# Fit a Negative Binomial GLM to the cars dataset
model$fit(data, params_init = list(beta=3, shape=2), inplace=TRUE)
params_opt <- model$get_params()

# Plot the resulting regression function
plot(datasets::cars)
abline(a = 0, b = params_opt$beta)

# Generate a sample for y for given x following the same distribution
x.new <- seq(min(x), max(x), by=2)
y.smpl <- model$sample_yx(x.new)
points(x.new, y.smpl, col="red")

# Evaluate the conditional density, distribution, quantile and regression
# function at given values
model$f_yx(y.smpl, x.new)
#>  [1] 0.0004021967 0.0410103566 0.0469285237 0.0386686964 0.0215782557
#>  [6] 0.0059793562 0.0220477710 0.0217933831 0.0194777683 0.0065222626
#> [11] 0.0174339511
model$F_yx(y.smpl, x.new)
#>  [1] 0.9991212 0.7653416 0.5079699 0.5018178 0.7711534 0.9438811 0.2167504
#>  [8] 0.2840711 0.5681305 0.8971193 0.3451189
model$F1_yx(y.smpl, x.new)
#> Warning: NaNs produced
#>  [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
y.pred <- model$mean_yx(x.new)
points(x.new, y.pred, col="blue")