R Linear Regression - R Programming language

What is linear regression in R?

Regression analysis is a statistical tool which is broadly used for establishing a relationship model between two variables. One of these variable is called predictor variable whose value will be gathered from experiments and the other variable is called response variable whose value will be derived from the predictor variable.

In Linear Regression, these two variables will be related through an equation, where exponent (power) of both these variables is 1. Mathematically a linear relationship represents a straight line when plotted as a graph and represents a non-linear relationship where the exponent of any variable is not equal to 1 creates a curve.

General mathematical equation for a linear regression is

Below is the description of the parameters used

  • y is the response variable.
  • x is the predictor variable.
  • a and b are constants which are called the coefficients.

Steps to Establish a Regression

A simple example of regression is guessing weight of a person through height. For this we relationship between height and weight of a person is required.

Steps to create a relationship is

  • Gather a sample of experimental values of height and its corresponding weight.
  • Then create a relationship model by using lm() functions in R.
  • Discover the coefficients from the created model and create mathematical equation using these coefficients.
  • Get a summary of the relationship model to know the average error in prediction. Also called residuals.
  • Use predict() function in R for predicting the weight of new person.

Input Data

Below is the sample data representing the observations

lm() Function

This Im() function will create a relationship model between the predictor and the response variable.

Syntax

Basic syntax for lm() function in linear regression is

Below is the description of the parameters used

  • formula is a symbol which represents the relation between x and y.
  • data is the vector on which the formula will be applied.

Create Relationship Model & get the Coefficients

When above code is executed, it produces following result

Get the Summary of the Relationship

When above code is executed, it produces following result

predict() Function

Syntax

Basic syntax for predict() in linear regression is

Below is the description of the parameters used

  • object is a formula which is created using lm() function.
  • newdata is the vector containing new value for predictor variable.

Predict the weight of new persons

When above code is executed, it produces following result

Visualize the Regression Graphically

When above code is executed, it produces following result

linearregression

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

R Programming language Topics