The main purpose of developing this package was to develop a Linreg function that calculates regression values by using the least square methods, and this function is incorporated in RC class, with some other functions.
This function is taking two arguments(formula and data), and is performing the following calculations:
1.Regression Coefficients
2.Fitted Values
3.The Residuals
3.Degrees of Freedom
4.Residual Varience
5.Varience of Regression Coefficients
6.T-values
The following code shows the calculations Performed by linreg function:
linreg<-function(formula,data) { x<-model.matrix(formula,data) y<-all.vars(formula)[1] RC<-c((solve((t(x)%*%x))%*%t(x))%*%data[,y]) FV<-as.vector(x%*%RC) RD<-c(as.matrix(data[,y]-FV)) DF<- nrow(data)-ncol(x) RV<- c(t(RD)%*%RD)%/%DF VRC<-RV*(solve(t(x)%*%x)) tvalues<-RC/sqrt(diag(VRC)) tvalues<-round(x=tvalues,digits = 2) }
Mentioning some of the important functions incorporated with Lin_Class
*Coefficients()
*Resid()
*Pred()
The code for the above mentioned function:
resid = function() { return(resd) } pred = function() { return(fitted) } coef = function() { return(resvariance) }
We have build a seperate Class for plots, The plot() functions in this class are delivering two plots.
First plot is between Fitted values (on x-axis) and Residuals (on y-axis).
Second plot is between Fitted Values (on x-axis) and Standard Residuals (on y-axis).
plot_class <- setRefClass( "plot_class", fields = list( D1 = "data.frame", D2 = "data.frame" ), methods = list( plot = function() { library(gridExtra) graf1<-ggplot(data=D1,aes(x = x, y = y))+ ggtitle("residuals Vs Fitted ")+ xlab("fitted_values")+ ylab("residuals")+ geom_point()+geom_smooth(aes(col = "red"), method = "lm") graf2<-ggplot(data=D2, aes(x = x, y = y))+ ggtitle("scale location ")+ xlab("fitted_values")+ ylab("standard_residuals")+ geom_point()+geom_smooth(aes(col = "red"), method = "lm") grid.arrange(graf1,graf2) }) )
testplots<-linreg(Petal.Length~Species,iris) testplots$plot$plot()
We use a well-known dataset of "iris" and run the functions of lm() to fit a linear model and create unit tests, compairing the results of our function linreg() and the returning values of the lm().
library(testthat) library(Regression) test_that("Checking if the output is same",{ expect_that(as.numeric(linreg(formula = as.formula(Petal.Length ~ Petal.Width), data = iris)$formulas$beta), equals(as.numeric(lm(formula =as.formula(Petal.Length ~ Petal.Width), data = iris)$coefficients))) expect_that(as.numeric(linreg(formula =as.formula(Petal.Length ~ Petal.Width), data =iris)$formulas$resd), equals(as.numeric(lm(formula =as.formula(Petal.Length ~ Petal.Width), data = iris)$residuals))) expect_that(as.numeric(linreg(formula =as.formula(Petal.Length ~ Petal.Width), data =iris)$formulas$fitted), equals(as.numeric(lm(formula =as.formula(Petal.Length ~ Petal.Width), data = iris)$fitted.values))) expect_that(linreg(formula =as.formula(Petal.Length ~ Petal.Width), data = iris)$formulas$df, equals(148)) } )
tests<-linreg(Petal.Length~Species,iris) tests$formulas$print() tests$formulas$resid() tests$formulas$summary() tests$formulas$pred()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.