-
Notifications
You must be signed in to change notification settings - Fork 12
/
91-AppendixB-ANES-CB.Rmd
175 lines (143 loc) · 4.13 KB
/
91-AppendixB-ANES-CB.Rmd
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
# ANES derived variable codebook {#anes-cb}
```{r}
#| label: anes-cb-setup
#| echo: FALSE
#| error: FALSE
#| warning: FALSE
#| message: FALSE
library(tidyverse)
library(janitor)
library(kableExtra)
library(knitr)
data(anes_2020)
attrlist <- map(anes_2020, attributes)
NULL_to_NA <- function(x){
if (is.null(x)){
NA
}else{
x
}
}
anes_var_info <- tibble(
Vars=names(attrlist),
Section=map_chr(attrlist, "Section") %>% unname(),
Question=map(attrlist, "Question") %>% map(NULL_to_NA) %>% unlist(use.names = FALSE),
Description=map_chr(attrlist, "label") %>% unname(),
VarType=map(anes_2020, class) ,) %>%
rowwise() %>%
mutate(
VarClass=str_c(VarType, collapse=", "),
VarType=case_when(
Vars%in%c("V200001", "CaseID")~ list("ID"),
Vars=="V201507x"~list("numeric2"),
TRUE~list(VarType)
)
) %>%
ungroup()
cb_count <- function(dat, var){
t <- dat %>%
count(.data[[var]]) %>%
mutate(`Unweighted Freq` = n / sum(n)) %>%
janitor::adorn_totals(where="row", fill="-", na.rm=TRUE, name="Total", n, `Unweighted Freq`) %>%
mutate(`Unweighted Freq`= round(`Unweighted Freq`, 3)) %>%
kbl(position="H")
if (knitr:::is_html_output()){
t %>% kable_minimal() %>% print()
} else{
t %>% print()
}
}
cb_count_labelled <- function(dat, var){
dat2 <- dat %>%
mutate(
Label=as_factor(.data[[var]], levels="labels"),
) %>% haven::zap_labels()
t <- dat2 %>%
count(.data[[var]], Label) %>%
mutate(`Unweighted Freq` = n / sum(n)) %>%
janitor::adorn_totals(where="row", fill="-", na.rm=TRUE, name="Total", n, `Unweighted Freq`) %>%
mutate(`Unweighted Freq`= round(`Unweighted Freq`, 3)) %>%
kbl(position="H")
if (knitr:::is_html_output()){
t %>% kable_minimal() %>% print()
} else{
t %>% print()
}
}
cb_continuous <- function(dat, var){
t <- dat %>%
summarize(
`N Missing`=sum(is.na(.data[[var]])),
Minimum = min(.data[[var]], na.rm = TRUE),
Median = median(.data[[var]], na.rm = TRUE),
Maximum = max(.data[[var]], na.rm = TRUE)) %>%
kbl(position="H")
if (knitr:::is_html_output()){
t %>% kable_minimal() %>% print()
} else{
t %>% print()
}
}
cb_continuous_spec <- function(dat, var){
dat2 <- dat %>%
haven::zap_labels()
t_valid <- dat2 %>%
filter(.data[[var]]>0) %>%
summarize(
`N Missing`=sum(is.na(.data[[var]])),
Minimum = min(.data[[var]], na.rm = TRUE),
Median = median(.data[[var]], na.rm = TRUE),
Maximum = max(.data[[var]], na.rm = TRUE))
t_ref <- dat2 %>%
filter(.data[[var]]==-9) %>%
count(name="N Refused (-9)")
t <- t_ref %>% bind_cols(t_valid) %>%
select(`N Missing`, `N Refused (-9)`, everything()) %>%
kbl(position="H")
if (knitr:::is_html_output()){
t %>% kable_minimal() %>% print()
} else{
t %>% print()
}
}
make_section <- function(sec){
cat(str_c("## ", sec, "\n\n"))
make_sum <- function(var){
cat(str_c("#### ", var, " {-} \n\n"))
vi <- anes_var_info %>% filter(Vars==var)
de <- vi %>% pull(Description)
cat(str_c("Description: ", de, "\n\n"))
qt <- vi %>% pull(Question)
if (!is.na(qt)) cat(str_c("Question text: ", qt, "\n\n"))
vc <- vi %>% pull(VarClass)
cat(str_c("Variable class: ", vc, "\n\n"))
vt <- vi %>% pull(VarType) %>% unlist()
if (any(c("factor", "character", "logical") %in% vt)){
anes_2020 %>% cb_count(var)
cat("\n")
} else if ("haven_labelled" %in% vt){
anes_2020 %>% cb_count_labelled(var)
cat("\n")
} else if ("numeric" %in% vt){
anes_2020 %>% cb_continuous(var)
cat("\n")
} else if ("numeric2" %in% vt){
anes_2020 %>% cb_continuous_spec(var)
cat("\n")
}
}
anes_var_info %>% filter(Section==sec) %>% pull(Vars) %>%
walk(make_sum)
}
```
The full codebook with the original variables is available at @anes-cb.
This is a codebook for the ANES data used in this book (`anes_2020`) from the {srvyrexploR} package.
```{r}
#| label: anes-cb-write
#| echo: FALSE
#| results: asis
anes_var_info %>%
distinct(Section) %>%
pull(Section) %>%
walk(make_section)
```