Research Objective:

Which housing characteristics significantly predict house prices, and how accurately can multiple regression explain variations in house prices?

Background

Predicting house prices aids in real estimation of homes, approximation of net equity for particular individuals, and areas of prospective relocation for buyers. Statistical modelling helps us understand housing markets by observing which attributes (e.g. age, area, number of rooms, etc) are able to best predict a house price. It also helps us determine which attributes should be included and excluded when predicting a house price based on their correlation to the target variable.

Literature Review

Housing prices are influenced by many different factors, including characteristics of the property itself, location, and changes in the overall housing market. Since multiple factors can affect house prices at the same time, previous research has commonly used regression methods to understand the relationship between these variables. Multiple regression models are useful in housing research because they allow researchers to estimate the effect of individual factors. This approach helps provide a better understanding of which features of a property have the strongest relationship with its selling price.

Morel (2022) examined changes in housing prices across major Canadian cities and analyzed how structural and neighbourhood characteristics influenced property values. The study considered factors such as house size, age, and location, including the distance of properties from downtown areas. One of the main findings was that during the COVID-19 pandemic, the price advantage of homes located closer to downtown areas decreased. As remote work became more common, many buyers started to prefer larger suburban homes with more space rather than smaller properties in urban locations. This study demonstrates that housing prices are affected by more than just physical characteristics of a house, as market conditions and changes in buyer preferences can also influence the importance of different predictors. It also supports the use of regression models because they allow researchers to separate the effects of different housing and location factors.

Similarly, Schaefer and Panagiotoglou (2024) used a hedonic pricing model to analyze housing prices in Montreal. Hedonic pricing models are commonly used in real estate research because they estimate how different characteristics contribute to the overall value of a property. Their study included internal housing features, such as bedrooms, bathrooms, and unit size, along with external factors related to neighbourhood characteristics. By including multiple variables in their model, the researchers were able to measure the effect of a specific location-based factor while keeping other housing characteristics constant. This approach is closely related to the method used in this project, where multiple regression is used to determine the individual effects of variables such as Overall Quality, Gr Liv Area, and Neighborhood on SalePrice.

Another important source for this project is De Cock (2011), which introduced the Ames Housing dataset used in this analysis. The paper describes the dataset as an alternative to the Boston Housing dataset and provides detailed information about residential properties in Ames, Iowa. The dataset includes many variables related to housing characteristics, including living area, basement size, garage features, construction quality, year built, and neighbourhood information. While De Cock (2011) does not directly investigate which factors affect housing prices, it provides a valuable dataset for applying regression techniques. The large number of available predictors makes the dataset useful for studying how different property characteristics are associated with variations in sale prices.

Overall, previous research shows that factors such as house size, quality, and location are important predictors of housing prices. Studies have consistently found that structural characteristics and neighbourhood differences can significantly affect property values. These findings support the selection of variables used in this project, including Overall Quality, Gr Liv Area, Total Basement SF, Garage characteristics, Year Built, and Neighborhood. By applying multiple regression analysis to the Ames Housing dataset, this project builds on previous research by examining which factors remain significant when multiple housing characteristics are considered together.

Data Cleaning

This data cleaning step selects the relevant columns for predicting the house price, removes any rows which have null data values, and converts Neighborhood column to factor format.

Overall.Qual Gr.Liv.Area Total.Bsmt.SF Garage.Cars Year.Built Full.Bath Bedroom.AbvGr TotRms.AbvGrd Garage.Area Neighborhood SalePrice
6 1656 1080 2 1960 1 3 7 528 NAmes 215000
5 896 882 1 1961 1 2 5 730 NAmes 105000
6 1329 1329 1 1958 1 3 6 312 NAmes 172000
7 2110 2110 2 1968 2 3 8 522 NAmes 244000
5 1629 928 2 1997 2 3 6 482 Gilbert 189900
6 1604 926 2 1998 2 3 7 470 Gilbert 195500
8 1338 1338 2 2001 2 2 6 582 StoneBr 213500
8 1280 1280 2 1992 2 2 5 506 StoneBr 191500
8 1616 1595 2 1995 2 2 5 608 StoneBr 236500
7 1804 994 2 1999 2 3 7 442 Gilbert 189000

