Linear Model Estimation Using Ordinary Least Squares
Fits the usual weighted or unweighted linear regression model using the
same fitting routines used by lm
, but also storing the variance-covariance
matrix var
and using traditional dummy-variable coding for categorical
factors.
Also fits unweighted models using penalized least squares, with the same
penalization options as in the lrm
function. For penalized estimation,
there is a fitter function call lm.pfit
.
ols(formula, data=environment(formula), weights, subset, na.action=na.delete, method="qr", model=FALSE, x=FALSE, y=FALSE, se.fit=FALSE, linear.predictors=TRUE, penalty=0, penalty.matrix, tol=1e-7, sigma, var.penalty=c('simple','sandwich'), ...)
formula |
an S formula object, e.g.
|
data |
name of an S data frame containing all needed variables. Omit this to use a data frame already in the S “search list”. |
weights |
an optional vector of weights to be used in the fitting
process. If specified, weighted least squares is used with
weights |
subset |
an expression defining a subset of the observations to use in the fit. The default
is to use all observations. Specify for example |
na.action |
specifies an S function to handle missing data. The default is the function |
method |
specifies a particular fitting method, or |
model |
default is |
x |
default is |
y |
default is |
se.fit |
default is |
linear.predictors |
set to |
penalty |
|
penalty.matrix |
see |
tol |
tolerance for information matrix singularity |
sigma |
If |
var.penalty |
the type of variance-covariance matrix to be stored in the |
... |
For penalized estimation, the penalty factor on the log likelihood is
-0.5 β' P β / σ^2, where P is defined above.
The penalized maximum likelihood estimate (penalized least squares
or ridge estimate) of beta is (X'X + P)^{-1} X'Y.
The maximum likelihood estimate of σ^2 is (sse + β'
P β) / n, where
sse
is the sum of squared errors (residuals).
The effective.df.diagonal
vector is the
diagonal of the matrix X'X/(sse/n) σ^{2} (X'X + P)^{-1}.
the same objects returned from lm
(unless penalty
or penalty.matrix
are given - then an
abbreviated list is returned since lm.pfit
is used as a fitter)
plus the design attributes
(see rms
).
Predicted values are always returned, in the element linear.predictors
.
The vectors or matrix stored if y=TRUE
or x=TRUE
have rows deleted according to subset
and
to missing data, and have names or row names that come from the
data frame used as input data. If penalty
or penalty.matrix
is given,
the var
matrix
returned is an improved variance-covariance matrix
for the penalized regression coefficient estimates. If
var.penalty="sandwich"
(not the default, as limited simulation
studies have found it provides variance estimates that are too low) it
is defined as
σ^{2} (X'X + P)^{-1} X'X (X'X + P)^{-1}, where P is
penalty factors * penalty.matrix
, with a column and row of zeros
added for the
intercept. When var.penalty="simple"
(the default), var
is
σ^{2} (X'X + P)^{-1}.
The returned list has a vector stats
with named elements
n, Model L.R., d.f., R2, g, Sigma
. Model L.R.
is the model
likelihood ratio chi-square statistic, and R2
is
R^2. For penalized estimation, d.f.
is the
effective degrees of freedom, which is the sum of the elements of another
vector returned, effective.df.diagonal
, minus one for the
intercept.
g
is the g-index.
Sigma
is the penalized maximum likelihood estimate (see below).
Frank Harrell
Department of Biostatistics, Vanderbilt University
fh@fharrell.com
set.seed(1) x1 <- runif(200) x2 <- sample(0:3, 200, TRUE) distance <- (x1 + x2/3 + rnorm(200))^2 d <- datadist(x1,x2) options(datadist="d") # No d -> no summary, plot without giving all details f <- ols(sqrt(distance) ~ rcs(x1,4) + scored(x2), x=TRUE) # could use d <- datadist(f); options(datadist="d") at this point, # but predictor summaries would not be stored in the fit object for # use with Predict, summary.rms. In that case, the original # dataset or d would need to be accessed later, or all variable values # would have to be specified to summary, plot anova(f) which.influence(f) summary(f) summary.lm(f) # will only work if penalty and penalty.matrix not used # Fit a complex model and approximate it with a simple one x1 <- runif(200) x2 <- runif(200) x3 <- runif(200) x4 <- runif(200) y <- x1 + x2 + rnorm(200) f <- ols(y ~ rcs(x1,4) + x2 + x3 + x4) pred <- fitted(f) # or predict(f) or f$linear.predictors f2 <- ols(pred ~ rcs(x1,4) + x2 + x3 + x4, sigma=1) # sigma=1 prevents numerical problems resulting from R2=1 fastbw(f2, aics=100000) # This will find the best 1-variable model, best 2-variable model, etc. # in predicting the predicted values from the original model options(datadist=NULL)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.