Final Project

Yingfa Gao

2022-12-07

library

library(ggplot2)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ✔ purrr   0.3.4      
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
library(grid)
library(dplyr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## 
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(tidyselect)
library(viridis) 
## Loading required package: viridisLite
library(scales)
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:viridis':
## 
##     viridis_pal
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
library(ggridges)
library(knitr)
library(plotly)
## 
## 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
library(readr)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## 
## Attaching package: 'xts'
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
library(dygraphs)
library(tseries)
library(ggthemes)

Issue Description

House sale prices vary with many factors and understanding the main drivers of the costs are of interest to many parties. In this analysis, I will get the price range for the house market whcih is most important for a family when they decide to buy a house. Meanwhile, I WILL look into the most common factors that influence the house price. Also, I want to help the buyer find the most popular house at King county market. They can avoid bidding with more buyers and get it at a reasonable price. Also, I will try to predict the price change and forecast the price trend in the future.

Loading Data

housing <- read_csv('~/Desktop/R FINAL PROJECT/kc_house_data.csv')
## Rows: 21613 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (1): id
## dbl  (19): price, bedrooms, bathrooms, sqft_living, sqft_lot, floors, waterf...
## dttm  (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Extract data

summary(housing)
##       id                 date                            price        
##  Length:21613       Min.   :2014-05-02 00:00:00.00   Min.   :  75000  
##  Class :character   1st Qu.:2014-07-22 00:00:00.00   1st Qu.: 321950  
##  Mode  :character   Median :2014-10-16 00:00:00.00   Median : 450000  
##                     Mean   :2014-10-29 04:38:01.96   Mean   : 540088  
##                     3rd Qu.:2015-02-17 00:00:00.00   3rd Qu.: 645000  
##                     Max.   :2015-05-27 00:00:00.00   Max.   :7700000  
##     bedrooms        bathrooms      sqft_living       sqft_lot      
##  Min.   : 0.000   Min.   :0.000   Min.   :  290   Min.   :    520  
##  1st Qu.: 3.000   1st Qu.:1.750   1st Qu.: 1427   1st Qu.:   5040  
##  Median : 3.000   Median :2.250   Median : 1910   Median :   7618  
##  Mean   : 3.371   Mean   :2.115   Mean   : 2080   Mean   :  15107  
##  3rd Qu.: 4.000   3rd Qu.:2.500   3rd Qu.: 2550   3rd Qu.:  10688  
##  Max.   :33.000   Max.   :8.000   Max.   :13540   Max.   :1651359  
##      floors        waterfront            view          condition    
##  Min.   :1.000   Min.   :0.000000   Min.   :0.0000   Min.   :1.000  
##  1st Qu.:1.000   1st Qu.:0.000000   1st Qu.:0.0000   1st Qu.:3.000  
##  Median :1.500   Median :0.000000   Median :0.0000   Median :3.000  
##  Mean   :1.494   Mean   :0.007542   Mean   :0.2343   Mean   :3.409  
##  3rd Qu.:2.000   3rd Qu.:0.000000   3rd Qu.:0.0000   3rd Qu.:4.000  
##  Max.   :3.500   Max.   :1.000000   Max.   :4.0000   Max.   :5.000  
##      grade          sqft_above   sqft_basement       yr_built   
##  Min.   : 1.000   Min.   : 290   Min.   :   0.0   Min.   :1900  
##  1st Qu.: 7.000   1st Qu.:1190   1st Qu.:   0.0   1st Qu.:1951  
##  Median : 7.000   Median :1560   Median :   0.0   Median :1975  
##  Mean   : 7.657   Mean   :1788   Mean   : 291.5   Mean   :1971  
##  3rd Qu.: 8.000   3rd Qu.:2210   3rd Qu.: 560.0   3rd Qu.:1997  
##  Max.   :13.000   Max.   :9410   Max.   :4820.0   Max.   :2015  
##   yr_renovated       zipcode           lat             long       
##  Min.   :   0.0   Min.   :98001   Min.   :47.16   Min.   :-122.5  
##  1st Qu.:   0.0   1st Qu.:98033   1st Qu.:47.47   1st Qu.:-122.3  
##  Median :   0.0   Median :98065   Median :47.57   Median :-122.2  
##  Mean   :  84.4   Mean   :98078   Mean   :47.56   Mean   :-122.2  
##  3rd Qu.:   0.0   3rd Qu.:98118   3rd Qu.:47.68   3rd Qu.:-122.1  
##  Max.   :2015.0   Max.   :98199   Max.   :47.78   Max.   :-121.3  
##  sqft_living15    sqft_lot15    
##  Min.   : 399   Min.   :   651  
##  1st Qu.:1490   1st Qu.:  5100  
##  Median :1840   Median :  7620  
##  Mean   :1987   Mean   : 12768  
##  3rd Qu.:2360   3rd Qu.: 10083  
##  Max.   :6210   Max.   :871200
kinghousing <- housing[,c("date","price","bathrooms","bedrooms",
                  "sqft_living", "sqft_lot", "floors", "condition","yr_built")]
head(kinghousing)
## # A tibble: 6 × 9
##   date                  price bathrooms bedrooms sqft_l…¹ sqft_…² floors condi…³
##   <dttm>                <dbl>     <dbl>    <dbl>    <dbl>   <dbl>  <dbl>   <dbl>
## 1 2014-10-13 00:00:00  221900      1           3     1180    5650      1       3
## 2 2014-12-09 00:00:00  538000      2.25        3     2570    7242      2       3
## 3 2015-02-25 00:00:00  180000      1           2      770   10000      1       3
## 4 2014-12-09 00:00:00  604000      3           4     1960    5000      1       5
## 5 2015-02-18 00:00:00  510000      2           3     1680    8080      1       3
## 6 2014-05-12 00:00:00 1225000      4.5         4     5420  101930      1       3
## # … with 1 more variable: yr_built <dbl>, and abbreviated variable names
## #   ¹​sqft_living, ²​sqft_lot, ³​condition

Price distribution of the market

0 ~ $ 500K

kinghousing %>% 
  filter(price < 500000) %>% 
  ggplot(aes(price)) + 
  geom_histogram(binwidth = 30000, color="Gold", fill="steelblue1") +   geom_vline(xintercept = mean(housing$price), color = 'red')

  labs(x = "Price", y = "numbers", title = "kinghousing distrubution in price (0 ~ $500K)") + 
  scale_y_continuous(labels = unit_format(unit = "K", scale = 1e-03)) + 
  scale_x_continuous(labels = scales::dollar_format()) 
## NULL
kinghousing %>% 
  filter(price < 750000) %>% 
  ggplot(aes(price)) + 
  geom_histogram(binwidth = 30000, color="Gold", fill="steelblue1") +   geom_vline(xintercept = mean(housing$price), color = 'red')

  labs(x = "Price", y = "numbers", title = "kinghousing distrubution in price (0 ~ $750K)") + 
  scale_y_continuous(labels = unit_format(unit = "K", scale = 1e-03)) + 
  scale_x_continuous(labels = scales::dollar_format()) 
## NULL
kinghousing %>% 
  filter(price < 1000000) %>% 
  ggplot(aes(price)) + 
  geom_histogram(binwidth = 30000, color="Gold", fill="steelblue1") +   geom_vline(xintercept = mean(housing$price), color = 'red')

  labs(x = "Price", y = "numbers", title = "kinghousing distrubution in price (0 ~ $1000K)") + 
  scale_y_continuous(labels = unit_format(unit = "K", scale = 1e-03)) + 
  scale_x_continuous(labels = scales::dollar_format()) 
## NULL
kinghousing %>% 
  filter(price < 1500000) %>% 
  ggplot(aes(price)) + 
  geom_histogram(binwidth = 30000, color="Gold", fill="steelblue1") +   geom_vline(xintercept = mean(housing$price), color = 'red')

  labs(x = "Price", y = "numbers", title = "kinghousing distrubution in price (0 ~ $1500K)") + 
  scale_y_continuous(labels = unit_format(unit = "K", scale = 1e-03)) + 
  scale_x_continuous(labels = scales::dollar_format()) 
## NULL

Conclusion: when zoomed in to the range of 0- 500K, the average house price willn’t stay within this price range. It is not the standard price that we are looking for. Then, I increase the capacity to 0 - 750 k. There’re still lots of costs that fall outside this range. Then, I tried 1000 k and 1500 k. Find out the standard price people are afforded in king county is more than 250k and less than 1000K. It’s higher than the average house price compared with nearby counties. Also, from the 200K to 800K range, it is pretty even on the distribution, indicating the people may need to pay extra for bidding on the house.

Bedrooms influnece the housing market

kinghousing %>%  
    group_by(bedrooms) %>% 
    summarise(Count = n()) %>% 
    ggplot(aes(reorder(bedrooms, Count), Count)) + 
    geom_col() +  
    coord_flip() +  
    scale_y_continuous(labels = comma) +  
    labs(x = "bedrooms", y = "Count", title = "Bedrooms influnece the housing market") + 
    scale_y_continuous(labels = unit_format(unit = "K", scale = 1e-03)) 
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.

From the total distribution of the housing structure in the king county housing market, three and four bedrooms housing are highly demand for most families. Two bedrooms housing is less prevalent. Five bedrooms accommodation still has a call in king county. One and six bedrooms housing are hard to sell in this market.

Bathrooms influnece the housing market

kinghousing %>%  
    group_by(bathrooms) %>% 
    summarise(Count = n()) %>% 
    ggplot(aes(reorder(bathrooms, Count), Count)) + 
    geom_col() +  
    coord_flip() +  
    labs(x = "bedrooms", y = "Count", title = "Bathrooms influnece the housing market") + 
    scale_y_continuous(labels = unit_format(unit = "K", scale = 1e-03)) 

From the total distribution of the king county housing market, one bathroom to 3.25 bathrooms, housing is highly demanded by most families. People won’t like to have more than four bathrooms. Combinate the data from the Bedrooms influence in the housing market, people would like to have more bedrooms instead of bathrooms. The 2.5 bathroom, 3 - 4 bedrooms housing should be the buyer’s top choice.

Does Built year and condition matters to the housing market?

Built year

kinghousing %>% 
  filter(yr_built>1985 & price < 1500000) %>% 
  ggplot(aes(x = price, y = yr_built, group = yr_built)) + 
  geom_density_ridges(fill = "lightblue") + 
  theme_tufte() +  
  labs(x = "housing price", y = "Built year", title = "price change by built year") + 
  scale_x_continuous(labels = scales::dollar_format()) 
## Picking joint bandwidth of 70700

House builds within 20 years are the most popular in the market. People are afraid to buy old houses since they are worried that they need to pay extra for the roof, plumbing, and floors. The new house or close-year well-maintained house is the buyer’s top choice.

by condition

kinghousing %>% 
  filter(condition <= 5 & price < 1500000) %>% 
  ggplot(aes(x = price, y = condition, group = condition)) + 
  geom_density_ridges(fill = "lightblue") + 
  theme_tufte() +  
  labs(x = "housing price", y = "condition", title = "price change by condition") + 
  scale_x_continuous(labels = scales::dollar_format())  
## Picking joint bandwidth of 48500

People will always look for a good-conditioned house. From the density and grids line, we can tell lower condition house has less price change. The people would like to pay a higher price for a new construction house If the house condition is over than 4, they are going to have a big price gap. Some of those still sell for a higher price since they have a good location and can be rebuilt or renovated.

Data Transformation and Visualisation

kinghousing$dateM <- floor_date(kinghousing$date, unit = "month")
kinghousing$dateY <- floor_date(kinghousing$date, unit = "year")
kable(kinghousing %>% group_by(dateY) %>% summarize(n = n()))
dateY n
2014-01-01 14633
2015-01-01 6980
kinghousing_time1 <- kinghousing %>%  filter(dateM >= ymd("2014-01-01") & dateM < ymd("2016-01-01")) %>% 
  group_by(dateM) %>%  
  summarize(mean = mean(price))

kinghousing_xts <- xts(kinghousing_time1[,-1], order.by = kinghousing_time1$dateM)
dygraph(kinghousing_xts, main = "Sales Count & Price", 
        ylab = "Average Monthly Price") %>%
  dySeries("mean", label = "Mean Price") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)