Exploratory Data Analysis

This numerical analyis analyzes the mean, median, standard deviation, minimum, and maximum of each column in the dataset.

Overall.Qual_Mean Overall.Qual_Median Overall.Qual_StandardDeviation Overall.Qual_Minimum Overall.Qual_Maximum Gr.Liv.Area_Mean Gr.Liv.Area_Median Gr.Liv.Area_StandardDeviation Gr.Liv.Area_Minimum Gr.Liv.Area_Maximum Total.Bsmt.SF_Mean Total.Bsmt.SF_Median Total.Bsmt.SF_StandardDeviation Total.Bsmt.SF_Minimum Total.Bsmt.SF_Maximum Garage.Cars_Mean Garage.Cars_Median Garage.Cars_StandardDeviation Garage.Cars_Minimum Garage.Cars_Maximum Year.Built_Mean Year.Built_Median Year.Built_StandardDeviation Year.Built_Minimum Year.Built_Maximum Full.Bath_Mean Full.Bath_Median Full.Bath_StandardDeviation Full.Bath_Minimum Full.Bath_Maximum Bedroom.AbvGr_Mean Bedroom.AbvGr_Median Bedroom.AbvGr_StandardDeviation Bedroom.AbvGr_Minimum Bedroom.AbvGr_Maximum TotRms.AbvGrd_Mean TotRms.AbvGrd_Median TotRms.AbvGrd_StandardDeviation TotRms.AbvGrd_Minimum TotRms.AbvGrd_Maximum Garage.Area_Mean Garage.Area_Median Garage.Area_StandardDeviation Garage.Area_Minimum Garage.Area_Maximum SalePrice_Mean SalePrice_Median SalePrice_StandardDeviation SalePrice_Minimum SalePrice_Maximum
6.09597 6 1.410831 1 10 1499.784 1442 505.522 334 5642 1051.68 990 440.6759 0 6110 1.767076 2 0.7605642 0 5 1971.381 1973 30.23884 1872 2010 1.566598 2 0.5529723 0 4 2.854508 3 0.8278589 0 8 6.443989 6 1.572832 2 15 472.8856 480 215.0537 0 1488 180841 160000 79889.9 12789 755000

Visualization

This visualization compares different attributes with each other graphically, performs a correlation analysis, and creates a heatmap.

Overall.Qual Gr.Liv.Area Total.Bsmt.SF Garage.Cars Year.Built Full.Bath Bedroom.AbvGr TotRms.AbvGrd Garage.Area SalePrice
Overall.Qual 1.00 0.57 0.55 0.60 0.60 0.52 0.06 0.38 0.56 0.80
Gr.Liv.Area 0.57 1.00 0.44 0.49 0.24 0.63 0.52 0.81 0.48 0.71
Total.Bsmt.SF 0.55 0.44 1.00 0.44 0.41 0.33 0.05 0.28 0.49 0.63
Garage.Cars 0.60 0.49 0.44 1.00 0.54 0.48 0.09 0.36 0.89 0.65
Year.Built 0.60 0.24 0.41 0.54 1.00 0.47 -0.06 0.11 0.48 0.56
Full.Bath 0.52 0.63 0.33 0.48 0.47 1.00 0.36 0.53 0.41 0.55
Bedroom.AbvGr 0.06 0.52 0.05 0.09 -0.06 0.36 1.00 0.67 0.07 0.14
TotRms.AbvGrd 0.38 0.81 0.28 0.36 0.11 0.53 0.67 1.00 0.33 0.50
Garage.Area 0.56 0.48 0.49 0.89 0.48 0.41 0.07 0.33 1.00 0.64
SalePrice 0.80 0.71 0.63 0.65 0.56 0.55 0.14 0.50 0.64 1.00

