<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/kaggles-advanced-regression-competition-predicting-housing-prices-in-ames-iowa/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://nycdatascience.com/blog/student-works/kaggles-advanced-regression-competition-predicting-housing-prices-in-ames-iowa/"> 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: 1024px) 100vw, 1024px" src="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Homes-sold-home-for-sale-report-1024x681.jpg?w=450&ssl=1" alt="" class="wp-post-image" data-recalc-dims="1"></p>
<p><strong>Introduction</strong></p>
<hr>
<p><a rel="nofollow" href="https://www.kaggle.com/" target="_blank">Kaggle.com</a> is a website designed for data scientists and data enthusiasts to connect and compete with each other. It is an open community that hosts forums and competitions in the wide field of data. In each Kaggle competition, competitors are given a training data set, which is used to train their models, and a test data set, used to test their models. Kagglers can then submit their predictions to view how well their score (e.g., accuracy or error) compares to others’.</p>
<p>As a team, we joined the <a rel="nofollow" href="https://www.kaggle.com/c/house-prices-advanced-regression-techniques" target="_blank">House Prices: Advanced Regression Techniques</a> Kaggle challenge to test our model building and machine learning skills. For this competition, we were tasked with predicting housing prices of residences in Ames, Iowa. Our training data set included 1460 houses (i.e., observations) accompanied by 79 attributes (i.e., features, variables, or predictors) and the sales price for each house. Our testing set included 1459 houses with the same 79 attributes, but sales price was not included as this was our target variable.</p>
<p>To view our code (split between R and Python) and our project presentation slides for this project see our shared GitHub <a rel="nofollow" href="https://github.com/nmaloof/KaggleProject" target="_blank">repository</a>.</p>
<p> </p>
<p><strong>Understanding the Data</strong></p>
<hr>
<p>Of the 79 variables provided, 51 were categorical and 28 were continuous.</p>
<p>Our first step was to combine these data sets into a single set both to account for the total missing values and to fully understand all the classes for each categorical variable. That is, there might be missing values or different class types in the test set that are not in the training set.</p>
<p> </p>
<p><strong>Processing the Data</strong></p>
<hr>
<p><span>Response Variable</span></p>
<p>As our response variable, Sale Price, is continuous, we will be utilizing regression models. One assumption of linear regression models is that the error between the observed and expected values (i.e., the residuals) should be normally distributed. Violations of this assumption often stem from a skewed response variable. Sale Price has a right skew, so we log + 1 transform it to normalize its distribution.</p>
<p><a rel="nofollow" href="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Sale-Price.png?ssl=1" target="_blank"><img sizes="(max-width: 900px) 100vw, 900px" src="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Sale-Price-1024x440.png?w=450&ssl=1" alt="" class="wp-image-26095" data-recalc-dims="1"></a></p>
<p> </p>
<p><span>Missing Data</span></p>
<p>Machine learning algorithms do not handle missing values very well, so we must obtain an understanding of the missing values in our data to determine the best way to handle them. We find that 34 of the predictor variables have values that are interpreted by R and Python as missing (i.e., “NA” and “NaN”). Below we describe examples of some of the ways we treated these missing data.</p>
<p> </p>
<p>1) NA/NaN is actually a class:</p>
<p>In many instances, what R and Python interpret as a missing value is actually a class of the variable. For example, Pool Quality is comprised of 5 classes: Excellent, Good, Fair, Typical, and NA. The NA class describes houses that do not have a pool, but our coding languages interpret houses of NA class as a missing value instead of a class of the Pool Quality variable.</p>
<p>Our solution was to impute most of the NA/NaN values to a value of “None.”</p>
<p><a rel="nofollow" href="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Screen-Shot-2017-11-19-at-7.10.10-PM-2.png?ssl=1" target="_blank"><img sizes="(max-width: 500px) 100vw, 500px" src="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Screen-Shot-2017-11-19-at-7.10.10-PM-2-300x206.png?w=450&ssl=1" alt="" class="wp-image-26106" data-recalc-dims="1"></a></p>
<p> </p>
<p>2) Not every NA/NaN corresponds to a missing attribute:</p>
<p>While we found that most NA/NaN values corresponded to an actual class for different variables, some NA/NaN values actually represented missing data. For example, we find that three houses with NA/NaN values for Pool Quality, also have a non-zero value for the variable, Pool Area (square footage of pool). These three houses likely have a pool, but its quality was not assessed or input into the data set.</p>
<p>Our solution was to first calculate mean Pool Area for each class of Pool Quality, then impute the missing Pool Quality classes based on how close that house’s Pool Area was to the mean Pool Areas for each Pool Quality class. For example, the first row in the below picture on the left has a Pool Area of 368 square feet. The average Pool Area for houses with Excellent pool quality (Ex) is about 360 square feet (picture on the right). Therefore, we imputed this house to have a Pool Quality of Excellent.</p>
<p><a rel="nofollow" href="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Impute-Pool-QC.png?ssl=1" target="_blank"><img sizes="(max-width: 600px) 100vw, 600px" src="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Impute-Pool-QC-1024x295.png?w=450&ssl=1" alt="" class="wp-image-26108" data-recalc-dims="1"></a></p>
<p>3) Domain knowledge:</p>
<p>Some variables had a moderate amount of missingness. For example, about 17% of the houses were missing the continuous variable, Lot Frontage, the linear feet of street connected to the property. Intuitively, attributes related to the size of a house are likely important factors regarding the price of the house. Therefore, dropping these variables seems ill-advised.</p>
<p>Our solution was based on the assumption that houses in the same neighborhood likely have similar features. Thus, we imputed the missing Lot Frontage values based on the median Lot Frontage for the neighborhood in which the house with missing value was located.</p>
<p><a rel="nofollow" href="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/neighborhood.png?ssl=1" target="_blank"><img sizes="(max-width: 294px) 100vw, 294px" src="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/neighborhood-294x300.png?resize=294%2C300&ssl=1" alt="" class="wp-image-26110" data-recalc-dims="1"></a></p>
<p>4) Imputing with mode:</p>
<p>Most variables have some intuitive relationship to other variables, and imputation can be based on these related features. But some missing values are found in variables with no apparent relation to others. For example, the Electrical variable, which describes the electrical system, was missing for a single observation.</p>
<p>Our solution was to simply find the most common class for this categorical variable and impute for this missing value.</p>
<p><a rel="nofollow" href="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/electrical.png?ssl=1" target="_blank"><img sizes="(max-width: 500px) 100vw, 500px" src="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/electrical-300x220.png?w=450&ssl=1" alt="" class="wp-image-26111" data-recalc-dims="1"></a></p>
<p><span>Ordinal Categories</span></p>
<p>For linear (but not tree-based) models, categorical variables must be treated as continuous. There are two types of categorical features: ordinal, where there is an inherent order to the classes (e.g., Excellent is greater than Good, which is greater than Fair), and nominal, where there is no obvious order (e.g., red, green, and blue).</p>
<p>Our solution for ordinal variables was to simply assign the classes a number corresponding to their relative ranks. For example, Kitchen Quality has five classes: Excellent, Good, Typical, Fair, and Poor, which we encoded (i.e., converted) to the numbers 5, 4, 3, 2, and 1, respectively.</p>
<p><a rel="nofollow" href="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/label-encode.png?ssl=1" target="_blank"><img sizes="(max-width: 500px) 100vw, 500px" src="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/label-encode.png?w=450&ssl=1" alt="" class="wp-image-26112" data-recalc-dims="1"></a></p>
<p><span>Nominal Categories</span></p>
<p>The ranking of nominal categories is not appropriate as there is no actual rank among the classes.</p>
<p>Our solution was to one-hot encode these variables, which creates a new variable for each class with values of zero (not present) or one (present).</p>
<p><a rel="nofollow" href="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Screen-Shot-2017-11-19-at-6.53.37-PM-2.png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Screen-Shot-2017-11-19-at-6.53.37-PM-2-1024x297.png?w=450&ssl=1" alt="" class="wp-image-26100" data-recalc-dims="1"></a></p>
<p><span>Outliers</span></p>
<p>An outlier can be defined with a quantitative (i.e., statistical) or qualitative definition. We opted for the qualitative version when looking for outliers: observations that are abnormally far from other values. Viewing the relationship between Above Ground Living Area and Sale Price, we noticed some very large areas for very low prices.</p>
<p>Our solution was to remove these observations as we thought they fit our chosen definition of an outlier, and because they might increase our models’ errors.</p>
<p><a rel="nofollow" href="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/outliers.png?ssl=1" target="_blank"><img sizes="(max-width: 500px) 100vw, 500px" src="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/outliers.png?w=450&ssl=1" alt="" class="wp-image-26102" data-recalc-dims="1"></a></p>
<p> </p>
<p><span>Skewness</span></p>
<p>While there are few assumptions regarding the independent variables of regression models, often transforming skewed variables to a normal distribution can improve model performance.</p>
<p>Our solution was to log + 1 transform several of the predictors.</p>
<p> </p>
<p><span>Near Zero Predictors</span></p>
<p>Predictors with very low variance offer little predictive power to models.</p>
<p>Our solution was to find the ratio of the second most frequent value to the most frequent value for each predictor, and to remove variables where this ratio was less than 0.05. This roughly translates to dropping variables where 95% or more of the values are the same.</p>
<p> </p>
<p><strong>Feature Engineering</strong></p>
<hr>
<p>Feature (variable or predictor) engineering is one of the most important steps in model creation. Often there is valuable information “hidden” in the predictors that is only revealed when manipulating these features in some way. Below are just some examples of the features we created:</p>
<ul>
<li><span>Remodeled (categorical)</span>: Yes or No if Year Built is different from Year Remodeled; If the year the house was remodeled is different from the year it was built, the remodeling likely increases property value</li>
<li><span>Seasonality (categorical)</span>: Combined Month Sold with Year Sold; While more houses were sold during summer months, this likely varies across years, especially during the time period these houses were sold, which coincides with the housing crash (2006-2010)</li>
<li><span>New House (categorical)</span>: Yes or No if Year Sold is equal to Year Built; If a house was sold the same year it was built, we might expect it was in high demand and might have a higher Sale Price</li>
<li><span>Total Area (continuous)</span>: Sum of all variables that describe the area of different sections of a house; There are many variables that pertain to the square footage of different aspects of each house, we might expect that the total square footage has a strong influence on Sale Price</li>
</ul>
<p> </p>
<p><strong>Models and Results</strong></p>
<hr>
<p>Now that we have prepared our data set, we can begin training our models and use them to predict Sale Price.</p>
<p>We trained and tested dozens of versions of the models described below with different combinations of engineered features and processed variables. The information in the table represents our best results for each model. The table explains the pros and cons for each model type, the optimal hyperparameters found through either grid search or Bayesian optimization, our test score, and the score we received from Kaggle. Our scores the root mean square error (RMSE) of our predictions, which is a metric for describing the difference between the observed values and our predicted values for Sale Price; scores closer to zero are better.</p>
<p><a rel="nofollow" href="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Screen-Shot-2017-11-19-at-4.48.14-PM-2.png?ssl=1" target="_blank"><img sizes="(max-width: 1753px) 100vw, 1753px" src="https://i1.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Screen-Shot-2017-11-19-at-4.48.14-PM-2.png?w=450&ssl=1" alt="" class="wp-image-26060" data-recalc-dims="1"></a></p>
<p>For brevity, we will not describe the details of the different models. However, see the following links for more information about how each model is used to create predictions: <a rel="nofollow" href="https://en.wikipedia.org/wiki/Random_forest" target="_blank">random forest</a>, <a rel="nofollow" href="https://en.wikipedia.org/wiki/Gradient_boosting" target="_blank">gradient boost</a>, <a rel="nofollow" href="https://machinelearningmastery.com/gentle-introduction-xgboost-applied-machine-learning/" target="_blank">XGBoost</a>, <a rel="nofollow" href="https://en.wikipedia.org/wiki/Elastic_net_regularization" target="_blank">elastic net regularization for regression</a>.</p>
<p> </p>
<p>Below are plots summarizing variables that contribute most to the respective model’s prediction of Sale Price.</p>
<p>For most models, predictors related to square footage (Area), quality (different Quality measures), and age (Year Built) have the strongest impact on each model’s predictions.</p>
<p><a rel="nofollow" href="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Screen-Shot-2017-11-19-at-5.07.14-PM-2.png?ssl=1" target="_blank"><img sizes="(max-width: 1624px) 100vw, 1624px" src="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Screen-Shot-2017-11-19-at-5.07.14-PM-2.png?w=450&ssl=1" alt="" class="wp-image-26065" data-recalc-dims="1"></a></p>
<p><a rel="nofollow" href="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Gradient-boost.png?ssl=1" target="_blank"><img sizes="(max-width: 1588px) 100vw, 1588px" src="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/Gradient-boost.png?w=450&ssl=1" alt="" class="wp-image-26066" data-recalc-dims="1"></a></p>
<p> </p>
<p><strong><a rel="nofollow" href="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/XGBoost.png?ssl=1" target="_blank"><img sizes="(max-width: 1559px) 100vw, 1559px" src="https://i0.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/XGBoost.png?w=450&ssl=1" alt="" class="wp-image-26067" data-recalc-dims="1"></a><br>
</strong></p>
<p><a rel="nofollow" href="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/coeffs-1.png?ssl=1" target="_blank"><img sizes="(max-width: 1830px) 100vw, 1830px" src="https://i2.wp.com/nycdatascience.com/blog/wp-content/uploads/2017/11/coeffs-1.png?w=450&ssl=1" alt="" class="wp-image-26074" data-recalc-dims="1"></a></p>
<p> </p>
<p>There is no visualization for our best model, which was an ensemble of four other models. The predictions for this ensembled model are calculated by averaging the predictions from the separate models (two linear regression models and two tree-based models). The idea is that each model’s predictions include error both above and below the real values, and the averaged predictions of the best models might have less overall error than any one single model.</p>
<p>One note is that tree-based models (random forest, gradient boosting, and XGBoosting) cannot provide an idea of positive or negative influence for each variable on Sale Price, rather they can only provide an idea of how important that variable is to the models’ predictions overall. In contrast, linear models can provide information about which variables positively and negatively influence Sale Price. For the figure immediately above, the strongest predictor, residency in a Commercial Zone, is actually negatively related to Sale Price.</p>
<p> </p>
<p><strong>Conclusions</strong></p>
<hr>
<p>The objective of this Kaggle competition was to build models to predict housing prices of different residences in Ames, IA. Our best model resulted in an RMSE of 0.1071, which translates to an error of about $9000 (or about 5%) for the average-priced house.</p>
<p>While this error is quite low, the interpretability of our model is poor. Each model found within our ensembled model varies with respect to the variables that are most important to predicting Sale Price. The best way to interpret our ensemble is to look for shared variables among its constituent models. The variables seen as most important or as strongest predictors through our models were those related to square footage, the age and condition of the home, the neighborhood where the house was located, the city zone where the house was located, and the year the house was sold.</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/kaggles-advanced-regression-competition-predicting-housing-prices-in-ames-iowa/"> 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/rXRRE-E5sII" height="1"></a></div>