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.
You can install the development version of linr from GitHub with:
# install.packages("devtools")
devtools::install_github("SelinaSong0412/linr")
#> Skipping install of 'linr' from a github remote, the SHA1 (61fcc8a4) has not changed since last install.
#> Use `force = TRUE` to force installation
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.
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
#> linr(formula = y ~ x)
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
#> model.linr.coefficients model.linr.std.error model.linr.T_statistic
#> (Intercept) -0.02391520 0.05990805 -0.3991985
#> x 0.03327784 0.05911758 0.5629094
#> model.linr.p_value.T
#> (Intercept) 0.6900329
#> x 0.5739198
# 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
#> model.linr.MSE model.linr.R.square model.linr.Adj.R.square
#> 1 1.067786 0.001062183 -0.002289958
#> model.linr.F_statistic model.linr.p_value.F
#> 1 0.316867 0.5739198
# 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:
(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
#> linr(formula = Y ~ X, method = "cholesky")
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
#> model.linr.mul.coefficients model.linr.mul.std.error
#> (Intercept) 0.0309058167 0.06077744
#> X1 0.0209145277 0.06116199
#> X2 0.0854376238 0.06162063
#> X3 0.0538444130 0.06016107
#> X4 0.0328247119 0.05751099
#> X5 -0.0301000687 0.06354595
#> X6 0.0382620128 0.06172076
#> X7 0.0341104589 0.06042210
#> X8 0.0488963187 0.05735986
#> X9 0.0003243385 0.05921584
#> X10 -0.0063349299 0.05630479
#> X11 0.0224374156 0.06418188
#> X12 0.0056121768 0.05840200
#> X13 0.0467891426 0.06190284
#> X14 -0.0726998793 0.06034583
#> X15 0.0654460858 0.06007482
#> X16 -0.0022217723 0.06005472
#> X17 0.0260674615 0.05914002
#> X18 0.0327618228 0.05487284
#> X19 0.0014093068 0.06026550
#> X20 -0.0146655425 0.06222920
#> model.linr.mul.T_statistic model.linr.mul.p_value.T
#> (Intercept) 0.508508068 0.6114989
#> X1 0.341953053 0.7326437
#> X2 1.386510020 0.1666981
#> X3 0.895004232 0.3715563
#> X4 0.570755427 0.5686248
#> X5 -0.473674041 0.6361028
#> X6 0.619921292 0.5358156
#> X7 0.564536129 0.5728429
#> X8 0.852448348 0.3946965
#> X9 0.005477225 0.9956337
#> X10 -0.112511378 0.9104989
#> X11 0.349591109 0.7269093
#> X12 0.096095628 0.9235136
#> X13 0.755848044 0.4503780
#> X14 -1.204720840 0.2293320
#> X15 1.089409615 0.2769134
#> X16 -0.036995801 0.9705148
#> X17 0.440775292 0.6597170
#> X18 0.597049847 0.5509583
#> X19 0.023384969 0.9813599
#> X20 -0.235669795 0.8138618
# 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
#> model.linr.mul.MSE model.linr.mul.R.square model.linr.mul.Adj.R.square
#> 1 1.009784 0.03345579 -0.03583053
#> model.linr.mul.F_statistic model.linr.mul.p_value.F
#> 1 0.4828628 0.9717
# 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
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
#> linr(formula = dist ~ speed, data = cars)
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
#> model.linr.cars.coefficients model.linr.cars.std.error
#> (Intercept) -17.579095 6.7584402
#> speed 3.932409 0.4155128
#> model.linr.cars.T_statistic model.linr.cars.p_value.T
#> (Intercept) -2.601058 1.231882e-02
#> speed 9.463990 1.489836e-12
# 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
#> model.linr.cars.MSE model.linr.cars.R.square model.linr.cars.Adj.R.square
#> 1 236.5317 0.6510794 0.6438102
#> model.linr.cars.F_statistic model.linr.cars.p_value.F
#> 1 89.56711 1.489836e-12
# 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
#> linr(formula = disp ~ mpg + wt + carb, data = mtcars)
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
#> model.linr.mtcars.coefficients model.linr.mtcars.std.error
#> (Intercept) 143.670967 142.740385
#> mpg -7.304425 3.669182
#> wt 76.663396 20.865454
#> carb -4.566736 7.529811
#> model.linr.mtcars.T_statistic model.linr.mtcars.p_value.T
#> (Intercept) 1.0065194 0.3227853099
#> mpg -1.9907502 0.0563492712
#> wt 3.6741782 0.0009992845
#> carb -0.6064875 0.5490772554
# 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
#> model.linr.mtcars.MSE model.linr.mtcars.R.square
#> 1 3146.542 0.8149811
#> model.linr.mtcars.Adj.R.square model.linr.mtcars.F_statistic
#> 1 0.7951576 41.11196
#> model.linr.mtcars.p_value.F
#> 1 2.171366e-10
# 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