2024-03-21

Introduction to Simple Linear Regression

  • Definition: Simple linear regression is a correlation between the independent and possibly dependent variable that aims to find a linear link between them
  • Purpose:It is used to predict the value of a variable (dependent) based on the value of another variable (independent).
  • Applications: This method is widely used in fields such as economics, biology, engineering, and more.

Theoretical Background

  • The equation is: \(y = \beta_0 + \beta_1x + \epsilon\)

  • Meaning of terms:

    • \(y\): Dependent variable
    • \(x\): Independent variable
    • \(\beta_0\): Intercept of the regression line
    • \(\beta_1\): Slope of the regression line
    • \(\epsilon\): Error term

Calculating Slope and Intercept

The slope (\(\beta_1\)) of the regression line is calculated as:

\[ \beta_1 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n} (x_i - \bar{x})^2} \]

The intercept (\(\beta_0\)) is calculated using:

\[ \beta_0 = \bar{y} - \beta_1\bar{x} \]

where:

  • \(\bar{x}\) is the mean of the independent variable,
  • \(\bar{y}\) is the mean of the dependent variable.

Assumptions

  • Linearity: The relationship between \(x\) and \(y\) is linear.
  • Independence: Observations are independent of each other.
  • Homoscedasticity: Constant variance of error terms.
  • Normality: The residuals of the model are normally distributed.

Example with R Code

## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

Explanation of previous slide

  • Call: This part shows the formula used in the model. Here, it’s indicating that the model predicts mpg (miles per gallon) as a function of wt (weight of the car) using the mtcars dataset.
  • Residuals: This section provides a summary of the residuals, which are the differences between the observed values and the values predicted by the model. It shows the minimum, 1st quartile (1Q), median, 3rd quartile (3Q), and maximum residuals.
  • Coefficients: The intercept _0 is 37.2851. This represents the estimated value of mpg when wt is zero
  • Standard Errors: These are the standard errors associated with the estimates of the coefficients, giving a measure of the variability of the estimates.

Explanation of previous slide part 2

  • Pr(>|t|): The p-values associated with the t-statistics, which are used to determine the statistical significance of the coefficients. Here, both coefficients are highly significant (indicated by the ***), as their p-values are less than 0.001.
  • Residual standard error: An estimate of the standard deviation of the residuals.
  • Multiple R-squared: This value (0.7528) indicates that approximately 75.28% of the variability in mpg can be explained by the model.
  • Adjusted R-squared: Adjusted for the number of predictors in the model, it’s slightly lower at 0.7446, which is common as it accounts for the number of terms in the model.
  • F-statistic:This tests the null hypothesis that all regression coefficients are equal to zero. The F-statistic value is quite large, and the associated p-value (1.294e-10) is very small, indicating that the model is statistically significant.

Visualizing the Results

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

Explanation of ggplot

  • The x-axis represents the independent variable (wt, which is the weight of the cars).
  • The y-axis represents the dependent variable (mpg, which is the miles per gallon).
  • The points represent the observed values from the dataset.
  • The blue line is the regression line, which shows the best linear fit to the data according to the least squares criterion.
  • The grey area around the regression line is the confidence interval, indicating the uncertainty about the line’s position.

Scatter Plot with Plotly

Histogram of MPG with ggplot2