Package 'glam'

Title: Generalized Additive and Linear Models (GLAM)
Description: Contains methods for fitting Generalized Linear Models (GLMs) and Generalized Additive Models (GAMs). Generalized regression models are common methods for handling data for which assuming Gaussian-distributed errors is not appropriate. For instance, if the response of interest is binary, count, or proportion data, one can instead model the expectation of the response based on an appropriate data-generating distribution. This package provides methods for fitting GLMs and GAMs under Beta regression, Poisson regression, Gamma regression, and Binomial regression (currently GLM only) settings. Models are fit using local scoring algorithms described in Hastie and Tibshirani (1990) <doi:10.1214/ss/1177013604>.
Authors: Andrew Cooper [aut, cre, cph]
Maintainer: Andrew Cooper <[email protected]>
License: MIT + file LICENSE
Version: 1.0.2
Built: 2024-11-08 04:28:04 UTC
Source: https://github.com/cran/glam

Help Index


Generalized Linear/Additive Model (GLAM)

Description

glam fits Generalized Linear Models (GLMs) and Generalized Additive Models (GAMs).

Usage

glam(x, y, model, family, intercept = TRUE, tol = 1e-08, max_iter = 100)

Arguments

x

a matrix with covariate data.

y

a vector of responses.

model

a string stating whether a GLM ("linear") or GAM ("additive") should be fit.

family

the distributional family from which to model response. Options are "beta", "binomial" (GLM only), "poisson", and "gamma".

intercept

a logical indicating whether an intercept is desired. Only applicable when model = "linear". Default is TRUE.

tol

the tolerance for convergence. Must be positive. Default is 1e-8.

max_iter

an integer specifying the maximum number of allowed iterations. Default is 100.

Details

This is a function for training and fitting Generalized Linear Models (GLMs) and Generalized Additive Models (GAMs). It implements these models using Iterative Reweighted Least Squares (IRLS) described in Hastie and Tibshirani 1990 (doi:10.1214/ss/1177013604). This function supports Beta regression, Logistic regression, Poisson regression, and Gamma regression (Logistic GAMs are currently not supported).

Value

The output is a list containing:

eta a vector of un-transformed fitted values.
mu a vector of transformed fitted values.
num_iter the number of iterations until convergence or timeout.
dev the convergence criteria achieved at the end.
coef a numeric vector of estimated coefficients (only applicable when model = "linear").

Examples

# generate random inputs
set.seed(10)
n <- 200
X <- sort(runif(n))
# Beta GLM vs. GAM
Y <- (X - 0.5)^2 + 0.5 + rnorm(n, sd = 1e-1)
beta_glm <- glam(cbind(X, X^2), Y, model = "linear", family = "beta")
beta_gam <- glam(cbind(X, X^2), Y, model = "additive", family = "beta")
plot(X, Y, pch = 20, xlab = "X", ylab = "Y", main = "Beta Regression Example")
lines(X, beta_glm$mu, col = "red", lwd = 2)
lines(X, beta_gam$mu, col = "blue", lwd = 2)
legend("bottomright", legend = c("GLM", "GAM"), col = c("red", "blue"), lwd = 2)