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