---
title: "American Time Use Survey (ATUS)"
subtitle: "Exploratory Data Analysis"
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
---

This set of exercises exposes you to several important aspects of data science. 

1. This is a big dataset (relatively speaking)
2. It's been touched by many hands, and the artifacts show.  
3. It involves download, import, manipulation and requires you to read the documentation to understand the data.


# ATUS Data

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

How to use ATUS microdata: [ATUS Microdata](https://www.bls.gov/tus/other-documentation/howto.htm#basicfiles)


ATUS data concerns sex, age, race, work, and normal activities of human life.

Some interesting variables:

- TRTALONE: Time spent alone
- TUFINGLWGT: statistical weight for generating estimates representative of the U.S. civilian, noninstitutional population.  This is a very important variable for generating estimates from the data.  See [ATUS Microdata](https://www.bls.gov/tus/other-documentation/howto.htm#basicfiles) for more information.

## Data Dictionary

- [pdf](atusintcodebk25.pdf)
- [simplified](atus_dictionary_simplified.qmd)
- [Activity Lexicon](https://www.bls.gov/tus/lexicons/lexiconwex2025.pdf)


## Download the Data

Make a new folder inside your data309 folder called ATUS.  Open the ATUS folder in Positron.  

Copy the following code into a new R script and run it.  It will download the data files into your ATUS folder. Notice that we are reading a csv from the internet.  

```{r}
# as usual, we will use the tidyverse
library(tidyverse)

# responses concerning personal activity
atus_act = read_csv('http://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atusact_2025.dat')

# responses concerning demographics
atus_rost = read_csv('http://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atusrost_2025.dat')

# responses concerning types of jobs
atus_job = read_csv('http://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atusresp_2025.dat')

# respondent info with labor force status & earnings
atus_resp = read_csv('http://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atusresp_2025.dat')

atus_dict = read_csv('http://euclid.nmu.edu/~joshthom/dat309/data/ATUS/atus_dict.csv')
```

## 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?

### unique()

We know the `TUCASEID` variable is the unique identifier for each person in the data.  How many people are in the data?  How many unique caseiatus_rost are there?  There are several ways to answer this.  

```{r unique}
#| eval: TRUE
unique_iatus_rost <- unique(atus_rost$TUCASEID)
length(unique_iatus_rost)

# or alternatively
atus_rost |> distinct(TUCASEID) |>
  nrow()

# or 
atus_rost$TUCASEID |> unique() |>
  length()
```




We can also rename variables to make them easier to read.  The `rename()` function is useful for this.

```{r rename}
# rename work variable 
#| eval: FALSE
atus_rost <- rename(atus_rost,"sex" = TESEX)
atus_rost <- rename(atus_rost,"age" = TEAGE)


```

## Filter

From the [simplified codebook](atus_dictionary_simplified.qmd) we can see that the `job` variable is `TRDTOCC1`.  The detailed [codebook](data/ATUS/atusintcodebk25.pdf) has a list of jobs and their corresponding numbers.  We can filter the data to just a few jobs to make it easier to visualize.

For the `TRDTOCC1` variable, the following numbers correspond to the following jobs:

- 1 Management occupations
- 2 Business and financial operations occupations
- 3 Computer and mathematical occupations
- 4 Architecture and engineering occupations
- 5 Life, physical, and social science occupations
- 6 Community and social service occupations
- 7 Legal occupations
- 8 Educational instruction and library occupations
- 9 Arts, design, entertainment, sports, and media occupations
- 10 Healthcare practitioner and technical occupations
- 11 Healthcare support occupations
- 12 Protective service occupations
- 13 Food preparation and serving related occupations
- 14 Building and grounatus_rost cleaning and maintenance occupations
- 15 Personal care and service occupations
- 16 Sales and related occupations
- 17 Office and administrative support occupations
- 18 Farming, fishing, and forestry occupations
- 19 Construction and extraction occupations
- 20 Installation, maintenance, and repair occupations
- 21 Production occupations
- 22 Transportation and material moving occupations

```{r filter and plot}
# get variable names
atus_job |> names()
atus_job <- atus_job |> rename("job" = TRDTOCC1)

# notice the syntax below (==) signs for boolean (yes/no) comparisons.  The `|` symbol means "or" in this context.
atus_filtered <- filter(atus_job,
job == 3 | job == 5 |job == 13)
ggplot(atus_filtered,aes(x=job)) + geom_bar()
```

## Visualizing Distributions & Relationships

[See examples in text](https://r4atus_rost.hadley.nz/data-visualize#visualizing-distributions) 

1. Categorical distribution: bar plot 
2. Numerical distribution: histogram, density plot
3. Numerical distribution vs. categorical variable: box plot
4. Two categorical variables: barplot filled with color or the same with `position = "fill"` in the geom.
5. Two-Three numerical variables: scatter plot with colors mapped to a variable. (see [text](https://r4atus_rost.hadley.nz/data-visualize#three-or-more-variables))

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

**Exercise**
1. Improve the plot below 
2. Add a `position = "fill"` to the geom_bar().  Does it help?

```{r}
ds <- filter(atus_job,job == 3 | job == 5 |job == 13)

ggplot(ds,aes(x=job, fill = job)) + geom_bar() +
  labs(fill = "Job")
```
# Sample questions

-   **Time use and day of week** — Does time spent alone (`TRTALONE`) or with family (`TRTFAMILY`) differ between weekdays and weekends (`TUDIARYDAY`)?
-   **Labor force status and daily activity** — How does labor force status (`TELFS`) relate to average minutes spent on a chosen activity category, joining `atus_resp` to `atus_act`?
-   **Occupation and time alone** — Does time spent alone (`TRTALONE`) vary across broad occupation groups (`TRDTOCC1`), similar to the worked weighted/unweighted example but extended to more categories?
-   **Age and activity patterns** — Using `atus_rost`'s `TEAGE`, do time-use patterns (e.g., time with family, time alone) differ across age brackets students define themselves (e.g., 18–29, 30–49, 50+)?
-   **Sex, age, and occupation together** — A three-variable exploration (as in Exercise 3's "Part C") combining `TESEX`, `TEAGE` buckets, and `TRDTOCC1` to see which occupation groups skew toward particular age/sex profiles in the sample.
-   **Weighted vs. unweighted divergence across a variable of choice** — Students pick any categorical variable (sex, occupation, day type) and replicate the weighted-vs-unweighted comparison exercise, then discuss for which variables the gap is largest and hypothesize why.
