<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/maps-ii-how-does-competition-affect-gas-prices/"></div></div><div><a data-url="https://www.r-bloggers.com/maps-ii-how-does-competition-affect-gas-prices/" data-counturl="https://www.r-bloggers.com/maps-ii-how-does-competition-affect-gas-prices/" data-count="horizontal" data-text="Maps II – How does Competition affect Gas Prices?" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://flovv.github.com/Gas_price-Proximity/"> Florian Teschner</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In the last <a target="_blank" rel="nofollow" href="http://flovv.github.io/Gas_price-Mapping/">post</a>, I mapped gas stations and gas prices in Germany.<br>
After posting it, I started to look at the dataset from a different angle.<br>
The starting question was; “How can I model gas prices? What are the influencing factors?”<br>
One well known fact is that certain gas station brands demand higher prices.</p>
<p><img alt="plot of chunk unnamed-chunk-2" src="https://i0.wp.com/flovv.github.com/figures/post14/unnamed-chunk-2-1.png?w=456" data-recalc-dims="1"></p>
<p>That is also the case in the present data. All but one of major brands have higher prices than the non-brand (“other”) gas stations.<br>
As an interesting fact, apparently, these brands have their home turfs, as illustrated by the following map.</p>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i1.wp.com/flovv.github.com/figures/post14/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p>From the data, we can also calculate the distance between each station. <a target="_blank" rel="nofollow" href="http://eurekastatistics.com/calculating-a-distance-matrix-for-geographic-points-using-r/">Peter Rosenmai</a> provides a very nice function to do that for all station combinations (2792 * 2792). On my pretty old laptop it takes roughly 30 minutes to calculate the 8 million distances. From that we can count how many stations are within a certain distance from a single station. For example, there are on average ~8 other gas stations within a 5 kilometer radius.<br>
I created three variables, dist5 (5 kilometer radius, dist10, 10 km radius and dist20).<br>
In order to see which of these variables has predictive power on the gas price, we can run a simple “horse-race” regression on the price.</p>
<figure>
<pre><code data-lang="r"><span>summary</span><span>(</span><span>lm</span><span>(</span><span>price</span><span> </span><span>~</span><span> </span><span>dist5</span><span> </span><span>+</span><span> </span><span>dist10</span><span> </span><span>+</span><span> </span><span>dist20</span><span>,</span><span> </span><span>data</span><span>=</span><span>df4</span><span>))</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## 
## Call:
## lm(formula = price ~ dist5 + dist10 + dist20, data = df4)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.112957 -0.042385 -0.019434  0.006249  0.286630 
## 
## Coefficients:
##                Estimate  Std. Error t value             Pr(>|t|)    
## (Intercept)  1.31952601  0.00226420 582.778 &lt; 0.0000000000000002 ***
## dist5       -0.00019993  0.00040290  -0.496               0.6198    
## dist10      -0.00061953  0.00026656  -2.324               0.0202 *  
## dist20      -0.00048398  0.00007588  -6.378       0.000000000209 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.07342 on 2788 degrees of freedom
## Multiple R-squared:  0.1283,	Adjusted R-squared:  0.1274 
## F-statistic: 136.8 on 3 and 2788 DF,  p-value: &lt; 0.00000000000000022</code></pre>
</figure>
<p>We see that both the 10km and 20km variables carry weight and significance.<br>
That result is also clearly visible by plotting the gas price by the number of competitors within 20km.</p>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i1.wp.com/flovv.github.com/figures/post14/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<p>The posted gas price decreases from 1.33 to 1.25 for no competition to 100 competitors in close proximity.<br>
One way to cross-validate the result, is to split the gas stations in 4 similar sized groups and check if the result holds. I do that by splitting the stations by their median(latitude/longitude). The map illustrates that.</p>
<p><img alt="plot of chunk unnamed-chunk-6" src="https://i1.wp.com/flovv.github.com/figures/post14/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"></p>
<p>I use the blue numbers to label/group the stations according to their location.<br>
Then I do the same plot; gas price by the number of competitors within 20km for each quadrant separately.</p>
<p><img alt="plot of chunk unnamed-chunk-7" src="https://i1.wp.com/flovv.github.com/figures/post14/unnamed-chunk-7-1.png?w=456" data-recalc-dims="1"></p>
<p>Overall the results hold. Prices decrease with increasing local competition.<br>
One factor biasing the analysis is that inner-city gas stations are known to have lower prices compared to stations along the “Autobahn”. I somewhat controlled for that by just including gas stations which are positioned at the Autobahn. It might be interesting to re-run the analysis on a wider dataset.</p>
<p>To come back to the starting question; What are the key gas price influencing factors?<br>
We can do a simple model comparison using the awesome <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/BayesFactor/BayesFactor.pdf">BayesFactor</a> package.<br>
The plot shows all models and ranks them according to their BayesFactor (higher is better).</p>
<figure>
<pre><code data-lang="r"><span>reg</span><span> </span><span>&lt;-</span><span> </span><span>generalTestBF</span><span>(</span><span>price</span><span> </span><span>~</span><span> </span><span>dist5</span><span> </span><span>+</span><span> </span><span>dist10</span><span> </span><span>+</span><span> </span><span>dist20</span><span> </span><span>+</span><span> </span><span>brand</span><span>,</span><span> </span><span>data</span><span>=</span><span>df4</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>reg</span><span> </span><span>/</span><span>reg</span><span>[</span><span>4</span><span>],</span><span> </span><span>logbase</span><span> </span><span>=</span><span> </span><span>&quot;ln&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-8" src="https://i0.wp.com/flovv.github.com/figures/post14/unnamed-chunk-8-1.png?w=456" data-recalc-dims="1"></p>
<p>We see that the single most important factor is the number of close competitors in 20km (dist20).<br>
Also, the best model includes dist10 and brand, but excludes dist5.</p>
<p>Hence, the next time you see low gas prices, you know it is because the high regional competition ;).</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/maps-ii-how-does-competition-affect-gas-prices/"></div></div><div><a data-url="https://www.r-bloggers.com/maps-ii-how-does-competition-affect-gas-prices/" data-counturl="https://www.r-bloggers.com/maps-ii-how-does-competition-affect-gas-prices/" data-count="vertical" data-text="Maps II – How does Competition affect Gas Prices?" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://flovv.github.com/Gas_price-Proximity/"> Florian Teschner</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/dTyYNY7n93Q"></a></div>