Provides a custom implementation of R's stats::lm()
linear regression
function that uses the S3 system.
Usage
my_lm(x, ...)
# Default S3 method
my_lm(x, ...)
# S3 method for class 'formula'
my_lm(formula, data = list(), ...)
# S3 method for class 'matrix'
my_lm(x, y, ...)
Arguments
- x
Either a design matrix with dimensions \(n \times p\) or
formula
with thedata
parameter specified- ...
Not used.
- formula
An object of class
formula()
which provides a symbolic description of the model to be fitted.- data
An optional data frame, list or environment.
- y
A
vector
of responses with dimensions \(n \times 1\).
Value
An object of class my_lm
that contains:
coefficients
: Estimated parameter values of \(\hat{\beta}\) with dimensions \(p \times 1\)cov_mat
: Covariance matrix of estimated parameter values with dimensions \(p \times p\).sigma
: Standard deviation of residualsdf
: Degrees of Freedom given by \(df = N - p\)fitted.values
: Fitted Values given by \(\hat{y} = X\hat{\beta}\)residuals
: Residuals given by \(e = y - \hat{y}\)call
: Information on how themy_lm()
function was called.
Details
Given a response vector \(y\) with dimensions \(n \times 1\), a design matrix \(X\) with dimensions \(n \times p\), a vector of parameters \(\beta\) with dimensions \(p \times 1\), and an error vector \(\epsilon\) with dimensions \(n \times 1\) from \(\epsilon \sim N\left( {0,{\sigma ^2}} \right)\), the standard linear regression model can be stated as:
$$y = {X'}\beta + \epsilon$$
The least ordinary squares (OLS) solutions are then:
$$\hat \beta = {\left( {{X'}X} \right)^{ - 1}}{X'}y$$ $$cov\left( {\hat \beta } \right) = {\sigma ^2}{\left( {{X^T}X} \right)^{ - 1}}$$
Examples
## Matrix interface
# Create a design matrix
x = cbind(1, mtcars$disp)
# Extract response
y = mtcars$mpg
# Calculate outcome
my_model = my_lm(x, y)
## Formula interface
# Calculate
my_model = my_lm(mpg ~ disp, data = mtcars)