Compact sports cars has treated me well for the past few years (other than the spinning out and totaling the E36 M3 in a snow storm) but I feel like it’s finally time to move on to the next stage of my life and get something a bit more practical – like a SUV of some sort so I can transport my work equipment.

To help decide on which SUV to get, I will be using R to compare data from Edmunds API (http://developer.edmunds.com/).

For the purpose of this comparison I will be looking at the 4Runner, Xterra and Grand Cherokee based on price, performance (HP and torque) and efficiency (MPG).

<pre class="r"><code class="r"><span class="keyword"># some initial processing codes put into a separate file to help tidy things up
source</span><span class="paren">(</span><span class="string">"edmunddata.R"</span><span class="paren">)</span>
<span class="keyword">source</span><span class="paren">(</span><span class="string">"car_v2.R"</span><span class="paren">)

</span></code></pre>
<pre class="r"><code class="r"><span class="identifier"># Get data for the SUV of interest:
year</span> <span class="operator">=</span> <span class="identifier">c</span><span class="paren">(</span><span class="number">2010</span><span class="operator">:</span><span class="number">2015</span><span class="paren">)</span>
<span class="identifier">gc</span> <span class="operator">&lt;-</span> <span class="identifier">tmv</span><span class="paren">(</span><span class="identifier">d</span>, <span class="string">"jeep"</span>, <span class="string">"grand-cherokee"</span>, <span class="identifier">year</span><span class="paren">)</span>
<span class="identifier">fr</span> <span class="operator">&lt;-</span> <span class="identifier">tmv</span><span class="paren">(</span><span class="identifier">d</span>, <span class="string">"toyota"</span>, <span class="string">"4runner"</span>, <span class="identifier">year</span><span class="paren">)</span>
<span class="identifier">xt</span> <span class="operator">&lt;-</span> <span class="identifier">tmv</span><span class="paren">(</span><span class="identifier">d</span>, <span class="string">"nissan"</span>, <span class="string">"xterra"</span>, <span class="identifier">year</span><span class="paren">)</span>

<span class="comment">## gotta have 4WD for winter, although technically I believe a few of the 'Limited' models actually has AWD</span>
<span class="identifier">gc4wd</span> <span class="operator">&lt;-</span> <span class="identifier">gc</span><span class="paren">[</span><span class="identifier">grepl</span><span class="paren">(</span><span class="string">"4WD"</span>, <span class="identifier">gc</span><span class="operator">$</span><span class="identifier">style_name</span><span class="paren">)</span>, <span class="paren">]</span>
<span class="identifier">fr4wd</span> <span class="operator">&lt;-</span> <span class="identifier">fr</span><span class="paren">[</span><span class="identifier">grepl</span><span class="paren">(</span><span class="string">"4WD"</span>, <span class="identifier">fr</span><span class="operator">$</span><span class="identifier">style_name</span><span class="paren">)</span>, <span class="paren">]</span>
<span class="identifier">xt4wd</span> <span class="operator">&lt;-</span> <span class="identifier">xt</span><span class="paren">[</span><span class="identifier">grepl</span><span class="paren">(</span><span class="string">"4WD"</span>, <span class="identifier">xt</span><span class="operator">$</span><span class="identifier">style_name</span><span class="paren">)</span>, <span class="paren">]</span>

<span class="identifier">suv</span> <span class="operator">&lt;-</span> <span class="identifier">rbind</span><span class="paren">(</span><span class="identifier">gc4wd</span>, <span class="identifier">fr4wd</span>, <span class="identifier">xt4wd</span><span class="paren">)</span>
<span class="identifier">suv</span><span class="operator">$</span><span class="identifier">model</span> <span class="operator">&lt;-</span> <span class="identifier">factor</span><span class="paren">(</span><span class="identifier">suv</span><span class="operator">$</span><span class="identifier">model</span><span class="paren">)</span>

<span class="comment">## get data for performance (hp, torque, mpg, etc.)</span>
<span class="identifier">suv</span> <span class="operator">&lt;-</span> <span class="identifier">performance</span><span class="paren">(</span><span class="identifier">suv</span><span class="paren">)</span>

<span class="comment">## only considering 6 cyl, Limited trims (Pro-4X) or SE and only</span>
<span class="identifier">suv</span> <span class="operator">&lt;-</span> <span class="identifier">subset</span><span class="paren">(</span><span class="identifier">suv</span>, <span class="identifier">cyl</span> <span class="operator">==</span> <span class="number">6</span><span class="paren">)</span>
<span class="identifier">suv</span> <span class="operator">&lt;-</span> <span class="identifier">subset</span><span class="paren">(</span><span class="identifier">suv</span>, <span class="identifier">trim</span> <span class="operator">==</span> <span class="string">"Limited"</span> <span class="operator">|</span> <span class="identifier">trim</span> <span class="operator">==</span> <span class="string">"Pro-4X"</span> <span class="operator">|</span> <span class="identifier">trim</span> <span class="operator">==</span> <span class="string">"PRO-4X"</span> <span class="operator">|</span> <span class="identifier">trim</span> <span class="operator">==</span> <span class="string">"SE"</span><span class="paren">)</span></code></pre>

See how the price changes over time:

<pre class="r"><code class="r"><span class="keyword">library</span><span class="paren">(</span><span class="identifier">ggplot2</span><span class="paren">)</span>
<span class="comment">## retail price .vs year (line plot)</span>
<span class="identifier">g</span> <span class="operator">&lt;-</span> <span class="identifier">ggplot</span><span class="paren">(</span><span class="identifier">data</span> <span class="operator">=</span> <span class="identifier">suv</span>, <span class="identifier">aes</span><span class="paren">(</span><span class="identifier">x</span> <span class="operator">=</span> <span class="identifier">year</span>, <span class="identifier">y</span> <span class="operator">=</span> <span class="identifier">retail</span><span class="operator">/</span><span class="number">1000</span>, <span class="identifier">group</span> <span class="operator">=</span> <span class="identifier">model</span>, <span class="identifier">color</span> <span class="operator">=</span> <span class="identifier">model</span><span class="paren">)</span><span class="paren">)</span>
<span class="identifier">g</span> <span class="operator">+</span> <span class="identifier">geom_point</span><span class="paren">(</span><span class="identifier">size</span> <span class="operator">=</span> <span class="number">2</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">geom_line</span><span class="paren">(</span><span class="identifier">alpha</span> <span class="operator">=</span> <span class="number">.5</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">ylab</span><span class="paren">(</span><span class="string">"Retail price [k]"</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">theme</span><span class="paren">(</span><span class="identifier">axis.text.x</span> <span class="operator">=</span> <span class="identifier">element_text</span><span class="paren">(</span><span class="identifier">angle</span> <span class="operator">=</span> <span class="operator">-</span><span class="number">45</span>, <span class="identifier">hjust</span> <span class="operator">=</span> <span class="number">0</span><span class="paren">)</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">ggtitle</span><span class="paren">(</span><span class="string">"Retail price for given year"</span><span class="paren">)</span></code></pre>

car_price

Surprisingly linear as a function of year..

Using the horsepower and torque as a measure of performance:

library</span><span class="paren">(</span><span class="identifier">reshape2</span><span class="paren">)</span>
<span class="identifier">suvm</span> <span class="operator">&lt;-</span> <span class="identifier">melt</span><span class="paren">(</span><span class="identifier">data</span> <span class="operator">=</span> <span class="identifier">suv</span>, <span class="identifier">id</span> <span class="operator">=</span> <span class="identifier">c</span><span class="paren">(</span><span class="string">"model"</span>, <span class="string">"year"</span>, <span class="string">"trim"</span><span class="paren">)</span>, <span class="identifier">measure.vars</span> <span class="operator">=</span> <span class="identifier">c</span><span class="paren">(</span><span class="string">"hp"</span>, <span class="string">"torque"</span><span class="paren">)</span><span class="paren">)</span>
<span class="identifier">g</span> <span class="operator">&lt;-</span> <span class="identifier">ggplot</span><span class="paren">(</span><span class="identifier">data</span> <span class="operator">=</span> <span class="identifier">suvm</span>, <span class="identifier">aes</span><span class="paren">(</span><span class="identifier">x</span> <span class="operator">=</span> <span class="identifier">year</span>, <span class="identifier">y</span> <span class="operator">=</span> <span class="identifier">value</span>, <span class="identifier">group</span> <span class="operator">=</span> <span class="identifier">model</span>, <span class="identifier">color</span> <span class="operator">=</span> <span class="identifier">model</span><span class="paren">)</span><span class="paren">)</span>
<span class="identifier">g</span> <span class="operator">+</span> <span class="identifier">geom_point</span><span class="paren">(</span><span class="identifier">size</span> <span class="operator">=</span> <span class="number">2</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">geom_line</span><span class="paren">(</span><span class="identifier">alpha</span> <span class="operator">=</span> <span class="number">.5</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">facet_grid</span><span class="paren">(</span>. <span class="operator">~</span> <span class="identifier">variable</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">theme</span><span class="paren">(</span><span class="identifier">axis.text.x</span> <span class="operator">=</span> <span class="identifier">element_text</span><span class="paren">(</span><span class="identifier">angle</span> <span class="operator">=</span> <span class="operator">-</span><span class="number">45</span>, <span class="identifier">hjust</span> <span class="operator">=</span> <span class="number">0</span><span class="paren">)</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">ggtitle</span><span class="paren">(</span><span class="string">"HP and Torque"</span><span class="paren">)
</span></code>


performance.png

Pretty much the same except for the Grand Cherokee in 2010, which was low.

Comparing the MPG as a measure of the efficiency of each vehicle:

</code>
<pre class="r"><code class="r"><span class="identifier">suvm</span> <span class="operator"><-</span> <span class="identifier">melt</span><span class="paren">(</span><span class="identifier">data</span> <span class="operator">=</span> <span class="identifier">suv</span>, <span class="identifier">id</span> <span class="operator">=</span> <span class="identifier">c</span><span class="paren">(</span><span class="string">"model"</span>, <span class="string">"year"</span>, <span class="string">"trim"</span><span class="paren">)</span>, <span class="identifier">measure.vars</span> <span class="operator">=</span> <span class="identifier">c</span><span class="paren">(</span><span class="string">"mpg_city"</span>, <span class="string">"mpg_hwy"</span><span class="paren">)</span><span class="paren">)</span>
<span class="identifier">g</span> <span class="operator"><-</span> <span class="identifier">ggplot</span><span class="paren">(</span><span class="identifier">data</span> <span class="operator">=</span> <span class="identifier">suvm</span>, <span class="identifier">aes</span><span class="paren">(</span><span class="identifier">x</span> <span class="operator">=</span> <span class="identifier">year</span>, <span class="identifier">y</span> <span class="operator">=</span> <span class="identifier">value</span>, <span class="identifier">group</span> <span class="operator">=</span> <span class="identifier">model</span>, <span class="identifier">color</span> <span class="operator">=</span> <span class="identifier">model</span><span class="paren">)</span><span class="paren">)</span>
<span class="identifier">g</span> <span class="operator">+</span> <span class="identifier">geom_point</span><span class="paren">(</span><span class="identifier">size</span> <span class="operator">=</span> <span class="number">2</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">geom_line</span><span class="paren">(</span><span class="identifier">alpha</span> <span class="operator">=</span> <span class="number">.5</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">facet_grid</span><span class="paren">(</span>. <span class="operator">~</span> <span class="identifier">variable</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">scale_y_continuous</span><span class="paren">(</span><span class="identifier">breaks</span> <span class="operator">=</span> <span class="identifier">seq</span><span class="paren">(</span><span class="number">15</span>, <span class="number">25</span>, <span class="identifier">by</span> <span class="operator">=</span> <span class="number">2</span><span class="paren">)</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">theme</span><span class="paren">(</span><span class="identifier">axis.text.x</span> <span class="operator">=</span> <span class="identifier">element_text</span><span class="paren">(</span><span class="identifier">angle</span> <span class="operator">=</span> <span class="operator">-</span><span class="number">45</span>, <span class="identifier">hjust</span> <span class="operator">=</span> <span class="number">0</span><span class="paren">)</span><span class="paren">)</span> <span class="operator">+</span> <span class="identifier">ggtitle</span><span class="paren">(</span><span class="string">"MPG"</span><span class="paren">)</span></code></pre>

mpg.png

The only car that’s made any improvement over the years seems to be the Grand Cherokee - the late model looked especially promising.

At the end of the day, I ended up going with the Subaru Outback which actually met my needs much better. I just didn't need that much torque for a daily driver and the MPG were great for long drives (which is what I use the car for the most often).

Advertisements