2023-02-06

Introduction

Simple linear regression is a statistical method used to model the relationship between two continuous variables. In this presentation, we will explore the concept of simple linear regression and see how it can be applied in R.

Understanding Simple Linear Regression

The simple linear regression equation can be represented as:

\[ y = \beta_0 + \beta_1x \]

where \(y\) is the dependent variable, \(x\) is the independent variable, \(\beta_0\) is the intercept, and \(\beta_1\) is the slope of the regression line. This equation assumes a linear relationship between the dependent and independent variables.

Plotting the Data

To visualize the relationship between the two variables, we can create a scatterplot of the data.In this case it is the weight of the car in relation ot its average MPG

Plotting the Regression Line (Code)

Using this code we can create a simple linear regression on the graph (which will be displayed on the next slide)

model <- lm(mpg ~ wt, data = mtcars)

ggplot(mtcars, aes(x = wt, y = mpg)) +

geom_point() +

geom_abline(intercept = model\(coef[1], slope = model\)coef[2]) +

labs(x = “Weight (wt)”, y = “Miles per Gallon (mpg)”) +

ggtitle(“Scatterplot with Regression Line”)

Plot with linear regression

This is the same plot but now a regression line has been added to depict the general trend in the data

Math Notation in Latex

The regression line can also be represented mathematically using the equation of a line:

\[y = \beta_0 + \beta_1x\]

where \(\beta_0\) is the intercept and \(\beta_1\) is the slope of the regression line.

Initiate plotly

## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Plotly plot

This is the same data including the regression line but made in plotly so each data point can be interacted with.