library(readr)
data <- read_csv("/Users/sunyang/Downloads/data.csv")
## Rows: 4600 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (4): street, city, statezip, country
## dbl  (13): 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.
library(ggplot2)

options(repr.plot.width = 12, repr.plot.height = 8)
data <- read.csv(file = '/Users/sunyang/Downloads/data.csv')
head(data)
tail(data)
print(paste("Number of records: ", nrow(data)))
## [1] "Number of records:  4600"
print(paste("Number of features: ", ncol(data)))
## [1] "Number of features:  18"
summary(data)
##      date               price             bedrooms       bathrooms    
##  Length:4600        Min.   :       0   Min.   :0.000   Min.   :0.000  
##  Class :character   1st Qu.:  322875   1st Qu.:3.000   1st Qu.:1.750  
##  Mode  :character   Median :  460943   Median :3.000   Median :2.250  
##                     Mean   :  551963   Mean   :3.401   Mean   :2.161  
##                     3rd Qu.:  654962   3rd Qu.:4.000   3rd Qu.:2.500  
##                     Max.   :26590000   Max.   :9.000   Max.   :8.000  
##   sqft_living       sqft_lot           floors        waterfront      
##  Min.   :  370   Min.   :    638   Min.   :1.000   Min.   :0.000000  
##  1st Qu.: 1460   1st Qu.:   5001   1st Qu.:1.000   1st Qu.:0.000000  
##  Median : 1980   Median :   7683   Median :1.500   Median :0.000000  
##  Mean   : 2139   Mean   :  14852   Mean   :1.512   Mean   :0.007174  
##  3rd Qu.: 2620   3rd Qu.:  11001   3rd Qu.:2.000   3rd Qu.:0.000000  
##  Max.   :13540   Max.   :1074218   Max.   :3.500   Max.   :1.000000  
##       view          condition       sqft_above   sqft_basement   
##  Min.   :0.0000   Min.   :1.000   Min.   : 370   Min.   :   0.0  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:1190   1st Qu.:   0.0  
##  Median :0.0000   Median :3.000   Median :1590   Median :   0.0  
##  Mean   :0.2407   Mean   :3.452   Mean   :1827   Mean   : 312.1  
##  3rd Qu.:0.0000   3rd Qu.:4.000   3rd Qu.:2300   3rd Qu.: 610.0  
##  Max.   :4.0000   Max.   :5.000   Max.   :9410   Max.   :4820.0  
##     yr_built     yr_renovated       street              city          
##  Min.   :1900   Min.   :   0.0   Length:4600        Length:4600       
##  1st Qu.:1951   1st Qu.:   0.0   Class :character   Class :character  
##  Median :1976   Median :   0.0   Mode  :character   Mode  :character  
##  Mean   :1971   Mean   : 808.6                                        
##  3rd Qu.:1997   3rd Qu.:1999.0                                        
##  Max.   :2014   Max.   :2014.0                                        
##    statezip           country         
##  Length:4600        Length:4600       
##  Class :character   Class :character  
##  Mode  :character   Mode  :character  
##                                       
##                                       
## 
colnames(data)
##  [1] "date"          "price"         "bedrooms"      "bathrooms"    
##  [5] "sqft_living"   "sqft_lot"      "floors"        "waterfront"   
##  [9] "view"          "condition"     "sqft_above"    "sqft_basement"
## [13] "yr_built"      "yr_renovated"  "street"        "city"         
## [17] "statezip"      "country"
unique(data$city)
##  [1] "Shoreline"           "Seattle"             "Kent"               
##  [4] "Bellevue"            "Redmond"             "Maple Valley"       
##  [7] "North Bend"          "Lake Forest Park"    "Sammamish"          
## [10] "Auburn"              "Des Moines"          "Bothell"            
## [13] "Federal Way"         "Kirkland"            "Issaquah"           
## [16] "Woodinville"         "Normandy Park"       "Fall City"          
## [19] "Renton"              "Carnation"           "Snoqualmie"         
## [22] "Duvall"              "Burien"              "Covington"          
## [25] "Inglewood-Finn Hill" "Kenmore"             "Newcastle"          
## [28] "Mercer Island"       "Black Diamond"       "Ravensdale"         
## [31] "Clyde Hill"          "Algona"              "Skykomish"          
## [34] "Tukwila"             "Vashon"              "Yarrow Point"       
## [37] "SeaTac"              "Medina"              "Enumclaw"           
## [40] "Snoqualmie Pass"     "Pacific"             "Beaux Arts Village" 
## [43] "Preston"             "Milton"
maindf <- data[,c("price","bedrooms","sqft_living","floors",
                  "sqft_lot", "condition", "view", "yr_built")]
