<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/housing-prices-in-ames-iowa-kaggles-advanced-regression-competition/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://nycdatascience.com/blog/student-works/housing-prices-in-ames-iowa-kaggles-advanced-regression-competition/"> R – NYC Data Science Academy Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 722px) 100vw, 722px" src="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/neighborhood-puzzle.jpg?w=450&ssl=1" alt="" class="wp-post-image" data-recalc-dims="1"></p>
<p><b>Introduction</b></p>
<p>Residential real estate prices are fascinating… and frustrating. The homebuyer, the home-seller, the real estate agent, the economist, and the banker are all interested in housing prices, but they’re sufficiently subtle that no one person or industry has a complete understanding.</p>
<p>For our third overall project and first group project we were assigned <a rel="nofollow" href="https://www.kaggle.com/c/house-prices-advanced-regression-techniques" target="_blank">Kaggle’s Advanced Regression Techniques Competition</a>. <em>The goal, for the project and the original competition, was to predict housing prices in Ames, Iowa.</em> While this particular competition is no longer active, the premise proved to be a veritable playground for testing our knowledge of data cleaning, exploratory data analysis, statistics, and, most importantly, machine learning. What follows is the combined work of Quentin Picard, Paul Ton, Kathryn Bryant, and Hans Lau.</p>
<p><strong>Data</strong></p>
<p>As stated on the Kaggle competition description page, the data for this project was compiled by Dean De Cock for educational purposes, and it includes 79 predictor variables (house attributes) and one target variable (price). As a result of the educational nature of the competition, the data was pre-split into a training set and a test set; the two datasets were given in the forms of csv files, each around 450 KB in size. Each of the predictor variables could fall under one of the following:</p>
<ul>
<li>lot/land variables</li>
<li>location variables</li>
<li>age variables</li>
<li>basement variables</li>
<li>roof variables</li>
<li>garage variables</li>
<li>kitchen variables</li>
<li>room/bathroom variables</li>
<li>utilities variables</li>
<li>appearance variables</li>
<li>external features (pools, porches, etc.) variables</li>
</ul>
<p>The specifics of the 79 predictor variables are omitted for brevity but can be found in the <a rel="nofollow" href="https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data" target="_blank">data_description.txt file</a> on the competition website. The target variable was Sale Price, given in US dollars.</p>
<p><strong>Project Overview (Process)</strong></p>
<p>The lifecycle of our project was a typical one. We started with data cleaning and basic exploratory data analysis, then proceeded to feature engineering, individual model training, and ensembling/stacking. Of course, the process in practice was not quite so linear and the results of our individual models alerted us to areas in data cleaning and feature engineering that needed improvement. We used root mean squared error (RMSE) of log Sale Price to evaluate model fit as this was the metric used by Kaggle to evaluate submitted models.</p>
<p>Data cleaning, EDA, feature engineering, and private train/test splitting (and one spline model!) were all done in R but  we used Python for individual model training and ensembling/stacking. Using R and Python in these ways worked well, but the decision to split work in this manner was driven more by timing with curriculum than by anything else.</p>
<p>Throughout the project our group wanted to mimic a real-world machine learning project as much as possible, which meant that although we were given both a training set and a test set, we opted to treat the given test set as if it were “future” data. As a result, we further split the Kaggle training data into a private training set and a private testing set, with an 80/20 split, respectively. This allowed us to evaluate models in two ways before predicting on the Kaggle test data: with RMSE  of predictions made on the private test set and with cross validation RMSE of the entire training set.</p>
<p>Given the above choices, the process for training and evaluating each individual model was broken down as follows:</p>
<ol>
<li>Grid search to tune hyper parameters (if applicable) to private train set</li>
<li>Fit model to private train set with tuned parameters</li>
<li>Predict on private test set; if RMSE okay, proceed.</li>
<li>Fit new model to Kaggle train set with private train hyperparameters</li>
<li>Cross-validate on Kaggle train set; if CV RMSE okay, proceed.</li>
<li>Predict on Kaggle test set</li>
<li>Submit predictions to Kaggle</li>
</ol>
<p>Computing RMSE of predictions made on the private test set served as a group sanity check, in that if anything was amiss with a model we found out at this point and could correct for it before proceeding. The decision to fit a new model to the Kaggle train in step 4 set using the <em>private train</em> hyperparameters found in step 1 was one for which we never felt completely at ease; we were trying to minimize overfitting to the Kaggle training set, but also recognized that we weren’t using all the possible information we could by not re-tuning using the full training set. One benefit to this choice was that we never saw any major discrepancies between our own cross validation scores in step 5 and the Kaggle scores from step 7. With more time, we would have further investigated whether keeping the private train parameters or re-tuning for the final models was more beneficial.</p>
<p>The last noteworthy choice we made in our overall process was to feed differently-cleaned datasets to our linear-based models and our tree-based models. In linear-based models (linear, ridge, LASSO, elastic net, spline), prediction values are continuous and sensitive to outliers so we opted to sacrifice information about “rare” houses in favor of gaining better predictions on “common” houses. We also wanted to minimize the likelihood of rare levels only showing up in our test data and/or causing columns of all 0’s in dummifed data. We executed this tradeoff by releveling any nominal categorical variables that contained extremely rare classes, where “rare” was defined to be “occuring in less than 1% of the observations.” <a rel="nofollow" href="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/releveling_motivation.jpg?ssl=1" target="_blank"><img sizes="(max-width: 417px) 100vw, 417px" src="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/releveling_motivation-300x203.jpg?resize=417%2C282&ssl=1" alt="" class="wp-image-26023" data-recalc-dims="1"></a></p>
<p>For example, the Heating variable had six levels but four of them combined accounted for only about 1% of the observations; so, we combined these four levels into a new ‘other’ level so that releveled Heating had only three levels, all accounting for 1% or more of the observations. Depending on the variable, we either created an ‘other’ level as in the example or we grouped rare levels into existing levels according to level similarity in the variable documentation.</p>
<p>We opted not to relevel data fed into our tree-based models because tree predictions are more robust to outliers and rare classes; trees can separate rare observations from others through splits, which prevents common observation predictions from being distorted by rare observations in fitting.</p>
<p><strong>Data Cleaning and EDA</strong></p>
<p>We were fortunate with the Kaggle data in that it came to us relatively clean. The only basic cleaning tasks were to correct typos in levels of categorical variables, specify numeric or categorical variables in R, and rename variables beginning with numbers to satisfy R’s variable name requirements. There were a number of  “quality” and “condition” variables that had levels of Poor, Fair, Typical/Average, Good, and Excellent which we label encoded as integers 1-5 to preserve their inherent ordinality. For nominal categorical variables, we used one-hot encoding from the ‘vtreat’ package in R. (Most machine learning algorithms in Python require all variables to have numeric values, and although R’s machine learning algorithms can handle nominal categorical variables in non-numeric/string form, R’s computations effectively use one-hot encoding under the hood in these cases.)</p>
<p>After taking care of these basic cleaning issues, we needed to address missingness in our data. Below is a plot that shows the variables containing missing values and the degree to which missingness occurs in each (shown in yellow):</p>
<p><a rel="nofollow" href="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/missingdata.jpg?ssl=1" target="_blank"><img sizes="(max-width: 408px) 100vw, 408px" src="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/missingdata-300x198.jpg?resize=408%2C269&ssl=1" alt="" class="wp-image-26020" data-recalc-dims="1"></a></p>
<p>For all variables except Garage Year Built and Lot Frontage, we performed basic mode imputation. Mode imputation was chosen for simplicity and because it could be done with both categorical and numerical data. Of course, mode imputation  has the negative side effect of artificially decreasing variance within the affected variable and therefore would not be appropriate for variables with higher degrees of missingness. It was for this reason, in fact, that we approached missingness differently for Garage Year Built and Lot Frontage.</p>
<p>For missing values in Garage Year Built, we imputed the Year Built for the house. We justified this because most garages are built at the same time as the house, and houses without garages get no penalty or benefit by having the Garage Year Built equal to the Year Built for the house.</p>
<p>For Lot Frontage, the variable with the greatest degree of missingness, was researched and explored in order to arrive at an imputation strategy. Lot Frontage was defined to be the linear feet of street connected to the property. Given that most properties were either regularly or only slightly irregularly shaped according to Lot Shape, we deduced that most properties would have Lot Frontage values that were correlated with Lot Area values. However, since length is measured in units and area is measure in <em>square</em> units, we found it most appropriate to relate log(Lot Frontage) with log(Lot Area), so as to get a linear relationship between the two. See plot below.</p>
<p> </p>
<p><a rel="nofollow" href="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/frontage_impute1.jpg?ssl=1" target="_blank"><img sizes="(max-width: 622px) 100vw, 622px" src="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/frontage_impute1-300x150.jpg?w=450&ssl=1" alt="" class="wp-image-26021" data-recalc-dims="1"></a></p>
<p>Thus, we imputed missing values for Lot Frontage by fitting a linear model of log(Lot Frontage) regressed onto log(Lot Area), predicting on the missing values for Lot Frontage using Lot Area, and then exponentiating (inverse log-ing) the result.</p>
<p>The next step in EDA after finding and addressing missingness was to look at outliers. Looking at boxplots of both Sale Price and log(Sale Price) we saw that there were quite a few outliers, where ‘outliers’ mathematically defined to be observations lying more than 1.5*IQR (Inner Quartile Range) above the third quartile and below the first quartile.</p>
<p><a rel="nofollow" href="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/SalePrice_boxplots.png?ssl=1" target="_blank"><img sizes="(max-width: 440px) 100vw, 440px" src="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/SalePrice_boxplots-300x235.png?resize=440%2C345&ssl=1" alt="" class="wp-image-26035" data-recalc-dims="1"></a></p>
<p>Although removing the outliers may have improved our predictions for average-priced homes, we were hesitant to do so due to the relatively small size of our sample (~1600 observations in Kaggle training set). We felt that removing any outliers without further justification than them simply being outliers would likely jeopardize our predictions for houses in the extremes.</p>
<p>Since outliers would have the most impact on the fit of linear-based models, we further investigated outliers by training a basic multiple linear regression model on the Kaggle training set with all observations included; we then looked at the resulting influence and studentized residuals plots:</p>
<p><a rel="nofollow" href="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/influenceplot.jpeg?ssl=1" target="_blank"><img sizes="(max-width: 581px) 100vw, 581px" src="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/influenceplot-300x134.jpeg?w=450&ssl=1" alt="" class="wp-image-26047" data-recalc-dims="1"></a></p>
<p><a rel="nofollow" href="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Residuals-Leverage.jpeg?ssl=1" target="_blank"><img sizes="(max-width: 591px) 100vw, 591px" src="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Residuals-Leverage-300x134.jpeg?w=450&ssl=1" alt="" class="wp-image-26044" data-recalc-dims="1"></a></p>
<p>From these, we saw that there were only two observations that could justifiably be removed: observation 1299 and observation 251. These were both beyond or on the lines representing Cook’s Distance, meaning that as individual observations they had a significant impact on the regression formula; as such, we removed these two observations from consideration.</p>
<p>The last bit of preprocessing we did was dealing with multicollinearity. This, as for outliers, was cleaning done mostly for the benefit of linear-based models; in fact, it was done <em>only</em> for vanilla multiple linear regression since regularization in ridge, LASSO, and elastic net models deals with collinearity by construction. To eliminate collinearity issues, we used the findLinearCombos() function from the ‘caret’ package in R on our dummified data. This function identified linear combinations between predictor variables and allowed us to easily drop linearly dependent variables.</p>
<p><strong>Feature Engineering</strong></p>
<p>For feature engineering we took a two-pronged approach: we used anecdotal data/personal knowledge and we did research.</p>
<ul>
<li><strong>Garage interaction:</strong> Garage Quality * Number of Cars Garage Holds.
<ul>
<li>If a home has a really great or really poor garage, the impact of that quality component on price will be exacerbated by the size of the garage.</li>
</ul>
</li>
<li><strong>Total number of bathrooms:</strong> Full Bath + Half Bath + Basement Full Bath + Basement Half Bath.
<ul>
<li>In our experience, houses are often listed in terms of total number of bedrooms and total number of bathrooms. Our data had total number of bedrooms, but lacked total number of bathrooms.</li>
</ul>
</li>
<li><strong>Average room size:</strong> Above-Ground Living Area / Total Number of R00ms Above Ground.
<ul>
<li>“Open concept” homes have been gaining popularity and homes with large rooms have always been popular, and with the provided variables we believe average room size might address both of these trends.</li>
</ul>
</li>
<li><strong>Bathroom to room ratio:</strong> (Full Bath + Half Bath) / Number of Bedrooms Above Ground
<ul>
<li>The number of bathrooms desired in a house depends on the number of bedrooms. A home with one bedroom will not be viewed nearly as negatively for having only one bathroom as would a house with three bedrooms.</li>
</ul>
</li>
<li><strong>Comparative size of living area:</strong> Above-Ground Living Area / mean(Above-Ground Living Area)
<ul>
<li>This variable attempts to capture house size as it directly compares to other houses, but in a manner different from mere number of rooms.</li>
</ul>
</li>
<li><strong>Landscape-ability interaction:</strong> Lot Shape * Land Contour
<ul>
<li>Landscaped homes tend to sell for more (<a rel="nofollow" href="https://www.washingtonpost.com/news/where-we-live/wp/2015/04/01/how-much-value-does-landscaping-add-to-your-home-when-selling/?utm_term=.c1af8199b46b" target="_blank">see this article</a>) and the ability for a lot to be easily landscaped is, in part, determined by the shape of the lot (regular, slightly irregular, irregular, very irregular) and the land contour (level, banked, hillside, depressed). Certain combinations may be workable (regular shape, hillside) while other combinations (very irregular shape, hillside) may make landscaping difficult and/or expensive.</li>
</ul>
</li>
</ul>
<p>Of the six features that we added, only “landscape-ability” resulted from research; we either already had the important industry variables in the data (total above-ground square footage or neighborhood, for example) or we did not have the information we needed to create the desired variables. Additional features we would have liked to have added include geolocation data for proximity to schools and shopping centers, quality of nearby schools, and specific rooms/house features remodeled (if applicable).</p>
<p>One step that we glossed over a bit was extensive adding and removing of features. We noticed with just a few trials that removing existing features seemed to negatively impact the performance of our models and that the models improved when we added features. Given that the regularized linear models would give preference to more important features via larger coefficients and tree-based models would give preference to more important features by splitting preferences, we opted not to spend too much time manually adding and removing features. Essentially, we allowed our models to select the most predictive features themselves.</p>
<p><strong>Modeling</strong></p>
<p>For our individual models, we trained the following:</p>
<ul>
<li>Multiple linear regression</li>
<li>Ridge regression</li>
<li>LASSO regression</li>
<li>Elastic net regression</li>
<li>Spline regression</li>
<li>Basic decision tree</li>
<li>Random forest</li>
<li>Gradient boosted tree</li>
<li>XGBoosted tree</li>
</ul>
<p>Overall our models consistently had RMSE values between .115 and .145. As stated earlier, our cross validation scores were usually very close to our scores on Kaggle. To our surprise, our linear-based models did very well compared to our tree-based models. Even the much-hyped XGBoost model was at best on par with our linear-based spline and elastic net models, and our random forest models tended to be much worse. An abbreviated table of our results is shown below:</p>
<table cellspacing="5">
<tbody>
<tr>
<td></td>
<td>Elastic Net</td>
<td>Spline</td>
<td>Random Forest</td>
<td>Gradient Boost</td>
<td>XGBoost</td>
<td>Ensemble</td>
</tr>
<tr>
<td>Cross Validation</td>
<td>0.12157</td>
<td>0.11181</td>
<td>0.14171</td>
<td>0.12491</td>
<td>0.12282</td>
<td>0.11227</td>
</tr>
<tr>
<td>Held-out Test</td>
<td>0.11762</td>
<td>0.11398</td>
<td>0.13834</td>
<td>0.11403</td>
<td>0.11485</td>
<td>n/a</td>
</tr>
<tr>
<td>Kaggle</td>
<td>0.12107</td>
<td>0.11796</td>
<td>n/a</td>
<td>n/a</td>
<td>n/a</td>
<td>0.11710</td>
</tr>
</tbody>
</table>
<p>As we endeavored to find a final, “best” model via ensembling and stacking, we followed the advice of our instructor (and successful Kaggler) Zeyu by attempting to combine models with different strengths and weaknesses.</p>
<p>We took the predictions from each of our best base models (Spline, Gradient Boost, XGBoost) as the features to use for the next level. Our best result came from a weighted average of the three, with weights determined by a grid search. The blend of 76% Spline, 14.5% Gradient Boost, and 9.5% Xgboost gave us a 0.11227 RMSE on our training set and 0.11710 on Kaggle.</p>
<p>We also experimented with using a second level meta-model to do the stacking, but with a linear meta-model, the coefficients were highly unstable because the predictions were so closely related to each other, and with a gradient boost meta-model, we were unable to beat our best base model alone.</p>
<p><strong>Conclusions</strong></p>
<p>As mentioned above, we were surprised by the strength of our various linear models in comparison to our tree models. We suspect this had a lot to do with the data itself, in that Sale Price (or rather, log(Sale Price)) likely has a relatively linear relationship with the predictor variables. This highlights one of the most important takeaways from this project: that linear models have a real place in machine learning.</p>
<p>In situations like this, where performance between a simpler model and a more complex model are similar, the better interpretability and the ease of training of the simpler model may also prove to be deciding factors on model choice.  In most cases, stacking multiple base-layer models into a second-layer is impractical, despite performing slightly better.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://nycdatascience.com/blog/student-works/housing-prices-in-ames-iowa-kaggles-advanced-regression-competition/"> R – NYC Data Science Academy Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/pJuZBhtby6E" height="1"></a></div>