As the graph suggests, the decade started with an average house price of around 548000$. A decrease from July 2014 to Feb 2015. A sharp increase from Feb 2015 to Apr 2015. During the spring and summer, market has a big demand for the housing. Then, the housing market goes slowly since it’s close to the end of the year and people will like to save some money for the hoilday expense and travelling.

*The variance in the data keeps on increasing with time.

Forcast the price

kinghousing_ts <- ts(kinghousing_time1$mean,start = c(2014, 1),frequency=6)
kinghousing_ts
## Time Series:
## Start = c(2014, 1) 
## End = c(2016, 1) 
## Frequency = 6 
##  [1] 548080.3 558002.2 544788.8 536445.3 529253.8 539027.0 521961.0 524461.9
##  [9] 525870.9 507851.4 543977.2 561837.8 558126.8
decomp = stl(kinghousing_ts, s.window="periodic")
deseasonal_ts <- seasadj(decomp)
final_ts <- diff(deseasonal_ts, d = 1)
fit2 = arima(final_ts, order=c(3,0,3))
fcast <- forecast(fit2, h=6)
plot(fcast)

From the Arima model, we can tell the house price will keep going up. The trade market will increase from year to year. I may need an extensive dataset to generate a more accurate forecast for the future king house market.

Conclusion:

This project shows that the king house market still is the top house market in Washington state. The price ranges from 250k to 750K with 2.5 bathrooms, 3-4 bathrooms is the buyer’s first choice. Also, people will look for a well-maintained or new house so they can just move in anytime. The house price will keep going up with the salary rise and market demand.