Skip to content

Commit 0eff701

Browse files
authored
Merge pull request #2 from tidy-survey-r/design-exercises
Survey design exercises
2 parents b7508b9 + 04e5d50 commit 0eff701

File tree

5 files changed

+499
-0
lines changed

5 files changed

+499
-0
lines changed

Exercises/data/chis_adult_2023.rds

11.6 MB
Binary file not shown.

Exercises/data/nsduh_2023.rds

636 KB
Binary file not shown.

Exercises/design-exercises.qmd

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: "Design exercises"
3+
format: html
4+
---
5+
6+
7+
```{r}
8+
#| label: load-packages
9+
10+
library(dplyr)
11+
library(srvyr)
12+
library(readr)
13+
```
14+
15+
16+
In these exercises, you will be given a study and the data. How would you create the survey object with design variables or replicate weights, as applicable?
17+
18+
1. California Health Interview Survey (CHIS) - 2023
19+
20+
- [CHIS Design](https://healthpolicy.ucla.edu/our-work/california-health-interview-survey-chis/chis-design-and-methods/chis-design)
21+
- [CHIS Resources](https://healthpolicy.ucla.edu/our-work/california-health-interview-survey-chis/access-chis-data)
22+
- The code below reads in a dataset select set of columns of the 2023 Adult CHIS Public Use File. Create the survey object.
23+
24+
25+
```{r}
26+
#| label: design-exercise-1
27+
28+
chis_adult_2023 <- read_rds(here::here("Exercises", "data", "chis_adult_2023.rds"))
29+
glimpse(chis_adult_2023)
30+
31+
32+
```
33+
34+
35+
2. National Survey on Drug Use and Health (NSDUH) - 2023
36+
37+
- [NSDUH Methodology](https://www.samhsa.gov/data/data-we-collect/nsduh-national-survey-drug-use-and-health/methodology)
38+
- [NSDUH Download Data Files](https://www.samhsa.gov/data/data-we-collect/nsduh-national-survey-drug-use-and-health/datafiles)
39+
- The code below reads in a dataset select set of columns of the 2023 NSDUH Public Use File. Create the survey object.
40+
41+
```{r}
42+
#| label: design-exercise-2
43+
44+
nsduh_2023 <- read_rds(here::here("Exercises", "data", "nsduh_2023.rds"))
45+
glimpse(nsduh_2023)
46+
47+
48+
49+
```
50+
51+
3. Bonus exercise
52+
53+
- Find a public use file of your own
54+
- Download the data
55+
- Read in the data
56+
- Create the survey object
57+
58+
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
title: "Data creation - design exercises"
3+
format: html
4+
---
5+
6+
```{r}
7+
#| label: load-libraries
8+
9+
library(tidyverse)
10+
library(haven)
11+
library(osfr)
12+
osf_auth(Sys.getenv("OSF_PAT"))
13+
14+
```
15+
16+
# CHIS - 2023
17+
18+
```{r}
19+
#| label: readin-data-chis
20+
21+
chis_sas_files <- osf_retrieve_node("https://osf.io/z5c3m/") %>%
22+
osf_ls_files(path = "CHIS_2023",
23+
n_max = 40,
24+
pattern = ".sas7bdat")
25+
26+
filedet <- chis_sas_files %>%
27+
filter(name=="adult.sas7bdat") %>%
28+
osf_download(conflicts = "overwrite")
29+
30+
chis_in <- filedet %>%
31+
pull(local_path) %>%
32+
read_sas()
33+
34+
unlink(pull(filedet, "local_path"))
35+
```
36+
37+
```{r}
38+
#| label: subset-data-cols-chis
39+
40+
41+
chis_slim <-
42+
chis_in %>%
43+
select(PUF1Y_ID, ASTCUR, RACEHP2_P1, contains("RAKED")) %>%
44+
mutate(
45+
ASTCUR=factor(ASTCUR, labels=c("Current asthma", "No current asthma")),
46+
RACEHP2_P1=factor(RACEHP2_P1, labels=c("Latino", "Other single/multiple race", "American Indian/Alaska Native", "Asian", "African American", "White"))
47+
)
48+
49+
chis_in %>% count(ASTCUR)
50+
chis_slim %>% count(ASTCUR)
51+
52+
chis_in %>% count(RACEHP2_P1)
53+
chis_slim %>% count(RACEHP2_P1)
54+
55+
```
56+
57+
```{r}
58+
#| label: save-dat-chis
59+
60+
summary(chis_slim)
61+
glimpse(chis_slim)
62+
63+
write_rds(chis_slim, here::here("Exercises", "data", "chis_adult_2023.rds"), compress="xz")
64+
```
65+
66+
# NSDUH - 2023
67+
68+
```{r}
69+
#| label: readin-data-nsduh
70+
71+
dir.create(here::here("Exercises", "raw-data", "nsduh-temp"))
72+
download.file("https://www.samhsa.gov/data/system/files/media-puf-file/NSDUH-2023-DS0001-bndl-data-r_v1.zip",
73+
here::here("Exercises", "raw-data", "nsduh-temp", "nsduh-2023.zip"))
74+
75+
unzip(here::here("Exercises", "raw-data", "nsduh-temp", "nsduh-2023.zip"),
76+
exdir = here::here("Exercises", "raw-data", "nsduh-temp"))
77+
78+
load(here::here("Exercises", "raw-data", "nsduh-temp", "NSDUH_2023.Rdata"))
79+
80+
unlink(here::here("Exercises", "raw-data", "nsduh-temp"), recursive= TRUE)
81+
```
82+
83+
```{r}
84+
#| label: subset-data-cols-nsduh
85+
86+
nsduh_slim <- puf2023_102124 %>%
87+
select(QUESTID2, ANALWT2_C, VESTR_C, VEREP, AGE3, ALCMON, ALCMDAYS, MRJMON, MRJMDAYS) %>%
88+
mutate(
89+
AGE3=factor(AGE3, labels=c("12-13", "14-15", "16-17", "18-20", "21-23", "24-25", "26-29", "30-34", "35-49", "50-64", "65+")),
90+
ALCMON=factor(ALCMON, labels=c("Did not use in past month", "Used in the past month")),
91+
MRJMON=factor(MRJMON, labels=c("Did not use in past month", "Used in the past month")),
92+
ALCMDAYS=factor(ALCMDAYS, labels=c("1-2 days", "3-5 days", "6-19 days", "20-30 days", "Non user or no past month use")),
93+
MRJMDAYS=factor(MRJMDAYS, labels=c("1-2 days", "3-5 days", "6-19 days", "20-30 days", "Non user or no past month use"))
94+
)
95+
96+
puf2023_102124 %>% count(AGE3)
97+
nsduh_slim %>% count(AGE3)
98+
99+
puf2023_102124 %>% count(ALCMON)
100+
nsduh_slim %>% count(ALCMON)
101+
102+
puf2023_102124 %>% count(MRJMON)
103+
nsduh_slim %>% count(MRJMON)
104+
105+
puf2023_102124 %>% count(ALCMDAYS)
106+
nsduh_slim %>% count(ALCMDAYS)
107+
108+
puf2023_102124 %>% count(MRJMDAYS)
109+
nsduh_slim %>% count(MRJMDAYS)
110+
```
111+
112+
113+
```{r}
114+
#| label: save-dat-nsduh
115+
116+
summary(nsduh_slim)
117+
glimpse(nsduh_slim)
118+
119+
write_rds(nsduh_slim, here::here("Exercises", "data", "nsduh_2023.rds"), compress="xz")
120+
```

0 commit comments

Comments
 (0)