#Training & Testing the Model
knitr::opts_chunk$set(echo = FALSE)

t_train <- prepared_data %>% 
  filter(year_sold == 2006 | year_sold == 2007 | year_sold == 2008 | year_sold == 2009) 

t_test <- prepared_data %>% 
  filter(year_sold == 2010)

Histograms

Correlation

Model

## 
## Call:
## lm(formula = sale_price ~ year_built + garage_area + mas_vnr_area + 
##     overall_qual_as_num + total_bsmt_sf, data = t_train)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -201501  -21204   -3191   15837  232248 
## 
## Coefficients:
##                        Estimate  Std. Error t value             Pr(>|t|)    
## (Intercept)         -238228.608   78869.887  -3.021              0.00257 ** 
## year_built               85.831      41.726   2.057              0.03987 *  
## garage_area              65.272       5.475  11.922 < 0.0000000000000002 ***
## mas_vnr_area             67.082       6.896   9.728 < 0.0000000000000002 ***
## overall_qual_as_num   28350.228    1011.535  28.027 < 0.0000000000000002 ***
## total_bsmt_sf            38.288       2.848  13.444 < 0.0000000000000002 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 35940 on 1399 degrees of freedom
## Multiple R-squared:  0.7677, Adjusted R-squared:  0.7669 
## F-statistic: 924.6 on 5 and 1399 DF,  p-value: < 0.00000000000000022

RSME & R^2

#RSME
t_train <- t_train %>% 
  mutate(predicted = predict(model, newdata = t_train),
         residual = predicted - sale_price)
mean(t_train$residual)
## [1] -0.0000000003143103
rsme_train = sqrt(mean(t_train$residual^2))
print(rsme_train)
## [1] 35863.4
t_test <- t_test %>% 
  mutate(predicted = predict(model, newdata = t_test),
         residual = predicted - sale_price)
mean(t_test$residual)
## [1] -2467.148
rsme_test = sqrt(mean(t_test$residual^2))
print(rsme_test)
## [1] 36804.88
#R^2
#train
residual_sum_of_squares <- sum(t_train$residual^2)

total_variation_in_model <-mean(t_train$sale_price) - t_train$sale_price

total_sum_of_squares <- sum(total_variation_in_model^2)

r2 <- 1-(residual_sum_of_squares/total_sum_of_squares) 

print(c('r-squared for train', r2))
## [1] "r-squared for train" "0.767681562286635"
#test
sum_squared_error <- sum(t_test$residual^2)

diff_of_mean <- mean(t_test$residual) - t_test$residual

squared_var <- sum(diff_of_mean^2)

r2 <- 1-(squared_var/diff_of_mean)

