Analysis

This is the second part of the multi-part data analysis document. Here we’ll use the pre-processed dataset produced in Part 1 for a basic analysis.

box_fig <- ggplot(ext_cars, aes(x=origin, y=mpg)) + geom_boxplot() + theme_bw()

An initial look at the data suggests that there is a pronounced difference in the miles per gallon achieved by US and international cars (Figure 1).

**Figure 1:** Boxplot of MPG for US and internationally build cars. (Download as [PDF](part2_files/figure-html//boxplot-1.pdf))

Figure 1: Boxplot of MPG for US and internationally build cars. (Download as PDF)

A reasonable initial hypothesis for why this might be the case is that US built cars may tend to be larger (and therefore heavier) and have larger engines. As it turns out car weight and engine displacement are highly correlated (\(\rho\) = 0.888), so we’ll investigate this by fitting a linear regression model with weight as explanatory variable.

fit <- lm(mpg ~ wt, data=ext_cars)
ext_cars$fitted <- fit$fitted.values

This appears to explain the difference in MPG reasonably well, although it is clearly not the only relevant factor:

Table 1: Linear model fit for mpg ~ wt

 EstimateStd. Errort valuePr(>|t|)
wt-5.340.559-9.561.29e-10
(Intercept)37.31.8819.98.24e-19

We’ll also prepare a plot visualising the result for inclusion in the main document.

reg_fig <- ggplot(ext_cars, aes(x=wt, y=mpg, colour=origin, text=rownames(ext_cars))) + geom_point(size=2) +
  geom_abline(slope=fit$coefficients['wt'], intercept=fit$coefficients[1], linetype='dashed', colour='darkgrey') +
  geom_segment(aes(yend=fitted, xend=(fitted-fit$coefficients[1])/fit$coefficients['wt'])) +
  theme_bw()

Appendix

Custom Functions

## Custom functions used in the analysis should go into this chunk.
## They will be listed in their own section of the appendix.

Configuration

## This chunk should contain global configuration commands.
## Use this to set knitr options and related things. Everything
## in this chunk will be included in an appendix to document the
## configuration used.

## Pander options
panderOptions("digits", 3)
panderOptions("table.split.table", 160)

Version

Document version

Fri Mar 17 13:48:10 2017

Session Info

  • platform:

    • version: R version 3.3.1 (2016-06-21)
    • system: x86_64, mingw32
    • ui: RTerm
    • language: (EN)
    • collate: English_Australia.1252
    • tz: Australia/Sydney
    • date: 2017-03-17
  • packages:

    package*versiondatesource
    assertthat0.12013-12-06CRAN (R 3.3.1)
    backports1.0.52017-01-18CRAN (R 3.3.2)
    base64enc0.1-32015-07-28CRAN (R 3.3.0)
    callr1.0.0.90002017-02-16Github (mangothecat/callr@c02300a)
    codetools0.2-142015-07-15CRAN (R 3.3.1)
    colorspace1.3-22016-12-14CRAN (R 3.3.2)
    colourpicker0.32016-12-05CRAN (R 3.3.2)
    DBI0.62017-03-09CRAN (R 3.3.3)
    devtools1.12.02016-06-24CRAN (R 3.3.1)
    digest0.6.122017-01-27CRAN (R 3.3.2)
    dplyr0.5.02016-06-24CRAN (R 3.3.1)
    evaluate0.102016-10-11CRAN (R 3.3.2)
    ggplot2*2.2.1.90002017-03-16Github (tidyverse/ggplot2@08e135e)
    gtable0.2.02016-02-26CRAN (R 3.3.1)
    htmltools0.3.52016-03-21CRAN (R 3.3.1)
    htmlwidgets0.82016-11-09CRAN (R 3.3.2)
    httpuv1.3.32015-08-04CRAN (R 3.3.2)
    httr1.2.12016-07-03CRAN (R 3.3.1)
    jsonlite1.32017-02-28CRAN (R 3.3.3)
    knitr*1.15.12016-11-22CRAN (R 3.3.2)
    labeling0.32014-08-23CRAN (R 3.3.0)
    lazyeval0.2.02016-06-12CRAN (R 3.3.1)
    magrittr1.52014-11-22CRAN (R 3.3.1)
    memoise1.0.02016-01-29CRAN (R 3.3.1)
    mime0.52016-07-07CRAN (R 3.3.1)
    miniUI0.1.12016-01-15CRAN (R 3.3.2)
    munsell0.4.32016-02-13CRAN (R 3.3.1)
    pander*0.6.02015-11-23CRAN (R 3.3.1)
    plotly*4.5.6.90002017-02-16Github (ropensci/plotly@cbf5885)
    plyr1.8.42016-06-08CRAN (R 3.3.1)
    purrr0.2.22016-06-18CRAN (R 3.3.2)
    R62.2.02016-10-05CRAN (R 3.3.2)
    Rcpp0.12.92017-01-14CRAN (R 3.3.2)
    reportMD*0.4.02017-02-28local
    rmarkdown1.32016-12-21CRAN (R 3.3.2)
    rprojroot1.22017-01-16CRAN (R 3.3.2)
    scales0.4.12016-11-09CRAN (R 3.3.2)
    shiny1.0.02017-01-12CRAN (R 3.3.2)
    stringi1.1.22016-10-01CRAN (R 3.3.2)
    stringr1.2.02017-02-18CRAN (R 3.3.3)
    tibble1.22016-08-26CRAN (R 3.3.1)
    tidyr0.6.12017-01-10CRAN (R 3.3.2)
    viridisLite0.1.32016-03-12CRAN (R 3.3.2)
    withr1.0.22016-06-20CRAN (R 3.3.1)
    xml21.1.12017-01-24CRAN (R 3.3.2)
    xtable1.8-22016-02-05CRAN (R 3.3.2)
    yaml2.1.142016-11-12CRAN (R 3.3.2)

Created with reportMD