2024-02-04

Simple Linear Regression

Simple linear regression is a common statistical method used evaluate the relationship between two variables. This method is most effectively used where there is a linear relationship between the independent variable \(X\) and the dependent variable \(Y\).

The basic form of this type of linear equation is as follows:

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

Linear Equation Definition

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

  • \(Y\) is the dependent variable you are trying to predict or explain.
  • \(X\) is the independent variable you are using to make predictions.
  • \(\beta_0\) is the y-intercept, representing the expected value of \(Y\) when \(X\) is 0.
  • \(\beta_1\) is the slope of the line, representing the change in \(Y\) for a one-unit change in \(X\).
  • \(\epsilon\) is the error term, accounting for the variability in \(Y\) that cannot be explained by the linear relationship with \(X\).

Example Data - Real Estate

Price vs. Square Feet

In real estate, it can be common for the price to scale based on the size of the property (square footage).

Plot with linear regression

Filtered for only Townhouses

Narrowing the type of property to evaluate more similar type of real estate listings.

Filtered for Townhomes in Brookly, NY (Code)

Reducing the scope further to only include townhouses in Brooklyn

data_townhouses_Brooklyn <- data_townhouses %>% filter(STREET_NAME == "Brooklyn")

plot4 <- ggplot(data_townhouses_Brooklyn, aes(x = PROPERTYSQFT, y = PRICE)) +
  geom_point() +  # Scatter plot
  geom_smooth(method = "lm", col = "blue") +  # Linear regression line
  stat_regline_equation(aes(label =  ..eq.label.., x = PROPERTYSQFT, y = PRICE)) +
  theme_minimal() +
  scale_y_continuous(labels = label_comma()) +
  labs(title = "Price vs. Property Square Footage for Townhouses in Brooklyn",
       x = "Property Square Footage",
       y = "Price (USD)")

Filtered for Townhomes in Brookly, NY (Plot)