Skip to contents

Supply the same family object you would use in glm(). These examples use a common design with three active predictors and a GIC penalty. An unpenalized intercept is included in each fit. The helper returns the selected predictor indices, with indexing starting at the first column of X.

set.seed(34)
n <- 300L
X <- matrix(rnorm(n * 20L), n, 20L)
eta <- 0.4 + 0.6 * X[, 2] - 0.5 * X[, 7] + 0.4 * X[, 12]
fit <- function(y, family) {
  l0glmFit(X, y, family=family, intercept=TRUE, lambda="gic")
}
selected <- function(model) which(as.numeric(coef(model))[-1L] != 0)

Gaussian and quasi with constant variance

Use an identity link for a sparse linear model. A quasi family lets you specify a link and variance function directly. Here a constant variance gives the same mean model as Gaussian regression.

y <- eta + rnorm(n, sd=0.5)
gaussian_fit <- fit(y, gaussian())
quasi_fit <- fit(y, quasi(link="identity", variance="constant"))
selected(gaussian_fit)
#> [1]  2  7 12
selected(quasi_fit)
#> [1]  2  7 12

Binomial and quasibinomial

A binary response can be given as zeroes and ones. For proportions calculated from trial counts, pass the number of trials as weights. Quasibinomial allows extra variability around the binomial mean.

prob <- plogis(1.5 * eta)
binary <- fit(rbinom(n, 1, prob), binomial())
trials <- rep(12, n)
successes <- rbinom(n, trials, plogis(qlogis(prob) + rnorm(n, sd=0.5)))
quasi_binary <- l0glmFit(X, successes / trials, weights=trials,
  family=quasibinomial(), intercept=TRUE, lambda="gic")
selected(binary)
#> [1]  2  7 12
selected(quasi_binary)
#> [1]  2  7  8 11 12

Poisson and quasipoisson

The log link gives multiplicative effects on the mean. With quasipoisson, the variance is a fitted or supplied multiple of the mean. The negative binomial tutorial covers two likelihood models for overdispersed counts.

mu <- exp(eta)
counts <- fit(rpois(n, mu), poisson())
overdispersed <- fit(rnbinom(n, mu=mu, size=2), quasipoisson())
selected(counts)
#> [1]  2  7 12
selected(overdispersed)
#> [1]  2  7 12 14

Gamma with fixed shape or common scale

Use Gamma(link="log") for positive continuous responses with \operatorname{Var}(Y_i)=\phi\mu_i^2. Its Gamma shape is 1/\phi. Use gammaScale() when the variance is proportional to the mean. It uses a common scale b, shape \mu_i/b, and variance b\mu_i.

fixed_shape <- fit(rgamma(n, shape=8, scale=mu / 8), Gamma(link="log"))
b <- 0.2
common_scale <- fit(rgamma(n, shape=mu / b, scale=b), gammaScale(scale=b))
selected(fixed_shape)
#> [1]  2  7 12
selected(common_scale)
#> [1]  1  2  7 12

When recorded zeroes denote intensities below a detection limit, gammaCensored(dispersion=1/8, censor=0.5) retains the fixed-shape model and includes those observations through their lower-tail probabilities. gammaScale() also accepts a detection limit. Signal families reference

Inverse Gaussian

This model is useful for positive outcomes whose variance grows cubically with the mean, \operatorname{Var}(Y_i)=\phi\mu_i^3.

duration <- statmod::rinvgauss(n, mean=mu, dispersion=0.08)
inverse_gaussian <- fit(duration, inverse.gaussian(link="log"))
selected(inverse_gaussian)
#> [1]  2  7 10 12
predict(inverse_gaussian, X[1:4, ], type="response")
#>           [,1]
#> [1,] 0.4024799
#> [2,] 1.0660856
#> [3,] 1.6695728
#> [4,] 0.5752356

See the R family reference for the available links and the model selection tutorial for tuning by information criteria or cross-validation.