knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(lab4a)
lab4 is a package implementing a function called "linreg" to habdle linear regression model. linreg will take tow arguments/; formula and data. "formula" is a formula object indicating the relationship between dependent variable and independent variable(s). "data" is a data set from which the variables in formula would take their values.
The general form of the formula is as: dependent var ~ independent vars
The return value of function linreg will be a model which is an object as RC class. This object consists of the predicted values for dependent variable as well as the regression coefficients of the model parameters, the residual values, the t and p values for each coefficient, the degrees of freedom and the standard error.
The RC class defined in this function has five methods: print(), plot(), resid(), pred(), and summary() each which has a return object as follows:
According to wikipedia, linear regression is a linear approach to modeling the relationship between a scalar response (or dependent variable) and one or more explanatory variables (or independent variables). The case of one explanatory variable is called simple linear regression. For more than one explanatory variable, the process is called multiple linear regression. Give a data set including $\left{ y_i, X_{i,j} \right}$, a linear regression tries to model the relationship between $y_i$ and $X_{i,j}$ linearly as follow: $$\hat{y} = {X\beta} + \hat{e}$$ where $\hat{y}$ is the predicted variable, $X$ is(are) independent variable(s), $\beta$ is regression coefficients, and $\hat{e}$ is the residuals.
To show the how to implement the function "linreg", we will use the data set "iris" to model the relationship between Petal.Length and Species.
# The first ten rows of iris: data(iris) print(iris[1:10,])
As mentioned earlier, "linreg" takes two arguments. "formula", and "data". In this example the data would be "iris", and the formula would be **Petal.Length $\sim$ Species**. Thus we call the function as follow:
# Call the function linreg: model <- linreg$new(Petal.Length~Species, data = iris)
This call will create an RC class object named model which represents the regression model regarding the given formula and dataset.
To access the fields and employ the methods of this RC class object the $ sign should be implemented as following examples:
# Displaying the redression coefficients: model$Coefficients model$degrees_of_freedom
# Print() method: model$print()
print() method Prints out the coefficients and coefficient names, in addition to the call of the function. The regression coefficients and the parameter names are known.
# plot() method model$plot()
plot() method Plots two different plots. The first one shows The fiitted values vs the residuals, and the second one illustrates the fiitted values vs square root of standardized residuals.
# resid() method model$resid()
resid() method returns a vector of residual values, or the error term vector.
# pred() method model$pred()
pred() method returns the predicted values for dependent variable.
# coef() method model$coef()
coef() method returns the regression coefficients of the model.
# summary() method model$summary()
summary() method returns the coefficients with their standard error, t-value and p-value as well as the estimate of standard error and the degrees of freedom in the model.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.