## [1] "Regression Equation"
## [1] "Sale Price = -579147.08118759 + 15953.1779830161X1 + 55.5035252763029X2 + 23.8421072570379X3 + 5015.54477253232X4 + 270.604746195334X5 + -2444.13969518329X6 + -6755.05378383826X7 + 596.372320278439X8 + 24.8969212294092X9 + -9673.14905145241X10 + -11893.5512962776X11 + 15943.6259994067X12 + 32669.2065507717X13 + 18272.4455445877X14 + 40523.6496612719X15 + 9145.45773804568X16 + 14189.3552428328X17 + 3026.32036866331X18 + 111260.282633442X19 + 6388.68205618999X20 + -7082.04709406952X21 + 5102.94956046942X22 + 14100.5831428921X23 + 13451.699909124X24 + 63887.7023457756X25 + -6811.63863556273X26 + 66613.6026551609X27 + 8857.95886794316X28 + 4038.3379870906X29 + 16921.6242974364X30 + 9673.71761727502X31 + 23074.5857489341X32 + 73114.0259877148X33 + 5643.76710114497X34 + 36300.6683978616X35 + 32114.6289378811X36"
## [1] "Coefficients:"
##         (Intercept)        Overall.Qual         Gr.Liv.Area       Total.Bsmt.SF 
##       -579147.08119         15953.17798            55.50353            23.84211 
##         Garage.Cars          Year.Built           Full.Bath       Bedroom.AbvGr 
##          5015.54477           270.60475         -2444.13970         -6755.05378 
##       TotRms.AbvGrd         Garage.Area NeighborhoodBlueste  NeighborhoodBrDale 
##           596.37232            24.89692         -9673.14905        -11893.55130 
## NeighborhoodBrkSide NeighborhoodClearCr NeighborhoodCollgCr NeighborhoodCrawfor 
##         15943.62600         32669.20655         18272.44554         40523.64966 
## NeighborhoodEdwards NeighborhoodGilbert  NeighborhoodGreens NeighborhoodGrnHill 
##          9145.45774         14189.35524          3026.32037        111260.28263 
##  NeighborhoodIDOTRR NeighborhoodLandmrk NeighborhoodMeadowV NeighborhoodMitchel 
##          6388.68206         -7082.04709          5102.94956         14100.58314 
##   NeighborhoodNAmes NeighborhoodNoRidge NeighborhoodNPkVill NeighborhoodNridgHt 
##         13451.69991         63887.70235         -6811.63864         66613.60266 
##  NeighborhoodNWAmes NeighborhoodOldTown  NeighborhoodSawyer NeighborhoodSawyerW 
##          8857.95887          4038.33799         16921.62430          9673.71762 
## NeighborhoodSomerst NeighborhoodStoneBr   NeighborhoodSWISU  NeighborhoodTimber 
##         23074.58575         73114.02599          5643.76710         36300.66840 
## NeighborhoodVeenker 
##         32114.62894
## [1] "T Value"
##         (Intercept)        Overall.Qual         Gr.Liv.Area       Total.Bsmt.SF 
##          -6.2323424          20.5597820          20.2843994          12.6857187 
##         Garage.Cars          Year.Built           Full.Bath       Bedroom.AbvGr 
##           2.5166737           5.7659518          -1.4205728          -5.9981348 
##       TotRms.AbvGrd         Garage.Area NeighborhoodBlueste  NeighborhoodBrDale 
##           0.7497428           3.6384511          -0.7799227          -1.3109624 
## NeighborhoodBrkSide NeighborhoodClearCr NeighborhoodCollgCr NeighborhoodCrawfor 
##           2.0223108           3.9079175           2.6926161           5.2970893 
## NeighborhoodEdwards NeighborhoodGilbert  NeighborhoodGreens NeighborhoodGrnHill 
##           1.2563128           2.0432089           0.2233374           4.5464539 
##  NeighborhoodIDOTRR NeighborhoodLandmrk NeighborhoodMeadowV NeighborhoodMitchel 
##           0.7892929          -0.2085310           0.5781195           1.9349029 
##   NeighborhoodNAmes NeighborhoodNoRidge NeighborhoodNPkVill NeighborhoodNridgHt 
##           1.9162187           8.2414644          -0.7147279           9.5771613 
##  NeighborhoodNWAmes NeighborhoodOldTown  NeighborhoodSawyer NeighborhoodSawyerW 
##           1.2300022           0.5261767           2.3027462           1.3514526 
## NeighborhoodSomerst NeighborhoodStoneBr   NeighborhoodSWISU  NeighborhoodTimber 
##           3.3456049           9.2069361           0.6454791           4.8317779 
## NeighborhoodVeenker 
##           3.4024272
## [1] "P-Value"
##         (Intercept)        Overall.Qual         Gr.Liv.Area       Total.Bsmt.SF 
##        5.264070e-10        8.890400e-88        1.226022e-85        6.319675e-36 
##         Garage.Cars          Year.Built           Full.Bath       Bedroom.AbvGr 
##        1.190055e-02        8.978311e-09        1.555488e-01        2.243989e-09 
##       TotRms.AbvGrd         Garage.Area NeighborhoodBlueste  NeighborhoodBrDale 
##        4.534706e-01        2.790803e-04        4.355002e-01        1.899746e-01 
## NeighborhoodBrkSide NeighborhoodClearCr NeighborhoodCollgCr NeighborhoodCrawfor 
##        4.323616e-02        9.523024e-05        7.130281e-03        1.264990e-07 
## NeighborhoodEdwards NeighborhoodGilbert  NeighborhoodGreens NeighborhoodGrnHill 
##        2.091041e-01        4.112225e-02        8.232887e-01        5.679932e-06 
##  NeighborhoodIDOTRR NeighborhoodLandmrk NeighborhoodMeadowV NeighborhoodMitchel 
##        4.300056e-01        8.348291e-01        5.632285e-01        5.309965e-02 
##   NeighborhoodNAmes NeighborhoodNoRidge NeighborhoodNPkVill NeighborhoodNridgHt 
##        5.543578e-02        2.548852e-16        4.748349e-01        2.065767e-21 
##  NeighborhoodNWAmes NeighborhoodOldTown  NeighborhoodSawyer NeighborhoodSawyerW 
##        2.187964e-01        5.988058e-01        2.136382e-02        1.766562e-01 
## NeighborhoodSomerst NeighborhoodStoneBr   NeighborhoodSWISU  NeighborhoodTimber 
##        8.315176e-04        6.262050e-20        5.186680e-01        1.423763e-06 
## NeighborhoodVeenker 
##        6.769837e-04
## [1] "Std. Error"
##         (Intercept)        Overall.Qual         Gr.Liv.Area       Total.Bsmt.SF 
##        92926.068677          775.941008            2.736267            1.879445 
##         Garage.Cars          Year.Built           Full.Bath       Bedroom.AbvGr 
##         1992.926108           46.931496         1720.531067         1126.192393 
##       TotRms.AbvGrd         Garage.Area NeighborhoodBlueste  NeighborhoodBrDale 
##          795.435886            6.842725        12402.702783         9072.381402 
## NeighborhoodBrkSide NeighborhoodClearCr NeighborhoodCollgCr NeighborhoodCrawfor 
##         7883.865238         8359.748330         6786.131068         7650.172972 
## NeighborhoodEdwards NeighborhoodGilbert  NeighborhoodGreens NeighborhoodGrnHill 
##         7279.602616         6944.642483        13550.439386        24471.881964 
##  NeighborhoodIDOTRR NeighborhoodLandmrk NeighborhoodMeadowV NeighborhoodMitchel 
##         8094.184098        33961.605601         8826.808078         7287.488905 
##   NeighborhoodNAmes NeighborhoodNoRidge NeighborhoodNPkVill NeighborhoodNridgHt 
##         7019.919031         7751.984293         9530.393581         6955.464180 
##  NeighborhoodNWAmes NeighborhoodOldTown  NeighborhoodSawyer NeighborhoodSawyerW 
##         7201.579889         7674.870967         7348.453902         7158.014509 
## NeighborhoodSomerst NeighborhoodStoneBr   NeighborhoodSWISU  NeighborhoodTimber 
##         6896.984719         7941.189643         8743.531458         7512.900906 
## NeighborhoodVeenker 
##         9438.740973

