Section 24 Linear Model: function lm
24.1 lm
We can fit a linear regression model using the function lm
. It is available when R is installed and no additionall installation of package is required.
24.2 Usage
lm(formula, data, subset,
weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE,
singular.ok = TRUE, contrasts = NULL, offset, ...)
24.3 Arguments
Arguments | Explanation |
---|---|
formula |
an object of class formula (or one that can be coerced to that class): a symbolic description of the model to be fitted. |
data |
an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. If not found in data, the variables are taken from environment(formula), typically the environment from which lm is called. |
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
weights |
an optional vector of weights to be used in the fitting process. Should be NULL or a numeric vector. If non-NULL, weighted least squares is used with weights weights (that is, minimizing sum(w*e^2)); otherwise ordinary least squares is used. |
na.action |
a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is na.fail if that is unset. The default is na.omit . Another possible value is NULL , no action. Value na.exclude can be useful. |
method |
the method to be used; for fitting, currently only method = qr is supported. |
model, x, y, qr |
logicals. If TRUE the corresponding components of the fit (the model frame, the model matrix, the response, the QR decomposition) are returned. |
singular.ok |
logical. If FALSE (the default in S but not in R) a singular fit is an error. |
contrasts |
an optional list of contrasts, useful when models include factors. |
offset |
this can be used to specify an a priori known component to be included in the linear predictor during fitting. This should be NULL or a numeric vector of length equal to the number of cases. |