2024-06-08

Components of the Regression Equation

Total Sum of squares = Sum of squares due to regression plus sum of squares due to error \[ SST = \sum_{i=1}^{n} (y_i - \bar{y})^2 \] \[ SSR = \sum_{i=1}^{n} (\hat{y}_i - \bar{y})^2 \] \[ SSE = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 \]

Formulas

The correlation coefficient shows the magnitude and direction of the relationship, but not whether the relationship is statistically significant. I am looking at the variables that are correlated with price in a small dataset about houses. \[ r = \frac{\sum{(X_i - \bar{X})(Y_i - \bar{Y})}}{\sqrt{\sum{(X_i - \bar{X})^2} \sum{(Y_i - \bar{Y})^2}}} \]

The squared correlation coefficient is the \(R^2\)

Plot Code

To create a colored matrix with ggplot, displaying only the lower triangle

housescor = houses %>%
  select(-Neighborhood)
corr = round(cor(housescor), 3)
ggcorrplot(corr, hc.order = TRUE,
           type = "lower", title = "Correlation Matrix of House Attributes")

I chose year and house sq ft to make a 3D scatter plot of price, primarily because I thought this combination could look more interesting, since year takes on more values than most of the other variables.

Correlation Between All Numerical Variables

3D Plot

Regression line equation

\[ \widehat{Y} = \beta_0 + \beta_1 \cdot \text{house.sqft} + \beta_2 \cdot \text{year} \]

\[ \widehat{Y} = -4,095,000 + 150 \cdot \text{sqft} + 2,052 \cdot \text{year} \]

Analyzing the model results, I computed the variance inflation factor, because sq ft and year are highly correlated with each other. The standard error of each predictor is inflated by the \[\sqrt{VIF}\], so the coefficients may not meet the desired confidence level.

Variance Inflation Factor
 6.039943 6.039943

Plotting the residuals, I also identified some patterns that indicate this is not a great model. The magnitude of the residuals increase with increase in the predicted variable, known as heteroskedasticity. It could potentially be improved by considering other predictor variables or using a nonlinear modeling technique.

Model

Call:
lm(formula = price ~ house.sqft + year, data = houses)

Residuals:
   Min     1Q Median     3Q    Max 
-47840 -24717   2976  20416  54000 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept) -4.095e+06  1.010e+06  -4.055 9.65e-05 ***
house.sqft   1.500e+02  1.374e+01  10.919  < 2e-16 ***
year         2.052e+03  5.207e+02   3.941 0.000147 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 27870 on 105 degrees of freedom
Multiple R-squared:  0.9247,    Adjusted R-squared:  0.9232 
F-statistic: 644.4 on 2 and 105 DF,  p-value: < 2.2e-16

Residual Plot

Scatter Matrix

Plot each variable distribution and scatter plot.