--- title: "Confounding example" author: "Nicholas Horton (nhorton@amherst.edu)" date: "May 9, 2015" output: html_document: fig_height: 3 fig_width: 5 pdf_document: fig_height: 3 fig_width: 5 word_document: fig_height: 3 fig_width: 5 --- ```{r include=FALSE} # Don't delete this chunk if you are using the mosaic package # This loads the mosaic and dplyr packages require(mosaic) ``` ```{r include=FALSE} # Some customization. You can alter or delete as desired (if you know what you are doing). # This changes the default colors in lattice plots. trellis.par.set(theme=theme.mosaic()) # knitr settings to control how R chunks work. require(knitr) opts_chunk$set( tidy=FALSE, # display code as typed size="small" # slightly smaller font for code ) ``` ```{r, include=FALSE} # Load additional packages here. Uncomment the line below to use Project MOSAIC data sets. require(mosaicData) ``` See http://www.amherst.edu/~nhorton/precursors for more papers plus commentary on this example. ```{r} xyplot(sat ~ salary, type=c("r", "p"), xlab="Average teacher salary", ylab="State average SAT score", data=SAT) mod1 <- lm(sat ~ salary, data=SAT); confint(mod1) ``` What do we conclude? ``` ``` ```{r} head(SAT) SAT <- mutate(SAT, fracgrp = cut(frac, breaks=c(0, 22, 49, 81), labels=c("low fraction", "medium fraction", "high fraction"))) head(SAT) bwplot(frac ~ fracgrp, data=SAT) ``` ```{r} xyplot(sat ~ salary, type=c("r", "p"), groups=fracgrp, auto.key=TRUE, xlab="Average teacher salary", ylab="State average SAT score", data=SAT) mod2 <- lm(sat ~ salary + frac, data=SAT); confint(mod2) ``` ```{r} filter(SAT, salary > 45) ``` ```{r} filter(SAT, salary < 29) ``` ```{r} pairs(select(SAT, salary, sat, frac)) ```