-
Notifications
You must be signed in to change notification settings - Fork 449
/
pew.Rmd
400 lines (294 loc) · 12.1 KB
/
pew.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# Pew Research Center (PEW) {-}
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) <img src='https://img.shields.io/badge/Tested%20Locally-Windows%20Laptop-brightgreen' alt='Local Testing Badge'>
Public opinion polling on U.S. Politics & Policy, Journalism & Media, Internet, Science & Tech, Religion & Public Life, Hispanic Trends, Global Attitudes & Trends, and Social & Demographic Trends.
* Generally one table per survey, with one row per sampled respondent.
* Complex samples generalizing to the noninstitutionalized adults in the nation(s) surveyed.
* Varying publication dates for both [American Trends Panel](https://www.pewresearch.org/our-methods/u-s-surveys/the-american-trends-panel/) surveys of the United States and also for [International Surveys](https://www.pewresearch.org/our-methods/international-surveys/). [National Public Opinion Reference Survey](https://www.pewresearch.org/methods/2021/05/03/how-pew-research-center-uses-its-national-public-opinion-reference-survey-npors/) released annually since 2020.
* Administered by the [Pew Research Center](http://www.pewresearch.org/).
---
Please skim before you begin:
1. [U.S. Surveys](https://www.pewresearch.org/our-methods/u-s-surveys/)
2. Country Specific Methodology, for example the [2022 Global Attitudes Survey](https://www.pewresearch.org/methods/interactives/international-methodology/global-attitudes-survey/all-country/2022/)
3. A haiku regarding this microdata:
```{r}
# sock puppet pundit
# throws 'ssue, cites pew-laced news, sighs
# "unbutton your eyes!"
```
---
## Download, Import, Preparation {-}
1. Register for a Pew Research Center account at https://www.pewresearch.org/profile/registration/.
2. `DOWNLOAD THIS DATASET` at https://www.pewresearch.org/global/dataset/spring-2022-survey-data/.
3. Download the SPSS dataset `Pew-Research-Center-Global-Attitudes-Spring-2022-Survey-Data.zip`:
```{r eval = FALSE , results = "hide" }
library(haven)
pew_fn <-
file.path(
path.expand( "~" ) ,
"Pew Research Center Global Attitudes Spring 2022 Dataset.sav"
)
pew_tbl <- read_sav( pew_fn )
pew_label <- lapply( pew_tbl , function( w ) attributes( w )[['label']] )
pew_labels <- lapply( pew_tbl , function( w ) attributes( w )[['labels']] )
pew_tbl <- zap_labels( pew_tbl )
pew_df <- data.frame( pew_tbl )
names( pew_df ) <- tolower( names( pew_df ) )
```
Collapse country-specific cluster and strata variables into two all-country cluster and strata variables:
```{r eval = FALSE , results = "hide" }
# create the constructed psu and strata variables from among the
# non-missing country-specific columns
pew_df[ , 'psu_constructed' ] <-
apply(
pew_df[ , grep( "^psu_" , names( pew_df ) ) ] ,
1 ,
function( w ) w[ which.min( is.na( w ) ) ]
)
pew_df[ , 'stratum_constructed' ] <-
apply(
pew_df[ , grep( "^stratum_" , names( pew_df ) ) ] ,
1 ,
function( w ) w[ which.min( is.na( w ) ) ]
)
# for countries without clustering variables, give every record a unique identifier for the psu..
pew_df[ is.na( pew_df[ , 'psu_constructed' ] ) , 'psu_constructed' ] <-
rownames( pew_df[ is.na( pew_df[ , 'psu_constructed' ] ) , ] )
# ..and zeroes for the stratum
pew_df[ is.na( pew_df[ , 'stratum_constructed' ] ) , 'stratum_constructed' ] <- 0
```
### Save Locally \ {-}
Save the object at any point:
```{r eval = FALSE , results = "hide" }
# pew_fn <- file.path( path.expand( "~" ) , "PEW" , "this_file.rds" )
# saveRDS( pew_df , file = pew_fn , compress = FALSE )
```
Load the same object:
```{r eval = FALSE , results = "hide" }
# pew_df <- readRDS( pew_fn )
```
### Survey Design Definition {-}
Construct a complex sample survey design:
```{r eval = FALSE , results = "hide" }
library(survey)
options( survey.lonely.psu = "adjust" )
pew_design <-
svydesign(
id = ~ psu_constructed ,
strata = ~ interaction( country , stratum_constructed ) ,
data = pew_df ,
weights = ~ weight ,
nest = TRUE
)
```
### Variable Recoding {-}
Add new columns to the data set:
```{r eval = FALSE , results = "hide" }
pew_design <-
update(
pew_design ,
one = 1 ,
topcoded_respondent_age = ifelse( age >= 99 , NA , ifelse( age >= 97 , 97 , age ) ) ,
human_rights_priority_with_china =
ifelse(
china_humanrights_priority > 2 ,
NA ,
as.numeric( china_humanrights_priority == 1 )
) ,
favorable_unfavorable_one_to_four_us = ifelse( fav_us > 4 , NA , fav_us ) ,
favorable_unfavorable_one_to_four_un = ifelse( fav_un > 4 , NA , fav_un ) ,
country_name =
factor(
country ,
levels = as.integer( pew_labels[[ 'country' ]] ) ,
labels = names( pew_labels[['country']] )
) ,
econ_sit =
factor(
econ_sit ,
levels = 1:4 ,
labels = c( 'Very good' , 'Somewhat good' , 'Somewhat bad' , 'Very bad' )
)
)
```
---
## Analysis Examples with the `survey` library \ {-}
### Unweighted Counts {-}
Count the unweighted number of records in the survey sample, overall and by groups:
```{r eval = FALSE , results = "hide" }
sum( weights( pew_design , "sampling" ) != 0 )
svyby( ~ one , ~ country_name , pew_design , unwtd.count )
```
### Weighted Counts {-}
Count the weighted size of the generalizable population, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ one , pew_design )
svyby( ~ one , ~ country_name , pew_design , svytotal )
```
### Descriptive Statistics {-}
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ topcoded_respondent_age , pew_design , na.rm = TRUE )
svyby( ~ topcoded_respondent_age , ~ country_name , pew_design , svymean , na.rm = TRUE )
```
Calculate the distribution of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ econ_sit , pew_design , na.rm = TRUE )
svyby( ~ econ_sit , ~ country_name , pew_design , svymean , na.rm = TRUE )
```
Calculate the sum of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ topcoded_respondent_age , pew_design , na.rm = TRUE )
svyby( ~ topcoded_respondent_age , ~ country_name , pew_design , svytotal , na.rm = TRUE )
```
Calculate the weighted sum of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ econ_sit , pew_design , na.rm = TRUE )
svyby( ~ econ_sit , ~ country_name , pew_design , svytotal , na.rm = TRUE )
```
Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svyquantile( ~ topcoded_respondent_age , pew_design , 0.5 , na.rm = TRUE )
svyby(
~ topcoded_respondent_age ,
~ country_name ,
pew_design ,
svyquantile ,
0.5 ,
ci = TRUE , na.rm = TRUE , na.rm.all = TRUE
)
```
Estimate a ratio:
```{r eval = FALSE , results = "hide" }
svyratio(
numerator = ~ favorable_unfavorable_one_to_four_un ,
denominator = ~ favorable_unfavorable_one_to_four_us ,
pew_design ,
na.rm = TRUE
)
```
### Subsetting {-}
Restrict the survey design to :
```{r eval = FALSE , results = "hide" }
sub_pew_design <- subset( pew_design , country_name == 'South Korea' )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
svymean( ~ topcoded_respondent_age , sub_pew_design , na.rm = TRUE )
```
### Measures of Uncertainty {-}
Extract the coefficient, standard error, confidence interval, and coefficient of variation from any descriptive statistics function result, overall and by groups:
```{r eval = FALSE , results = "hide" }
this_result <- svymean( ~ topcoded_respondent_age , pew_design , na.rm = TRUE )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ topcoded_respondent_age ,
~ country_name ,
pew_design ,
svymean ,
na.rm = TRUE
)
coef( grouped_result )
SE( grouped_result )
confint( grouped_result )
cv( grouped_result )
```
Calculate the degrees of freedom of any survey design object:
```{r eval = FALSE , results = "hide" }
degf( pew_design )
```
Calculate the complex sample survey-adjusted variance of any statistic:
```{r eval = FALSE , results = "hide" }
svyvar( ~ topcoded_respondent_age , pew_design , na.rm = TRUE )
```
Include the complex sample design effect in the result for a specific statistic:
```{r eval = FALSE , results = "hide" }
# SRS without replacement
svymean( ~ topcoded_respondent_age , pew_design , na.rm = TRUE , deff = TRUE )
# SRS with replacement
svymean( ~ topcoded_respondent_age , pew_design , na.rm = TRUE , deff = "replace" )
```
Compute confidence intervals for proportions using methods that may be more accurate near 0 and 1. See `?svyciprop` for alternatives:
```{r eval = FALSE , results = "hide" }
svyciprop( ~ human_rights_priority_with_china , pew_design ,
method = "likelihood" , na.rm = TRUE )
```
### Regression Models and Tests of Association {-}
Perform a design-based t-test:
```{r eval = FALSE , results = "hide" }
svyttest( topcoded_respondent_age ~ human_rights_priority_with_china , pew_design )
```
Perform a chi-squared test of association for survey data:
```{r eval = FALSE , results = "hide" }
svychisq(
~ human_rights_priority_with_china + econ_sit ,
pew_design
)
```
Perform a survey-weighted generalized linear model:
```{r eval = FALSE , results = "hide" }
glm_result <-
svyglm(
topcoded_respondent_age ~ human_rights_priority_with_china + econ_sit ,
pew_design
)
summary( glm_result )
```
---
## Replication Example {-}
This matches statistics and standard errors from [How to analyze Pew Research Center survey data in R](
https://medium.com/pew-research-center-decoded/how-to-analyze-pew-research-center-survey-data-in-r-f326df360713):
1. `DOWNLOAD THIS DATASET` at https://www.pewresearch.org/politics/dataset/april-2017-political-survey/.
2. Download the SPSS dataset `Apr17-public-4.3-update.zip` dated 12/29/2017:
```{r eval = FALSE , results = "hide" }
political_survey_2017_fn <- file.path( path.expand( "~" ) , "Apr17 public.sav" )
political_survey_2017_tbl <- read_sav( political_survey_2017_fn )
political_survey_2017_df <- data.frame( political_survey_2017_tbl )
names( political_survey_2017_df ) <- tolower( names( political_survey_2017_df ) )
```
Construct a complex sample survey design:
```{r eval = FALSE , results = "hide" }
political_survey_2017_design <-
svydesign(
~ 0 ,
data = political_survey_2017_df ,
weights = ~ weight
)
```
Add new columns to the data set:
```{r eval = FALSE , results = "hide" }
political_survey_2017_design <-
update(
political_survey_2017_design ,
q1 =
factor(
q1 ,
levels = c( 1 , 2 , 9 ) ,
labels = c( 'Approve' , 'Disapprove' , 'DK/RF' )
)
)
```
Reproduce statistics and standard errors shown under `Estimating frequencies with survey weights`:
```{r eval = FALSE , results = "hide" }
result <- svymean( ~ q1 , political_survey_2017_design , na.rm = TRUE )
stopifnot( round( coef( result ) , 4 ) == c( 0.3940 , 0.5424 , 0.0636 ) )
stopifnot( round( SE( result ) , 4 ) == c( 0.0144 , 0.0147 , 0.0078 ) )
```
---
## Analysis Examples with `srvyr` \ {-}
The R `srvyr` library calculates summary statistics from survey data, such as the mean, total or quantile using [dplyr](https://github.com/tidyverse/dplyr/)-like syntax. [srvyr](https://github.com/gergness/srvyr) allows for the use of many verbs, such as `summarize`, `group_by`, and `mutate`, the convenience of pipe-able functions, the `tidyverse` style of non-standard evaluation and more consistent return types than the `survey` package. [This vignette](https://cran.r-project.org/web/packages/srvyr/vignettes/srvyr-vs-survey.html) details the available features. As a starting point for PEW users, this code replicates previously-presented examples:
```{r eval = FALSE , results = "hide" }
library(srvyr)
pew_srvyr_design <- as_survey( pew_design )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
pew_srvyr_design %>%
summarize( mean = survey_mean( topcoded_respondent_age , na.rm = TRUE ) )
pew_srvyr_design %>%
group_by( country_name ) %>%
summarize( mean = survey_mean( topcoded_respondent_age , na.rm = TRUE ) )
```