---
title: "M05 Lab — Building Table 1 from a Real Survey"
author: "Your Name"
date: today
format:
  html:
    toc: true
    toc-depth: 3
    theme: cosmo
    df-print: paged
    highlight-style: github
    embed-resources: true
execute:
  warning: false
  message: false
---

# Setup

Load the packages we'll use in this notebook. `haven` and `labelled` read SPSS files (`.sav`) into R while preserving the variable labels survey researchers attach; `skimr` helps you inspect your data before and after recoding.

```{r}
#| label: setup
#| warning: false
#| message: false

library(tidyverse)
library(here)
library(haven)
library(labelled)
library(gtsummary)
library(gt)
library(skimr)

# Keep numbers in plain form (e.g., 99, not 9.9e+01) so sentinel codes are easy to spot
options(scipen = 999)
```

# Introduction

<!-- TODO (Step 0): In two or three sentences, describe what this analysis does.
Name the data (which survey, which wave, when it was fielded), what the table
describes, and how it is organized. See the lab page for a model sentence to
react against — write your own rather than copying it. -->

# Data

The Pew American Trends Panel `.sav` file should be at `PSY652_project/data/pew_atp_w163.sav` once you finish **Step 1** of the lab. If you haven't downloaded it yet, the next chunk will throw a "file not found" error — that's expected. Procure the file first.

```{r}
#| label: import-data

pew_raw <- read_sav(here("data", "pew_atp_w163.sav"))

pew_raw |> glimpse()
```

# Select + inspect

```{r}
#| label: select-inspect

# TODO (Step 4): Narrow the data to your analysis variables and save that as
# pew_selected. Then inspect it through the two lenses from the lab page: the
# codebook view -- look_for() -- and the distribution view -- skim(). Run BOTH
# on pew_selected. This pair is your BEFORE snapshot: you will run the same two
# lenses again after recoding and compare.
```

# Wrangling

```{r}
#| label: recode

# TODO (Step 5): Build `pew` from `pew_selected` with two recodes:
#   1. A yes/no social-media-use factor — your stratifier — with "No" as the
#      first level.
#   2. The five demographic variables converted to factors, with the Refused
#      code sent to missing and unused levels dropped.
#
# The three social-media importance items come along untouched. Combining them
# into a score is this lab's optional challenge, not part of the main analysis.
```

```{r}
#| label: verify

# TODO (Step 5): Verify the recode two ways — a recode you don't check is one
# you don't trust.
#   1. Cross-tabulate the original variable against your recoded version to
#      confirm every value landed where you intended.
#   2. Save your analysis variables as pew_analysis, then re-run look_for() and
#      skim() ON pew_analysis (not on pew) and compare against the BEFORE
#      snapshot.
#
# Optional, as your skills grow: add a stopifnot() guardrail so the recode
# fails loudly if an assumption ever breaks.
```

# Table 1 with gtsummary

```{r}
#| label: table-one

# TODO (Step 6): Build the Table 1 from `pew` — the five demographics,
# stratified by social-media use, with an overall column placed after the
# stratified ones -- that is missing = "ifany" and add_overall(last = TRUE).
# Give the table a title and subtitle that say which survey and wave it is,
# and that the numbers are unweighted.
```

# Reflection

<!-- TODO (Step 7, at home): In 2-3 sentences, reflect on the WORKFLOW you just
carried out — not statistics you haven't covered yet:
 - the before/after verification — name one thing it let you confirm, or would
   have caught
 - the documentation — what did reading the codebook and questionnaire tell you
   that the data alone would not have?

DO NOT interpret the substantive differences between the groups in your Table 1
(who is more likely to use social media, and so on). That is a real question,
but answering it needs tools you meet in Part 2, and the numbers here are
unweighted. This paragraph is about the PROCESS, not the findings.

Delete this comment block once your paragraph is written. -->
