2024-03-20

Simple Linear Regression

  • Simple linear regression is a linear regression model that has one independent variable.
  • A linear regression model is a statistic model that estimates a relationship between different variables and their responses. In this case, it is between one independent variable (x) and one response (y).
  • SLR finds a linear function that is of the form:
  • \(y = \alpha + \beta x\)

SLR Linear Function

  • \(y = \alpha + \beta x\)
## `geom_smooth()` using formula = 'y ~ x'

Finding Best Fitting Line

  • The equation for the best-fitting line is and is similar to the linear equation from last slide:
  • \(\hat{y}_i = b_0 + b_1 x + e\)
  • where y denotes observed response for the independent variable x. \(\hat{y}_i\) is the predicted y (response) value. b0 and b1 are coefficients. e is the error (residual)

Predicting Y with SLR

  • We can hand calculate the best fitting line, but in r we can load different libraries to find the best fitting line in order to predict y.
  • Here we can load some data
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 

Predicting Y with SLR

  • For this data, we can say for the y variable, or response, can be rating. Rating can be influenced by another variable of the data. In this case, we can choose one independent variable.
  • Let’s choose length of movie to see if there is a relationship between sepal length and petal length
  • Cleaning up the data to make it easier to compare.
  • iristrim <- iris %>% 
      select(Sepal.Length, Petal.Length)
    tail(iristrim)
    ##     Sepal.Length Petal.Length
    ## 145          6.7          5.7
    ## 146          6.7          5.2
    ## 147          6.3          5.0
    ## 148          6.5          5.2
    ## 149          6.2          5.4
    ## 150          5.9          5.1

    Predicting Y with SLR

    • Let’s plot the data we have as a scatterplot.
    • Do you see any correlation?

    Predicting Y with SLR

    • Now we can draw a linear regression line using an r model.
    ## `geom_smooth()` using formula = 'y ~ x'

    The equation and Predicting Y

    • What is the equation of the linear regression line? Using a linear regression model in r, we can find these values.
    fit <- lm(Sepal.Length ~ Petal.Length, data=iristrim)
    fit
    ## 
    ## Call:
    ## lm(formula = Sepal.Length ~ Petal.Length, data = iristrim)
    ## 
    ## Coefficients:
    ##  (Intercept)  Petal.Length  
    ##       4.3066        0.4089
    • \(\hat{y} = 4.3066 + 0.4089 x\)