2024-06-04

Introduction to Simple Linear Regression

  • Simple linear regression is a statistical method used to model the relationship between two variables.
  • It assumes that there is a linear relationship between the independent variable (X) and the dependent variable (Y).

Example: Predicting House Prices

  • Let’s consider an example of predicting house prices based on their size (in square feet).
  • Our goal is to build a regression model to predict the price of a house given its size.

Math Behind Simple Linear Regression

  • The equation for simple linear regression is:

\[ Y = \beta_0 + \beta_1 X + \varepsilon \]

  • Where:
    • \(Y\) is the dependent variable (house price).
    • \(X\) is the independent variable (house size).
    • \(\beta_0\) is the intercept.
    • \(\beta_1\) is the slope.
    • \(\varepsilon\) is the error term.

Scatter Plot of House Prices vs. Size

Math Behind Simple Linear Regression (Continued)

The equation for simple linear regression can be expressed mathematically as:

\[ Y = \beta_0 + \beta_1 X + \varepsilon \]

Where: - \(Y\) is the dependent variable (house price). - \(X\) is the independent variable (house size). - \(\beta_0\) is the intercept. - \(\beta_1\) is the slope. - \(\varepsilon\) is the error term.

Scatter Plot of House Prices vs. Size (Continued)

## 
## 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
# Generate dummy data for location
location <- c("City A", "City B", "City C", "City A", "City B")
data$Location <- factor(location)

# 3D Scatter plot using plotly
plot_ly(data, x = ~Size, y = ~Price, z = ~Location, color = ~Location,
        type = "scatter3d", mode = "markers") %>%
  layout(title = "House Prices vs. Size vs. Location",
         scene = list(xaxis = list(title = "Size (sq ft)"),
                      yaxis = list(title = "Price ($)"),
                      zaxis = list(title = "Location")))

Hypothesis Testing in Simple Linear Regression

Hypothesis testing is a fundamental aspect of simple linear regression, aiming to evaluate the significance of the relationship between the independent and dependent variables.

The Null and Alternative Hypotheses

In simple linear regression, the null hypothesis (\(H_0\)) states that there is no significant linear relationship between the independent and dependent variables. The alternative hypothesis (\(H_1\)) suggests that there is a significant linear relationship.

  • \(H_0: \beta_1 = 0\)
  • \(H_1: \beta_1 \neq 0\)

Calculation of Test Statistic

To test the null hypothesis, we calculate the t-statistic using the formula:

\[ t = \frac{\beta_1 - 0}{SE(\beta_1)} \]

Where \(SE(\beta_1)\) is the standard error of the slope coefficient.

##     Size 
## 12.73012

Conclusion

Simple linear regression is a powerful statistical method that provides valuable insights into the relationship between two variables. By conducting hypothesis testing, we can determine whether the relationship is statistically significant and make informed decisions based on the results.