Performing Linear Regression in R: A Quick Reference

Introduction to Performing Linear Regression in R

Regression is to build a function of independent variables (also known as predictors, regressors, explanatory variables, and features) to predict a dependent variable (also called a response, target, and regressand). Here we will focus on performing linear regression in R Language.

Linear regression is to predict response with a linear function of predictors as $$y=\beta_0+\beta_1x_1+\beta_2x_2+\cdots + \beta_kx_k,$$ where $x_1, x_2, \cdots, x_k$ are predictors and $y$ is the response to predict.

Before performing the regression analysis it will be very helpful to computer the coefficient of correlation between dependent variable and independent variable and also better to draw the scatter diagram.

Performing Linear Regression in R

Load the mtcars data, and check the data structure using str().

str(mtcars)

You have data stored in some external file such as CSV, then you can use read.csv() function to load the data in R. To learn about importing data files in R follow the link: Import Data files in R

Let us want to check the impact of weight (wt) on miles per gallon (mpg) and test the significance of the regression coefficient and other statistics to see the goodness of our fitted model

mod <- lm(mpg ~ wt, data = mtcars)
summary(mod)
Performing Linear Regression in R Estimation and Testing

Now look at the objects of results stored in mod

names(mod)

Getting Coefficients and Different Regression Statistics

Let us get the coefficients of the fitted regression model in R

mod$coef
coef(mod)

To obtain the confidence intervals of the estimated coefficients, one can use the confint()

confint(mod)

Fitted values from the regression model can be obtained by using fitted()

mod$fitted
fitted(mod)

The residuals can be obtained for the regression model using residual() function

mod$resid
resid(mod)

One can check the formula used to perform the simple/ multiple regression. It will tell you which variable is used as a response and others as explanatory variables.

formula (mod)

Graphical Representation of Relationship

To graphically visualize the relationship between variables or pairs of variables one can use plot() or pair() functions. Let us draw the scatter diagram between the dependent variable mpg and the explanatory variable wt using the plot() function.

plot(mpg ~ wt, data = mtcars)
Scatter Plot and Performing Linear Regression in R

One can add a best-fitted line to the scatter plot. For this purpose use abline() with an object having the class lm such as mod in this case

abline(mod)

There are many other functions and R packages to perform linear regression models in the R Language.

FAQS about Performing Linear Regression Models in R

  1. What is the use of abline() function in R?
  2. How a simple linear regression model can be visualized in R?
  3. How one can obtain fitted/predicted values of the simple linear regression model in R?
  4. Write a command that saves the residuals of lm() model in a variable.
  5. State the step-by-step procedure of performing linear regression in R.

To learn more about the lm() function in R

https://itfeature.com

Leave a Reply

Discover more from R Language Frequently Asked Questions

Subscribe now to keep reading and get access to the full archive.

Continue reading