<p>What is my home worth?  Many homeowners in America ask themselves this question, and many have an answer.  What does the market think, though?  The best way to estimate a property’s value is by looking at other, similar properties that have sold recently in the same area – the comparable sales approach.  In an effort to allow homeowners to do some exploring (and because I needed a new project), I developed a small Shiny app with R.</p>
<p>My day job allows me access to the local multiple listing service, which provides a wealth of historic data.  The following project makes use of that data to map real estate that has sold near Raleigh, NC in the past six months.  Without getting too lost in the weeds I’ll go over a few key parts of the process.  Feel free to jump over to <a target="_blank" rel="nofollow" href="https://github.com/kiefersmith/House-Search-v2">my GitHub page </a>to check out the full source code.  <a target="_blank" rel="nofollow" href="https://kiefer.shinyapps.io/compsearchv08/">Click here to view the app!</a></p>
<ol>
<li><strong>Geocode everything.  </strong>The data did not come with latitude and longitude coordinates, so we’ll have to do some geocoding.  I haven’t found an efficient way to do this in R, so, like in the <a target="_blank" rel="nofollow" href="https://realdataweb.wordpress.com/2017/02/15/creating-a-mailing-list-in-qgis-and-r/">mailing list example</a>, I’ll use QGIS to process my data and return a .csv for each town I’m interested in.<br>
<img sizes="(max-width: 656px) 100vw, 656px" data-image-description="" data-orig-file="https://i2.wp.com/realdataweb.files.wordpress.com/2017/03/screen-shot-2017-03-12-at-5-43-28-pm.png?w=456&amp;ssl=1" data-orig-size="1016,602" data-image-title="Screen Shot 2017-03-12 at 5.43.28 PM" class="wp-image-394" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="394" data-permalink="https://realdataweb.wordpress.com/2017/03/16/mapping-housing-data-with-r/screen-shot-2017-03-12-at-5-43-28-pm/" alt="Screen Shot 2017-03-12 at 5.43.28 PM" data-medium-file="https://i2.wp.com/realdataweb.files.wordpress.com/2017/03/screen-shot-2017-03-12-at-5-43-28-pm.png?w=456&amp;ssl=1?w=300" data-large-file="https://i2.wp.com/realdataweb.files.wordpress.com/2017/03/screen-shot-2017-03-12-at-5-43-28-pm.png?w=456&amp;ssl=1?w=656" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i2.wp.com/realdataweb.files.wordpress.com/2017/03/screen-shot-2017-03-12-at-5-43-28-pm.png?w=456&ssl=1"></li>
<li><strong>Setup your data.  </strong>To make sure that everything runs smoothly later on, we’ve got to import our data using <code>readr</code> and make sure each attribute is typed properly.
<pre>
library(readr)
apex &lt;- read_csv(&quot;apex2.csv&quot;)

#Remove non-character elements from these columns.
df$`Sold Price` &lt;- as.numeric(gsub(&quot;[^0-9]&quot;,&quot;&quot;,df$`Sold Price`))
df$`List Price` &lt;- as.numeric(gsub(&quot;[^0-9]&quot;,&quot;&quot;,df$`List Price`))

#Some re-typing for later.
df$Fireplace &lt;- as.numeric(df$Fireplace)
df$`New Constr` &lt;- as.factor(df$`New Constr`)

#Assign some placeholders.
assign(&quot;latitude&quot;, NA, envir = .GlobalEnv)
assign(&quot;longitude&quot;, NA, envir = .GlobalEnv)
</pre>
</li>
<li><strong>Get info from the user. </strong> The first thing the app wants you to do is give it some characteristics about the subject property, a property that you are interested in valuating.  A function further down uses this information to produce a map using these inputs.
<pre>
 #What city's dataset are we using?
 selectInput(&quot;city&quot;, label = &quot;City&quot;, c(&quot;Apex&quot;, &quot;Cary&quot;, &quot;Raleigh&quot;))

 #Get some info.
 textInput(&quot;address&quot;,label = &quot;Insert Subject Property Address&quot;, value = &quot;2219 Walden Creek Drive&quot;),
 numericInput(&quot;dist&quot;, label = &quot;Miles from Subject&quot;, value = 5, max = 20),
 numericInput(&quot;footage&quot;,label = &quot;Square Footage&quot;, value = 2000),
 selectInput(&quot;acres&quot;,label = &quot;How Many Acres?&quot;, acresf)

 #Changes datasets based on what city you choose on the frontend.
 #This expression is followed by two more else if statements.
