my_lm | R Documentation |
Provides a custom implementation of R's stats::lm()
linear regression
function that uses the S3 system.
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, ...)
x |
Either a design matrix with dimensions |
... |
Not used. |
formula |
An object of class |
data |
An optional data frame, list or environment. |
y |
A |
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}}
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 residuals
df
: 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 the my_lm()
function was called.
summary.my_lm()
, print.my_lm()
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.