Model Fit Evaluation and Interpretation

## R-squared: 0.8284
## Adjusted R-squared: 0.8263
## F-statistic: 387.78 on 36 and 2891 DF
## Overall model p-value: <2e-16
Df Sum Sq Mean Sq F value Pr(>F)
Overall.Qual 1 1.193033e+13 1.193033e+13 10761.421297 0.0000000
Gr.Liv.Area 1 1.742398e+12 1.742398e+12 1571.681182 0.0000000
Total.Bsmt.SF 1 5.874237e+11 5.874237e+11 529.869210 0.0000000
Garage.Cars 1 2.920169e+11 2.920169e+11 263.405712 0.0000000
Year.Built 1 1.461517e+11 1.461517e+11 131.832051 0.0000000
Full.Bath 1 2.709337e+10 2.709337e+10 24.438816 0.0000008
Bedroom.AbvGr 1 9.814488e+10 9.814488e+10 88.528855 0.0000000
TotRms.AbvGrd 1 2.000196e+09 2.000196e+09 1.804221 0.1793085
Garage.Area 1 2.250597e+10 2.250597e+10 20.300879 0.0000069
Neighborhood 27 6.281905e+11 2.326632e+10 20.986732 0.0000000
Residuals 2891 3.205021e+12 1.108620e+09 NA NA

