lm(formula, data=<<see below>>, weights=<<see below>>, subset=<<see below>>, na.action=na.fail, method="qr", model=F, x=F, y=F, contrasts=NULL, ...)
The subset argument, like the terms in formula, is evaluated in the context of the data frame, if present. The specific action of the argument is as follows: the model frame, including weights and subset, is computed on all the rows, and then the appropriate subset is extracted. A variety of special cases make such an interpretation desirable (e.g., the use of lag or other functions that may need more than the data used in the fit to be fully defined). On the other hand, if you meant the subset to avoid computing undefined values or to escape warning messages, you may be surprised. For example, lm(y ~ log(x), mydata, subset = x > 0) will still generate warnings from log. If this is a problem, do the subsetting on the data frame directly: lm(y ~ log(x), mydata[mydata$x > 0, ])
Generic functions such as print and summary have methods to show the results of the fit. See lm.object for the components of the fit, but the functions residuals, coefficients, and effects should be used rather than extracting the components directly, since these functions take correct account of special circumstances, such as overdetermined models.
The response may be a single numeric variable or a matrix. In the latter case, coefficients, residuals, and effects will also be matrices, with columns corresponding to the response variables. In either case, the object inherits from class "lm". For multivariate response, the first element of the class is "mlm".
NAMES. Variables occurring in a formula are evaluated differently from arguments to S-PLUS functions, because the formula is an object that is passed around unevaluated from one function to another. The functions such as lm that finally arrange to evaluate the variables in the formula try to establish a context based on the data argument. (More precisely, the function model.frame.default does the actual evaluation, assuming that its caller behaves in the way described here.) If the data argument to lm is missing or is an object (typically, a data frame), then the local context for variable names is the frame of the function that called lm, or the top-level expression frame if the user called lm directly. Names in the formula can refer to variables in the local context as well as global variables or variables in the data object.
The data argument can also be a number, in which case that number defines the local context. This can arise, for example, if a function is written to call lm, perhaps in a loop, but the local context is definitely not that function. In this case, the function can set data to sys.parent(), and the local context will be the next function up the calling stack. See the third example below. A numeric value for data can also be supplied if a local context is being explicitly created by a call to new.frame. Notice that supplying data as a number implies that this is the only local context; local variables in any other function will not be available when the model frame is evaluated. This is potentially subtle. Fortunately, it is not something the ordinary user of lm needs to worry about. It is relevant for those writing functions that call lm or other such model-fitting functions.
Draper, N. R. and Smith, H. (1981). Applied Regression Analysis. (second edition). Wiley, New York.
Myers, R. H. (1986). Classical and Modern Regression with Applications. Duxbury, Boston.
Rousseeuw, P. J. and Leroy, A. (1987). Robust Regression and Outlier Detection. Wiley, New York.
Seber, G. A. F. (1977). Linear Regression Analysis. Wiley, New York.
Weisberg, S. (1985). Applied Linear Regression. Second Edition. Wiley, New York.
There is a vast literature on regression, the references above are just a small sample of what is available. The book by Myers is an introductory text that includes a discussion of much of the recent advances in regression technology. The Seber book is at a higher mathematical level and covers much of the classical theory of least squares.
lm(Fuel ~ . , fuel.frame)lm(cost ~ age + type + car.age, claims, weights = number, na.action = na.omit)
lm(freeny.y ~ freeny.x)
# myfit calls lm, using the caller to myfit # as the local context for variables in the formula # (see aov for an actual example) myfit <- function(formula, data = sys.parent(), ...) { .. .. fit <- lm(formula, data, ...) .. .. }