# Machine Learning through Predictive Analysis using Multi Linear Regression in R with an example Posted by in R Language category on for Beginner level | Points: 250 | Views : 7678 Rating: 5 out of 5
1 vote(s)

In the current topic, we will learn how to perform Machine Learning through Predictive Analysis using Multi Linear Regression in R with an example. Download source code for Machine Learning through Predictive Analysis using Multi Linear Regression in R with an example

## Introduction

In the previous article, we have seen how to use Machine Learning through Predictive Analysis using simple Linear Regression in R with an example.In the current topic, we will learn how to perform Machine Learning through Predictive Analysis using Multi Linear Regression in R with an example.

Multi Linear Regression can be defined as

Multiple linear regression attempts to model the relationship between two or more explanatory variables and a response variable by fitting a linear equation to observed data. Every value of the independent variable x is associated with a value of the dependent variable y.

We must bear in our mind that multiple linear regression has more than one independent variables. What it means will be clarified below.

We will use RStudio for this purpose.

Let us consider that we have the below set of data in "Car Sample Data.csv" file Let's say Price is the response variable(Y). And CylinderVolume(x1),Year(x2),MileagePerKM(x3) are the predictor variables.This is a linear relationship among one response variable and multiple predictor variables of the form

` y = a + b1x1 + b2x2 + b3x3 + ... + bnxn .`

where,

```	Y - dependent/predictor variable
x1/x2/x3..xn - independent/response variable(s)
a,b1,b2,b3...bn - are co-efficients.
```

## What we are going to solve ?

The above data presented to us is a set of training data / historical data. Using our training data, we have to train our Predictive Model by using Multiple Linear Regression algorithm. Once, our algorithm is trained i.e. the machine has learnt what to do, we will predict Y given a new value of predictor variables.

## Straight to experiment

Open RStudio. First we will establish a Relationship Model between x1/x2/x3[Predictor Variables] and Y(Response Variable) and obtain the Coefficients values. For this we will use the lm function of R that creates a relationship model between the predictor and the response variable.

```# Load data from csv
input <- read.csv('d:/Car Sample Data.csv', sep = ',', quote="\"", check.names=F)

# Create the relationship model.
model <- lm(Price~CylinderVolume+Year+MileagePerKM, data=input)

#print the relation
print(model)
``` Thus we obtain the mathematical equation for Multi Linear Regression Model based on the above intercept and coefficient values which is

`y = -4.683e+07 + x1 * 2.534e+02 + x2 * 2.339e+04 + x3 * -2.854e-01`

So far we have trained our Model using the training dataset. Means our machine has learnt the algorithm. The next step is to predict. Say, we would like to predict the price of a TOYOTA car whose CylinderVolume = 2000, Year=2020, MileagePerKM =90000.

It will be wrong if we directly use the mathematical equation obtained above to predict the result. First we have to filter the record based on the MAKE to obtain a right Mathematical model and then apply the predict function as shown below.

```# Load all data from csv
input <- read.csv('d:/Car Sample Data.csv', sep = ',', quote="\"", check.names=F)

#Filter data based on Make
filterRecord <-input[input\$Make == 'TOYOTA',]

#print the filtered result
print(filterRecord)

# Create the relationship model.
RelationModel <- lm(Price~CylinderVolume+Year+MileagePerKM, data=filterRecord)

#print the relation
print(RelationModel)
``` Thus we obtain the mathematical equation which is

`y = -4.838e+07 + x1 * 3.630e+02 + x2 * 2.415e+04 + x3 * -1.432e+00`

To obtain the predicted value let us run the below program

```# Load all data from csv
input <- read.csv('d:/Car Sample Data.csv', sep = ',', quote="\"", check.names=F)

#Filter data based on Make
filterRecord <-input[input\$Make == 'TOYOTA',]

# Create the relationship model.
RelationModel <- lm(Price~CylinderVolume+Year+MileagePerKM, data=filterRecord)

# predict the price of a TOYOTA car whose CylinderVolume = 2000, Year=2020, MileagePerKM =90000
PriceY <- data.frame(CylinderVolume=2000,Year=2020,MileagePerKM=90000)

#display the value
print( predict(RelationModel,PriceY) )
``` And we found the answer which is 9,89,693.7

## Conclusion

In this article we have learnt Machine Learning through Predictive Analysis using Multi Linear Regression methodology by using the language R with a simple example.Hope this helps. Thanks for reading. Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team

Login to vote for this post.

Comment using (Author doesn't get notification)
{"ft_lang_label":"__label__en","ft_lang_prob":0.8002807,"math_prob":0.96620506,"size":4407,"snap":"2019-51-2020-05","text_gpt3_token_len":1059,"char_repetition_ratio":0.115830116,"word_repetition_ratio":0.19758673,"special_character_ratio":0.24415702,"punctuation_ratio":0.12591687,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9985239,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18],"im_url_duplicate_count":[null,null,null,null,null,null,null,2,null,2,null,2,null,2,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-08T23:36:22Z\",\"WARC-Record-ID\":\"<urn:uuid:7fb43d04-803d-4ad4-8117-1fbb12729281>\",\"Content-Length\":\"56626\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:26a311fc-7d5a-453a-a906-053fa6523212>\",\"WARC-Concurrent-To\":\"<urn:uuid:09296083-6c5f-4e10-b5ae-47370bf903cd>\",\"WARC-IP-Address\":\"23.239.203.17\",\"WARC-Target-URI\":\"http://www.dotnetfunda.com/articles/show/3614/machine-learning-through-predictive-analysis-using-multi-linear-regres\",\"WARC-Payload-Digest\":\"sha1:HYFY4UP7MKSWHBSZJN2GTAHN2MDEGJAF\",\"WARC-Block-Digest\":\"sha1:3C7KED5BGN5KKJJ7FER5EGZN42OKQYTY\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540515344.59_warc_CC-MAIN-20191208230118-20191209014118-00048.warc.gz\"}"}