Skip to contents

Fits a GLM with intercept and single predictor using iteratively reweighted least squares (IRLS). Implements the model: $$g(E[y]) = \beta_0 + \beta_1 x + \text{offset}$$ where \(g\) is the link function determined by the selected family.

Arguments

x

Numeric vector of covariates (length n).

y

Numeric response vector (length n).

family

A stats::family object (e.g. gaussian(), binomial(), poisson()).

offset

Numeric scalar or vector (length n) giving the linear predictor offset.

max_iter

Integer. Maximum number of IRLS iterations.

tol

Numeric convergence tolerance for parameters.

Value

A list with components:

intercept

Numeric scalar: the estimated intercept \(\hat\beta_0\).

theta

Numeric scalar: the estimated coefficient \(\hat\beta_1\).

Details

This function implements the standard IRLS algorithm for GLMs, optimized for the univariate predictor case with intercept. It provides identical results to R's glm(y ~ x, family=family) but is more computationally efficient for the single-predictor case.

Numerical stability measures are implemented for handling extreme values, including special cases for Poisson regression and fallback methods for matrix solution if the standard solve fails.

Examples

if (FALSE) { # \dontrun{
set.seed(42)
n <- 100
x <- rnorm(n)

# Gaussian example
y_gaussian <- 2 + 0.5*x + rnorm(n)
result <- univariate_irls_glm(x, y_gaussian, gaussian(), offset=numeric(0))
print(result)

# Poisson example
eta <- 1 + 0.5*x
y_poisson <- rpois(n, exp(eta))
result <- univariate_irls_glm(x, y_poisson, poisson(), offset=numeric(0))
print(result)

# Binomial example
prob <- 1/(1 + exp(-(0.5 + 0.8*x)))
y_binomial <- rbinom(n, 1, prob)
result <- univariate_irls_glm(x, y_binomial, binomial(), offset=numeric(0))
print(result)
} # }