Interpretation of Predictors:

Null Hypothesis : B𝑖 = 0 (ith predictor variable is not significant) Alternative Hypothesis: B𝑖 ≠ 0 (ith predictor variable is significant)

## [1] -11893.551   5373.358  14100.583  32391.918 111260.283

Regression Diagnostics

VIF (Multicollinearity)

GVIF Df GVIF..1..2.Df.. VIF
Overall.Qual 3.164080 1 1.778786 3.164080
Gr.Liv.Area 5.051692 1 2.247597 5.051692
Total.Bsmt.SF 1.811080 1 1.345764 1.811080
Garage.Cars 6.065883 1 2.462901 6.065883
Year.Built 5.317397 1 2.305948 5.317397
Full.Bath 2.389856 1 1.545916 2.389856
Bedroom.AbvGr 2.294974 1 1.514917 2.294974
TotRms.AbvGrd 4.132520 1 2.032860 4.132520
Garage.Area 5.717315 1 2.391091 5.717315
Neighborhood 14.142115 27 1.050282 1.103092

For this results_vif, we can see that Gr.Liv.Area, Garage.Cars, Year.Built, and Garage.Area all pose moderate problems (vif >= 5). None of the variables pose serious problems since the vif was not >= 10.

Residuals vs Fitted displays non-linearity because of its curved appearance and heteroscedasticity of residuals due to the funnel appearance.

The Normal Q-Q plot displays that most residuals follow of a normal distribution, although there is a lot of deviation along both tails.

The Scale-Location plot displays a violation of the homoscedasticity because some residuals are far from the trend line.

The Residuals v. Leverage plot illustrates that most points are low leverage and some are either high leverage or have very low residuals. The trend line is reasonably flat, although some points cause a bump in the trend line due to their slightly high residuals.

Discussion

Key Findings

The following analysis is done holding all other variables constant:

  • Overall Quality is the one of the most important predictor as for every one unit increase in overall quality, the sale price positively increases by $15953.18. In practical terms, this indicates that the quality of a home can be highly correlated and predictive of the selling price.

  • Bedroom.AbvGr, Garage.Cars, and Full.Bath are subordinate predictors as for every one unit increase in above-ground bedrooms, number of cars spots in garage, and number of full bathrooms, the sale price decreases by $6755.05, increases by $5015.54, and decreases by $2444.14. In practical terms, this indicates that the structural attributes of a home can moderately influence its sale price in the negative or positive direction.

  • Year.Built is also an important predictor as for every one unit increase in year a home was built, there is a $270.60 positive increase in the price of the home. In practical terms, this indicates that the newer a home is, the more likely it is to be more expensive (yet by a small amount).

  • Total.Bsmt.SF increases the sale price by small positive amounts for a one-unit increase of each (holding all other variables constant). That being said, since these predictors are measured in square feet, the range for these values are larger and should not being under-estimated. In practical terms, a 200 sq.ft. increase in the basement area may cause the sale price to dramatically increase (scaling effect).

  • Neighborhood is an important predictor, but because it is a categorical attribute, the change in the Sale Price is dependent on which Neighborhood a home is in. Therefore, we can provide a five-number summary of the change in Sale Price based on the Neighborhood.

## [1] -11893.551   5373.358  14100.583  32391.918 111260.283

This five-number summary shows that the Neighborhoood has a large positive/negative effect on the Sale Price (holding all other variables constant). In practical terms, a buyer/investor should consider the neighborhood of a home when locating best areas to buy from.

TotRms.AbvGrd is not useful to the model since it’s p-value > 0.05. In practical terms, this may indicate that TotRms.AbvGrd needs to be excluded from the model development.

Gr.Liv.Area, Garage.Cars, Year.Built, and Garage.Area all exhibited moderate multi-collinearity. In practical terms, this indicates that there is high correlation among the independent variables.

Expected vs. Actual Results

Most results were consistent with literature reviews and initial expectations. The size, structure, nieghborhood location,and quality of a home all strongly influence the sale price. The most unexpected result was how little the year a home was built influenced the Sale Price.

Limitations

Only 10 of 80+ available variables were used; some relevant characteristics (lot size, exterior condition, remodel history) were excluded. The model was also linear, which may cause certain instances to not be collected. A non-linear approach may catch non-linear relationships.

Future Research

Future work could add more predictors (lot area, exterior quality, remodel age), condense features with high multi-collinearity, investigate the flagged outlier sales, and compare against nonlinear Machine Learning approaches.

Conclusion

This project used multiple linear regression to model house sale prices in Ames, Iowa using overall quality, above-ground living area, total basement square footage, garage capacity, garage area, year built, full bathrooms, bedrooms above grade, total rooms above grade, and neighborhood (encoded using factors). The model achieved R² = 0.8284 (Adjusted R² = 0.8263), meaning that the roughly 83% of the variation in sale price could be explained by the predictors. Overall Qual, Bedroom.AbvGr, Garage.Cars, and Full.Bath are all highly relevant predictors. Gr.Liv.Area, Total.Bsmt.SF, and Garage.Area have small contributions to the sale price using a unit-by-unit analysis (scaling effect can, however, cause a large change in sale price).

References

Ames Housing Dataset (Iowa State University / Kaggle version). Retrieved from https://www.kaggle.com/datasets/prevek18/ames-housing-dataset

De Cock, D. (2011). Ames, Iowa: Alternative to the Boston Housing Data as an End of Semester Regression Project. Journal of Statistics Education, 19(3). https://jse.amstat.org/v19n3/decock.pdf

Morel, L. (2022). Analyzing the house price boom in the suburbs of Canada’s major cities during the pandemic. Bank of Canada Staff Analytical Note, No. 2022-7. https://www.bankofcanada.ca/2022/06/staff-analytical-note-2022-7/

Schaefer, M., & Panagiotoglou, D. (2024). Evaluating the effects of supervised consumption sites on housing prices in Montreal, Canada using interrupted time series and hedonic price models. Drug and Alcohol Dependence Reports, 11, 100242. https://doi.org/10.1016/j.dadr.2024.100242

Appendix: Source Code

knitr::opts_chunk$set(echo = FALSE, warning = FALSE)
suppressPackageStartupMessages({
library(reshape2)
library(tidyverse)
library(car)
library(gglm)
})
df <- read.csv("AmesHousing.csv")
df <- df %>%
    select(Overall.Qual,Gr.Liv.Area,Total.Bsmt.SF,Garage.Cars,Year.Built,Full.Bath, Bedroom.AbvGr
           ,TotRms.AbvGrd,Garage.Area, Neighborhood, SalePrice) %>%
    filter(if_all(everything(), ~!is.na(.))) %>%
    mutate(Neighborhood = as.factor(Neighborhood)) 

knitr::kable(head(df, 10))

statistics <- df %>%
  select(where(is.numeric)) %>%
  summarize(
    across(everything(), list(
      Mean = ~ mean(.),
      Median = ~ median(.),
      StandardDeviation = ~ sd(.),
      Minimum = ~ min(.),
      Maximum = ~ max(.)))
  )

knitr::kable(statistics)

df %>%
  ggplot(mapping = aes(x = SalePrice)) + geom_histogram(bins = 30, fill='skyblue', color='black') + labs(title='Figure 1: Distribution of   House Prices', x = 'Sale Price', y = 'Number Of Homes')

df %>%
  ggplot(mapping = aes(x=Gr.Liv.Area, y=SalePrice)) + geom_point() + labs(title='Figure 2: Abvove Ground Living Area To House Price', x = 'Above Ground Living Area (sq. ft.)', y='Sale Price')

df %>% 
  ggplot(mapping = aes(x=Overall.Qual, y = SalePrice)) + geom_point() + labs(title='Figure 3: Overall Quality To Sale Price', x='Overall Quality', y='Sale Price')

numeric_df <- df %>% select(where(is.numeric))
cor_matrix <- cor(numeric_df)
knitr::kable(round(cor_matrix, 2))

cor_melt <- melt(cor_matrix)

ggplot(cor_melt, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile() +
  geom_text(aes(label = round(value, 2)), size = 3) +
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0,
                        limit = c(-1,1), name = "Correlation") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Figure 4: Correlation Matrix of Numeric Predictors", x = "", y = "")


model <- lm(SalePrice ~., data = df)

summary <- summary(model)

coefficients <- summary$coefficients[, "Estimate"]
names <- names(coefficients)
 
# paste0() concatenates the character and numeric with 0 spaces in between  and converts the numeric to a character
regression_equation <- paste0("Sale Price = ", paste0(coefficients[1], " + "))
for (i in 2:length(names)){
  regression_equation = paste0(
    regression_equation, paste0(
      coefficients[names[i]], 
        paste0(
          "X",  paste0(i-1, paste0(ifelse(i == length(names), "", " + ")))
        )
    )
  )
}

print("Regression Equation")
regression_equation

print("Coefficients:")
coefficients

print("T Value")
summary$coefficients[,"t value"]

print("P-Value")
summary$coefficients[,"Pr(>|t|)"]

print("Std. Error")
summary$coefficients[,"Std. Error"]


r_squared <- summary$r.squared
adj_r_squared <- summary$adj.r.squared

cat("R-squared:", round(r_squared, 4), "\n")
cat("Adjusted R-squared:", round(adj_r_squared, 4), "\n")

f_stat <- summary$fstatistic
f_value <- f_stat["value"]
f_df1 <- unname(f_stat["numdf"])
f_df2 <- unname(f_stat["dendf"])
f_p_value <- pf(f_value, f_df1, f_df2, lower.tail = FALSE)

cat("F-statistic:", round(f_value, 2), "on", f_df1, "and", f_df2, "DF\n")
cat("Overall model p-value:", format.pval(f_p_value, digits = 3), "\n")

anova_table <- anova(model)
knitr::kable(anova_table)
modelCoefficients = data.frame(
    "colNames"= names(coefficients),
    'coefficients' = coefficients
)

colNamesLogical <- sapply(modelCoefficients$colNames, function(colName) grepl("Neighborhood", colName))

modelCoefficients <- modelCoefficients %>%
                      filter(colNamesLogical)

neighborhoodSummary <- fivenum(modelCoefficients$coefficients)
neighborhoodSummary
results_gvif <- vif(model)
results_gvif <- data.frame(results_gvif)

results_vif <- results_gvif %>%
    mutate(VIF = (results_gvif$GVIF ** (1/results_gvif$Df)))

knitr::kable(results_vif)
gglm(model)
neighborhoodSummary