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).
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:
Estimate | Std. Error | t value | Pr(>|t|) | |
---|---|---|---|---|
wt | -5.34 | 0.559 | -9.56 | 1.29e-10 |
(Intercept) | 37.3 | 1.88 | 19.9 | 8.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()
## Custom functions used in the analysis should go into this chunk.
## They will be listed in their own section of the appendix.
## 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)
platform:
packages:
package | * | version | date | source |
---|---|---|---|---|
assertthat | 0.1 | 2013-12-06 | CRAN (R 3.3.1) | |
backports | 1.0.5 | 2017-01-18 | CRAN (R 3.3.2) | |
base64enc | 0.1-3 | 2015-07-28 | CRAN (R 3.3.0) | |
callr | 1.0.0.9000 | 2017-02-16 | Github (mangothecat/callr@c02300a) | |
codetools | 0.2-14 | 2015-07-15 | CRAN (R 3.3.1) | |
colorspace | 1.3-2 | 2016-12-14 | CRAN (R 3.3.2) | |
colourpicker | 0.3 | 2016-12-05 | CRAN (R 3.3.2) | |
DBI | 0.6 | 2017-03-09 | CRAN (R 3.3.3) | |
devtools | 1.12.0 | 2016-06-24 | CRAN (R 3.3.1) | |
digest | 0.6.12 | 2017-01-27 | CRAN (R 3.3.2) | |
dplyr | 0.5.0 | 2016-06-24 | CRAN (R 3.3.1) | |
evaluate | 0.10 | 2016-10-11 | CRAN (R 3.3.2) | |
ggplot2 | * | 2.2.1.9000 | 2017-03-16 | Github (tidyverse/ggplot2@08e135e) |
gtable | 0.2.0 | 2016-02-26 | CRAN (R 3.3.1) | |
htmltools | 0.3.5 | 2016-03-21 | CRAN (R 3.3.1) | |
htmlwidgets | 0.8 | 2016-11-09 | CRAN (R 3.3.2) | |
httpuv | 1.3.3 | 2015-08-04 | CRAN (R 3.3.2) | |
httr | 1.2.1 | 2016-07-03 | CRAN (R 3.3.1) | |
jsonlite | 1.3 | 2017-02-28 | CRAN (R 3.3.3) | |
knitr | * | 1.15.1 | 2016-11-22 | CRAN (R 3.3.2) |
labeling | 0.3 | 2014-08-23 | CRAN (R 3.3.0) | |
lazyeval | 0.2.0 | 2016-06-12 | CRAN (R 3.3.1) | |
magrittr | 1.5 | 2014-11-22 | CRAN (R 3.3.1) | |
memoise | 1.0.0 | 2016-01-29 | CRAN (R 3.3.1) | |
mime | 0.5 | 2016-07-07 | CRAN (R 3.3.1) | |
miniUI | 0.1.1 | 2016-01-15 | CRAN (R 3.3.2) | |
munsell | 0.4.3 | 2016-02-13 | CRAN (R 3.3.1) | |
pander | * | 0.6.0 | 2015-11-23 | CRAN (R 3.3.1) |
plotly | * | 4.5.6.9000 | 2017-02-16 | Github (ropensci/plotly@cbf5885) |
plyr | 1.8.4 | 2016-06-08 | CRAN (R 3.3.1) | |
purrr | 0.2.2 | 2016-06-18 | CRAN (R 3.3.2) | |
R6 | 2.2.0 | 2016-10-05 | CRAN (R 3.3.2) | |
Rcpp | 0.12.9 | 2017-01-14 | CRAN (R 3.3.2) | |
reportMD | * | 0.4.0 | 2017-02-28 | local |
rmarkdown | 1.3 | 2016-12-21 | CRAN (R 3.3.2) | |
rprojroot | 1.2 | 2017-01-16 | CRAN (R 3.3.2) | |
scales | 0.4.1 | 2016-11-09 | CRAN (R 3.3.2) | |
shiny | 1.0.0 | 2017-01-12 | CRAN (R 3.3.2) | |
stringi | 1.1.2 | 2016-10-01 | CRAN (R 3.3.2) | |
stringr | 1.2.0 | 2017-02-18 | CRAN (R 3.3.3) | |
tibble | 1.2 | 2016-08-26 | CRAN (R 3.3.1) | |
tidyr | 0.6.1 | 2017-01-10 | CRAN (R 3.3.2) | |
viridisLite | 0.1.3 | 2016-03-12 | CRAN (R 3.3.2) | |
withr | 1.0.2 | 2016-06-20 | CRAN (R 3.3.1) | |
xml2 | 1.1.1 | 2017-01-24 | CRAN (R 3.3.2) | |
xtable | 1.8-2 | 2016-02-05 | CRAN (R 3.3.2) | |
yaml | 2.1.14 | 2016-11-12 | CRAN (R 3.3.2) |
Created with reportMD