2025-06-02

Introduction to Simple Linear Regression

Simple Linear Regression is a statistical method that allows us to summarize and study relationships between two continuous variables:

  • Dependent Variable (Response Variable): The variable we are trying to predict or explain, denoted as \(Y\).
  • Independent Variable (Predictor Variable): The variable used to predict or explain the dependent variable, denoted as \(X\).

The goal is to find a linear equation that best describes the relationship between \(X\) and \(Y\).

The Regression Equation

The simple linear regression model can be expressed mathematically as:

\[ Y_i = \beta_0 + \beta_1 X_i + \epsilon_i \]

Where: * \(Y_i\) is the dependent variable for the \(i\)-th observation. * \(X_i\) is the independent variable for the \(i\)-th observation. * \(\beta_0\) is the Y-intercept, representing the expected value of \(Y\) when \(X\) is 0. * \(\beta_1\) is the slope of the regression line, representing the change in \(Y\) for a one-unit change in \(X\). * \(\epsilon_i\) is the error term (or residual), representing the difference between the observed value \(Y_i\) and the predicted value \(\hat{Y}_i\).

Assumptions of Simple Linear Regression (Part 1)

For the regression model to be valid and for inferences to be reliable, several assumptions must be met:

  1. Linearity: The relationship between \(X\) and \(Y\) is linear. \[ Y = \beta_0 + \beta_1 X \]
  2. Independence of Errors: The error terms \(\epsilon_i\) are independent of each other.

Assumptions of Simple Linear Regression (Part 2)

Continuing with the assumptions for a valid regression model:

  1. Homoscedasticity: The variance of the error terms is constant across all levels of \(X\). \[ Var(\epsilon_i) = \sigma^2 \]
  2. Normality of Errors: The error terms \(\epsilon_i\) are normally distributed. \[ \epsilon_i \sim N(0, \sigma^2) \]

Example: Data Generation & Model Fitting Explained

For our example, we’ll create a synthetic dataset to demonstrate simple linear regression. We generate 50 data points for ‘Advertising Spend’ (our independent variable) and ‘Sales’ (our dependent variable). The ‘Sales’ data is created with a linear relationship to ‘Advertising Spend’, plus some random noise to simulate real-world variability. A random seed is set to ensure that the generated data is consistent every time the code is run.

Once our data is prepared, we fit the simple linear regression model. In R, this is done using the lm() function, which stands for “linear model.” We specify that ‘Sales’ is the dependent variable and ‘Advertising_Spend’ is the independent variable. The lm() function then calculates the optimal intercept and slope that best fit the data.

Example: Data Generation & Model Fitting (R Code)

Here’s the R code used to generate the sample data and fit the linear regression model.

set.seed(123)
advertising_data <- data.frame(
  Advertising_Spend = runif(50, 10, 100),
  Sales = 50 + 1.5 * runif(50, 10, 100) + rnorm(50, 0, 15)
)
model <- lm(Sales ~ Advertising_Spend, data = advertising_data)

Example: Data & Model Summary Overview

Our generated dataset consists of 50 observations with ‘Advertising_Spend’ (in thousands of dollars) and corresponding ‘Sales’ (in thousands of units).

After fitting the linear regression model, a summary provides key statistical insights: * Coefficients: Estimated intercept and slope for ‘Advertising_Spend’. * R-squared: Proportion of variance in Sales explained by Advertising Spend. * P-values: Indicate statistical significance of coefficients.

You would typically observe a statistically significant positive slope for Advertising_Spend and a reasonable R-squared value.

ggplot2 Plot 1: Scatter Plot with Regression Line

This plot visualizes the relationship between Advertising Spend and Sales, along with the fitted regression line. This is the first of two required ggplot2 plots.

ggplot2 Plot 2: Residuals Plot

This plot helps to check the homoscedasticity assumption. Ideally, residuals should be randomly scattered around zero with no discernible pattern. This is the second required ggplot2 plot.

Plotly Plot: Interactive Scatter Plot

This interactive plot allows you to hover over data points to see their exact values. This fulfills the plotly plot requirement.

Interpretation of Coefficients

From our model summary:

  • The intercept (\(\beta_0\)) represents the estimated sales when advertising spend is zero.
    • For example, if the intercept is \(55.2\), it means that even with no advertising spend, we might expect \(55,200\) units in sales (this is an extrapolation and might not be meaningful if \(X=0\) is outside the observed range).
  • The slope (\(\beta_1\)) for Advertising_Spend represents the estimated change in sales for every one thousand dollar increase in advertising spend.
    • For example, if the slope is \(1.4\), it means that for every additional $1,000 spent on advertising, sales are predicted to increase by \(1.4\) units (in thousands).

Model Evaluation: R-squared

The coefficient of determination, or \(R^2\), is a key metric in regression analysis. It indicates the proportion of the variance in the dependent variable that is predictable from the independent variable(s).

\[ R^2 = 1 - \frac{SS_{res}}{SS_{tot}} \]

Where: * \(SS_{res}\) is the sum of squares of residuals (unexplained variance). * \(SS_{tot}\) is the total sum of squares (total variance in Y).

A higher \(R^2\) value (closer to 1) indicates that the model explains a larger proportion of the variance in the dependent variable, suggesting a better fit.

Conclusion

Simple Linear Regression is a powerful tool for understanding and predicting relationships between two variables. By carefully examining the assumptions and interpreting the coefficients, we can gain valuable insights from data.

This presentation covered: * The basic concept and equation of simple linear regression. * Key assumptions for a valid model. * An example demonstrating data creation, model fitting, and visualization using ggplot2 and plotly. * Interpretation of the model’s coefficients and an introduction to \(R^2\).