<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/a-simple-spatial-equilibrium-model-with-tidycensus/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ignaciomsarmiento.github.io/2018/07/10/A-Simple-Spatial-Equilibrium-Model-with-Tidycensus.html"> Ignacio Sarmiento Barbieri</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The Spatial Equilibrium concept is well known to urban economists. In a nutshell, it states that <strong>in equilibrium there are no rents to be gained by changing locations</strong>. Ed Glaeser begins Chapter 2 of his book: <a rel="nofollow" href="https://www.amazon.com/Agglomeration-Spatial-Equilibrium-Lindahl-Lectures/dp/019929044X" target="_blank">“Cities, Agglomeration, and Spatial Equilibrium”</a> with the well known Alonso-Muth-Mills model. In this post, I want to summarize it briefly following Ed Glaeser presentation and reproduce his 2.1. and 2.2. figures. This is the perfect excuse to play around with the “tidycensus” package.</p>
<h2>Spatial equilibrium in the Alons-Muth-Mills model</h2>
<p>In its simplest form, the model assumes a monocentric closed city. Furthermore, we assume that there are $N$ city dwellers that maximize their utility which depends on  consumption and  units of land</p>
<p></p>
<p>City dwellers use  units of land, consume , and commute to the city center. Consumption is equal to their income, minus transport costs and rental costs per unit of land. City dwellers income is an exogenous wage <em>w</em>. Commuting costs are a function of distance and for simplicity we assume it linear . Rental costs per unit of land are , where  is the rental cost per unit of land. Thus,</p>
<p></p>
<p>To get to the spatial equilibrium we first rewrite utility function</p>
<p></p>
<p>Then, the first order conditions imply</p>
<p></p>
<p></p>
<p>The equilibrium condition states that rents must decline exactly to offset the increase in transportation cost. The simple assumption that commuting costs are linear implies that the rent gradient is linear:</p>
<p></p>
<p>The key prediction of the model is that land rents will decline with distance to the city center.</p>
<h2>R code</h2>
<p>Let’s take the model to the data and reproduce figures 2.1. and 2.2 of <a rel="nofollow" href="https://www.amazon.com/Agglomeration-Spatial-Equilibrium-Lindahl-Lectures/dp/019929044X" target="_blank">“Cities, Agglomeration, and Spatial Equilibrium”</a>. The focus are two cities, Chicago and Boston. These cities are chosen because both differ in how easy is to access to their city centers. Chicago is fairly easy, Boston is more complicated. Our model then implies that gradients then should reflect the differential costs to access the city centers.</p>
<p>So let’s begin, the first step is to get some data. To do so I’m are going to use the <em>“tidycensus”</em> package. This package will allow me to get data from the census website using their API. We are also going to need the help of three other packages: <em>“sf”</em> to handle spatial data, <em>“dplyr”</em> my go-to package to wrangle data, and <em>“ggplot2”</em> to plot my results.</p>
<div>
<div>
<pre><code><span>require</span><span>(</span><span>&quot;tidycensus&quot;</span><span>,</span><span> </span><span>quietly</span><span>=</span><span>TRUE</span><span>)</span><span>
</span><span>require</span><span>(</span><span>&quot;sf&quot;</span><span>,</span><span> </span><span>quietly</span><span>=</span><span>TRUE</span><span>)</span><span>
</span><span>require</span><span>(</span><span>&quot;dplyr&quot;</span><span>,</span><span> </span><span>quietly</span><span>=</span><span>TRUE</span><span>)</span><span>
</span><span>require</span><span>(</span><span>&quot;ggplot2&quot;</span><span>,</span><span> </span><span>quietly</span><span>=</span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>In order to get access to the Census API, I need to supply a key, which can be obtained from <a rel="nofollow" href="http://api.census.gov/data/key_signup.html" target="_blank">http://api.census.gov/data/key_signup.html</a>.</p>
<div>
<div>
<pre><code><span>census_api_key</span><span>(</span><span>&quot;YOUR CENSUS API HERE&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## To install your API key for use in future sessions, run this function with `install = TRUE`.
</code></pre>
</div>
</div>
<p>Let’s get Median Housing Values from the latest ACS. I’ll use the variable “B25077_001E” which is the estimated Median Value of Owner-Occupied Housing Units. The package <em>“tidycensus”</em> provides the <em>get_acs</em> function that will let me retrieve this variable for the county at the block group level. It is important to note the option <em>geometry=T</em>. This option downloads also geometry features of the census block that will allow later on to compute distances.</p>
<div>
<div>
<pre><code><span>chicago</span><span>&lt;-</span><span>get_acs</span><span>(</span><span>geography</span><span> </span><span>=</span><span> </span><span>&quot;block group&quot;</span><span>,</span><span> </span><span>variables</span><span> </span><span>=</span><span> </span><span>&quot;B25077_001E&quot;</span><span>,</span><span> </span><span>state</span><span> </span><span>=</span><span> </span><span>&quot;IL&quot;</span><span>,</span><span>county</span><span> </span><span>=</span><span> </span><span>&quot;Cook County&quot;</span><span>,</span><span>year</span><span>=</span><span>2016</span><span>,</span><span>geometry</span><span> </span><span>=</span><span> </span><span>T</span><span>)</span><span> </span><span>#retrieve median housing values for Cook County</span><span>
</span><span>boston</span><span>&lt;-</span><span>get_acs</span><span>(</span><span>geography</span><span> </span><span>=</span><span> </span><span>&quot;block group&quot;</span><span>,</span><span> </span><span>variables</span><span> </span><span>=</span><span> </span><span>&quot;B25077_001E&quot;</span><span>,</span><span> </span><span>state</span><span> </span><span>=</span><span> </span><span>&quot;MA&quot;</span><span>,</span><span>county</span><span> </span><span>=</span><span> </span><span>&quot;Suffolk County&quot;</span><span>,</span><span>year</span><span>=</span><span>2016</span><span>,</span><span>geometry</span><span> </span><span>=</span><span> </span><span>T</span><span>)</span><span> </span><span>#retrieve median housing values for Suffolk County</span><span>
</span></code></pre>
</div>
</div>
<p>Next, we need the city centers. I create two <em>“sf”</em> objects that have the coordinates to the city center.</p>
<div>
<div>
<pre><code><span>chicago_cbd</span><span>&lt;-</span><span>st_as_sf</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>read.table</span><span>(</span><span>text</span><span>=</span><span>&quot;-87.627800 41.881998&quot;</span><span>),</span><span>
</span><span>coords</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>),</span><span>
</span><span>crs</span><span> </span><span>=</span><span> </span><span>&quot;+proj=longlat +datum=WGS84&quot;</span><span>)</span><span>
</span><span>boston_cbd</span><span>&lt;-</span><span>st_as_sf</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>read.table</span><span>(</span><span>text</span><span>=</span><span>&quot;-71.057083 42.361145&quot;</span><span>),</span><span>
</span><span>coords</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>),</span><span>
</span><span>crs</span><span> </span><span>=</span><span> </span><span>&quot;+proj=longlat +datum=WGS84&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>Now, I need to put everything in the same projection. I re-project my cbds to the same projection as my tibbles.</p>
<div>
<div>
<pre><code><span>chicago_cbd</span><span> </span><span>&lt;-</span><span>chicago_cbd</span><span> </span><span>%>%</span><span> </span><span>st_transform</span><span>(</span><span>st_crs</span><span>(</span><span>chicago</span><span>)</span><span> </span><span>)</span><span>
</span><span>boston_cbd</span><span> </span><span>&lt;-</span><span>boston_cbd</span><span> </span><span>%>%</span><span> </span><span>st_transform</span><span>(</span><span>st_crs</span><span>(</span><span>boston</span><span>)</span><span> </span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>The next step is to create distances. For that, I use the <em>st_distance</em> function. Given the projection of my tibbles is in meters, I transform all my distances to miles, one meter is 0.000621371 miles.</p>
<div>
<div>
<pre><code><span>chicago</span><span>$</span><span>dist_CBD</span><span>&lt;-</span><span>st_distance</span><span>(</span><span>chicago</span><span>,</span><span>chicago_cbd</span><span>)</span><span> </span><span>#computes distance to CBD (in meters)</span><span>
</span><span>chicago</span><span>$</span><span>dist_CBD</span><span>&lt;-</span><span>as.numeric</span><span>(</span><span>chicago</span><span>$</span><span>dist_CBD</span><span>)</span><span>*</span><span>0.000621371</span><span> </span><span>#change units to miles</span><span>
</span><span>boston</span><span>$</span><span>dist_CBD</span><span>&lt;-</span><span>st_distance</span><span>(</span><span>boston</span><span>,</span><span>boston_cbd</span><span>)</span><span> </span><span>#computes distance to CBD (in meters)</span><span>
</span><span>boston</span><span>$</span><span>dist_CBD</span><span>&lt;-</span><span>as.numeric</span><span>(</span><span>boston</span><span>$</span><span>dist_CBD</span><span>)</span><span>*</span><span>0.000621371</span><span> </span><span>#change units to miles</span><span>
</span></code></pre>
</div>
</div>
<p>The last part is some cleaning up. I add a column with the name of the city, keep block groups in Cook County that are within 10 miles of the city center, and keep the <em>data.frames</em> from my tibbles,. Finally, I bind the <em>data.frames</em> together.</p>
<div>
<div>
<pre><code><span>boston</span><span>$</span><span>City</span><span>&lt;-</span><span>&quot;Boston&quot;</span><span>
</span><span>chicago</span><span>$</span><span>City</span><span>&lt;-</span><span>&quot;Chicago&quot;</span><span>
</span><span>chicago</span><span>&lt;-</span><span>chicago</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>dist_CBD</span><span>&lt;=</span><span>10</span><span>)</span><span>
</span><span>chicago</span><span>&lt;-</span><span>data.frame</span><span>(</span><span>chicago</span><span>)</span><span>
</span><span>boston</span><span>&lt;-</span><span>data.frame</span><span>(</span><span>boston</span><span>)</span><span>
</span><span>dta</span><span>&lt;-</span><span>rbind</span><span>(</span><span>chicago</span><span>,</span><span>boston</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>We are now ready to plot. I use a regular scatter plot with hollow circles and add a linear regression line on a black and white background (<em>theme_bw()</em>)</p>
<div>
<div>
<pre><code><span>ggplot</span><span>(</span><span>dta</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span>=</span><span>dist_CBD</span><span>,</span><span> </span><span>y</span><span>=</span><span>estimate</span><span>,</span><span> </span><span>color</span><span>=</span><span>City</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>shape</span><span>=</span><span>1</span><span>)</span><span> </span><span>+</span><span> </span><span># Use hollow circles</span><span>
</span><span>geom_smooth</span><span>(</span><span>method</span><span>=</span><span>lm</span><span>)</span><span> </span><span>+</span><span> </span><span># Add linear regression line</span><span>
</span><span>xlab</span><span>(</span><span>&quot;Distance to CBD (miles)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylab</span><span>(</span><span>&quot;Median Housing Prices ($)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_bw</span><span>()</span><span>
</span></code></pre>
</div>
</div>
<p><img align="middle" src="https://i2.wp.com/ignaciomsarmiento.github.io/assets/images/amm_model/amm_figure.png?w=450&ssl=1" data-recalc-dims="1"> </p>
<p>Conclusion: the data reflect the predictions of the simple Alons-Muth-Mills model, land rents will decline with distance to the city center. The speed at which they decline depends on transportation costs.</p>
<h2>Session Info</h2>
<div>
<div>
<pre><code><span>sessionInfo</span><span>()</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.5

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] bindrcpp_0.2.2   ggplot2_2.2.1    dplyr_0.7.4      sf_0.6-3
[5] tidycensus_0.4.6

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     plyr_1.8.4       pillar_1.2.2     compiler_3.5.0
 [5] bindr_0.1.1      class_7.3-14     tools_3.5.0      uuid_0.1-2
 [9] digest_0.6.15    gtable_0.2.0     jsonlite_1.5     evaluate_0.10.1
[13] tibble_1.4.2     lattice_0.20-35  pkgconfig_2.0.1  rlang_0.2.0
[17] DBI_0.8          curl_3.2         rgdal_1.2-18     yaml_2.1.18
[21] spData_0.2.8.3   e1071_1.6-8      httr_1.3.1       stringr_1.3.0
[25] knitr_1.20       xml2_1.2.0       hms_0.4.2        rappdirs_0.3.1
[29] tigris_0.7       tidyselect_0.2.4 classInt_0.2-3   rprojroot_1.3-2
[33] grid_3.5.0       glue_1.2.0       R6_2.2.2         foreign_0.8-70
[37] rmarkdown_1.9    sp_1.2-7         readr_1.1.1      tidyr_0.8.0
[41] purrr_0.2.5      udunits2_0.13    magrittr_1.5     scales_0.5.0
[45] backports_1.1.2  htmltools_0.3.6  units_0.5-1      maptools_0.9-2
[49] assertthat_0.2.0 rvest_0.3.2      colorspace_1.3-2 labeling_0.3
[53] stringi_1.1.7    lazyeval_0.2.1   munsell_0.4.3    lwgeom_0.1-4
</code></pre>
</div>
</div>
	<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://ignaciomsarmiento.github.io/2018/07/10/A-Simple-Spatial-Equilibrium-Model-with-Tidycensus.html"> Ignacio Sarmiento Barbieri</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/nCm0xsmNgGk" height="1"></a></div>