observeEvent(input$city, {
 if(input$city == &quot;Apex&quot;) {
 framework_retype(apex)
 cityschools &lt;-schoolsdf$features.properties %>%
 filter(ADDRCITY_1 == &quot;Apex&quot;)
 assign(&quot;cityschools&quot;, cityschools, envir = .GlobalEnv)

 #Draw the map on click.
 observeEvent(input$submit, {
 output$map01 &lt;- renderLeaflet({distanceFrom(input$address, input$footage, input$acres,tol = .15, input$dist)
 })
 })

</pre>
</li>
<li><strong>Filter the data.  </strong>The <code>distanceFrom</code> function above uses dplyr to filter the properties in the selected city by square footage, acreage, and distance from the subject property.  The <code>tol</code> argument is used to give a padding around square footage – few houses match exactly in that respect.
<pre>
 #Filter once.
 houses_filtered &lt;- houses %>%
  filter(Acres == acres)%>%
  filter(LvngAreaSF >= ((1-tol)*sqft)) %>%
  filter(LvngAreaSF &lt;= ((1+tol)*sqft))

 #This grabs lat &amp; long from Google.
 getGeoInfo(subj_address)
 longitude_subj &lt;- as.numeric(longitude)
 latitude_subj &lt;- as.numeric(latitude)

 #Use the comparable house locations.
 xy &lt;- houses_filtered[,1:2]
 xy &lt;- as.matrix(xy)

 #Calculate distance.
 d &lt;- spDistsN1(xy, c(longitude_subj, latitude_subj), longlat = TRUE)
 d &lt;- d/1.60934
 d &lt;- substr(d, 0,4)

 #Filter again.
 distance &lt;- houses_filtered %>%
  filter(distanceMi &lt;= dist)
</pre>
</li>
<li><strong>Draw the map. </strong>The most important piece, the map, is drawn using Leaflet.  I have the Schools layer hidden initially because it detracts from the main focus – the houses.
<pre>
map &lt;- leaflet() %>%
 addTiles(group = &quot;Detailed&quot;) %>%
 addProviderTiles(&quot;CartoDB.Positron&quot;, group = &quot;Simple&quot;) %>%
 addAwesomeMarkers(lng = longitude, lat = latitude, popup = subj_address, icon = awesomeIcons(icon='home', markerColor = 'red'), group = &quot;Subject Property&quot;) %>%
 addAwesomeMarkers(lng = distance$X, lat = distance$Y, popup = paste(distance$Address,distance$`Sold Price`, distance$distanceMi, sep = &quot;&quot;), icon = awesomeIcons(icon = 'home', markerColor = 'blue'), group = &quot;Comps&quot;)%>%
 addAwesomeMarkers(lng = schoolsdf$long, lat = schoolsdf$lat, icon = awesomeIcons(icon = 'graduation-cap',library = 'fa', markerColor = 'green', iconColor = '#FFFFFF'), popup = schoolsdf$features.properties$NAMELONG, group = &quot;Schools&quot;)%>%
  addLayersControl(
   baseGroups = c(&quot;Simple&quot;, &quot;Detailed&quot;),
   overlayGroups = c(&quot;Subject Property&quot;, &quot;Comps&quot;, &quot;Schools&quot;),
   options = layersControlOptions(collapsed = FALSE))

map &lt;- map %>% hideGroup(group = &quot;Schools&quot;) </pre>
</li>
<li><strong>Regression model. </strong> The second tab at the top of the page leads to more information input that is used in creating a predictive model for the subject property.  The implementation is somewhat messy, so if you’d like to check it out the code is at the bottom of <code>app.R</code> in the GitHub repo.</li>
</ol>
<p>That’s it!  It took a while to get all the pieces together, but I think the final product is useful and I learned a lot along the way.  There are a few places I want to improve: simplify the re-typing sections, make elements refresh without clicking submit, among others.  If you have any questions about the code please leave a comment or feel free to <a target="_blank" rel="nofollow" href="https://realdataweb.wordpress.com/contact/">send me an email</a>.</p>
<p>Happy coding,</p>
<p>Kiefer Smith</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/realdataweb.wordpress.com/296/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/realdataweb.wordpress.com/296/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/SPBQaHpphqU">