Learn R Programming

vcdExtra (version 0.9.1)

glmlist: Create a Model List Object

Description

glmlist creates a glmlist object containing a list of fitted glm objects with their names. loglmlist does the same for loglm objects.

get_models() extracts the model formulas or bracket notation from each model in a glmlist or loglmlist object. This is useful for labeling models in summaries and plots.

Usage

glmlist(...)

loglmlist(...)

# S3 method for glmlist coef(object, result = c("list", "matrix", "data.frame"), ...)

get_models(x, type = c("brackets", "formula"), abbrev = FALSE, ...)

Value

An object of class glmlist

loglmlist, just like a list, except that each model is given a name attribute.

A named character vector with the model formulas or bracket notations.

Arguments

...

Additional arguments passed to loglin2string (for loglmlist) such as sep and collapse.

object

a "glmlist" object

result

type of the result to be returned

x

A glmlist or loglmlist object

type

Type of output: "brackets" for loglinear bracket notation (e.g., "[AB] [C]"), or "formula" for R formula notation. For glmlist objects, only "formula" is meaningful.

abbrev

Logical or integer. If TRUE or a positive integer, abbreviate factor names to that many characters (default 1 when TRUE). Only applies to bracket notation.

Author

Michael Friendly; coef method by John Fox

Details

The intention is to provide object classes to facilitate model comparison, extraction, summary and plotting of model components, etc., perhaps using lapply or similar.

There exists a anova.glm method for glmlist objects. Here, a coef method is also defined, collecting the coefficients from all models in a single object of type determined by result.

The arguments to glmlist or loglmlist are of the form value or name=value.

Any objects which do not inherit the appropriate class glm or loglm are excluded, with a warning.

In the coef method, coefficients from the different models are matched by name in the list of unique names across all models.

Model notation

For loglmlist objects created by seq_loglm, the bracket notation distinguishes between models fit to marginal sub-tables and models fit to the full table. Parentheses are used for marginal sub-tables, e.g., "(Class) (Sex)", while square brackets are used for the full table, e.g., "[Class,Sex,Age] [Survived]".

The get_models function extracts these model strings, and the abbrev argument can be used to abbreviate factor names for more compact display, e.g., "[C,S,A] [S]".

For loglmlist objects created by seq_loglm, the bracket notation is stored in the model.string component. For other loglm objects, it is constructed from the margin component using loglin2string.

For glmlist objects, the formula is extracted using formula().

See Also

The function llist in package Hmisc is similar, but perplexingly more general.

The function anova.glm also handles glmlist objects

LRstats gives LR statistics and tests for a glmlist object.

glmlist, loglmlist, loglin2string, LRstats

Other glmlist functions: Kway(), LRstats(), mosaic.glmlist()

Other loglinear models: get_model(), joint(), seq_loglm()

Other glmlist functions: Kway(), LRstats(), mosaic.glmlist()

Examples

Run this code

data(Mental)
indep <- glm(Freq ~ mental+ses,
                family = poisson, data = Mental)
Cscore <- as.numeric(Mental$ses)
Rscore <- as.numeric(Mental$mental)

coleff <- glm(Freq ~ mental + ses + Rscore:ses,
                family = poisson, data = Mental)
roweff <- glm(Freq ~ mental + ses + mental:Cscore,
                family = poisson, data = Mental)
linlin <- glm(Freq ~ mental + ses + Rscore:Cscore,
                family = poisson, data = Mental)

# use object names
mods <- glmlist(indep, coleff, roweff, linlin)
names(mods)

# assign new names
mods <- glmlist(Indep=indep, Col=coleff, Row=roweff, LinxLin=linlin)
names(mods)

LRstats(mods)

coef(mods, result='data.frame')

#extract model components
unlist(lapply(mods, deviance))

res <- lapply(mods, residuals)
boxplot(as.data.frame(res), main="Residuals from various models")

data(Titanic)
# Sequential models of joint independence
tit.joint <- seq_loglm(Titanic, type = "joint")
get_models(tit.joint)
get_models(tit.joint, type = "formula")

# With abbreviated factor names
get_models(tit.joint, abbrev = TRUE)
get_models(tit.joint, abbrev = 2)

Run the code above in your browser using DataLab