Linear Regression in R with lm() function - A Practical Tutorial
In this tutorial, we will learn how to perform a simple linear regression in R using lm() function.
Simple Linear regression is one of the popular and common statistical methods that is used to understand the relationship between two numerical or quantitative variables, like height and weight of humans, age and height, years of education and salary, and so on. One can think of doing simple linear regression as trying answer the question are the two numerical variables of interest are associated/related.
Statistically, the act of doing linear regression analysis amounts to this, given a data set of the form (x1,y1), (x2,y2), (x3,y3),…, (xn,yn), we are trying to fit a linear model y = mx + c, where c is intercept, where the line meets y-axis and m is the of the slope of the straight line.
We need some data to start with fitting linear regression model. Let us simulate data for both x and y as follows.
set.seed(42)
y |t|)
## (Intercept) -0.69442 0.37363 -1.859 0.0692 .
## x 0.93262 0.05808 16.059
ggplot(aes(x,y))+
geom_point()+
theme_bw(16)+
geom_smooth(method = "lm", se = FALSE)
Scatterplot with linear regression line