metachronistic http://swingleydev.com/blog/ Latest metachronistic posts en-us Wed, 03 Jan 2018 19:32:58 -0900 Lincoln in the Bardo, George Saunders http://swingleydev.com/blog/p/2007/ <div class="document"> <p>Well that was disappointing. I’ve read some of George Saunders’s short stories and was entertained, but I didn’t much enjoy <a class="reference external" href="https://www.amazon.com/dp/0812995341">Lincoln in the Bardo</a>. It’s the story of Abraham Lincoln coming to the graveyard to visit his newly dead son William, told from the perspective of a variety of lost souls that don’t believe they’re dead. There was no plot to speak of, and none of the large cast of characters was appealing. I did enjoy the sections that were fictional quotes from contemporary histories, many of which contradicted each other on the details, and some of the characters told funny stories, but it didn’t hold together as a novel.</p> <p>Widely acclaimed, winner of the Man Booker Prize, on many best of 2017 lists. Not my cup of tea.</p> <p>Music I listened to while reading this:</p> <ul class="simple"> <li>Carlow Town, Seamus Fogarty</li> <li>You’ve Got Tonight, Wiretree</li> </ul> </div> Wed, 03 Jan 2018 19:32:58 -0900 http://swingleydev.com/blog/p/2007/ books George Saunders All Our Wrong Todays, Elan Mastai http://swingleydev.com/blog/p/2006/ <div class="document"> <p>It’s one day until <a class="reference external" href="https://themorningnews.org/archive/tag/tob18">The Tournament of Books</a> announces the list of books for this year’s competition, and I’ve been reading some of the <a class="reference external" href="https://themorningnews.org/article/the-year-in-fiction-2017">Long List</a>, including the book commented on here, Elan Mastai’s <a class="reference external" href="https://www.amazon.com/dp/1101985135">All Our Wrong Todays</a>. I throughly enjoyed it. The writing sparkles, the narrator is hilarously self-deprecating, and because of the premise, there is a lot of insightful commentary about contemporary society.</p> <p>The main plot line is that the main character grew up in an alternative timeline where a device that produces free energy was invented in 1965 and put into the public domain. With free energy and fifty plus years, his world is something of a techonological utopia (especially compared with our present). However, for reasons best left unspoiled, he alters the timeline and is stuck here in our timeline with the rest of us.</p> <p>The narrator on waking up for the first time in our timeline:</p> <blockquote> Here, it’s like nobody has considered using even the most rudimentary technology to improve the process. Mattresses don’t subtly vibrate to keep your muscles loose. Targeted steam valves don’t clean your body in slumber. I mean, blankets are made from tufts of plant fiber spun into thread and occasionally stuffed with feathers. Feathers. Like from actual birds.</blockquote> <p>While there’s a lot of science-fiction concepts in the story, it’s really more of a love story than what it sounds like it’d be. There were a couple plot points I probably would have written differently, but the book is really funny, touching and thoughful. I highly recommend it. Best book I’ve read in 2018 so far…</p> <p>A couple other quotes I found particularly timely:</p> <blockquote> Part of the problem is this world is basically a cesspool of misogyny, male entitlement, and deeply demented gender constructs accepted as casual fact by outrageously large swaths of the human population.</blockquote> <p>and</p> <blockquote> People are despondent about the future because they’re increasingly aware that we, as a species, chased an inspiring dream that led us to ruin. We told ourselves the world is here for us to control, so the better our technology, the better our control, the better our world will be. The fact that for every leap in technology the world gets more sour and chaotic is deeply confusing. The better things we build keep making it worse. The belief that the world is here for humans to control is the philosophical bedrock of our civilization, but it’s a mistaken belief. Optimism is the pyre on which we’ve been setting ourselves aflame.</blockquote> <p>Music I listened to while reading this book:</p> <ul class="simple"> <li>Jesus Christ, Brand New</li> <li>House of Cards, Radiohead</li> <li>Conundrum, Hak Baker</li> <li>Die Young, Sylvan Esso</li> <li>Feat &amp; Force, Vagabon</li> <li>No War, Cari Cari</li> </ul> </div> Tue, 02 Jan 2018 15:18:40 -0900 http://swingleydev.com/blog/p/2006/ books Elan Mastai Big South Fork River & Recreation Area, February Climate http://swingleydev.com/blog/p/2005/ <div class="document"> <div class="section" id="introduction"> <h1>Introduction</h1> <p>I’m planning a short trip to visit family in Florida and thought I’d take advantage of being in a new place to do some late winter backpacking where it’s warmer than in Fairbanks. I think I’ve settled on a 3‒5 day backpacking trip in Big South Fork National River and Recreation Area, which is in northeastern Tennesee and southeastern Kentucky.</p> <p>Except for a couple summer trips in New England in the 80s, my backpacking experience has been in summer, in places where it doesn’t rain much and is typically hot and dry (California, Oregon). So I’d like to find out what the weather should be like when I’m there.</p> </div> <div class="section" id="data"> <h1>Data</h1> <p>I’ll use the <a class="reference external" href="https://www.ncdc.noaa.gov/ghcn-daily-description">Global Historical Climatology Network — Daily</a> dataset, which contains daily weather observations for more than 100&nbsp;thousand stations across the globe. There are more than 26&nbsp;thousand active stations in the United States, and data for some U.S. stations goes back to 1836. I loaded the entire dataset—2.4&nbsp;billion records as of last week—into a PostgreSQL database, partitioning the data by year. I’m interested in daily minimum and maximum temperature (<tt class="docutils literal">TMIN</tt>, <tt class="docutils literal">TMAX</tt>), precipitation (<tt class="docutils literal">PRCP</tt>) and snowfall (<tt class="docutils literal">SNOW</tt>), and in stations within 50&nbsp;miles of the center of the recreation area.</p> <p>The following map shows the recreation area boundary (with some strange drawing errors, probably due to using the <tt class="docutils literal">fortify</tt> command) in green, the Tennessee/Kentucky border across the middle of the plot, and the 19&nbsp;stations used in the analysis.</p> <div class="figure"> <img alt="//media.swingleydev.com/img/blog/2017/12/biso_stations.svgz" class="img-responsive" src="//media.swingleydev.com/img/blog/2017/12/biso_stations.svgz" /> </div> <p>Here are the details on the stations:</p> <table border="1" class="docutils"> <colgroup> <col width="15%" /> <col width="32%" /> <col width="13%" /> <col width="11%" /> <col width="11%" /> <col width="12%" /> <col width="7%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">station_id</th> <th class="head">station_name</th> <th class="head">start_year</th> <th class="head">end_year</th> <th class="head">latitude</th> <th class="head">longitude</th> <th class="head">miles</th> </tr> </thead> <tbody valign="top"> <tr><td>USC00407141</td> <td>PICKETT SP</td> <td>2000</td> <td>2017</td> <td>36.5514</td> <td>-84.7967</td> <td>6.13</td> </tr> <tr><td>USC00406829</td> <td>ONEIDA</td> <td>1959</td> <td>2017</td> <td>36.5028</td> <td>-84.5308</td> <td>9.51</td> </tr> <tr><td>USC00400081</td> <td>ALLARDT</td> <td>1928</td> <td>2017</td> <td>36.3806</td> <td>-84.8744</td> <td>12.99</td> </tr> <tr><td>USC00404590</td> <td>JAMESTOWN</td> <td>2003</td> <td>2017</td> <td>36.4258</td> <td>-84.9419</td> <td>14.52</td> </tr> <tr><td>USC00157677</td> <td>STEARNS 2S</td> <td>1936</td> <td>2017</td> <td>36.6736</td> <td>-84.4792</td> <td>16.90</td> </tr> <tr><td>USC00401310</td> <td>BYRDSTOWN</td> <td>1998</td> <td>2017</td> <td>36.5803</td> <td>-85.1256</td> <td>24.16</td> </tr> <tr><td>USC00406493</td> <td>NEWCOMB</td> <td>1999</td> <td>2017</td> <td>36.5517</td> <td>-84.1728</td> <td>29.61</td> </tr> <tr><td>USC00158711</td> <td>WILLIAMSBURG 1NW</td> <td>2011</td> <td>2017</td> <td>36.7458</td> <td>-84.1753</td> <td>33.60</td> </tr> <tr><td>USC00405332</td> <td>LIVINGSTON RADIO WLIV</td> <td>1961</td> <td>2017</td> <td>36.3775</td> <td>-85.3364</td> <td>36.52</td> </tr> <tr><td>USC00154208</td> <td>JAMESTOWN WWTP</td> <td>1971</td> <td>2017</td> <td>37.0056</td> <td>-85.0617</td> <td>39.82</td> </tr> <tr><td>USC00406170</td> <td>MONTEREY</td> <td>1904</td> <td>2017</td> <td>36.1483</td> <td>-85.2650</td> <td>40.04</td> </tr> <tr><td>USC00406619</td> <td>NORRIS</td> <td>1936</td> <td>2017</td> <td>36.2131</td> <td>-84.0603</td> <td>41.13</td> </tr> <tr><td>USC00402202</td> <td>CROSSVILLE ED &amp; RESEARCH</td> <td>1912</td> <td>2017</td> <td>36.0147</td> <td>-85.1314</td> <td>41.61</td> </tr> <tr><td>USW00053868</td> <td>OAK RIDGE ASOS</td> <td>1999</td> <td>2017</td> <td>36.0236</td> <td>-84.2375</td> <td>42.24</td> </tr> <tr><td>USC00401561</td> <td>CELINA</td> <td>1948</td> <td>2017</td> <td>36.5408</td> <td>-85.4597</td> <td>42.31</td> </tr> <tr><td>USC00157510</td> <td>SOMERSET 2 N</td> <td>1950</td> <td>2017</td> <td>37.1167</td> <td>-84.6167</td> <td>42.36</td> </tr> <tr><td>USW00003841</td> <td>OAK RIDGE ATDD</td> <td>1948</td> <td>2017</td> <td>36.0028</td> <td>-84.2486</td> <td>43.02</td> </tr> <tr><td>USW00003847</td> <td>CROSSVILLE MEM AP</td> <td>1954</td> <td>2017</td> <td>35.9508</td> <td>-85.0814</td> <td>43.87</td> </tr> <tr><td>USC00404871</td> <td>KINGSTON</td> <td>2000</td> <td>2017</td> <td>35.8575</td> <td>-84.5278</td> <td>45.86</td> </tr> </tbody> </table> <p>To perform the analysis, I collected all valid observations for the stations listed, then reduced the results, including observations where the day of the year was between 45 and 52 (February&nbsp;14‒21).</p> <table border="1" class="docutils"> <colgroup> <col width="43%" /> <col width="57%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">variable</th> <th class="head">observations</th> </tr> </thead> <tbody valign="top"> <tr><td>PRCP</td> <td>5,942</td> </tr> <tr><td>SNOW</td> <td>5,091</td> </tr> <tr><td>TMAX</td> <td>4,900</td> </tr> <tr><td>TMIN</td> <td>4,846</td> </tr> </tbody> </table> </div> <div class="section" id="results"> <h1>Results</h1> <div class="section" id="temperature"> <h2>Temperature</h2> <p>We will consider temperature first. The following two plots show the distribution of daily minimum and maximum temperatures. In both plots, the bars represent the number of observations at that temperature, the vertical red line through the middle of the plot shows the average temperature, and the light orange and blue sections show the ranges of temperatures enclosing 80% and 98% of the data.</p> <div class="figure"> <img alt="//media.swingleydev.com/img/blog/2017/12/min_temp_dist.svgz" class="img-responsive" src="//media.swingleydev.com/img/blog/2017/12/min_temp_dist.svgz" /> </div> <div class="figure"> <img alt="//media.swingleydev.com/img/blog/2017/12/max_temp_dist.svgz" class="img-responsive" src="//media.swingleydev.com/img/blog/2017/12/max_temp_dist.svgz" /> </div> <p>The minimum daily temperature figure shows that the average minimum temperature is below freezing, (28.9&nbsp;°F) and eighty percent of all days in the third week of February were between 15 and 43&nbsp;°F (the light orange region). The minimum temperature was colder than 15&nbsp;°F or warmer than 54&nbsp;°F 2% of the time (the light blue region). Maximum daily temperature was an average of 51&nbsp;°F, and was rarely below freezing or above 72&nbsp;°F.</p> <p>Another way to look at this sort of data is to count particular occurances and divide by the total, “binning” the data into groups. Here we look at the number of days that were below freezing, colder than 20&nbsp;°F or colder than 10&nbsp;°F.</p> <table border="1" class="docutils"> <colgroup> <col width="34%" /> <col width="32%" /> <col width="34%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">temperature</th> <th class="head">observed&nbsp;days</th> <th class="head">percent&nbsp;chance</th> </tr> </thead> <tbody valign="top"> <tr><td>below freezing</td> <td>3,006</td> <td>62.0</td> </tr> <tr><td>colder than 20</td> <td>1,079</td> <td>22.3</td> </tr> <tr><td>colder than 10</td> <td>203</td> <td>4.2</td> </tr> <tr><td>TOTAL</td> <td>4,846</td> <td>100.0</td> </tr> </tbody> </table> <p>What about the daily maximum temperature?</p> <table border="1" class="docutils"> <colgroup> <col width="36%" /> <col width="34%" /> <col width="30%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">temperature</th> <th class="head">observed days</th> <th class="head">percent chance</th> </tr> </thead> <tbody valign="top"> <tr><td>colder than 20</td> <td>22</td> <td>0.4</td> </tr> <tr><td>below freezing</td> <td>371</td> <td>7.6</td> </tr> <tr><td>below 40</td> <td>1,151</td> <td>23.5</td> </tr> <tr><td>above 50</td> <td>2,569</td> <td>52.4</td> </tr> <tr><td>above 60</td> <td>1,157</td> <td>23.6</td> </tr> <tr><td>above 70</td> <td>80</td> <td>1.6</td> </tr> <tr><td>TOTAL</td> <td>4,900</td> <td>100.0</td> </tr> </tbody> </table> <p>The chances of it being below freezing during the day are pretty slim, and more than half the time it’s warmer than 50&nbsp;°F, so even if it’s cold at night, I should be able to get plenty warm hiking during the day.</p> </div> <div class="section" id="precipitation"> <h2>Precipitation</h2> <p>How often it rains, and how much falls when it does is also important for planning a successful backpacking trip. Most of my backpacking has been done in the summer in California, where rainfall is rare and even when it does rain, it’s typically over quickly. Daily weather data can’t tell us about the hourly pattern of rainfall, but we can find out how often and how much it has rained in the past.</p> <table border="1" class="docutils"> <colgroup> <col width="35%" /> <col width="32%" /> <col width="33%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">rainfall&nbsp;amount</th> <th class="head">observed&nbsp;days</th> <th class="head">percent&nbsp;chance</th> </tr> </thead> <tbody valign="top"> <tr><td>raining</td> <td>2,375</td> <td>40.0</td> </tr> <tr><td>tenth</td> <td>1,610</td> <td>27.1</td> </tr> <tr><td>quarter</td> <td>1,136</td> <td>19.1</td> </tr> <tr><td>half</td> <td>668</td> <td>11.2</td> </tr> <tr><td>inch</td> <td>308</td> <td>5.2</td> </tr> <tr><td>TOTAL</td> <td>5,942</td> <td>100.0</td> </tr> </tbody> </table> <p>This data shows that the chance of rain on any given day between February&nbsp;14th and the 21st is 40%, and the chance of getting at least a tenth of an inch is 30%. That’s certainly higher than in the Sierra Nevada in July, although by August, afternoon thunderstorms are more common in the mountains.</p> <p><em>When there is precipitation</em>, the distribution of precipitation totals looks like this:</p> <table border="1" class="docutils"> <colgroup> <col width="60%" /> <col width="40%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">cumulative&nbsp;frequency</th> <th class="head">precipition</th> </tr> </thead> <tbody valign="top"> <tr><td>1%</td> <td>0.01</td> </tr> <tr><td>5%</td> <td>0.02</td> </tr> <tr><td>10%</td> <td>0.02</td> </tr> <tr><td>25%</td> <td>0.07</td> </tr> <tr><td>50%</td> <td>0.22</td> </tr> <tr><td>75%</td> <td>0.59</td> </tr> <tr><td>90%</td> <td>1.18</td> </tr> <tr><td>95%</td> <td>1.71</td> </tr> <tr><td>99%</td> <td>2.56</td> </tr> </tbody> </table> <p>These numbers are cumulative which means that on 1&nbsp;percent of the days with precipition, there was a hundredth of an inch of liquid precipitation <em>or less</em>. Ten percent of the days had 0.02&nbsp;inches or less. And 50&nbsp;percent of rainy days had 0.22&nbsp;inches or liquid precipitation or less. Reading the numbers from the top of the distribution, there was more than an inch of rain 10&nbsp;percent of the days on which it rained, which is a little disturbing.</p> <p>One final question about precipitation is how long it rains once it starts raining? Do we get little showers here and there, or are there large storms that dump rain for days without a break? To answer this question, I counted the number of days between zero-rainfall days, which is equal to the number of consecutive days where it rained.</p> <table border="1" class="docutils"> <colgroup> <col width="52%" /> <col width="48%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">consecutive&nbsp;days</th> <th class="head">percent&nbsp;chance</th> </tr> </thead> <tbody valign="top"> <tr><td>1</td> <td>53.0</td> </tr> <tr><td>2</td> <td>24.4</td> </tr> <tr><td>3</td> <td>11.9</td> </tr> <tr><td>4</td> <td>7.5</td> </tr> <tr><td>5</td> <td>2.2</td> </tr> <tr><td>6</td> <td>0.9</td> </tr> <tr><td>7</td> <td>0.1</td> </tr> </tbody> </table> <p>The results show that more than half the time, a single day of rain is followed by at least one day without. And the chances of having it rain every day of a three day trip to this area in mid-February is 11.9%.</p> </div> <div class="section" id="snowfall"> <h2>Snowfall</h2> <p>Repeating the precipitation analysis with snowfall:</p> <table border="1" class="docutils"> <colgroup> <col width="35%" /> <col width="32%" /> <col width="33%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">snowfall&nbsp;amount</th> <th class="head">observed&nbsp;days</th> <th class="head">percent&nbsp;chance</th> </tr> </thead> <tbody valign="top"> <tr><td>snowing</td> <td>322</td> <td>6.3</td> </tr> <tr><td>inch</td> <td>148</td> <td>2.9</td> </tr> <tr><td>two</td> <td>115</td> <td>2.3</td> </tr> <tr><td>TOTAL</td> <td>5,091</td> <td>100.0</td> </tr> </tbody> </table> <p>Snowfall isn’t common on these dates, but it did happen, so I will need to be prepared for it. Also, the <tt class="docutils literal">PRCP</tt> variable includes melted snow, so a small portion of the precipitation from the previous section overlaps with the snowfall shown here.</p> </div> </div> <div class="section" id="conclusion"> <h1>Conclusion</h1> <p>Based on this analysis, a 3‒5&nbsp;day backpacking trip to the Big South Fork National River and Recreation area seems well within my abilities and my gear. It will almost certainly be below freezing at night, but isn’t likely to be much below 20&nbsp;°F, snowfall is uncommon, and even though I will probably experience some rain, it shouldn’t be too much or carry on for the entire trip.</p> </div> <div class="section" id="appendix"> <h1>Appendix</h1> <p>The R code for this analysis appears below. I’ve loaded the GHCND data into a PostgreSQL database with observation data partitioned by year. The database tables are structured basically as they come from the National Centers for Environmental Information.</p> <div class="highlight"><pre><span></span><span class="kn">library</span><span class="p">(</span>tidyverse<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>dbplyr<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>glue<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>maps<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>sp<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>rgdal<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>scales<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>knitr<span class="p">)</span> noaa <span class="o">&lt;-</span> src_postgres<span class="p">(</span>dbname <span class="o">=</span> <span class="s">&quot;noaa&quot;</span><span class="p">)</span> biso_stations <span class="o">&lt;-</span> noaa <span class="o">%&gt;%</span> tbl<span class="p">(</span>build_sql<span class="p">(</span> <span class="s">&quot;WITH inv AS (</span> <span class="s"> SELECT station_id, max(start_year) AS start_year,</span> <span class="s"> min(end_year) AS end_year,</span> <span class="s"> array_agg(variable::text) AS variables</span> <span class="s"> FROM ghcnd_inventory</span> <span class="s"> WHERE variable IN (&#39;TMIN&#39;, &#39;TMAX&#39;, &#39;PRCP&#39;, &#39;SNOW&#39;)</span> <span class="s"> GROUP BY station_id)</span> <span class="s"> SELECT station_id, station_name, start_year, end_year,</span> <span class="s"> latitude, longitude,</span> <span class="s"> ST_Distance(ST_Transform(a.the_geom, 32617),</span> <span class="s"> ST_Transform(b.the_geom, 32617))/1609 AS miles</span> <span class="s"> FROM ghcnd_stations AS a</span> <span class="s"> INNER JOIN inv USING(station_id),</span> <span class="s"> (SELECT ST_SetSRID(</span> <span class="s"> ST_MakePoint(-84.701553,</span> <span class="s"> 36.506800), 4326) AS the_geom) AS b</span> <span class="s"> WHERE inv.variables @&gt; ARRAY[&#39;TMIN&#39;, &#39;TMAX&#39;, &#39;PRCP&#39;, &#39;SNOW&#39;]</span> <span class="s"> AND end_year = 2017</span> <span class="s"> AND ST_Distance(ST_Transform(a.the_geom, 32617),</span> <span class="s"> ST_Transform(b.the_geom, 32617))/1609 &lt; 65</span> <span class="s"> ORDER BY miles&quot;</span><span class="p">))</span> start_doy <span class="o">&lt;-</span> <span class="m">32</span> <span class="c1"># Feb 1</span> end_doy <span class="o">&lt;-</span> <span class="m">59</span> <span class="c1"># Feb 28</span> ghcnd_variables <span class="o">&lt;-</span> noaa <span class="o">%&gt;%</span> tbl<span class="p">(</span><span class="s">&quot;ghcnd_variables&quot;</span><span class="p">)</span> <span class="c1"># ghcnd_obs partitioned by year, so query by year</span> obs_by_year <span class="o">&lt;-</span> <span class="kr">function</span><span class="p">(</span>conn<span class="p">,</span> year<span class="p">,</span> start_doy<span class="p">,</span> end_doy<span class="p">)</span> <span class="p">{</span> <span class="kp">print</span><span class="p">(</span>year<span class="p">)</span> filter_start_dte <span class="o">&lt;-</span> glue<span class="p">(</span><span class="s">&quot;{year}-01-01&quot;</span><span class="p">)</span> filter_end_dte <span class="o">&lt;-</span> glue<span class="p">(</span><span class="s">&quot;{year}-12-31&quot;</span><span class="p">)</span> conn <span class="o">%&gt;%</span> tbl<span class="p">(</span><span class="s">&quot;ghcnd_obs&quot;</span><span class="p">)</span> <span class="o">%&gt;%</span> inner_join<span class="p">(</span>biso_stations<span class="p">)</span> <span class="o">%&gt;%</span> inner_join<span class="p">(</span>ghcnd_variables<span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>doy <span class="o">=</span> date_part<span class="p">(</span><span class="s">&#39;doy&#39;</span><span class="p">,</span> dte<span class="p">),</span> value <span class="o">=</span> raw_value <span class="o">*</span> raw_multiplier<span class="p">)</span> <span class="o">%&gt;%</span> filter<span class="p">(</span>dte <span class="o">&gt;=</span> filter_start_dte<span class="p">,</span> dte <span class="o">&lt;=</span> filter_end_dte<span class="p">,</span> doy <span class="o">&gt;=</span> start_doy<span class="p">,</span> doy <span class="o">&lt;=</span> end_doy<span class="p">,</span> <span class="kp">is.na</span><span class="p">(</span>qual_flag<span class="p">),</span> variable <span class="o">%in%</span> <span class="kt">c</span><span class="p">(</span><span class="s">&#39;TMIN&#39;</span><span class="p">,</span> <span class="s">&#39;TMAX&#39;</span><span class="p">,</span> <span class="s">&#39;PRCP&#39;</span><span class="p">,</span> <span class="s">&#39;SNOW&#39;</span><span class="p">))</span> <span class="o">%&gt;%</span> select<span class="p">(</span><span class="o">-</span><span class="kt">c</span><span class="p">(</span>raw_value<span class="p">,</span> time_of_obs<span class="p">,</span> qual_flag<span class="p">,</span> description<span class="p">,</span> raw_multiplier<span class="p">))</span> <span class="o">%&gt;%</span> collect<span class="p">()</span> <span class="p">}</span> feb_obs <span class="o">&lt;-</span> map_df<span class="p">(</span><span class="m">1968</span><span class="o">:</span><span class="m">2017</span><span class="p">,</span> <span class="kr">function</span><span class="p">(</span>x<span class="p">)</span> obs_by_year<span class="p">(</span>noaa<span class="p">,</span> x<span class="p">,</span> start_doy<span class="p">,</span> end_doy<span class="p">))</span> <span class="c1"># MAP</span> restrict_miles <span class="o">&lt;-</span> <span class="m">50</span> biso_filtered <span class="o">&lt;-</span> biso <span class="o">%&gt;%</span> filter<span class="p">(</span>miles <span class="o">&lt;</span> restrict_miles<span class="p">)</span> nps_boundary <span class="o">&lt;-</span> readOGR<span class="p">(</span><span class="s">&quot;nps_boundary.shp&quot;</span><span class="p">,</span> verbose <span class="o">=</span> <span class="kc">FALSE</span><span class="p">)</span> biso_boundary <span class="o">&lt;-</span> <span class="kp">subset</span><span class="p">(</span>nps_boundary<span class="p">,</span> UNIT_CODE <span class="o">==</span> <span class="s">&#39;BISO&#39;</span><span class="p">)</span> biso_df <span class="o">&lt;-</span> fortify<span class="p">(</span>biso_boundary<span class="p">)</span> <span class="o">%&gt;%</span> tbl_df<span class="p">()</span> q <span class="o">&lt;-</span> ggplot<span class="p">(</span>data <span class="o">=</span> biso_filtered<span class="p">,</span> aes<span class="p">(</span>x <span class="o">=</span> longitude<span class="p">,</span> y <span class="o">=</span> latitude<span class="p">))</span> <span class="o">+</span> theme_bw<span class="p">()</span> <span class="o">+</span> theme<span class="p">(</span>axis.text <span class="o">=</span> element_blank<span class="p">(),</span> axis.ticks <span class="o">=</span> element_blank<span class="p">(),</span> panel.grid <span class="o">=</span> element_blank<span class="p">())</span> <span class="o">+</span> geom_hline<span class="p">(</span>yintercept <span class="o">=</span> <span class="m">36.6</span><span class="p">,</span> colour <span class="o">=</span> <span class="s">&quot;darkcyan&quot;</span><span class="p">,</span> size <span class="o">=</span> <span class="m">0.5</span><span class="p">)</span> <span class="o">+</span> geom_point<span class="p">(</span>colour <span class="o">=</span> <span class="s">&quot;darkred&quot;</span><span class="p">)</span> <span class="o">+</span> geom_text<span class="p">(</span>aes<span class="p">(</span>label <span class="o">=</span> str_to_title<span class="p">(</span>station_name<span class="p">)),</span> size <span class="o">=</span> <span class="m">3</span><span class="p">,</span> hjust <span class="o">=</span> <span class="m">0.5</span><span class="p">,</span> vjust <span class="o">=</span> <span class="m">0</span><span class="p">,</span> nudge_y <span class="o">=</span> <span class="m">0.01</span><span class="p">)</span> <span class="o">+</span> geom_polygon<span class="p">(</span>data <span class="o">=</span> biso_df<span class="p">,</span> aes<span class="p">(</span>x <span class="o">=</span> long<span class="p">,</span> y <span class="o">=</span> lat<span class="p">),</span> fill <span class="o">=</span> <span class="s">&quot;darkgreen&quot;</span><span class="p">)</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>name <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="p">,</span> limits <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="kp">min</span><span class="p">(</span>biso_filtered<span class="o">$</span>longitude<span class="p">)</span> <span class="o">-</span> <span class="m">0.02</span><span class="p">,</span> <span class="kp">max</span><span class="p">(</span>biso_filtered<span class="o">$</span>longitude<span class="p">)</span> <span class="o">+</span> <span class="m">0.02</span><span class="p">))</span> <span class="o">+</span> scale_y_continuous<span class="p">(</span>name <span class="o">=</span> <span class="s">&quot;&quot;</span><span class="p">,</span> limits <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="kp">min</span><span class="p">(</span>biso_filtered<span class="o">$</span>latitude<span class="p">)</span> <span class="o">-</span> <span class="m">0.02</span><span class="p">,</span> <span class="kp">max</span><span class="p">(</span>biso_filtered<span class="o">$</span>latitude<span class="p">)</span> <span class="o">+</span> <span class="m">0.02</span><span class="p">))</span> <span class="o">+</span> coord_quickmap<span class="p">()</span> <span class="kp">print</span><span class="p">(</span><span class="kp">q</span><span class="p">)</span> <span class="c1"># OBS</span> feb_obs_filtered <span class="o">&lt;-</span> feb_obs <span class="o">%&gt;%</span> filter<span class="p">(</span>miles <span class="o">&lt;</span> restrict_miles<span class="p">,</span> doy <span class="o">&gt;=</span> <span class="m">45</span><span class="p">,</span> doy <span class="o">&lt;=</span> <span class="m">52</span><span class="p">)</span> <span class="c1"># feb 14-21</span> <span class="c1"># TEMP PLOTS</span> tmin_rects <span class="o">&lt;-</span> tibble<span class="p">(</span>pwidth <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="s">&quot;80&quot;</span><span class="p">,</span> <span class="s">&quot;98&quot;</span><span class="p">),</span> xmin <span class="o">=</span> quantile<span class="p">((</span>feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;TMIN&#39;</span><span class="p">))</span><span class="o">$</span>value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5+32</span><span class="p">,</span> <span class="kt">c</span><span class="p">(</span><span class="m">0.10</span><span class="p">,</span> <span class="m">0.01</span><span class="p">)),</span> xmax <span class="o">=</span> quantile<span class="p">((</span>feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;TMIN&#39;</span><span class="p">))</span><span class="o">$</span>value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5+32</span><span class="p">,</span> <span class="kt">c</span><span class="p">(</span><span class="m">0.90</span><span class="p">,</span> <span class="m">0.99</span><span class="p">)),</span> ymin <span class="o">=</span> <span class="o">-</span><span class="kc">Inf</span><span class="p">,</span> ymax <span class="o">=</span> <span class="kc">Inf</span><span class="p">)</span> q <span class="o">&lt;-</span> ggplot<span class="p">(</span>data <span class="o">=</span> feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;TMIN&#39;</span><span class="p">),</span> aes<span class="p">(</span>x <span class="o">=</span> value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5+32</span><span class="p">))</span> <span class="o">+</span> theme_bw<span class="p">()</span> <span class="o">+</span> geom_rect<span class="p">(</span>data <span class="o">=</span> tmin_rects <span class="o">%&gt;%</span> filter<span class="p">(</span>pwidth <span class="o">==</span> <span class="s">&quot;98&quot;</span><span class="p">),</span> inherit.aes <span class="o">=</span> <span class="kc">FALSE</span><span class="p">,</span> aes<span class="p">(</span>xmin <span class="o">=</span> xmin<span class="p">,</span> xmax <span class="o">=</span> xmax<span class="p">,</span> ymin <span class="o">=</span> ymin<span class="p">,</span> ymax <span class="o">=</span> ymax<span class="p">),</span> fill <span class="o">=</span> <span class="s">&quot;darkcyan&quot;</span><span class="p">,</span> alpha <span class="o">=</span> <span class="m">0.2</span><span class="p">)</span> <span class="o">+</span> geom_rect<span class="p">(</span>data <span class="o">=</span> tmin_rects <span class="o">%&gt;%</span> filter<span class="p">(</span>pwidth <span class="o">==</span> <span class="s">&quot;80&quot;</span><span class="p">),</span> inherit.aes <span class="o">=</span> <span class="kc">FALSE</span><span class="p">,</span> aes<span class="p">(</span>xmin <span class="o">=</span> xmin<span class="p">,</span> xmax <span class="o">=</span> xmax<span class="p">,</span> ymin <span class="o">=</span> ymin<span class="p">,</span> ymax <span class="o">=</span> ymax<span class="p">),</span> fill <span class="o">=</span> <span class="s">&quot;darkorange&quot;</span><span class="p">,</span> alpha <span class="o">=</span> <span class="m">0.2</span><span class="p">)</span> <span class="o">+</span> geom_vline<span class="p">(</span>xintercept <span class="o">=</span> <span class="kp">mean</span><span class="p">((</span>feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;TMIN&#39;</span><span class="p">))</span><span class="o">$</span>value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5+32</span><span class="p">),</span> colour <span class="o">=</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> size <span class="o">=</span> <span class="m">0.5</span><span class="p">)</span> <span class="o">+</span> geom_histogram<span class="p">(</span>binwidth <span class="o">=</span> <span class="m">1</span><span class="p">)</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>name <span class="o">=</span> <span class="s">&quot;Minimum temperature (°F)&quot;</span><span class="p">,</span> breaks <span class="o">=</span> pretty_breaks<span class="p">(</span>n <span class="o">=</span> <span class="m">10</span><span class="p">))</span> <span class="o">+</span> scale_y_continuous<span class="p">(</span>name <span class="o">=</span> <span class="s">&quot;Days&quot;</span><span class="p">,</span> breaks <span class="o">=</span> pretty_breaks<span class="p">(</span>n <span class="o">=</span> <span class="m">6</span><span class="p">))</span> <span class="o">+</span> ggtitle<span class="p">(</span><span class="s">&quot;Minimum daily temperature distribution, February 14‒21&quot;</span><span class="p">)</span> <span class="kp">print</span><span class="p">(</span><span class="kp">q</span><span class="p">)</span> max_temp_distribution <span class="o">&lt;-</span> quantile<span class="p">((</span>feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;TMAX&#39;</span><span class="p">))</span><span class="o">$</span>value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5</span> <span class="o">+</span> <span class="m">32</span><span class="p">,</span> <span class="kt">c</span><span class="p">(</span><span class="m">0.01</span><span class="p">,</span> <span class="m">0.05</span><span class="p">,</span> <span class="m">0.10</span><span class="p">,</span> <span class="m">0.25</span><span class="p">,</span> <span class="m">0.5</span><span class="p">,</span> <span class="m">0.75</span><span class="p">,</span> <span class="m">0.90</span><span class="p">,</span> <span class="m">0.95</span><span class="p">,</span> <span class="m">0.99</span><span class="p">))</span> tmax_rects <span class="o">&lt;-</span> tibble<span class="p">(</span>pwidth <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="s">&quot;80&quot;</span><span class="p">,</span> <span class="s">&quot;98&quot;</span><span class="p">),</span> xmin <span class="o">=</span> quantile<span class="p">((</span>feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;TMAX&#39;</span><span class="p">))</span><span class="o">$</span>value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5+32</span><span class="p">,</span> <span class="kt">c</span><span class="p">(</span><span class="m">0.10</span><span class="p">,</span> <span class="m">0.01</span><span class="p">)),</span> xmax <span class="o">=</span> quantile<span class="p">((</span>feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;TMAX&#39;</span><span class="p">))</span><span class="o">$</span>value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5+32</span><span class="p">,</span> <span class="kt">c</span><span class="p">(</span><span class="m">0.90</span><span class="p">,</span> <span class="m">0.99</span><span class="p">)),</span> ymin <span class="o">=</span> <span class="o">-</span><span class="kc">Inf</span><span class="p">,</span> ymax <span class="o">=</span> <span class="kc">Inf</span><span class="p">)</span> q <span class="o">&lt;-</span> ggplot<span class="p">(</span>data <span class="o">=</span> feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;TMAX&#39;</span><span class="p">),</span> aes<span class="p">(</span>x <span class="o">=</span> value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5+32</span><span class="p">))</span> <span class="o">+</span> theme_bw<span class="p">()</span> <span class="o">+</span> geom_rect<span class="p">(</span>data <span class="o">=</span> tmax_rects <span class="o">%&gt;%</span> filter<span class="p">(</span>pwidth <span class="o">==</span> <span class="s">&quot;98&quot;</span><span class="p">),</span> inherit.aes <span class="o">=</span> <span class="kc">FALSE</span><span class="p">,</span> aes<span class="p">(</span>xmin <span class="o">=</span> xmin<span class="p">,</span> xmax <span class="o">=</span> xmax<span class="p">,</span> ymin <span class="o">=</span> ymin<span class="p">,</span> ymax <span class="o">=</span> ymax<span class="p">),</span> fill <span class="o">=</span> <span class="s">&quot;darkcyan&quot;</span><span class="p">,</span> alpha <span class="o">=</span> <span class="m">0.2</span><span class="p">)</span> <span class="o">+</span> geom_rect<span class="p">(</span>data <span class="o">=</span> tmax_rects <span class="o">%&gt;%</span> filter<span class="p">(</span>pwidth <span class="o">==</span> <span class="s">&quot;80&quot;</span><span class="p">),</span> inherit.aes <span class="o">=</span> <span class="kc">FALSE</span><span class="p">,</span> aes<span class="p">(</span>xmin <span class="o">=</span> xmin<span class="p">,</span> xmax <span class="o">=</span> xmax<span class="p">,</span> ymin <span class="o">=</span> ymin<span class="p">,</span> ymax <span class="o">=</span> ymax<span class="p">),</span> fill <span class="o">=</span> <span class="s">&quot;darkorange&quot;</span><span class="p">,</span> alpha <span class="o">=</span> <span class="m">0.2</span><span class="p">)</span> <span class="o">+</span> geom_vline<span class="p">(</span>xintercept <span class="o">=</span> <span class="kp">mean</span><span class="p">((</span>feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;TMAX&#39;</span><span class="p">))</span><span class="o">$</span>value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5+32</span><span class="p">),</span> colour <span class="o">=</span> <span class="s">&quot;red&quot;</span><span class="p">,</span> size <span class="o">=</span> <span class="m">0.5</span><span class="p">)</span> <span class="o">+</span> geom_histogram<span class="p">(</span>binwidth <span class="o">=</span> <span class="m">1</span><span class="p">)</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>name <span class="o">=</span> <span class="s">&quot;Maximum temperature (°F)&quot;</span><span class="p">,</span> breaks <span class="o">=</span> pretty_breaks<span class="p">(</span>n <span class="o">=</span> <span class="m">10</span><span class="p">))</span> <span class="o">+</span> scale_y_continuous<span class="p">(</span>name <span class="o">=</span> <span class="s">&quot;Days&quot;</span><span class="p">,</span> breaks <span class="o">=</span> pretty_breaks<span class="p">(</span>n <span class="o">=</span> <span class="m">8</span><span class="p">))</span> <span class="o">+</span> ggtitle<span class="p">(</span><span class="s">&quot;Maximum daily temperature distribution, February 14‒21&quot;</span><span class="p">)</span> <span class="kp">print</span><span class="p">(</span><span class="kp">q</span><span class="p">)</span> <span class="c1"># TEMP BINS</span> below_freezing_percent <span class="o">&lt;-</span> feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;TMIN&#39;</span><span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span><span class="sb">`below freezing`</span> <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value <span class="o">&lt;</span> <span class="m">0</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">),</span> <span class="sb">`colder than 20`</span> <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5</span> <span class="o">+</span> <span class="m">32</span> <span class="o">&lt;</span> <span class="m">20</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">),</span> <span class="sb">`colder than 10`</span> <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value<span class="o">*</span><span class="m">9</span><span class="o">/</span><span class="m">5</span> <span class="o">+</span> <span class="m">32</span> <span class="o">&lt;</span> <span class="m">10</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">))</span> <span class="o">%&gt;%</span> summarize<span class="p">(</span><span class="sb">`below freezing`</span> <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span><span class="sb">`below freezing`</span><span class="p">),</span> <span class="sb">`colder than 20`</span> <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span><span class="sb">`colder than 20`</span><span class="p">),</span> <span class="sb">`colder than 10`</span> <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span><span class="sb">`colder than 10`</span><span class="p">),</span> TOTAL <span class="o">=</span> n<span class="p">(),</span> total <span class="o">=</span> n<span class="p">())</span> <span class="o">%&gt;%</span> gather<span class="p">(</span>temperature<span class="p">,</span> <span class="sb">`observed days`</span><span class="p">,</span> <span class="o">-</span>total<span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span><span class="sb">`percent chance`</span> <span class="o">=</span> <span class="sb">`observed days`</span> <span class="o">/</span> total <span class="o">*</span> <span class="m">100</span><span class="p">)</span> <span class="o">%&gt;%</span> select<span class="p">(</span>temperature<span class="p">,</span> <span class="sb">`observed days`</span><span class="p">,</span> <span class="sb">`percent chance`</span><span class="p">)</span> kable<span class="p">(</span>below_freezing_percent<span class="p">,</span> digits <span class="o">=</span> <span class="m">1</span><span class="p">,</span> align <span class="o">=</span> <span class="s">&quot;lrr&quot;</span><span class="p">,</span> format.args <span class="o">=</span> <span class="kt">list</span><span class="p">(</span>big.mark <span class="o">=</span> <span class="s">&quot;,&quot;</span><span class="p">))</span> <span class="c1"># PRCP BINS</span> prcp_percent <span class="o">&lt;-</span> feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;PRCP&#39;</span><span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>raining <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value <span class="o">&gt;</span> <span class="m">0</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">),</span> tenth <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value <span class="o">&gt;</span> <span class="m">0.1</span> <span class="o">*</span> <span class="m">25.4</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">),</span> quarter <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value <span class="o">&gt;</span> <span class="m">0.25</span> <span class="o">*</span> <span class="m">25.4</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">),</span> half <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value <span class="o">&gt;</span> <span class="m">0.5</span> <span class="o">*</span> <span class="m">25.4</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">),</span> inch <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value <span class="o">&gt;</span> <span class="m">1</span> <span class="o">*</span> <span class="m">25.4</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">))</span> <span class="o">%&gt;%</span> summarize<span class="p">(</span>raining <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span>raining<span class="p">),</span> tenth <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span>tenth<span class="p">),</span> quarter <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span>quarter<span class="p">),</span> half <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span>half<span class="p">),</span> inch <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span>inch<span class="p">),</span> TOTAL <span class="o">=</span> n<span class="p">(),</span> total <span class="o">=</span> n<span class="p">())</span> <span class="o">%&gt;%</span> gather<span class="p">(</span><span class="sb">`rainfall amount`</span><span class="p">,</span> <span class="sb">`observed days`</span><span class="p">,</span> <span class="o">-</span>total<span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span><span class="sb">`percent chance`</span> <span class="o">=</span> <span class="sb">`observed days`</span> <span class="o">/</span> total <span class="o">*</span> <span class="m">100</span><span class="p">)</span> <span class="o">%&gt;%</span> select<span class="p">(</span><span class="sb">`rainfall amount`</span><span class="p">,</span> <span class="sb">`observed days`</span><span class="p">,</span> <span class="sb">`percent chance`</span><span class="p">)</span> kable<span class="p">(</span>prcp_percent<span class="p">,</span> digits <span class="o">=</span> <span class="m">1</span><span class="p">,</span> align <span class="o">=</span> <span class="s">&quot;lrr&quot;</span><span class="p">,</span> format.args <span class="o">=</span> <span class="kt">list</span><span class="p">(</span>big.mark <span class="o">=</span> <span class="s">&quot;,&quot;</span><span class="p">))</span> <span class="c1"># PRCP DIST</span> prcp_cum_freq <span class="o">&lt;-</span> tibble<span class="p">(</span><span class="sb">`cumulative frequency`</span> <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="s">&quot;1%&quot;</span><span class="p">,</span> <span class="s">&quot;5%&quot;</span><span class="p">,</span> <span class="s">&quot;10%&quot;</span><span class="p">,</span> <span class="s">&quot;25%&quot;</span><span class="p">,</span> <span class="s">&quot;50%&quot;</span><span class="p">,</span> <span class="s">&quot;75%&quot;</span><span class="p">,</span> <span class="s">&quot;90%&quot;</span><span class="p">,</span> <span class="s">&quot;95%&quot;</span><span class="p">,</span> <span class="s">&quot;99%&quot;</span><span class="p">),</span> precipition <span class="o">=</span> quantile<span class="p">((</span>feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&quot;PRCP&quot;</span><span class="p">,</span> value <span class="o">&gt;</span> <span class="m">0</span><span class="p">))</span><span class="o">$</span>value<span class="o">/</span><span class="m">25.4</span><span class="p">,</span> <span class="kt">c</span><span class="p">(</span><span class="m">0.01</span><span class="p">,</span> <span class="m">0.05</span><span class="p">,</span> <span class="m">0.10</span><span class="p">,</span> <span class="m">0.25</span><span class="p">,</span> <span class="m">0.5</span><span class="p">,</span> <span class="m">0.75</span><span class="p">,</span> <span class="m">0.90</span><span class="p">,</span> <span class="m">0.95</span><span class="p">,</span> <span class="m">0.99</span><span class="p">)))</span> kable<span class="p">(</span>prcp_cum_freq<span class="p">,</span> digits <span class="o">=</span> <span class="m">2</span><span class="p">,</span> align<span class="o">=</span><span class="s">&quot;lr&quot;</span><span class="p">)</span> <span class="c1"># PRCP PATTERN</span> no_prcp <span class="o">&lt;-</span> feb_obs <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;PRCP&#39;</span><span class="p">,</span> value <span class="o">==</span> <span class="m">0</span><span class="p">,</span> miles <span class="o">&lt;</span> restrict_miles<span class="p">,</span> doy <span class="o">&gt;=</span> <span class="m">44</span><span class="p">,</span> doy <span class="o">&lt;=</span> <span class="m">53</span><span class="p">)</span> consecutive_rain <span class="o">&lt;-</span> no_prcp <span class="o">%&gt;%</span> group_by<span class="p">(</span>station_name<span class="p">)</span> <span class="o">%&gt;%</span> arrange<span class="p">(</span>station_name<span class="p">,</span> dte<span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>days <span class="o">=</span> <span class="kp">as.integer</span><span class="p">(</span>dte <span class="o">-</span> lag<span class="p">(</span>dte<span class="p">)</span> <span class="o">-</span> <span class="m">1</span><span class="p">))</span> <span class="o">%&gt;%</span> filter<span class="p">(</span><span class="o">!</span><span class="kp">is.na</span><span class="p">(</span>days<span class="p">),</span> days <span class="o">&gt;</span> <span class="m">0</span><span class="p">,</span> days <span class="o">&lt;</span> <span class="m">10</span><span class="p">)</span> consecutive_days_dist <span class="o">&lt;-</span> consecutive_rain <span class="o">%&gt;%</span> ungroup<span class="p">()</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>total <span class="o">=</span> n<span class="p">())</span> <span class="o">%&gt;%</span> arrange<span class="p">(</span>days<span class="p">)</span> <span class="o">%&gt;%</span> group_by<span class="p">(</span>days<span class="p">,</span> total<span class="p">)</span> <span class="o">%&gt;%</span> summarize<span class="p">(</span><span class="sb">`percent chance`</span> <span class="o">=</span> n<span class="p">()</span><span class="o">/</span><span class="kp">max</span><span class="p">(</span>total<span class="p">)</span><span class="o">*</span><span class="m">100</span><span class="p">)</span> <span class="o">%&gt;%</span> rename<span class="p">(</span><span class="sb">`consecutive days`</span> <span class="o">=</span> days<span class="p">)</span> <span class="o">%&gt;%</span> select<span class="p">(</span><span class="sb">`consecutive days`</span><span class="p">,</span> <span class="sb">`percent chance`</span><span class="p">)</span> kable<span class="p">(</span>consecutive_days_dist<span class="p">,</span> digits <span class="o">=</span> <span class="m">1</span><span class="p">,</span> align <span class="o">=</span> <span class="s">&quot;lr&quot;</span><span class="p">)</span> <span class="c1"># SNOW DIST</span> snow_percent <span class="o">&lt;-</span> feb_obs_filtered <span class="o">%&gt;%</span> filter<span class="p">(</span>variable <span class="o">==</span> <span class="s">&#39;SNOW&#39;</span><span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>snowing <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value <span class="o">&gt;</span> <span class="m">0</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">),</span> half <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value <span class="o">&gt;</span> <span class="m">0.5</span> <span class="o">*</span> <span class="m">25.4</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">),</span> inch <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value <span class="o">&gt;</span> <span class="m">1</span> <span class="o">*</span> <span class="m">25.4</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">),</span> two <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>value <span class="o">&gt;</span> <span class="m">2</span> <span class="o">*</span> <span class="m">25.4</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">0</span><span class="p">))</span> <span class="o">%&gt;%</span> summarize<span class="p">(</span>snowing <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span>snowing<span class="p">),</span> inch <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span>inch<span class="p">),</span> two <span class="o">=</span> <span class="kp">sum</span><span class="p">(</span>two<span class="p">),</span> TOTAL <span class="o">=</span> n<span class="p">(),</span> total <span class="o">=</span> n<span class="p">())</span> <span class="o">%&gt;%</span> gather<span class="p">(</span><span class="sb">`snowfall amount`</span><span class="p">,</span> <span class="sb">`observed days`</span><span class="p">,</span> <span class="o">-</span>total<span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span><span class="sb">`percent chance`</span> <span class="o">=</span> <span class="sb">`observed days`</span> <span class="o">/</span> total <span class="o">*</span> <span class="m">100</span><span class="p">)</span> <span class="o">%&gt;%</span> select<span class="p">(</span><span class="sb">`snowfall amount`</span><span class="p">,</span> <span class="sb">`observed days`</span><span class="p">,</span> <span class="sb">`percent chance`</span><span class="p">)</span> kable<span class="p">(</span>snow_percent<span class="p">,</span> digits <span class="o">=</span> <span class="m">1</span><span class="p">,</span> align <span class="o">=</span> <span class="s">&quot;lrr&quot;</span><span class="p">,</span> format.args <span class="o">=</span> <span class="kt">list</span><span class="p">(</span>big.mark <span class="o">=</span> <span class="s">&quot;,&quot;</span><span class="p">))</span> </pre></div> </div> </div> Sun, 31 Dec 2017 11:10:31 -0900 http://swingleydev.com/blog/p/2005/ R weather BISO Tennessee Kentucky Koidern, 2002—2017 http://swingleydev.com/blog/p/2004/ <div class="document"> <div class="figure align-right"> <a class="reference external image-reference" href="//media.swingleydev.com/img/blog/2017/06/koidern_bed.jpg"><img alt="Koidern on her bed" src="//media.swingleydev.com/img/blog/2017/06/koidern_bed_300.jpg" style="width: 300px; height: 225px;" /></a> <p class="caption">Koidern</p> </div> <p>Yesterday we lost Koidern to complications from laryngeal paralysis. Koidern came to us in 2006 from Andrea’s mushing partner who thought she was too “ornery.” It is true that she wouldn’t hesitate to growl at a dog or cat who got too close to her food bowl, and she was protective of her favorite bed, but in every other way she was a very sweet dog. When she was younger she loved to give hugs, jumping up on her hind legs and wrapping her front legs around your waist. She was part Saluki, which made her very distinctive in Andrea’s dog teams and she never lost her beautiful brown coat, perky ears, and curled tail. I will miss her continual energy in the dog yard racing around after the other dogs, how she’d pounce on dog bones and toss them around, “smash” the cats, and the way she’d bark right before coming into the house as if to announce her entrance.</p> <br clear="all" /><div class="figure"> <a class="reference external image-reference" href="//media.swingleydev.com/img/blog/2017/06/koidern_hug.jpg"><img alt="Koidern hug" class="img-responsive" src="//media.swingleydev.com/img/blog/2017/06/koidern_hug_600.jpg" /></a> <p class="caption">Koidern hug (with her sister Kluane and Carol Kaynor)</p> </div> <div class="figure"> <a class="reference external image-reference" href="//media.swingleydev.com/img/blog/2017/06/tok_with_piper_buddy.jpg"><img alt="Koidern with Piper and Buddy" class="img-responsive" src="//media.swingleydev.com/img/blog/2017/06/tok_with_piper_buddy_600.jpg" /></a> <p class="caption">Koidern in Tok, with Piper and Buddy</p> </div> </div> Sat, 10 Jun 2017 10:21:21 -0800 http://swingleydev.com/blog/p/2004/ Koidern dogs memorial Non-motorized commuting by state http://swingleydev.com/blog/p/2003/ <div class="document"> <div class="section" id="introduction"> <h1>Introduction</h1> <p>The Alaska Department of Transportation is working on updating their <a class="reference external" href="http://www.akbikeped.com/">bicycling and pedestrian master plan</a> for the state and their web site mentions Alaska as having high percentages of bicycle and pedestrian commuters relative to the rest of the country. I’m interested because I commute to work by bicycle (and occasionally ski or run) every day, either on the trails in the winter, or the roads in the summer. The company I work for (<a class="reference external" href="https://www.abrinc.com/">ABR</a>) pays it’s employees $3.50 per day for using non-motorized means of transportation to get to work. I earned more than $700 last year as part of this program and ABR has paid it’s employees almost $40K since 2009 not to drive to work.</p> <p>The Census Bureau keeps track of how people get to work in the American Community Survey, easily accessible from their web site. We’ll use this data to see if Alaska really does have higher than average rates of non-motorized commuters.</p> </div> <div class="section" id="data"> <h1>Data</h1> <p>The data comes from <a class="reference external" href="https://factfinder.census.gov/faces/nav/jsf/pages/searchresults.xhtml?refresh=t">FactFinder</a>. I chose ‘American Community Survey’ from the list of data sources near the bottom, searched for ‘bicycle’, chose ‘Commuting characteristics by sex’ (Table S0801), and added the ‘All States within United States and Puerto Rico’ as the Geography of interest. The site generates a zip file containing the data as a CSV file along with several other informational files. The code for extracting the data appears at the bottom of this post.</p> <p>The data are percentages of workers 16 years and over and their means of transportation to work. Here’s a table showing the top 10 states ordered by the combination of bicycling and walking percentage.</p> <table border="1" class="docutils"> <colgroup> <col width="6%" /> <col width="23%" /> <col width="13%" /> <col width="13%" /> <col width="11%" /> <col width="17%" /> <col width="8%" /> <col width="11%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">&nbsp;</th> <th class="head">state</th> <th class="head">total</th> <th class="head">motorized</th> <th class="head">carpool</th> <th class="head">public_trans</th> <th class="head">walk</th> <th class="head">bicycle</th> </tr> </thead> <tbody valign="top"> <tr><td>1</td> <td>District of Columbia</td> <td>358,150</td> <td>38.8</td> <td>5.2</td> <td>35.8</td> <td>14.0</td> <td>4.1</td> </tr> <tr><td>2</td> <td>Alaska</td> <td>363,075</td> <td>80.5</td> <td>12.6</td> <td>1.5</td> <td>7.9</td> <td>1.1</td> </tr> <tr><td>3</td> <td>Montana</td> <td>484,043</td> <td>84.9</td> <td>10.4</td> <td>0.8</td> <td>5.6</td> <td>1.6</td> </tr> <tr><td>4</td> <td>New York</td> <td>9,276,438</td> <td>59.3</td> <td>6.6</td> <td>28.6</td> <td>6.3</td> <td>0.7</td> </tr> <tr><td>5</td> <td>Vermont</td> <td>320,350</td> <td>85.1</td> <td>8.2</td> <td>1.3</td> <td>5.8</td> <td>0.8</td> </tr> <tr><td>6</td> <td>Oregon</td> <td>1,839,706</td> <td>81.4</td> <td>10.2</td> <td>4.8</td> <td>3.8</td> <td>2.5</td> </tr> <tr><td>7</td> <td>Massachusetts</td> <td>3,450,540</td> <td>77.6</td> <td>7.4</td> <td>10.6</td> <td>5.0</td> <td>0.8</td> </tr> <tr><td>8</td> <td>Wyoming</td> <td>289,163</td> <td>87.3</td> <td>10.0</td> <td>2.2</td> <td>4.6</td> <td>0.6</td> </tr> <tr><td>9</td> <td>Hawaii</td> <td>704,914</td> <td>80.9</td> <td>13.5</td> <td>7.0</td> <td>4.1</td> <td>0.9</td> </tr> <tr><td>10</td> <td>Washington</td> <td>3,370,945</td> <td>82.2</td> <td>9.8</td> <td>6.2</td> <td>3.7</td> <td>1.0</td> </tr> </tbody> </table> <p>Alaska has the second highest rates of walking and biking to work behind the District of Columbia. The table is an interesting combination of states with large urban centers (DC, New York, Oregon, Massachusetts) and those that are more rural (Alaska, Montana, Vermont, Wyoming).</p> <p>Another way to rank the data is by combining all forms of transportation besides single-vehicle motorized transport (car pooling, public transportation, walking and bicycling).</p> <table border="1" class="docutils"> <colgroup> <col width="6%" /> <col width="23%" /> <col width="13%" /> <col width="13%" /> <col width="11%" /> <col width="17%" /> <col width="8%" /> <col width="11%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">&nbsp;</th> <th class="head">state</th> <th class="head">total</th> <th class="head">motorized</th> <th class="head">carpool</th> <th class="head">public_trans</th> <th class="head">walk</th> <th class="head">bicycle</th> </tr> </thead> <tbody valign="top"> <tr><td>1</td> <td>District of Columbia</td> <td>358,150</td> <td>38.8</td> <td>5.2</td> <td>35.8</td> <td>14.0</td> <td>4.1</td> </tr> <tr><td>2</td> <td>New York</td> <td>9,276,438</td> <td>59.3</td> <td>6.6</td> <td>28.6</td> <td>6.3</td> <td>0.7</td> </tr> <tr><td>3</td> <td>Massachusetts</td> <td>3,450,540</td> <td>77.6</td> <td>7.4</td> <td>10.6</td> <td>5.0</td> <td>0.8</td> </tr> <tr><td>4</td> <td>New Jersey</td> <td>4,285,182</td> <td>79.3</td> <td>7.5</td> <td>11.6</td> <td>3.3</td> <td>0.3</td> </tr> <tr><td>5</td> <td>Alaska</td> <td>363,075</td> <td>80.5</td> <td>12.6</td> <td>1.5</td> <td>7.9</td> <td>1.1</td> </tr> <tr><td>6</td> <td>Hawaii</td> <td>704,914</td> <td>80.9</td> <td>13.5</td> <td>7.0</td> <td>4.1</td> <td>0.9</td> </tr> <tr><td>7</td> <td>Oregon</td> <td>1,839,706</td> <td>81.4</td> <td>10.2</td> <td>4.8</td> <td>3.8</td> <td>2.5</td> </tr> <tr><td>8</td> <td>Illinois</td> <td>6,094,828</td> <td>81.5</td> <td>7.9</td> <td>9.3</td> <td>3.0</td> <td>0.7</td> </tr> <tr><td>9</td> <td>Washington</td> <td>3,370,945</td> <td>82.2</td> <td>9.8</td> <td>6.2</td> <td>3.7</td> <td>1.0</td> </tr> <tr><td>10</td> <td>Maryland</td> <td>3,001,281</td> <td>82.6</td> <td>8.9</td> <td>9.0</td> <td>2.6</td> <td>0.3</td> </tr> </tbody> </table> <p>Here, the states with large urban centers come out higher because of the number of commuters using public transportation. Despite very low availability of public transportation, Alaska still winds up 5th on this list because of high rates of car pooling, in addition to walking and bicycling.</p> </div> <div class="section" id="map-data"> <h1>Map data</h1> <p>To look at regional patterns, we can make a map of the United States colored by non-motorized transportation percentage. This can be a little challenging because Alaska and Hawaii are so far from the rest of the country. What I’m doing here is loading the state data, transforming the data to a projection that’s appropriate for Alaska, and moving Alaska and Hawaii closer to the lower-48 for display. Again, the code appears at the bottom.</p> <div class="figure"> <a class="reference external image-reference" href="//media.swingleydev.com/img/blog/2017/04/non_motorized_commute_map.pdf"><object class="img-responsive" data="//media.swingleydev.com/img/blog/2017/04/non_motorized_commute_map.svg" type="image/svg+xml">Non-motorized commuting percentage by state</object></a> </div> <p>You can see that non-motorized transportation is very low throughout the deep south, and tends to be higher in the western half of the country, but the really high rates of bicycling and walking to work are isolated. High Vermont next to low New Hampshire, or Oregon and Montana split by Idaho.</p> </div> <div class="section" id="urban-and-rural-median-age-of-the-population"> <h1>Urban and rural, median age of the population</h1> <p>What explains the high rates of non-motorized commuting in Alaska and the other states at the top of the list? Urbanization is certainly one important factor explaining why the District of Columbia and states like New York, Oregon and Massachusetts have high rates of walking and bicycling. But what about Montana, Vermont, and Wyoming?</p> <p>Age of the population might have an effect as well, as younger people are more likely to walk and bike to work than older people. Alaska has the second youngest population (33.3 years) in the U.S. and DC is third (33.8), but the other states in the top five (Utah, Texas, North Dakota) don’t have high non-motorized transportation.</p> <p>So it’s more complicated that just these factors. California is a good example, with a combination of high urbanization (second, 95.0% urban), low median age (eighth, 36.2) and great weather year round, but is 19th for non-motorized commuting. Who walks in California, after all?</p> </div> <div class="section" id="conclusion"> <h1>Conclusion</h1> <p>I hope DOT comes up with a progressive plan for improving opportunities for pedestrian and bicycle transportation in Alaska They’ve made some progress here in Fairbanks; building new paths for non-motorized traffic; but they also seem blind to the realities of actually using the roads and paths on a bicycle. The “bike path” near my house abruptly turns from asphalt to gravel a third of the way down Miller Hill, and the shoulders of the roads I commute on are filled with deep snow in winter, gravel in spring, and all manner of detritus year round. Many roads don’t have a useable shoulder at all.</p> </div> <div class="section" id="code"> <h1>Code</h1> <div class="highlight"><pre><span></span><span class="kn">library</span><span class="p">(</span>tidyverse<span class="p">)</span> <span class="c1"># data import, manipulation</span> <span class="kn">library</span><span class="p">(</span>knitr<span class="p">)</span> <span class="c1"># pretty tables</span> <span class="kn">library</span><span class="p">(</span>rpostgis<span class="p">)</span> <span class="c1"># PostGIS support</span> <span class="kn">library</span><span class="p">(</span>rgdal<span class="p">)</span> <span class="c1"># geographic transformation</span> <span class="kn">library</span><span class="p">(</span>maptools<span class="p">)</span> <span class="c1"># geographic transformation</span> <span class="kn">library</span><span class="p">(</span>viridis<span class="p">)</span> <span class="c1"># color blind color palette</span> <span class="c1"># Read the heading</span> heading <span class="o">&lt;-</span> read_csv<span class="p">(</span><span class="s">&#39;ACS_15_1YR_S0801.csv&#39;</span><span class="p">,</span> n_max <span class="o">=</span> <span class="m">1</span><span class="p">)</span> <span class="o">%&gt;%</span> <span class="kp">names</span><span class="p">()</span> <span class="c1"># Read the data</span> s0801 <span class="o">&lt;-</span> read_csv<span class="p">(</span><span class="s">&#39;ACS_15_1YR_S0801.csv&#39;</span><span class="p">,</span> col_names <span class="o">=</span> <span class="kc">FALSE</span><span class="p">,</span> skip <span class="o">=</span> <span class="m">2</span><span class="p">)</span> <span class="kp">names</span><span class="p">(</span>s0801<span class="p">)</span> <span class="o">&lt;-</span> heading <span class="c1"># Extract only the columns we need, add state postal codes</span> commute <span class="o">&lt;-</span> s0801 <span class="o">%&gt;%</span> transmute<span class="p">(</span>state <span class="o">=</span> <span class="sb">`GEO.display-label`</span><span class="p">,</span> total <span class="o">=</span> HC01_EST_VC01<span class="p">,</span> motorized <span class="o">=</span> HC01_EST_VC03<span class="p">,</span> carpool <span class="o">=</span> HC01_EST_VC05<span class="p">,</span> public_trans <span class="o">=</span> HC01_EST_VC10<span class="p">,</span> walk <span class="o">=</span> HC01_EST_VC11<span class="p">,</span> bicycle <span class="o">=</span> HC01_EST_VC12<span class="p">)</span> <span class="o">%&gt;%</span> filter<span class="p">(</span>state <span class="o">!=</span> <span class="s">&#39;Puerto Rico&#39;</span><span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>state_postal <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="s">&#39;AL&#39;</span><span class="p">,</span> <span class="s">&#39;AK&#39;</span><span class="p">,</span> <span class="s">&#39;AZ&#39;</span><span class="p">,</span> <span class="s">&#39;AR&#39;</span><span class="p">,</span> <span class="s">&#39;CA&#39;</span><span class="p">,</span> <span class="s">&#39;CO&#39;</span><span class="p">,</span> <span class="s">&#39;CT&#39;</span><span class="p">,</span> <span class="s">&#39;DE&#39;</span><span class="p">,</span> <span class="s">&#39;DC&#39;</span><span class="p">,</span> <span class="s">&#39;FL&#39;</span><span class="p">,</span> <span class="s">&#39;GA&#39;</span><span class="p">,</span> <span class="s">&#39;HI&#39;</span><span class="p">,</span> <span class="s">&#39;ID&#39;</span><span class="p">,</span> <span class="s">&#39;IL&#39;</span><span class="p">,</span> <span class="s">&#39;IN&#39;</span><span class="p">,</span> <span class="s">&#39;IA&#39;</span><span class="p">,</span> <span class="s">&#39;KS&#39;</span><span class="p">,</span> <span class="s">&#39;KY&#39;</span><span class="p">,</span> <span class="s">&#39;LA&#39;</span><span class="p">,</span> <span class="s">&#39;ME&#39;</span><span class="p">,</span> <span class="s">&#39;MD&#39;</span><span class="p">,</span> <span class="s">&#39;MA&#39;</span><span class="p">,</span> <span class="s">&#39;MI&#39;</span><span class="p">,</span> <span class="s">&#39;MN&#39;</span><span class="p">,</span> <span class="s">&#39;MS&#39;</span><span class="p">,</span> <span class="s">&#39;MO&#39;</span><span class="p">,</span> <span class="s">&#39;MT&#39;</span><span class="p">,</span> <span class="s">&#39;NE&#39;</span><span class="p">,</span> <span class="s">&#39;NV&#39;</span><span class="p">,</span> <span class="s">&#39;NH&#39;</span><span class="p">,</span> <span class="s">&#39;NJ&#39;</span><span class="p">,</span> <span class="s">&#39;NM&#39;</span><span class="p">,</span> <span class="s">&#39;NY&#39;</span><span class="p">,</span> <span class="s">&#39;NC&#39;</span><span class="p">,</span> <span class="s">&#39;ND&#39;</span><span class="p">,</span> <span class="s">&#39;OH&#39;</span><span class="p">,</span> <span class="s">&#39;OK&#39;</span><span class="p">,</span> <span class="s">&#39;OR&#39;</span><span class="p">,</span> <span class="s">&#39;PA&#39;</span><span class="p">,</span> <span class="s">&#39;RI&#39;</span><span class="p">,</span> <span class="s">&#39;SC&#39;</span><span class="p">,</span> <span class="s">&#39;SD&#39;</span><span class="p">,</span> <span class="s">&#39;TN&#39;</span><span class="p">,</span> <span class="s">&#39;TX&#39;</span><span class="p">,</span> <span class="s">&#39;UT&#39;</span><span class="p">,</span> <span class="s">&#39;VT&#39;</span><span class="p">,</span> <span class="s">&#39;VA&#39;</span><span class="p">,</span> <span class="s">&#39;WA&#39;</span><span class="p">,</span> <span class="s">&#39;WV&#39;</span><span class="p">,</span> <span class="s">&#39;WI&#39;</span><span class="p">,</span> <span class="s">&#39;WY&#39;</span><span class="p">))</span> <span class="c1"># Print top ten tables</span> kable<span class="p">(</span>commute <span class="o">%&gt;%</span> select<span class="p">(</span><span class="o">-</span>state_postal<span class="p">)</span> <span class="o">%&gt;%</span> arrange<span class="p">(</span>desc<span class="p">(</span>walk <span class="o">+</span> bicycle<span class="p">))</span> <span class="o">%&gt;%</span> <span class="kp">head</span><span class="p">(</span>n <span class="o">=</span> <span class="m">10</span><span class="p">),</span> format.args <span class="o">=</span> <span class="kt">list</span><span class="p">(</span>big.mark <span class="o">=</span> <span class="s">&quot;,&quot;</span><span class="p">),</span> row.names <span class="o">=</span> <span class="kc">TRUE</span><span class="p">)</span> kable<span class="p">(</span>commute <span class="o">%&gt;%</span> select<span class="p">(</span><span class="o">-</span>state_postal<span class="p">)</span> <span class="o">%&gt;%</span> arrange<span class="p">(</span>motorized<span class="p">)</span> <span class="o">%&gt;%</span> <span class="kp">head</span><span class="p">(</span>n <span class="o">=</span> <span class="m">10</span><span class="p">),</span> format.args <span class="o">=</span> <span class="kt">list</span><span class="p">(</span>big.mark <span class="o">=</span> <span class="s">&quot;,&quot;</span><span class="p">),</span> row.names <span class="o">=</span> <span class="kc">TRUE</span><span class="p">)</span> <span class="c1"># Connect to the database with the state layer</span> layers <span class="o">&lt;-</span> src_postgres<span class="p">(</span>host <span class="o">=</span> <span class="s">&quot;localhost&quot;</span><span class="p">,</span> dbname <span class="o">=</span> <span class="s">&quot;layers&quot;</span><span class="p">)</span> states <span class="o">&lt;-</span> pgGetGeom<span class="p">(</span>layers<span class="o">$</span>con<span class="p">,</span> <span class="kt">c</span><span class="p">(</span><span class="s">&quot;public&quot;</span><span class="p">,</span> <span class="s">&quot;states&quot;</span><span class="p">),</span> geom <span class="o">=</span> <span class="s">&quot;wkb_geometry&quot;</span><span class="p">,</span> gid <span class="o">=</span> <span class="s">&quot;ogc_fid&quot;</span><span class="p">)</span> <span class="c1"># Transform to srid 3338 (Alaska Albers)</span> states_3338 <span class="o">&lt;-</span> spTransform<span class="p">(</span>states<span class="p">,</span> CRS<span class="p">(</span><span class="s">&quot;+proj=aea +lat_1=55 +lat_2=65 +lat_0=50</span> <span class="s"> +lon_0=-154 +x_0=0 +y_ 0=0 +ellps=GRS80</span> <span class="s"> +towgs84=0,0,0,0,0,0,0 +units=m</span> <span class="s"> +no_defs&quot;</span><span class="p">))</span> <span class="c1"># Convert to a data frame suitable for ggplot, move AK and HI</span> ggstates <span class="o">&lt;-</span> fortify<span class="p">(</span>states_3338<span class="p">,</span> region <span class="o">=</span> <span class="s">&quot;state&quot;</span><span class="p">)</span> <span class="o">%&gt;%</span> filter<span class="p">(</span>id <span class="o">!=</span> <span class="s">&#39;PR&#39;</span><span class="p">)</span> <span class="o">%&gt;%</span> inner_join<span class="p">(</span>commute<span class="p">,</span> by <span class="o">=</span> <span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="s">&quot;id&quot;</span> <span class="o">=</span> <span class="s">&quot;state_postal&quot;</span><span class="p">)))</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>lat <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>state <span class="o">==</span> <span class="s">&#39;Hawaii&#39;</span><span class="p">,</span> lat <span class="o">+</span> <span class="m">2300000</span><span class="p">,</span> lat<span class="p">),</span> long <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>state <span class="o">==</span> <span class="s">&#39;Hawaii&#39;</span><span class="p">,</span> long <span class="o">+</span> <span class="m">2000000</span><span class="p">,</span> long<span class="p">),</span> lat <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>state <span class="o">==</span> <span class="s">&#39;Alaska&#39;</span><span class="p">,</span> lat <span class="o">+</span> <span class="m">1000000</span><span class="p">,</span> lat<span class="p">),</span> long <span class="o">=</span> <span class="kp">ifelse</span><span class="p">(</span>state <span class="o">==</span> <span class="s">&#39;Alaska&#39;</span><span class="p">,</span> long <span class="o">+</span> <span class="m">2000000</span><span class="p">,</span> long<span class="p">))</span> <span class="c1"># Plot it</span> p <span class="o">&lt;-</span> ggplot<span class="p">()</span> <span class="o">+</span> geom_polygon<span class="p">(</span>data <span class="o">=</span> ggstates<span class="p">,</span> colour <span class="o">=</span> <span class="s">&quot;black&quot;</span><span class="p">,</span> aes<span class="p">(</span>x <span class="o">=</span> long<span class="p">,</span> y <span class="o">=</span> lat<span class="p">,</span> group <span class="o">=</span> group<span class="p">,</span> fill <span class="o">=</span> bicycle <span class="o">+</span> walk<span class="p">))</span> <span class="o">+</span> coord_fixed<span class="p">(</span>ratio <span class="o">=</span> <span class="m">1</span><span class="p">)</span> <span class="o">+</span> scale_fill_viridis<span class="p">(</span>name <span class="o">=</span> <span class="s">&quot;Non-motorized\n commuters (%)&quot;</span><span class="p">,</span> option <span class="o">=</span> <span class="s">&quot;plasma&quot;</span><span class="p">,</span> limits <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">9</span><span class="p">),</span> breaks <span class="o">=</span> <span class="kp">seq</span><span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">9</span><span class="p">,</span> <span class="m">3</span><span class="p">))</span> <span class="o">+</span> theme_void<span class="p">()</span> <span class="o">+</span> theme<span class="p">(</span>legend.position <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="m">0.9</span><span class="p">,</span> <span class="m">0.2</span><span class="p">))</span> width <span class="o">&lt;-</span> <span class="m">16</span> height <span class="o">&lt;-</span> <span class="m">9</span> resize <span class="o">&lt;-</span> <span class="m">0.75</span> svg<span class="p">(</span><span class="s">&quot;non_motorized_commute_map.svg&quot;</span><span class="p">,</span> width <span class="o">=</span> width<span class="o">*</span>resize<span class="p">,</span> height <span class="o">=</span> height<span class="o">*</span>resize<span class="p">)</span> <span class="kp">print</span><span class="p">(</span>p<span class="p">)</span> dev.off<span class="p">()</span> pdf<span class="p">(</span><span class="s">&quot;non_motorized_commute_map.pdf&quot;</span><span class="p">,</span> width <span class="o">=</span> width<span class="o">*</span>resize<span class="p">,</span> height <span class="o">=</span> height<span class="o">*</span>resize<span class="p">)</span> <span class="kp">print</span><span class="p">(</span>p<span class="p">)</span> dev.off<span class="p">()</span> <span class="c1"># Urban and rural percentages by state</span> heading <span class="o">&lt;-</span> read_csv<span class="p">(</span><span class="s">&#39;../urban_rural/DEC_10_SF1_P2.csv&#39;</span><span class="p">,</span> n_max <span class="o">=</span> <span class="m">1</span><span class="p">)</span> <span class="o">%&gt;%</span> <span class="kp">names</span><span class="p">()</span> dec10 <span class="o">&lt;-</span> read_csv<span class="p">(</span><span class="s">&#39;../urban_rural/DEC_10_SF1_P2.csv&#39;</span><span class="p">,</span> col_names <span class="o">=</span> <span class="kc">FALSE</span><span class="p">,</span> skip <span class="o">=</span> <span class="m">2</span><span class="p">)</span> <span class="kp">names</span><span class="p">(</span>dec10<span class="p">)</span> <span class="o">&lt;-</span> heading urban_rural <span class="o">&lt;-</span> dec10 <span class="o">%&gt;%</span> transmute<span class="p">(</span>state <span class="o">=</span> <span class="sb">`GEO.display-label`</span><span class="p">,</span> total <span class="o">=</span> D001<span class="p">,</span> urban <span class="o">=</span> D002<span class="p">,</span> rural <span class="o">=</span> D005<span class="p">)</span> <span class="o">%&gt;%</span> filter<span class="p">(</span>state <span class="o">!=</span> <span class="s">&#39;Puerto Rico&#39;</span><span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>urban_percentage <span class="o">=</span> urban <span class="o">/</span> total <span class="o">*</span> <span class="m">100</span><span class="p">)</span> <span class="c1"># Median age by state</span> heading <span class="o">&lt;-</span> read_csv<span class="p">(</span><span class="s">&#39;../age_sex/ACS_15_1YR_S0101.csv&#39;</span><span class="p">,</span> n_max <span class="o">=</span> <span class="m">1</span><span class="p">)</span> <span class="o">%&gt;%</span> <span class="kp">names</span><span class="p">()</span> s0101 <span class="o">&lt;-</span> read_csv<span class="p">(</span><span class="s">&#39;../age_sex/ACS_15_1YR_S0101.csv&#39;</span><span class="p">,</span> col_names <span class="o">=</span> <span class="kc">FALSE</span><span class="p">,</span> skip <span class="o">=</span> <span class="m">2</span><span class="p">)</span> <span class="kp">names</span><span class="p">(</span>s0101<span class="p">)</span> <span class="o">&lt;-</span> heading age <span class="o">&lt;-</span> s0101 <span class="o">%&gt;%</span> transmute<span class="p">(</span>state <span class="o">=</span> <span class="sb">`GEO.display-label`</span><span class="p">,</span> median_age <span class="o">=</span> HC01_EST_VC35<span class="p">)</span> <span class="o">%&gt;%</span> filter<span class="p">(</span>state <span class="o">!=</span> <span class="s">&#39;Puerto Rico&#39;</span><span class="p">)</span> <span class="c1"># Do urban percentage and median age explain anything about</span> <span class="c1"># non-motorized transit?</span> census_data <span class="o">&lt;-</span> commute <span class="o">%&gt;%</span> inner_join<span class="p">(</span>urban_rural<span class="p">,</span> by <span class="o">=</span> <span class="s">&quot;state&quot;</span><span class="p">)</span> <span class="o">%&gt;%</span> inner_join<span class="p">(</span>age<span class="p">,</span> by <span class="o">=</span> <span class="s">&quot;state&quot;</span><span class="p">)</span> <span class="o">%&gt;%</span> select<span class="p">(</span>state<span class="p">,</span> state_postal<span class="p">,</span> walk<span class="p">,</span> bicycle<span class="p">,</span> urban_percentage<span class="p">,</span> median_age<span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>non_motorized <span class="o">=</span> walk <span class="o">+</span> bicycle<span class="p">)</span> u <span class="o">&lt;-</span> ggplot<span class="p">(</span>data <span class="o">=</span> census_data<span class="p">,</span> aes<span class="p">(</span>x <span class="o">=</span> urban_percentage<span class="p">,</span> y <span class="o">=</span> non_motorized<span class="p">))</span> <span class="o">+</span> geom_text<span class="p">(</span>aes<span class="p">(</span>label <span class="o">=</span> state_postal<span class="p">))</span> svg<span class="p">(</span><span class="s">&quot;urban.svg&quot;</span><span class="p">,</span> width <span class="o">=</span> width<span class="o">*</span>resize<span class="p">,</span> height <span class="o">=</span> height<span class="o">*</span>resize<span class="p">)</span> <span class="kp">print</span><span class="p">(</span>u<span class="p">)</span> dev.off<span class="p">()</span> a <span class="o">&lt;-</span> ggplot<span class="p">(</span>data <span class="o">=</span> census_data<span class="p">,</span> aes<span class="p">(</span>x <span class="o">=</span> median_age<span class="p">,</span> y <span class="o">=</span> non_motorized<span class="p">))</span> <span class="o">+</span> geom_text<span class="p">(</span>aes<span class="p">(</span>label <span class="o">=</span> state_postal<span class="p">))</span> svg<span class="p">(</span><span class="s">&quot;age.svg&quot;</span><span class="p">,</span> width <span class="o">=</span> width<span class="o">*</span>resize<span class="p">,</span> height <span class="o">=</span> height<span class="o">*</span>resize<span class="p">)</span> <span class="kp">print</span><span class="p">(</span>a<span class="p">)</span> dev.off<span class="p">()</span> <span class="c1"># Not significant:</span> model <span class="o">=</span> lm<span class="p">(</span>data <span class="o">=</span> census_data<span class="p">,</span> non_motorized <span class="o">~</span> urban_percentage <span class="o">+</span> median_age<span class="p">)</span> <span class="c1"># Only significant because of DC (a clear outlier)</span> model <span class="o">=</span> lm<span class="p">(</span>data <span class="o">=</span> census_data<span class="p">,</span> non_motorized <span class="o">~</span> urban_percentage <span class="o">*</span> median_age<span class="p">)</span> </pre></div> </div> </div> Sat, 08 Apr 2017 11:30:55 -0800 http://swingleydev.com/blog/p/2003/ bicycling walking census data R