---
title: "Introduction to data with R"
output:
  pdf_document: default
  html_document:
    css: ../lab.css
    highlight: pygments
    theme: cerulean
---

In this lab, we'll explore public health
by generating simple graphical and numerical summaries of a data set collected
by the Centers for Disease Control and Prevention (CDC). This is a large, real world data
set so we'll use the software package R to aid the exploration


## The data

The Behavioral Risk Factor Surveillance System (BRFSS) is an annual telephone 
survey of 350,000 people in the United States. As its name implies, the BRFSS 
is designed to identify risk factors in the adult population and report 
emerging health trends. For example, respondents are asked about their diet and 
weekly physical activity, possible tobacco use, and even
their level of healthcare coverage. The BRFSS Web site 
([http://www.cdc.gov/brfss](http://www.cdc.gov/brfss)) contains a complete 
description of the survey, including the research questions that motivate the 
study and many interesting results derived from the data.

We will focus on a random sample of 20,000 people from the BRFSS survey 
conducted in 2000. While there are over 200  variables in this data set, we will
work with a small subset.

We begin by loading the data set of 20,000 observations into the R workspace. 
After launching RStudio, enter the following command.

```{r load-data, eval=FALSE}
cdc = read.csv("https://marksmath.org/data/cdc.csv")
```

The data set `cdc` that shows up in your workspace is a *data matrix*, with each
row representing a *case* and each column representing a *variable*.  R calls 
this data format a *data frame*, which is a term that will be used throughout 
the labs.

To view the names of the variables, type the command

```{r names, eval=FALSE}
names(cdc)
```

This returns the names `genhlth`, `exerany`, `hlthplan`, `smoke100`, `height`, 
`weight`, `wtdesire`, `age`, and `gender`. Each one of these variables 
corresponds to a question that was asked in the survey.  For example, for 
`genhlth`, respondents were asked to evaluate their general health, responding
either excellent, very good, good, fair or poor. The `exerany` variable 
indicates whether the respondent exercised in the past month (1) or did not (0).
Likewise, `hlthplan` indicates whether the respondent had some form of health 
coverage (1) or did not (0). The `smoke100` variable indicates whether the 
respondent had smoked at least 100 cigarettes in her lifetime. The other 
variables record the respondent's `height` in inches, `weight` in pounds as well
as their desired weight, `wtdesire`, `age` in years, and `gender`.

1.  How many cases are there in this data set?  How many variables?  For each 
    variable, identify its data type (e.g. categorical, discrete).

We can have a look at the first few entries (rows) of our data with the command

```{r head, eval=FALSE}
head(cdc)
```

You could also look at *all* of the data frame at once by typing its name into 
the console, but that might be unwise here.  We know `cdc` has 20,000 rows, so 
viewing the entire data set would mean flooding your screen.  It's better to 
take small peeks at the data with `head`, `tail` or various subsetting techniques.

## Summaries and tables

The BRFSS questionnaire is a massive trove of information.  A good first step in
any analysis is to distill all of that information into a few summary statistics
and graphics.  As a simple example, the function `summary` returns a numerical 
summary: minimum, first quartile, median, mean, second quartile, and maximum. 
For `weight` this is

```{r summary-weight, eval=FALSE}
summary(cdc$weight)
```


R also has built-in functions to compute summary statistics one by one.  For 
instance, to calculate the mean, median, and standard deviation of `weight`, type 

```{r weight-mean-var-median, eval=FALSE}
mean(cdc$weight) 
sd(cdc$weight)
median(cdc$weight)
```

We could also generate images like a boxplot:

```{r, eval=FALSE}
box(cdc$weight)
```

or a histogram:

```{r, eval=FALSE}
hist(cdc$weight)
```

Of course, `weight` is a quantitative variable. How might we deal with
categorical data?  We might instead consider the
sample frequency or relative frequency distribution.  The function `table` does
this for you by counting the number of times each kind of response was given.

```{r table-smoke, eval=FALSE}
table(cdc$smoke100)
```

or instead look at the relative frequency distribution by typing

```{r table-smoke-prop, eval=FALSE}
table(cdc$smoke100)/20000
```

Next, we 
make a bar plot of the entries in the table by putting the table inside the 
`barplot` command.

```{r table-smoke-barplot, eval=FALSE}
barplot(table(cdc$smoke100))
```

Notice what we've done here - we've computed the table of `cdc$smoke100` and then
immediately applied the graphical function, `barplot`. This is an important 
idea: R commands can be nested. You could also break this into two steps by 
typing the following:

```{r table-smoke-barplot-twosteps, eval=FALSE}
smoke = table(cdc$smoke100)

barplot(smoke)
```

Here, we've made a new object, a table, called `smoke` (the contents of which we
can see by typing `smoke` into the console) and then used it in as the input for
`barplot`. The special symbol `<-` or the normal symbol `=` performs an *assignment*, 
taking the output 
of one line of code and saving it into an object in your workspace.   This is 
another important idea that we'll return to later.

2.  Create a numerical summary for `height` and `age`, and compute the 
    interquartile range for each. Compute the relative frequency distribution for
    `gender` and `exerany`. How many males are in the sample? What proportion of
    the sample reports being in excellent health?

The `table` command can be used to tabulate any number of variables that you 
provide.  For example, to examine which participants have smoked across each 
gender, we could use the following.

```{r table-smoke-gender, eval=FALSE}
table(cdc$gender,cdc$smoke100)
```

Here, we see column labels of 0 and 1. Recall that 1 indicates a respondent has
smoked at least 100 cigarettes. The rows refer to gender. To create a mosaic 
plot of this table, we would enter the following command.

```{r mosaic-smoke-gender, eval=FALSE}
mosaicplot(table(cdc$smoke100,cdc$gender))
```

3.  What does the mosaic plot reveal about smoking habits and gender?

### 
