This class represents a generalized linear model with Gamma 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
-> GammaGLM
Methods
Method fit()
Calculates the maximum likelihood estimator for the model parameters based on given data.
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 tologlik_xy()
inplace
logical
; ifTRUE
, default model parameters are set accordingly and parameter estimator is not returned
Method f_yx()
Evaluates the conditional density function.
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
Method F_yx()
Evaluates the conditional distribution function.
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
Method F1_yx()
Evaluates the conditional quantile function.
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
Method sample_yx()
Generates a new sample of response variables with the same conditional distribution.
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
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 GammaGLM
model <- GammaGLM$new()
# Fit an Gamma GLM to the cars dataset
model$fit(data, params_init = list(beta=3, shape=1), 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] 2.631940e-02 2.680607e-02 4.768020e-02 5.822071e-03 3.208479e-02
#> [6] 9.263047e-03 7.369133e-03 1.690065e-02 1.896789e-02 1.613904e-02
#> [11] 2.219092e-05
model$F_yx(y.smpl, x.new)
#> [1] 0.91013645 0.04664996 0.32487047 0.95373608 0.38730096 0.88558751
#> [7] 0.89760357 0.65364127 0.31196951 0.23359127 0.99966102
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")