knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(linr)
This is the introduction to my awesome package. You can look at the following introductions and practice with the whole usage of linr
.
Using linr
with defined vector Y (observations) and vector/matrix X (predictions). (Without dataset)
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
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 feel free to try the following alternative methods by yourself: # 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 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
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
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
To check the correctness of function linr
, we compare its outputs with stats::lm
function
See more test for correctness and function efficiency in vignette("Efficiency_tests", package = "linr"
# simple example for checking correctness. See more tests in vignette. model.lm <- lm(y ~ x) all(c(all.equal(model.lm$coefficients, model.linr$coefficients), all.equal(model.lm$fitted.values, model.linr$fitted.values), all.equal(model.lm$residuals, model.linr$residuals))) model.lm.mul <- lm(Y ~ X) all(c(all.equal(model.lm.mul$coefficients, model.linr.mul$coefficients), all.equal(model.lm.mul$fitted.values, model.linr.mul$fitted.values), all.equal(model.lm.mul$residuals, model.linr.mul$residuals))) model.lm.cars <- lm(dist ~ speed, data = cars) all(c(all.equal(model.lm.cars$coefficients, model.linr.cars$coefficients), all.equal(model.lm.cars$fitted.values, model.linr.cars$fitted.values), all.equal(model.lm.cars$residuals, model.linr.cars$residuals))) model.lm.mtcars <- lm(disp ~ mpg + wt + carb, data = mtcars) all(c(all.equal(model.lm.mul$coefficients, model.linr.mul$coefficients), all.equal(model.lm.mul$fitted.values, model.linr.mul$fitted.values), all.equal(model.lm.mul$residuals, model.linr.mul$residuals)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.