Skip to contents

Computes the log-likelihood for a univariate generalized linear model (GLM) with a single covariate, intercept, and optional offset. Supports any family from stats::family() (e.g.\ Gaussian, Binomial, Poisson).

Arguments

x

Numeric vector of length n: the single covariate.

y

Numeric vector of length n: response values (for Binomial, can be 0/1 or a two-column matrix of counts).

family

An R family object (from stats::family(), default gaussian()).

theta

Numeric scalar: coefficient at which to evaluate the log-likelihood.

offset

Numeric vector of length n, or scalar, default 0: optional offset in the linear predictor.

intercept

Numeric scalar: intercept term in the linear predictor, default 0.

Value

Numeric scalar: the (profiled) log-likelihood at theta and intercept.

Details

The function forms the linear predictor η = intercept + offset + θ * x, then uses the family's linkinv and dev.resids functions to compute deviance residuals d_i. The log-likelihood is $$\ell = -\,\sum_i d_i / 2,$$ profiling out dispersion for families that estimate it (Gaussian, Gamma, inverse.gaussian) and leaving it fixed for others (Binomial, Poisson).

Examples

if (FALSE) { # \dontrun{
x <- rnorm(100)
y <- 1.5 + 2 * x + rnorm(100)
univariate_loglik_glm(x, y, family = gaussian(), theta = 2, intercept = 1.5)

x <- runif(200, -2, 2)
p <- plogis(0.5 + 1.5 * x)
y_bin <- rbinom(200, 1, p)
univariate_loglik_glm(x, y_bin, family = binomial(), theta = 1.5, intercept = 0.5)

x_p <- rpois(150, lambda = 2)
mu <- exp(-1 + 0.3 * x_p)
y_p <- rpois(150, mu)
univariate_loglik_glm(x_p, y_p, family = poisson(), theta = 0.3, intercept = -1)
} # }