library(ggplot2) library(methods) library(lib4af) data(iris)
The lib4af
package provides an alternate method of handling linear regression models by using linear algebra, returning the result as an RC class object linreg
. The function has two arguments - the formula in order to identify the dependant variable y
and independent variables X
and the dataset from which the data is derived from.
The package receives the dependant and independent variables y ~ X
, as such:
y <- as.matrix(data[all.vars(formula)[1]]) X <- model.matrix(formula, data)
The regression coefficients are calculated using QR decomposition:
QR <- qr(X) Q <- qr.Q(QR) R <- qr.R(QR) Coefficients <<- backsolve(R, crossprod(Q,y))
QR decomposition is the decomposition of a matrix X
into a product of an orthogonal matrix Q
and an upper triangular matrix R
.
Additionally, the fitted values, residuals, degrees of freedom, residual variance, regression coefficients variance, t values for each coefficient and p values are calculated using ordinary linear algebra.
There are several methods implemented in the object linreg
:
This method prints out the coefficients and their names.
mod_object <- linreg$new(Petal.Length~Species, data = iris) mod_object$print()
This method uses ggplot2
for visualizing, for example "Scale-Location"
mod_object$plot()
This method returns the residuals.
head(mod_object$resid())
This method returns the predicted values.
head(mod_object$pred())
This method returns a named coefficient vector.
mod_object$coef()
This method returns a similar printout as the summary of lm
objects.
mod_object$summary()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.