Skip to content
Related Articles

Related Articles

Perform Linear Regression Analysis in R Programming – lm() Function
  • Last Updated : 24 Jun, 2020

lm() function in R Language is a linear model function, used for linear regression analysis.

Syntax: lm(formula)

Parameters:
formula: model description, such as x ~ y

Example 1:




# R program to illustrate
# lm function
  
# Creating two vectors x and y
x <- c(rep(1:20))
y <- x * 2
  
# Calling lm() function to
# fit a linear model
f <- lm(x ~ y)
  
# Getting linear model
f

Output:



Call:
lm(formula = x ~ y)

Coefficients:
(Intercept)            y  
  1.589e-15    5.000e-01  

Example 2:




# R program to illustrate
# lm function
  
# Creating two vectors x and y
x <- c(2, 4, 6, 8)
y <- c(1, 3, 5, 7)
  
# Calling lm() function to
# fit a linear model
f <- lm(y ~ x)
  
# Getting linear model
f

Output:

Call:
lm(formula = y ~ x)

Coefficients:
(Intercept)            x  
         -1            1  

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :