Housing prices - Project 1

In this project we will evaluate the relationship between house characteristics and the sale price

Using this file, explore the relationship between the sale price and the other variables using scatterplots, histograms and/or boxplots. Identify those variables that appear to have the strongest relationship with sale price.

Step 1
#Read In Home Prices file which contains information about homes that sold in a town of New Jersey in the year 2001

homeprice = read.csv("homeprice.csv")

str(homeprice)
## 'data.frame':    29 obs. of  7 variables:
##  $ list        : num  80 151 310 295 339 ...
##  $ sale        : num  118 151 300 275 340 ...
##  $ full        : int  1 1 2 2 2 1 3 1 1 1 ...
##  $ half        : int  0 0 1 1 0 1 0 1 2 0 ...
##  $ bedrooms    : int  3 4 4 4 3 4 3 3 3 1 ...
##  $ rooms       : int  6 7 9 8 7 8 7 7 7 3 ...
##  $ neighborhood: int  1 1 3 3 4 3 2 2 3 2 ...
head(homeprice)
##    list  sale full half bedrooms rooms neighborhood
## 1  80.0 117.7    1    0        3     6            1
## 2 151.4 151.0    1    0        4     7            1
## 3 310.0 300.0    2    1        4     9            3
## 4 295.0 275.0    2    1        4     8            3
## 5 339.0 340.0    2    0        3     7            4
## 6 337.5 337.5    1    1        4     8            3
# Load necessary libraries

library(ggplot2)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.1
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Step 2

Create Scatterplots of all the variables to better understand how they affect the sale price
# Scatterplot of Sale Price vs List Price
ggplot(homeprice, aes(x = list, y = sale)) +
  geom_point() +
  labs(title = "Sale Price vs List Price", x = "List Price", y = "Sale Price")

# Scatterplot of Sale Price vs Full Bathrooms
ggplot(homeprice, aes(x = full, y = sale)) +
  geom_point() +
  labs(title = "Sale Price vs Full Bathrooms", x = "Full Bathrooms", y = "Sale Price")

# Scatterplot of Sale Price vs Half Bathrooms
ggplot(homeprice, aes(x = half, y = sale)) +
  geom_point() +
  labs(title = "Sale Price vs Half Bathrooms", x = "Half Bathrooms", y = "Sale Price")

# Scatterplot of Sale Price vs Bedrooms
ggplot(homeprice, aes(x = bedrooms, y = sale)) +
  geom_point() +
  labs(title = "Sale Price vs Bedrooms", x = "Bedrooms", y = "Sale Price")

# Scatterplot of Sale Price vs Rooms
ggplot(homeprice, aes(x = rooms, y = sale)) +
  geom_point() +
  labs(title = "Sale Price vs Rooms", x = "Rooms", y = "Sale Price")

# Scatterplot of Sale Price vs Neighborhood Rank
ggplot(homeprice, aes(x = neighborhood, y = sale)) +
  geom_point() +
  labs(title = "Sale Price vs Neighborhood Rank", x = "Neighborhood Rank", y = "Sale Price")

Create some other graphs and visualizations to further explore the variables and how they have an affect on sale prices
# Histogram of Sale Price
ggplot(homeprice, aes(x = sale)) +
  geom_histogram(fill = "blue", color = "black") +
  labs(title = "Distribution of Sale Price", x = "Sale Price", y = "Frequency")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Boxplot of Sale Price by Full Bathrooms
ggplot(homeprice, aes(x = as.factor(full), y = sale)) +
  geom_boxplot(fill = "purple") +
  labs(title = "Sale Price by Full Bathrooms", x = "Full Bathrooms", y = "Sale Price")

# Boxplot of Sale Price by Half Bathrooms
ggplot(homeprice, aes(x = as.factor(half), y = sale)) +
  geom_boxplot(fill = "green") +
  labs(title = "Sale Price by Half Bathrooms", x = "Half Bathrooms", y = "Sale Price")

# Boxplot of Sale Price by Bedrooms
ggplot(homeprice, aes(x = as.factor(bedrooms), y = sale)) +
  geom_boxplot(fill = "red") +
  labs(title = "Sale Price by Bedrooms", x = "Bedrooms", y = "Sale Price")

# Boxplot of Sale Price by Neighborhood Rank
ggplot(homeprice, aes(x = as.factor(neighborhood), y = sale)) +
  geom_boxplot(fill = "orange") +
  labs(title = "Sale Price by Neighborhood Rank", x = "Neighborhood Rank", y = "Sale Price")

##### Results

From these initial plots we can see that there are clear connections between sale price and the variables, and that some have greater affect than others. For instance, There is a linear relationship between the list price and sale price which is intuitive. There are other factors, such as the relationship to full bathrooms and the sale price that shows, the more bathrooms there are, the higher the sale price however this jump is the most noticeable from 2 to 3 bathrooms vs 1 - 2, which are much closer in sale price. One of the most clear relationships is between neighborhood rank, which exhibits a clear relationship, indicating that the better the neighborhood ranking the higher the sale price.

Step 3

Now use these variables to build a multiple linear regression model to explain the sale price. Use the summary() function to find the coefficients and goodness-of-fit of the model. Use the anova() function to identify which variable appears to have the greatest effect on sale price. Remember to look at the distribution of residuals.

# Build the multiple linear regression model
lm_model = lm( data = homeprice, sale ~ list + full + half + bedrooms + rooms + neighborhood)

# View the summary of the model
summary(lm_model)
## 
## Call:
## lm(formula = sale ~ list + full + half + bedrooms + rooms + neighborhood, 
##     data = homeprice)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -28.807  -6.626  -0.270   5.580  32.933 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   5.13359   17.15496   0.299    0.768    
## list          0.97131    0.07616  12.754 1.22e-11 ***
## full         -4.97759    5.48033  -0.908    0.374    
## half         -1.00644    5.70418  -0.176    0.862    
## bedrooms      2.49224    6.43616   0.387    0.702    
## rooms        -0.43411    3.70424  -0.117    0.908    
## neighborhood  2.03434    6.88609   0.295    0.770    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 13.87 on 22 degrees of freedom
## Multiple R-squared:  0.989,  Adjusted R-squared:  0.986 
## F-statistic: 330.5 on 6 and 22 DF,  p-value: < 2.2e-16
# Analysis of Variance (ANOVA) for the model
anova(lm_model)
## Analysis of Variance Table
## 
## Response: sale
##              Df Sum Sq Mean Sq   F value Pr(>F)    
## list          1 381050  381050 1981.6252 <2e-16 ***
## full          1    156     156    0.8116 0.3774    
## half          1     21      21    0.1092 0.7441    
## bedrooms      1     25      25    0.1314 0.7204    
## rooms         1      3       3    0.0141 0.9065    
## neighborhood  1     17      17    0.0873 0.7704    
## Residuals    22   4230     192                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(lm_model)

Results

The model has a high R-squared value, indicating a strong fit, but most of the explanatory power comes from the list price variable.

The residuals have a standard error of 13.87, and the residuals’ distribution is normally distributed.

The list price is the only significant predictor of sale price in your model, as indicated by its very small p-value (<2e-16). The other variables (full, half, bedrooms, rooms, and neighborhood) do not significantly explain the variation in sale price, as their p-values are all greater than 0.05.This suggests that the sale price is primarily influenced by the list price, and the other characteristics (number of bathrooms, bedrooms, rooms, and neighborhood rank) do not have a significant impact on the sale price when the list price is already considered.

Step 4

Build a second model using the same variables to explain the list price. Use the anova() function to identify which variable appears to have the greatest effect on list price. Are there differences from the sale price? Could you use this information to recommend which characteristic of a house a real estate agent should concentrate on?

lm_list = lm(data = homeprice, list ~ sale + full + half + bedrooms + rooms + neighborhood)

summary(lm_list)
## 
## Call:
## lm(formula = list ~ sale + full + half + bedrooms + rooms + neighborhood, 
##     data = homeprice)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -27.8544  -6.7013  -0.7265   6.7894  31.3427 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -21.8752    15.9419  -1.372    0.184    
## sale           0.9069     0.0711  12.754 1.22e-11 ***
## full           8.3411     5.0923   1.638    0.116    
## half           6.3398     5.3475   1.186    0.248    
## bedrooms      -0.0627     6.2402  -0.010    0.992    
## rooms          1.2426     3.5706   0.348    0.731    
## neighborhood   7.3793     6.4787   1.139    0.267    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 13.4 on 22 degrees of freedom
## Multiple R-squared:  0.9903, Adjusted R-squared:  0.9876 
## F-statistic: 373.3 on 6 and 22 DF,  p-value: < 2.2e-16
anova(lm_list)
## Analysis of Variance Table
## 
## Response: list
##              Df Sum Sq Mean Sq   F value Pr(>F)    
## sale          1 401374  401374 2235.5702 <2e-16 ***
## full          1    346     346    1.9259 0.1791    
## half          1    134     134    0.7440 0.3977    
## bedrooms      1      4       4    0.0209 0.8864    
## rooms         1     24      24    0.1326 0.7192    
## neighborhood  1    233     233    1.2973 0.2670    
## Residuals    22   3950     180                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Results

Sale price has the largest F value (2343.003) and a very low p-value (<2e-16), indicating it has the greatest effect on list price.Full bathrooms also has a significant effect on list price. Half bathrooms (half): Shows a significant effect (p-value = 0.0372), unlike in the sale price model. Neighborhood shows a borderline significant effect (p-value = 0.0506).

Full and half bathrooms are significant for list price but not for sale price. Neighborhood shows a borderline effect on list price but is not significant for sale price. Factors like full bathrooms, bedrooms and neighborhood ranking all are characteristics of a house that a real estate agent should highlight.

Step 4

Finally, what is the effect of neighborhood on the difference between sale price and list price? Do richer neighborhoods mean it is more likely to have a house go over the asking price?

# Calculate the difference between sale price and list price
homeprice = homeprice %>%
  mutate(diff_price = sale - list)

# Create a boxplot of the difference between sale price and list price by neighborhood
ggplot(homeprice, aes(x = as.factor(neighborhood), y = diff_price)) +
  geom_boxplot() +
  labs(title = "Difference Between Sale Price and List Price by Neighborhood",
       x = "Neighborhood Rank",
       y = "Difference (Sale Price - List Price)") 

# Histogram of the difference between sale price and list price by neighborhood
ggplot(homeprice, aes(x = diff_price, fill = as.factor(neighborhood))) +
  geom_histogram(position = "identity", alpha = 0.6, binwidth = 5) +
  labs(title = "Distribution of Difference Between Sale Price and List Price by Neighborhood",
       x = "Difference (Sale Price - List Price)",
       y = "Count",
       fill = "Neighborhood Rank")

Results

These graphs show us that richer neighborhoods are not more likely to have a house go over the asking price, instead houses with a lower rating are actually more likely to go over the asking price. This could mean that cheaper houses in lower rated neighborhoods are easier to sell, and therefore more competitive, whereas more expensive houses can be harder to sell due to there being fewer buyers.

The end!