print(c('r-squared for test', r2))
##                                         1                    2 
## "r-squared for test"   "4838602.24054263"   "99521422.6277849" 
##                    3                    4                    5 
##  "-5392492.70429925"   "20158230.3368639"   "5478778.67718587" 
##                    6                    7                    8 
##  "-4583849.23599076"   "5797253.36522829"  "-34059930.5905934" 
##                    9                   10                   11 
##  "-296927571.520737"   "-35988033.539681"   "6559565.24908022" 
##                   12                   13                   14 
##   "5621978.52770792"   "3815804.98899747"   "-3860663.9401994" 
##                   15                   16                   17 
##  "-4109081.06640097"   "10369239.1968261"   "3486233.96503932" 
##                   18                   19                   20 
##   "9906944.46074837"  "-1085419.78573003"   "74408637.1049727" 
##                   21                   22                   23 
##  "-3850748.30720525"   "62970105.0853265"    "9292506.0500242" 
##                   24                   25                   26 
##   "5095033.40161641"  "-27290819.6732714"   "23827069.3306999" 
##                   27                   28                   29 
##  "-50745950.9454602"  "-17768858.8246254"  "-9106825.51515567" 
##                   30                   31                   32 
##  "-70871332.9192792"  "-33481621.3561454"   "11477371.2125671" 
##                   33                   34                   35 
##  "-22205977.6556592"  "-6457586.52066818"  "-11488645.2248824" 
##                   36                   37                   38 
##   "9705088.20096276"  "-52842205.4766288"   "34012693.3622881" 
##                   39                   40                   41 
##   "3924975.40718116"   "38572795.6546105"  "-31910137.8955216" 
##                   42                   43                   44 
##   "35337757.2622817"   "175360199.345136"   "58705622.6016026" 
##                   45                   46                   47 
##   "26691195.6537062"  "-6951925.06543174"   "16106814.3558422" 
##                   48                   49                   50 
##   "5836768.66423366"   "6458839.72780954"   "6213561.02017195" 
##                   51                   52                   53 
##   "8186523.29024328"   "6543628.84931619"   "6110175.67014597" 
##                   54                   55                   56 
##   "444451279.489116"    "25734140.841295"    "10589752.314312" 
##                   57                   58                   59 
##  "-48323898.4642103"    "44963820.724761"  "-10030078.7397473" 
##                   60                   61                   62 
##   "3757786.21621532"  "-20755512.2797605"  "-6717846.00871418" 
##                   63                   64                   65 
##  "-25560457.1653602"  "-7476939.53772072"   "20814749.7820656" 
##                   66                   67                   68 
##   "12524539.1411833"   "13808105.5383167"   "2950794.01969596" 
##                   69                   70                   71 
##   "6448813.01941082"    "4748223.2003134"   "20346838.4445698" 
##                   72                   73                   74 
##  "-28328634.3799723"  "-159558117.687287"   "10077816.6485513" 
##                   75                   76                   77 
##   "34601117.0204813"   "6418181.03801006"  "-32163746.7877587" 
##                   78                   79                   80 
##    "29170916.056941"   "24282037.2194222"   "11606804.6808812" 
##                   81                   82                   83 
##  "-30209547.3287637"   "26264740.4754823"   "300493337.434655" 
##                   84                   85                   86 
##  "-25645218.4563155"  "-9311540.42968342"  "-20031538.8461696" 
##                   87                   88                   89 
##  "-23088494.8671351"   "22932696.6129872"   "87686977.9050808" 
##                   90                   91                   92 
##  "-85307806.1580801"   "13281825.0807522"    "13266243.276429" 
##                   93                   94                   95 
##  "-6661283.04229675"   "12876394.8759981"   "28523904.5876937" 
##                   96                   97                   98 
##  "-297797323.249289"   "8197229.56793993"   "4901265.09026103" 
##                   99                  100                  101 
##  "-16120108.0645849"   "9591198.23313874"   "38452030.6862684" 
##                  102                  103                  104 
##  "-13014238.4412343"   "29099582.5638882"   "26664219.5836726" 
##                  105                  106                  107 
##   "2322914937.71902"    "5992498.5760847"   "8348059.01963568" 
##                  108                  109                  110 
##  "-18474343.6856413"  "-4639122.48278737"  "-16856811.4601885" 
##                  111                  112                  113 
##   "212078144.892089"   "14642458.8378286"   "14290661.3396503" 
##                  114                  115                  116 
##   "88042505.1651527"   "15507695.2408896"   "4693009.25473404" 
##                  117                  118                  119 
##   "36798246.4808894"   "-11750984.116575"   "1112820868.31108" 
##                  120                  121                  122 
##  "-22336796.6879761"   "53452456.3184043"  "-8406039.77056131" 
##                  123                  124                  125 
##  "-2247590.12030139"  "-6422368.74240895"  "-6424717.51587087" 
##                  126                  127                  128 
##  "-6162930.83876711"   "8402023.63131992"   "8899091.25651308" 
##                  129                  130                  131 
##   "16964815.2758158"  "-3437816.60457575"   "5939818.52491619" 
##                  132                  133                  134 
##  "-6901980.53535312"  "-30384077.7037884"   "14287988.9513976" 
##                  135                  136                  137 
##   "7799458.07837513"   "6058829.85669857"  "-2933015.93901359" 
##                  138                  139                  140 
##  "-23323081.4960262"   "24070333.5943063"  "-16433343.2358465" 
##                  141                  142                  143 
##   "142256691.969223"   "18342116.2747034"  "-21547838.4995492" 
##                  144                  145                  146 
##  "-9605264.33388689"   "5971367.92961652"   "9832486.07754062" 
##                  147                  148                  149 
##   "5822254.42821318"  "-4630358.81120448"  "-5246678.47418745" 
##                  150                  151                  152 
##  "-9680630.86940142"  "-179060445.780501"    "25819689.631743" 
##                  153                  154                  155 
##   "6120110.60813678"   "203635633.388585"   "69800994.2324864" 
##                  156                  157                  158 
##    "41186048.497241"   "-10035987.063012"  "-5887100.47239393" 
##                  159                  160                  161 
##   "-3201729.8217045"  "-2601728.03086242"  "-12731922.7710864" 
##                  162                  163                  164 
##  "-3551570.81406548"  "-3893826.97690802"   "3451329.29820247" 
##                  165                  166                  167 
##   "7983934.42725758"  "-11757335.6628062"   "18458135.7650625" 
##                  168                  169                  170 
##   "6861864.91445222"  "-4460573.14483371"  "-19093153.6998392" 
##                  171                  172                  173 
##  "-3996280.79109905"  "-12919496.3662908"  "-10740418982.3292" 
##                  174                  175                  176 
##    "33610964.472571"   "28770772.6551662"   "7426988.15739295" 
##                  177                  178                  179 
##   "201266736.566178"  "-49283444.2848259"  "-26060151.4527308" 
##                  180                  181                  182 
##   "59419488.6222965"   "103654113.541407"  "-7016634.97624741"

