Skip to contents

Mirrors the C# GeneralizedLinearModel class: fits a linear predictor mapped to the response scale by a link function, by maximizing the family log-likelihood with a local optimizer.

Usage

ml_glm(
  x,
  y,
  intercept = TRUE,
  link = "identity",
  local_method = "nelder_mead",
  robust_se = FALSE,
  newdata = NULL,
  alpha = 0.1
)

Arguments

x

a numeric matrix or data frame of predictors, one row per observation.

y

the response, one value per row of x.

intercept

fit an intercept term. Default TRUE.

one of "identity", "log", "logit", "probit", "complementary_log_log".

local_method

the optimizer: one of "nelder_mead" (the default), "bfgs", "powell", "adam", "gradient_descent".

robust_se

use the sandwich (heteroskedasticity-consistent) covariance rather than the delta-method one. Changes the standard errors, not the coefficients. Default FALSE.

newdata

optional predictors to predict for. When supplied the result gains prediction and prediction_intervals.

alpha

the interval level for prediction_intervals. Default 0.1, a 90% interval.

Value

a list with coefficients, standard_errors, z_values, p_values, sigma, df, n, aic, aicc, bic, vcov and residuals; plus prediction and prediction_intervals (columns lower, mean, upper) when newdata is supplied.

Details

The link selects the family as well as the transform: "identity" is Normal, "log" is Poisson, and "logit", "probit" and "complementary_log_log" are Binomial.

local_method accepts all five optimizers here, unlike optim_minimize()'s local_method argument (which takes three) – the two upstream classes construct different sets, and this surface follows each one rather than imposing a single list.

See also

linear_regression() for ordinary least squares by SVD.

Examples

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y <- c(2.1, 3.9, 6.2, 7.8, 10.1, 12.2, 13.8, 16.1, 18.0, 20.2)
fit <- ml_glm(x, y)
round(fit$coefficients, 3)
#> [1] 0.112 1.991