---
title: "Sections 1.4-1.5"
subtitle: "Visualizing distributions & relationships"
format:
  html: 
    code-overflow: wrap
    toc: true
    toc-title: "Table of Contents"
include-in-header:
  - text: |
      <style>
      .cell-output-stdout code {
        word-break: break-wor !important;
        white-space: pre-wrap !important;
      }
      </style>
execute:
  warning: false
  message: false
---

# ATUS Data

Learn more and extract different data below: [American Time Use Survey](https://www.atusdata.org/atus/)

Our ATUS data concerns sex, age, race, stress & work

## Download

*Prepare* to download the data into your Dat309 folder (not Samba!), or a suitable sub-directory.

```{r directories}
#| eval: FALSE
# get the working diretory
getwd()
# set the working directory
# note: Windows users, use forward slash \
setwd("enter your directory here")
```

*Download* the `codebook`, the `ddi` file and the `zip` file. [ddi](http://euclid.nmu.edu/~joshthom/Teaching/DAT309/Week2/ATUS2/atus_00002.xml) [codebook](http://euclid.nmu.edu/~joshthom/Teaching/DAT309/Week2/ATUS2/codebook2.txt) [.zip](http://euclid.nmu.edu/~joshthom/Teaching/DAT309/Week2/ATUS2/atus_00002.dat.zip)

Use the codebook to parse meaning from the variables. You need the `ipumsr` package to load the data into R.

```{r load data}
#| eval: TRUE
# load ATUS data
# The data requires the IPUMSr package
library(haven)
library(ipumsr)
ddi <- read_ipums_ddi("ATUS2/atus_00002.xml")
data <- read_ipums_micro(ddi)
```

## Exploratory Questions

(i) What is the size of the data? How many rows & columns? (Use `dim()`, for dimension.)
(ii) What do the numbers mean in the rows?
(iii) What are the variable names and what do they mean?

The (very useful) `clean_names()` function makes the variable names a bit easier to read.

```{r filtering}
# filter to get stress data
#| eval: TRUE
library(tidyverse)
library(janitor)
ds <- clean_names(data)
ds <- filter(ds,scstress < 10)
```

The variable pertaining to "kind of job" was poorly named, so we change it.

```{r rename}
# rename work variable 
#| eval: FALSE
ds <- filter(ds,scstress < 10)
ds <- rename(ds,"job" = occ2_cps8)

```

## Filter

Choose a few jobs so the data isn't so big. Learn what how the numbers relate to jobs in the [codebook](ATUS2/codebook2.txt).

```{r filter and plot}
#| eval: FALSE
dmsf <- filter(ds,job == 120 | job == 122 |job == 132)
ggplot(dmsf,aes(x=scstress)) + geom_bar()
```

## Visualizing Distributions & Relationships

[See examples in text](https://r4ds.hadley.nz/data-visualize#visualizing-distributions) 
(i) Categorical distribution: bar plot 

# Exercise
(i) Improve the plot below with `fill = as_factor(job)` from the `haven` package.  
(ii) Add a `position = "fill"` to the geom_bar().  Does it help?

```{r}
dmsf <- filter(ds,job == 120 | job == 122 |job == 132)
ggplot(dmsf,aes(x=scstress, fill = job)) + geom_bar() +
  labs(fill = "Job")
```

(ii) Numerical distribution: histogram, density plot 
(iii) Categorical / Numerical relationship: box plot, density ![boxplot](boxplot.png)
(iv) Two categorical variables: barplot filled with color or the same with `position = "fill"` in the geom.
(v) Two-Three numerical variables: scatter plot with colors mapped to a variable. (see [text](https://r4ds.hadley.nz/data-visualize#three-or-more-variables))

(vi) Just a few categorical variables? Try [faceting](https://r4ds.hadley.nz/layers.html#facets):


# Exercise: 
(i) Are the `happy` and `stress` variables correlated? 

## Factors

(i) Remove `factor()` from the `fill = factor(job)` and observe the result.
(ii) Replace it with `fill = haven::as_factor(job)` library) and observe.  The syntax above is a way of using the `as_factor` function without loading the Haven library that caused problems earlier with data-typing.

# Exerises:

(i) Textbook: [1.5.5](https://r4ds.hadley.nz/data-visualize#exercises-2) (Due: Sept. 9)
(ii) Practice what you learned in 1.4-1.5 to the ATUS data.