Performance Description

Thesis: The sales price of the houses in Ames, Iowa area are influenced by the overall quality of the house, including garage area and basement square foot.

Variables:

Overall Quality: Looks at the quality of different aspects of the house. The data follows a normal distribution with gaps since the quality ratings fell in the areas of poor, fair, average, good, and excellent. This variable had the strongest positive correlation out of all my variables with the sales price. The quality is similar to the value of the house which is what people look for when purchasing a new home and why this variable plays a large role in determining sales price.

Year Built: States when the building was constructed. This variable was one of my less correlated variables to sales price, but still had a positive correlation. The data of houses is left-skewed slightly, showing more houses have been built in the 2000’s than the 1900’s which may hint to a higher price for houses since more are being built.

Garage Area: Here,the amount of space in the garage is given in squared feet. The data is fairly uniform and is slightly right-skewed, meaning most garages are around 500-600 square feet. There seem to be more houses with larger garage square footage, but they are spread out and differ in the actual square footage. The positive correlation between garage area and the sales price shows that the price goes up with a garage included in the house. If people want garages, that will make the price increase.

Total Basement Square Foot: The amount of basement space is given in squared feet. This variable had the lowest standard error or the least amount of discrepancy in the data. With this being the most accurate variable, it provides a reliable measure to help determine the sales price.

Masonry Veneer Area: If the house has any type of brick, this metric displays the measure of how much is on the outside of the house. While the data is right-skewed, there is a positive relationship with sales price. When veneer is present the house costs more and when there is more of the veneer on the house the price continues to rise. While not as significant as the other variables, it still adds to predicting the sales price of the houses.

Predicted Values and Errors: The residual standard error is extremely large. This means that the predicted variables vary drastically from what the true sales price is. Since the predicted variables at large are not accurate, that means they are not the most reliable to predict the sales price.

Visuals/Graphs

ggplot() +
  geom_abline(intercept = 0, slope = 1, color = 'purple') +
  geom_point(data = t_test, 
             mapping = aes(x = predicted, y = sale_price),
             color = 'blue') +
  geom_point(data = t_train, 
             mapping = aes(x = predicted, y = sale_price),
             color = 'orange')