2024-02-25

Simple Linear Regression

  • What is it?
  • Simple Linear Regression is used to model the relationship between two continuous variables
  • The objective is to predict the value of an output variable based on the value of an input variable

Slide 2

  • The variable used for simple linear regression are:
  • \(\beta 0\) - intercept (constant)
  • \(\beta 1\) - slope
  • X - input variable
  • Y - output variable

Slide 3

  • The formula to calculate simple linear regression is:
  • Y = \(\beta 0\) + \(\beta 1\)X

Slide 4

We can use plotly to create a scatterplot to view the data:

## Warning: No trace type specified and no positional attributes specified
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

Slide 5

We can also use ggplot to create a scatterplot of the data:

Slide 6

We can now use ggplot to create a simple linear regression line:

## `geom_smooth()` using formula = 'y ~ x'

Slide 7

This is a sample of how to write the code to create a simple linear regression model with the scatterplot.

g <- ggplot(data = trees, aes(x = Girth, y = Volume)) + 
  geom_point()  
s <- g + geom_smooth(method = "lm")
s
## `geom_smooth()` using formula = 'y ~ x'

Slide 8

  • Analysis:
  • We were able to use R to program a simple lienar regression model for a dataset
  • We can use ggplot and plotly to do so
  • We can compare the model to the scatterplot of the data to visualize the preciseness of the model