0% found this document useful (0 votes)
22 views3 pages

Program 8

Uploaded by

kbhavana0602
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Program 8

Uploaded by

kbhavana0602
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8. Implement simple linear regression method.

Linear Regression: It is a commonly used type of predictive analysis. It is a statistical


approach for modeling the relationship between a dependent variable and a given set of
independent variables.

There are two types of linear regression.


 Simple Linear Regression
 Multiple Linear Regression

Simple linear regression is used to model the relationship between two continuous variables.
Often, the objective is to predict the value of an output variable (or response) based on the
value of an input (or predictor) variable.
The general mathematical equation for a linear regression is
y = ax + b
Following 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 predicting weight of a person when his height is known. To do
this we need to have the relationship between height and weight of a person.
The steps to create the relationship is
 Carry out the experiment of gathering a sample of observed values of height and
corresponding weight.
 Create a relationship model using the lm() functions in R.
 Find the coefficients from the model created and create the mathematical equation using
these
 Get a summary of the relationship model to know the average error in prediction. Also
called residuals.
 To predict the weight of new persons, use the predict() function in R.

Input Data
Below is the sample data representing the observations −
# Values of height
151, 174, 138, 186, 128, 136, 179, 163, 152, 131

# Values of weight.
63, 81, 56, 91, 47, 57, 76, 72, 62, 48
lm() Function
This function creates the relationship model between the predictor and the response variable.
Syntax
The basic syntax for lm() function in linear regression is −
lm(formula,data)
Following is the description of the parameters used −
 formula is a symbol presenting the relation between x and y.
 data is the vector on which the formula will be applied.

#Create Relationship Model & get the Coefficients


Live Demo
x<- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
# Apply the lm() function.
relation <- lm(y~x)
print(relation)

#Get the Summary of the Relationship


Live Demo
print(summary(relation))

predict() Function
The basic syntax for predict() in linear regression is −
predict(object, newdata)
Following is the description of the parameters used −
 object is the formula which is already created using the lm() function.
 newdata is the vector containing the new value for predictor variable.

#Predict the weight of new persons


Li# The predictor vector.
x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)

# The resposne vector.


y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)

# Apply the lm() function.


relation <- lm(y~x)

# Find weight of a person with height 170.


a <- [Link](x = 170)
result <- predict(relation,a)
print(result)
ve Demo
Example:
********
print(getwd())

data1<-[Link]("[Link]")

data1

View(data1)

cor(data1$area,data1$bedrooms,method="pearson")

cor(data1$price,data1$area,method="pearson")

cor(data1$bathrooms,data1$bedrooms,method="pearson")

cor(data1$area,data1$stories,method="pearson")

plot(data1$price,data1$area)

plot(data1$area,data1$stories)

model1<-lm(data1$area~data1$price+data1$stories,data=data1)

summary(model1)

print(model1$coefficients)

print(model1$residuals)

You might also like