Repository navigation
Expand file tree
/
Copy pathcodebook.Rmd
More file actions
338 lines (260 loc) · 11.7 KB
/
Copy pathcodebook.Rmd
File metadata and controls
338 lines (260 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
---
title: "SHINE study codebook"
author: "Dani Cosme"
date: "`r Sys.Date()`"
output:
html_document:
code_folding: hide
df_print: paged
highlight: tango
theme: united
toc: yes
toc_float:
collapsed: yes
smooth_scroll: yes
pdf_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
options(scipen = 999)
```
```{r, include = FALSE}
# prep
# load packages
if (!require(pacman)) {
install.packages('pacman')
}
pacman::p_load(tidyverse, haven, sjlabelled, knitr)
# load data
baseline = read_sav("surveys/baseline.sav")
prescan = read_sav("surveys/prescan.sav")
postscan = read_sav("surveys/postscan.sav")
covid = read_sav("surveys/covid.sav")
followup_6months = read_sav("surveys/followup_6months.sav")
followup_12months = read_sav("surveys/followup_12months.sav")
ignore_items = read.csv("ignore_items.csv", stringsAsFactors = FALSE)
item_info = read.csv("items_info.csv", stringsAsFactors = FALSE)
missing = read.csv("missing.csv", stringsAsFactors = FALSE) %>%
mutate(item = as.character(item))
# tidy data
choice_options = enframe(get_labels(baseline)) %>% mutate(session = "baseline") %>%
bind_rows(enframe(get_labels(prescan)) %>% mutate(session = "prescan")) %>%
bind_rows(enframe(get_labels(postscan)) %>% mutate(session = "postscan")) %>%
bind_rows(enframe(get_labels(covid)) %>% mutate(session = "covid")) %>%
bind_rows(enframe(get_labels(followup_6months)) %>% mutate(session = "followup_6months")) %>%
bind_rows(enframe(get_labels(followup_12months)) %>% mutate(session = "followup_12months")) %>%
rename("choice_options" = value)
codebook_all = enframe(get_label(baseline)) %>% mutate(session = "baseline") %>%
bind_rows(enframe(get_label(prescan)) %>% mutate(session = "prescan")) %>%
bind_rows(enframe(get_label(postscan)) %>% mutate(session = "postscan")) %>%
bind_rows(enframe(get_label(covid)) %>% mutate(session = "covid")) %>%
bind_rows(enframe(get_label(followup_6months)) %>% mutate(session = "followup_6months")) %>%
bind_rows(enframe(get_label(followup_12months)) %>% mutate(session = "followup_12months")) %>%
left_join(., choice_options) %>%
extract(value, c("instruction", "item_text"), "(.*) - (.*)", remove = FALSE) %>%
mutate(instruction = ifelse(is.na(instruction), value, instruction),
name = gsub("Q64", "ERQ", name),
name = gsub("erq_1", "ERQ", name),
name = gsub("Q75", "fs", name),
name = gsub("Q31", "drink_intent", name),
name = gsub("Q33", "alc_change", name),
name = gsub("Q38|purpose_in_life1", "PILS_1", name),
name = gsub("psychDrugs", "psych_drugs", name),
name = gsub("loneliness_UCLA|ucla", "ULS", name),
name = gsub("flourishing|fs|fs_1", "FS", name),
name = gsub("cesd|Q70", "CES_D", name),
name = gsub("irq", "IRQ", name),
name = gsub("lotr", "LOTR", name),
name = gsub("ftp", "FTP", name),
name = gsub("sise", "SISE", name),
name = gsub("srhi", "SRHI", name),
name = gsub("ipaq", "IPAQ", name),
name = gsub("pls", "PILS", name),
name = gsub("stai", "STAI_state", name),
name = gsub("intolerance", "IUS", name),
name = gsub("autonomy", "IAF", name),
name = gsub("IPAQ", "IPAQ_", name),
name = gsub("tipi", "TIPI", name),
item_text = gsub("Alpha Phi Omega", "[group]", item_text),
item_text = gsub("Field-group", "group", item_text),
instruction = gsub("from 1 (not at all) to 7 (very much).", "from 1(not at all) to 7(very much)", instruction),
instruction = gsub("Alpha Phi Omega", "[group]", instruction),
instruction = gsub("Field-group", "group", instruction),
instruction = ifelse(name == "spirits_freq", "On how many days per week do you drink spirits (whisky, vodka, gin, rum, etc - but not beer or wine)?", instruction),
instruction = ifelse(name == "CES_D", "Below is a list of some of the ways you may have felt or behaved. Please indicate how often you have felt this way during the past week by checking the appropriate box for each question.", instruction),
choice_options = ifelse(name %in% c("grm_role", "eyes_rx2", "grm_year", "grm_memberStat", "housing_city",
"housing_country", "perspective1", "mindful1", "react1", "physdis",
"spirits_brands", "wine_brands", "beer_brands", "weight", "height",
"psych_drugs_names", "psych_drugs_dose", "psych_drugs_length"), NA, choice_options),
choice_options = ifelse(grepl("IPAQ|other_drugs|mhealth_|drugs_today_", name), NA, choice_options),
choice_options = ifelse(grepl("ladder", name), list(c("Off", "On")), choice_options),
choice_options = ifelse(name == "physdis_3", "text entry", choice_options),
choice_options = ifelse(name == "housing_member_fut", "text entry", choice_options),
choice_options = ifelse(instruction == "When did you join [group]?", "text entry", choice_options),
choice_options = as.character(choice_options),
choice_options = gsub("c\\(", "", choice_options),
choice_options = gsub("\\)$", "", choice_options),
choice_options = gsub('\\"', "", choice_options)) %>%
filter(!name %in% ignore_items$name & !grepl("TEXT|photo", name)) %>%
extract(name, c("survey", "item"), c("(.*)_([0-9]+)$"), remove = FALSE) %>%
mutate(survey = ifelse(is.na(survey), name, survey)) %>%
select(-name, -value)
# fix individual survey information
rpi = read.csv("rpi.csv", stringsAsFactors = FALSE)
prc_if = codebook_all %>%
filter(grepl("prc|^if_", survey)) %>%
mutate(item_text = "[name]",
item = NA) %>%
unique()
friendly_universe = codebook_all %>%
filter(grepl("fu_", survey)) %>%
mutate(item_text = "[name]",
item = NA,
choice_options = ifelse(grepl("age|category|networkname", survey), "text entry", choice_options)) %>%
filter(!grepl("fu_channel_[2-9]|fu_channel_[1]+", survey)) %>%
unique() %>%
select(-item)
full_network_survey = read.csv("fns.csv", stringsAsFactors = FALSE)
# full_network_survey = codebook_all %>%
# filter(grepl("fns|alcMost|closest|alcLeast|close_risk|risk_notclose|partnerGroup", survey)) %>%
# mutate(instruction = gsub("\\?.*", "", instruction),
# item_text = "[name]",
# item = NA,
# choice_options = ifelse(grepl("^alc|amount|freq|adjust|face|phone|text|closest_interact|partnerGroup", survey), choice_options, NA)) %>%
# unique() %>%
# select(-item)
codebook = codebook_all %>%
filter(!grepl("RPI|prc|^if_|fns|alcMost|closest|alcLeast|close_risk|risk_notclose|fu_|partnerGroup", survey)) %>%
filter(!survey %in% missing$survey) %>%
bind_rows(., rpi, prc_if, missing) %>%
filter(!grepl("close_risk|risk_notclose", survey)) %>%
filter(!(grepl("prc", survey) & grepl("NULL", choice_options))) %>%
mutate(single_item = ifelse((is.na(item_text) | item_text == "Selected Choice" | survey == "alc_change"), 1, 0),
item_text = ifelse(single_item == 1, instruction, item_text),
item_text = gsub("Selected Choice ", "", item_text),
item = ifelse(item_text == "I lead a purposeful and meaningful life", 1, item),
single_item = ifelse(grepl("IPAQ", survey), 0, single_item),
item = ifelse(single_item == 1, NA, item),
instruction = ifelse(single_item == 1, "", instruction)) %>%
select(-single_item) %>%
left_join(., item_info) %>%
group_by(session, survey) %>%
mutate(item = ifelse(!is.na(item) & !survey == "RPI", row_number(), NA)) %>%
select(survey_name, survey_citation, survey, everything()) %>%
ungroup()
```
<br><br>
# Full network survey
Codebook for items in the full network survey. Participants saw members of their campus social group and made nominations.
<br><br>
```{r, echo = FALSE}
full_network_survey %>%
filter(!survey %in% missing$survey) %>%
DT::datatable(filter = "top")
```
<br><br><br><br>
# Friendly Universe ego-network
Codebook for items in the self-generated [Friendly Universe ego-network survey](https://github.com/ruipei1/friendlyUniverse).
<br><br>
```{r, echo = FALSE}
friendly_universe %>%
DT::datatable(filter = "top")
```
<br><br><br><br>
# Non-network questionnaires {.tabset}
Codebook for questionnaire items. Items rated using slider scales are noted and anchor points are provided.
<br><br>
```{r, include = FALSE}
print_chunk = function(scales) {
for (variable in scales){
data_scale = data %>%
filter(survey_name == !!variable)
surveys = unique(data_scale$survey)
citation = as.array(unique(data_scale$survey_citation)[!unique(data_scale$survey_citation) == ""])
instructions_all = as.array(unique(data_scale$instruction))
items = as.array(unique(data_scale$item)[!unique(data_scale$item) == ""])
same_scale = unique(data_scale$same_instructions)
data_print = data_scale %>%
select(-survey_name, -survey_citation, -session, -same_instructions)
cat(paste0('\n\n### ', variable, '{.tabset}\n\n'))
cat('<br>')
if (length(citation) == 1) {
cat(paste0("**Citation**\n\n"), citation, "\n\n")
cat('<br>')
}
if (!is.na(same_scale) & length(instructions_all) == 1) {
cat(paste0("**Instructions**\n\n"), instructions_all, "\n\n")
cat('<br>')
cat(knitr::knit_print(DT::datatable(data_print %>%
select(-instruction),
width = "100%")))
} else if (length(instructions_all) == 1 & !instructions_all == "" & length(items) == 1) {
cat(paste0("**Instructions**\n\n"), instructions_all, "\n\n")
cat('<br>')
cat(knitr::knit_print(DT::datatable(data_print %>%
select(-instruction, -item),
width = "100%")))
} else if (instructions_all == "" & length(items) > 1) {
cat(knitr::knit_print(DT::datatable(data_print %>%
select(-instruction),
width = "100%")))
} else if (instructions_all == "" & length(items) == 1) {
cat(knitr::knit_print(DT::datatable(data_print %>%
select(-instruction, -item),
width = "100%")))
} else {
cat(knitr::knit_print(DT::datatable(data_print,
width = "100%")))
}
}
}
```
## baseline {.tabset}
<br><br>
```{r, results='asis', echo = FALSE}
data = codebook %>%
filter(session == "baseline")
scales = unique(data$survey_name)
print_chunk(scales)
```
## prescan {.tabset}
<br>
```{r, results='asis', echo = FALSE}
data = codebook %>%
filter(session == "prescan")
scales = unique(data$survey_name)
print_chunk(scales)
```
## COVID-19 {.tabset}
<br>
```{r, results='asis', echo = FALSE}
data = codebook %>%
filter(session == "covid")
scales = unique(data$survey_name)
print_chunk(scales)
```
## postscan {.tabset}
<br>
```{r, results='asis', echo = FALSE}
data = codebook %>%
filter(session == "postscan")
scales = unique(data$survey_name)
print_chunk(scales)
```
## followup: 6 months {.tabset}
<br>
```{r, results='asis', echo = FALSE}
data = codebook %>%
filter(session == "followup_6months")
scales = unique(data$survey_name)
print_chunk(scales)
```
## followup: 12 months {.tabset}
<br>
```{r, results='asis', echo = FALSE}
data = codebook %>%
filter(session == "followup_12months")
scales = unique(data$survey_name)
print_chunk(scales)
```