head(maindf)
maindf$oldbuilt <- as.integer(format(Sys.Date(), "%Y")) - maindf$yr_built

drops <- c("yr_built")
maindf = maindf[ , !(names(maindf) %in% drops)]
cor(maindf)
##                   price    bedrooms sqft_living      floors     sqft_lot
## price        1.00000000  0.20033629  0.43041003  0.15146080  0.050451295
## bedrooms     0.20033629  1.00000000  0.59488406  0.17789490  0.068819355
## sqft_living  0.43041003  0.59488406  1.00000000  0.34485027  0.210538454
## floors       0.15146080  0.17789490  0.34485027  1.00000000  0.003749750
## sqft_lot     0.05045130  0.06881935  0.21053845  0.00374975  1.000000000
## condition    0.03491454  0.02507986 -0.06282598 -0.27501339  0.000558114
## view         0.22850417  0.11102800  0.31100944  0.03121095  0.073906741
## oldbuilt    -0.02185683 -0.14246104 -0.28777522 -0.46748066 -0.050706346
##                condition       view    oldbuilt
## price        0.034914537 0.22850417 -0.02185683
## bedrooms     0.025079856 0.11102800 -0.14246104
## sqft_living -0.062825979 0.31100944 -0.28777522
## floors      -0.275013395 0.03121095 -0.46748066
## sqft_lot     0.000558114 0.07390674 -0.05070635
## condition    1.000000000 0.06307728  0.39969823
## view         0.063077281 1.00000000  0.06446506
## oldbuilt     0.399698234 0.06446506  1.00000000
library(ggcorrplot)
corr <- round(cor(maindf), 1)

# Plot
ggcorrplot(corr,
           type = "lower",
           lab = TRUE, 
           lab_size = 5,  
           colors = c("tomato2", "white", "springgreen3"),
           title="Correlogram of Housing Dataset", 
           ggtheme=theme_bw)

pairs(~bedrooms + sqft_living + floors + condition, data = maindf,
       main = "Scatterplot Matrix")

theme_set(theme_bw()) 
g <- ggplot(maindf, aes(bedrooms, floors))
g + geom_count(col="tomato3", show.legend=F) +
  labs(y="floors", 
       x="bedrooms", 
       title="Bedrooms vs Floors")

plot(x = maindf$sqft_living, y = maindf$sqft_lot,
     xlab = "sqft_living",
     ylab = "sqft_lot",
     xlim = c(0, 3000), 
     ylim = c(0, 20000),
     main = "sqft_living vs sqft_lot"
)

ggplot(maindf,aes(y=price,x=sqft_living)) +
       geom_point() + 
        xlim(0, 9000) +
        ylim(0, 5000000) +
        geom_smooth(formula = y ~ x,method="lm")
## Warning: Removed 5 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).

linearmodel = lm(price~bedrooms + sqft_living + floors + sqft_lot + condition + view + oldbuilt,
                 data = maindf)
summary(linearmodel)
## 
## Call:
## lm(formula = price ~ bedrooms + sqft_living + floors + sqft_lot + 
##     condition + view + oldbuilt, data = maindf)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2051655  -131243   -18918    89678 26356050 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.758e+05  5.646e+04  -3.113 0.001861 ** 
## bedrooms    -5.371e+04  1.022e+04  -5.255 1.55e-07 ***
## sqft_living  2.806e+02  1.097e+01  25.588  < 2e-16 ***
## floors       6.329e+04  1.620e+04   3.907 9.46e-05 ***
## sqft_lot    -7.094e-01  2.116e-01  -3.352 0.000809 ***
## condition    2.870e+04  1.204e+04   2.384 0.017159 *  
## view         5.880e+04  1.018e+04   5.773 8.28e-09 ***
## oldbuilt     2.099e+03  3.036e+02   6.914 5.37e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 500500 on 4592 degrees of freedom
## Multiple R-squared:  0.2132, Adjusted R-squared:  0.212 
## F-statistic: 177.7 on 7 and 4592 DF,  p-value: < 2.2e-16