terms.object
Class of Objects for Terms in a Model

Description

Objects of class c("terms", "formula") contain a modelling formula along with information gained by analyzing the formula. Other modelling functions can then use this extra information instead of analyzing the formula themselves.

Details

These objects are used by almost all formula-based model-fitting functions and are returned as a component of the fitted object by the fitting functions lm, etc. They are mainly of interest to people writing new modelling functions, not to users of modelling functions.
Generation
These objects are primarily generated by the terms function, but other functions like delete.response take a terms object and return a modified one and model.frame may add information to a terms object before attaching it to a model.frame object. (Functions that use a terms object should use the terms-specific attributes, not the formula itself, as the functions that alter a terms object may not alter the formula.)
Methods
Generic functions that have methods for "terms" objects include:
formula, labels.
Structure
The object is a formula object with added attributes that describe the formula. The following attributes should be present in all terms objects.
variables
a call to the list function (not a list!) whose arguments are expressions (generally names or calls) specifying the main (non-interaction) effects in the model. If there is a response term, it will be the the first argument in this call. To get the n-th variable use as.list(variables)[[n+1]].

factors
a numeric matrix with a row for each variable and a column for each expanded term (of the form v1:v2:...:vn) showing which variables occur in which terms. The entries are 0 if the variable does not occur in a term, 1 if it appears and should be coded by contrasts, and 2 if it appears and should be coded by dummy variables for all levels. The row labels are character versions of the expressions in the variables attribute and the column labels are identical to the "term.labels" attribute.

term.labels
a character vector with an entry for each expanded term in the formula for the form "v1:v2:...:vn" (indicating it is the n-th order interaction between variables v1, v2, ..., vn).

order
an integer vector with an entry for each expanded term giving the order of the terms (1 for main effects, 2 for two-way interactions, etc.)

intercept
1 if there is an intercept, 0 otherwise.

response
the variable number corresponding to the response (the expression on the left side of the formula) or 0 if there is no response in the formula. In practice, it is either 1 or 0.

.Environment
Copied from the formula that the terms object was derived from, it gives the environment in which the formula was created. Names in the formula, hence in the variables attribute of the terms object, should refer to objects in that environment or its ancestral environments.
The following attributes may or may not be present in a terms object.
specials
A list which is only present when the specials argument to the terms function is is used to name functions in the formula to flag. There will be one named entry in the list for each element of specials and that entry will contain the indices of the entries in the variables attribute that are calls to that special function. If there are no such calls the entry is set to NULL.

offset
A integer vector present only if there are calls to offset in the formula. It there are, this gives the indices of the entries in the variables attribute that are calls to offset.

predvars
This attribute is added by model.frame (via makepredictcall) before it attaches the terms attribute to the data.frame that it produces. It is usually a copy of the variables attribute but if an entry in that is a call to a function known to use data-dependent default values it will add to the call explicit arguments specifying the values of those default values based on the data argument to model.frame. E.g., poly's argument coefs depends on the range and norm of its main argument so those will be in the calls in the predvars attribute. Prediction code should use predvars instead of variables when evaluating the calls with new data.

dataClasses
This attribute is added by model.frame (via .MFclass) before it attaches the terms attribute to the data.frame that it produces. It gives a coded description of each entry in the variables attribute so that prediction code can check (via .checkMFClasses) that the variables in the newdata argument are compatible with the data argument used to fit the model.
See Also
formula.object, terms, model.frame, delete.response.
Examples
terms(Yield ~ Temp * Conc + Error(blocks), specials="Error")

Data <- data.frame(X1=sin(1:20), X2=1:20, X3=cos(1:20), X4=rep(c("Small","Medium","Large"),c(6,6,8))) terms(aov(X1 ~ poly(X2,2) + offset(0.1*X3) + X4, Data))

Package stats version 6.1.1-7
Package Index