LR: Linear Regression

Description Usage Arguments Value Examples

View source: R/LR.R

Description

LR is used to fit linear model. It yields the same results as lm( ), summary(lm( )), confint( ), and hatvalues(lm( )) functions. In addition to the conventional residuals, LR also yields standardized residuals, studentized residuals, and externally studentized residuals. Further, this function can also be used to predict outcome based on fitted model.

Usage

1
LR(formula, data, include.intercept = TRUE, to.predict = NULL)

Arguments

formula

An object of class "formula": a symbolic description of the model to be fitted. A typical model has the form outcome ~ covariates where outcome is the numeric response vector (which people usually denote as Y in statistical formula) and covariates are predictor of response.

data

A data frame (or object coercible by as.data.frame to a data frame) containing the variables in the model.

include.intercept

If the model should fit with intercept, include.intercept = TRUE; if model should fit without intercept, then include.intercept = FALSE. The default setting for include.intercept is TRUE.

to.predict

The default argument is set to NULL. If wants to use the current model for prediction, enter a n by p matrix (or object coercible by as.matrix to a matrix) to obtain predicted values. n is the number of prediction desired, p is the number of covariates included in the model.

Value

LR does not explicitly return anything unless extract the value with $ followed with the name of desired output. The returned output is a list containing at least the following components:

coefficients

a named vector of coefficients

residuals

the residuals (i.e. response minus fitted values)

standardized_res

the standardized residuals

studentized_res

the internally studentized residuals

ex_stud_res

the externally studentized residuals

fitted.values

the predicted value

sigma

the residual standard error

leverage

obtain leverage

df

degrees of freedom

coeff_summary

mimic the result from using summary(lm()) which includes estimates of beta coefficients, standard error, t value, and p-value

R_squared

the proportion of the variance for a dependent variable that's explained by independent variable(s)

adj_R_squared

a penalized version of R_squared

CI

95% confidence interval of estimates (i.e. coefficients)

fstatistic

Give the overall F statistic and its corresponding degrees of freedom of numerator and denominator

p_value_F_test

p-value for overall F test

predicted

Give the predicted value using the current fitted model

Examples

1
2
3
4
5
6
7
LR(mpg ~ cyl + wt, mtcars)$coefficients ## Obtain beta coefficient estimates
LR(mpg ~ cyl + wt + disp, mtcars)$coeff_summary ## Obtain summary of beta coefficients
LR(mpg ~ cyl + wt, mtcars)$sigma ## Obtain residual standard error
LR(mpg ~ cyl + wt + qsec, mtcars)$CI ## Obtain 95% confidence interval
LR(mpg ~ cyl + wt, mtcars, include.intercept = FALSE) ## omitting intercept
LR(mpg ~ cyl + wt + qsec + disp, mtcars, include.intercept = FALSE)$df ## Extract degrees of freedom when fitting a model without an intercept
LR(cyl~mpg+wt, mtcars, to.predict = matrix(c(mean(mtcars$mpg), mean(mtcars$wt)), 1, 2))$predicted

AChenAC/LinearRegression documentation built on Dec. 17, 2021, 6:41 a.m.