knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

linr

R-CMD-check codecov

linr is used to fit a linear model. In this function, linear regression can be done by three matrix decomposition methods, which are the QR decomposition, Cholesky decomposition and the singular value decomposition (SVD). The defalt fitting method used is the Cholesky decomposition method. All three decomposition methods can fit linear model with high efficiency.

Installation

You can install the development version of linr from GitHub with:

# install.packages("devtools")
devtools::install_github("SelinaSong0412/linr")

Usage

To learn the whole usage of linr, you can see vignette("Intro_to_linr", package = "linr") for an overview of the package. Also see vignette("Efficiency_tests", package = "linr" for efficiency testing of linr, which compares output with the stats::lm function.

Example

The following are basic examples which shows you how to fit a linear model with linr You can look at the examples and practice with the whole usage of linr.

First, library 'linr' function.

library(linr)

Using linr with defined vector Y (observations) and vector/matrix X (predictions). (Without dataset)

Conducting simple linear regression with linr

You can fit your model like this:

y = rnorm(300)
x = rnorm(300)
model.linr <- linr(y ~ x)

Then you could check many items generated by linr as follows:

model.linr$Call                      # This is how your model looks like
data.frame(model.linr$coefficients,  # Coefficient estimators
           model.linr$std.error,     # Standard error of estimators
           model.linr$T_statistic,   # T statistics of estimators
           model.linr$p_value.T)     # p value for T test 

# Other items also can be checked by 
data.frame(model.linr$MSE,           # Mean square error 
           model.linr$R.square,      # R^2 
           model.linr$Adj.R.square,  # Adjusted R^2 
           model.linr$F_statistic,   # F statistics of estimators
           model.linr$p_value.F)     # p value for F test 

# You could also look at the fitted values and residuals like this:
# head(model.linr$fitted.values)       # Look at the first 6 fitted values
# head(model.linr$residuals)           # Look at the first 6 residuals

Conducting multiple linear regression with linr

You can fit your multiple regression model with 3 options of matrix decompositiom methods:

(1). QR decompositiom methods

(2). SVD decompositiom methods

(3). Cholesky decompositiom methods (Default)

Here we first look at the default method:

Y = rnorm(300)
X = matrix(rnorm(6000), nrow = 300, ncol = 20)
model.linr.mul <- linr(Y ~ X, method = "cholesky")
# model.linr.mul <- linr(Y ~ X)                  # This do the same thing

You can use the other two method like this.

# model.linr.mul <- linr(Y ~ X, method = "qr")   # Using QR decomposition
# model.linr.mul <- linr(Y ~ X, method = "svd")  # Using SVD decomposition

Then you could check many items generated by linr as follows:

model.linr.mul$Call                      # This is how your model looks like
data.frame(model.linr.mul$coefficients,  # Coefficient estimators
           model.linr.mul$std.error,     # Standard error of estimators
           model.linr.mul$T_statistic,   # T statistics of estimators
           model.linr.mul$p_value.T)     # p value for T test 

# Other items also can be checked by 
head(data.frame(model.linr.mul$MSE,           # Mean square error 
           model.linr.mul$R.square,      # R^2 
           model.linr.mul$Adj.R.square,  # Adjusted R^2 
           model.linr.mul$F_statistic,   # F statistics of estimators
           model.linr.mul$p_value.F))    # p value for F test 

# You could also look at the fitted values and residuals like this:
# head(model.linr.mul$fitted.values)       # Look at the first 6 fitted values
# head(model.linr.mul$residuals)           # Look at the first 6 residuals

Using linr with regression formula and your own dataset

Conducting simple linear regression with linr on your data

# Here we use the R build-in dataset cars as an example:
# fit a linear model with dist as outcome and speed as predictor
model.linr.cars <- linr(dist ~ speed, data = cars) 

Then you could check items generated by linr as follows:

model.linr.cars$Call                      # This is how your model looks like
data.frame(model.linr.cars$coefficients,  # Coefficient estimators
           model.linr.cars$std.error,     # Standard error of estimators
           model.linr.cars$T_statistic,   # T statistics of estimators
           model.linr.cars$p_value.T)     # p value for T test 

# Other items also can be checked by 
data.frame(model.linr.cars$MSE,           # Mean square error 
           model.linr.cars$R.square,      # R^2 
           model.linr.cars$Adj.R.square,  # Adjusted R^2 
           model.linr.cars$F_statistic,   # F statistics of estimators
           model.linr.cars$p_value.F)     # p value for F test 

# You could also look at the fitted values and residuals like this:
# head(model.linr.cars$fitted.values)       # Look at the first 6 fitted values
# head(model.linr.cars$residuals)           # Look at the first 6 residuals

Conducting multiple linear regression with linr on your data

# Here we use the R build-in dataset mtcars as an example:
# fit a linear model with disp as outcome and mpg, wt, carb as predictors
model.linr.mtcars <- linr(disp ~ mpg + wt + carb, data = mtcars)

Then you could check items generated by linr as follows:

model.linr.mtcars$Call                      # This is how your model looks like
data.frame(model.linr.mtcars$coefficients,  # Coefficient estimators
           model.linr.mtcars$std.error,     # Standard error of estimators
           model.linr.mtcars$T_statistic,   # T statistics of estimators
           model.linr.mtcars$p_value.T)     # p value for T test 

# Other items also can be checked by 
data.frame(model.linr.mtcars$MSE,           # Mean square error 
           model.linr.mtcars$R.square,      # R^2 
           model.linr.mtcars$Adj.R.square,  # Adjusted R^2 
           model.linr.mtcars$F_statistic,   # F statistics of estimators
           model.linr.mtcars$p_value.F)     # p value for F test 

# You could also look at the fitted values and residuals like this:
# head(model.linr.mtcars$fitted.values)       # Look at the first 6 fitted values
# head(model.linr.mtcars$residuals)           # Look at the first 6 residuals

Useful links:



SelinaSong0412/linr documentation built on Dec. 18, 2021, 1:04 p.m.