lm.fit

General fitting for linear (regression) models

Provides general fitting for linear (regression) models and fitting for linear models with weights. Includes support for the functions gam() and lm().

lm.fit(x, y, offset = NULL, method = "qr", tol = 1e-07, singular.ok = TRUE, ...) lm.wfit(x, y, w, offset = NULL, method = "qr", tol = 1e-07, singular.ok = TRUE, ...) .lm.fit(x, y, tol = 1e-07)

x | a numeric matrix that specifies the predictors in a linear model. To fit an intercept in the model, x must contain a column of 1s. |

y | a matrix or numeric vectors that specify the values of the response variable in a linear model. |

w | a numeric vector that specifies the weights for each observation (row) in x. Before the function performs the fit, x andy are multiplied by sqrt(w). |

offset | a numeric vector subtracted from y before the fit calculation, and then added to the result fitted.values after the fit. |

method | a character string that specifies how to do the fitting. The only option is "qr", meaning to use the QR decompostion of the x matrix. |

tol | a small positive numeric value that is used to determine when to drop a term from the model because it would make it numerically singular. The default is 1e-07. |

singular.ok | a logical value. If FALSE, and if the x matrix is numerically singular, it gives an error. If TRUE (the default), it assigns NA (or 0 in .lm.fit) as the coefficient of the columns that would make it singular, and then continues. |

... | Not used. |

None of the numeric arguments can contain non-finite values (NAs, NaNs, Inf, or -Inf).

lm.fit, lm.wfit and .lm.fit use the QR decomposition
to solve the linear least squares problem. They use a modification of the LAPACK QR
decomposition routine dgeqp3 that does limited pivoting: Linearly-redundant
columns are moved to the the end as they are discovered, but the order of the
linearly-independent columns (those that end up in the model) is not changed.

lm.fit and lm.wfit rearrange the coefficients and other components
after the pivoting so that they are in the order implied by the inputs.

.lm.fit does not rearrange coefficients and other components,
but it returns the pivoting vector and a flag indicating if such rearrangement is needed.
.lm.fit inserts zeros in the coefficients where lm.fit would insert NAs,
so further matrix arithmetic is easier to perform.
Also, .lm.fit does not copy the dimnames from x to relevant
parts of its output.
.lm.fit is considerably faster than lm.fit.

Value

lm.fit and lm.wfit return a list. The members of the list are as follows:

.lm.fit returns a list with following components. (Many are the same as those described for the return value
list items of lm.fit.)

"coefficients" | a least-square solution of the fitting problem. The names of coefficients are taken from the column names of x, or given names "x1" to "xN". If column count is higher than the rank of x, missing values are placed in the inestimable coefficients. |

"residuals" | residuals from the fit. |

"fitted.values" | fitted values from the fit. |

"effects" | orthogonal, single-degree-of-freedom effects. These are computed using the formula t(Q) %*% y. The first rank of the results correspond to degrees of freedom in the model and are named accordingly. |

"weights" | a copy of input weights. w for lm.wfit. |

"rank" | the computed rank (the number of linearly-independent columns in the model matrix). |

"assign" | a copy of the assign attribute on the matrix x. |

"qr" | a qr decomposition object. See qr for the structure of the object. |

"df.residual" | the number of degrees of freedom for residuals. |

"qr" | a matrix the same size as x. The same as the "qr" component of the qr decomposition object that lm.fit returns. |

"coefficients", "residuals", "effects", "rank" | the same as the return value list items describe for lm.fit. |

"pivot" | the pivoting of columns of x used to produce the decomposition. The same as the "pivot" component of the qr decomposition object that is returned in a list for lm.fit. |

"qraux" | a vector of length ncol(x) containing part of the compact representation of the Q matrix. The ame as the "qraux" component of qr decomposition object that is returned in a list for lm.fit. |

"tol" | the tolerance used for QR decomposition. |

"pivoted" | a logical value. If TRUE, indicates that QR decomposition pivoted. |

See Also

Examples

# Linear regression with intercept fitI <- lm.fit(x=cbind(1,as.matrix(Sdatasets::fuel.frame[,c("Weight","Disp.")])), y=Sdatasets::fuel.frame$Mileage) coef(fitI)

# Linear regression without intercept fitNI <- lm.fit(x=as.matrix(Sdatasets::fuel.frame[,c("Weight","Disp.")]), y=Sdatasets::fuel.frame$Mileage) coef(fitNI)