Multiple Linear Regression in R

Multiple Linear Regression in R is an extension of linear regression in the relationship between two or more variables. In linear regression, we have one input and done output, but in multiple linear regression in R, we have more than one predictor and one outcome.

The mathematical equation for multiple regression is as follow:

Here:

  • y is a response variable.
  • b0, b1, b2…bn are the coefficients.
  • x1, x2, …xn are the predictor variables.

The regression model created using the lm() function determines the value of the coefficients using the input data. Now we can predict the value of the response variable for a given set of predictor variables using these coefficients.

lm() Function for Multiple Linear Regression in R

The lm() function is used to create a relationship between the response variable and predictor.

The syntax for lm() function is given below:

Here:

  • formula represent the relation between x and .
  • data is the vector
df <- data.frame (
  a = c(34, 67, 87,64,32,54,64,32,76, 56, 34,57,85,25, 78,65,54,43,65,55,43),
  b = c(6, 5, 8,5,4,4,5,5,3, 6, 5,4,5,8, 8,5,4,4,5,5,3),
  c = c(134, 167, 187,264,232,154,264,332,176, 256, 234,257,285,325, 178,265,154,143,265,255,243)
)

# Create the model.
mlr <- lm(a~b+c, data = df)

print(mlr)

# Get the Intercept and coefficients 
cat("# # # # The Coefficient Values # # # ","\n")

a <- coef(mlr)[1]
print(a)

Xb <- coef(mlr)[2]
Xc <- coef(mlr)[3]

print(Xb)
print(Xc)

Output:

Multiple Linear Regression in R

Create Equation for Regression Model

Using the intercept and coefficient values we can create a mathematical equation as follow: