Introduction : Simple Linear Regression

What is Linear Regression and where can we use it in real life?

-Simple linear regression is a method that shows the relationship between two continuous variables(independent and dependent) by using a straight line.

-In our daily life, we can use it to answer questions like if I drink more coffee cups, will I get more tasks done!

-The equation is represented as \[\hat{Y} = b_0 + b_1 X\]

What are X and Y

-\(\hat{Y}\) is the dependent variable, which is the variable that we are trying to predict.

-X is the independent variable which we are using to predict \(\hat{Y}\)

  • we calculate the slope by using the formula \[b_1 = \frac{\sum(X_i - \bar{X})(Y_i - \bar{Y})}{\sum(X_i - \bar{X})^2}\]

  • We calculate the intercept by using the formula \[b_0 = \hat{Y} - b_1 \bar{X}\]

Example Time!

Problem : Is there a positive relation between the number of coffee cups i drink and the number of tasks that I complete in a day?

lets see how we can answer this simple question in our day to day lives using simple linear regression.

we will look at

  • visual representation of the answer (ggplot)

  • Residual plot(ggplot)

  • An interactive plot that shows values when hovered on a point on the graph(plotly plot)

soultion

Lets look at the example data that I created.

Here I used lm() function (linear model function) to calculate the intercept and coefficient(coffeeCups_consumed)

coffeeCups_consumed <- c(1,2,3,4,5,8,8,2,1,1) #coffees per day
tasks_completed <- c(2,3,4,6,7,9,8,4,2,3) # tasks per day
result <- lm(tasks_completed ~ coffeeCups_consumed)#linear model function
coef(result) # displaying intercept and slope
##         (Intercept) coffeeCups_consumed 
##           1.6421053           0.9022556

Lets see how the results looks

We know the numbers but lets see how the visual representation looks in a ggplot!

data <- data.frame(coffeeCups_consumed, tasks_completed)#create data frame 
ggplot(data, aes(x = coffeeCups_consumed, y = tasks_completed) )+ #axis labels 
  geom_point() + #points
  ggtitle("Coffee vs Tasks")+ #title
  geom_smooth(method = "lm", se = FALSE) + #regression line
  theme_classic() #clean theme

What does the plot indicate?

In this example it shows:

  • the relationship between number of coffee cups consumed and tasks completed is strong, meaning, more coffee = more tasks done.

  • if the points are close to the straight line, it means their relation is strong

  • note: even though we have data for 10 days, we are only seeing few because the dots are overlapped due to repetetion of the data

Checking Errors in our Prediction

This is called a residual plot.

Now lets see the accuracy of our prediction using another ggplot!

What does the Residual Plot indicate?

In this example it shows:

  • There is no particular pattern with the dots in the plot and are randomly scattered around the zero line…

  • If there is no pattern, it means that accuracy of our prediction is good

  • Our Linear model is good!

Look at data clearly!

In this plot we can see the data more clearly. When the mouse pointer is hovered over the dots, it shows data for number of coffees consumed and tasks done in a day according to the data that I created.

Title of plot : Coffee vs Tasks

Conclusion

At last, now we know what Simple Linear Regression is!

  • It helps us understand how one variable effects another variable (X and Y)

  • It can be used to understand daily life situations

  • Plots make the interpretaion of the results easy

  • Simple Linear Regression method is a good method for prediction and anlysis

———————–Thank You———————–