-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGallupTrust.Rmd
99 lines (80 loc) · 2.58 KB
/
GallupTrust.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
---
title: "Gallup Trust EDA"
output: html_notebook
---
```{r message=FALSE, include=FALSE}
#Required libraries
# Tidyverse for data science and exploration
require(tidyverse)
require(tidymodels)
require(dplyr)
require(tidyr)
require(readr)
require(tibble)
require(stringr)
require(purrr)
require(forcats)
require(rlang)
# enhances tidyverse
require(tidylog) # additional logging
require(magrittr) # additional data pipe syntax
# for reading data in multiple formats
require(readxl)
require(haven)
require(labelled)
# visual analysis
require(ggplot2)
require(GGally) # extensions to ggplot
require(gt) # well formatted tables
# client-side interactive publishable graphics
require(plotly)
require(leaflet)
require(crosstalk)
require(htmlwidgets)
# server-side interactive graphics
require(shiny)
require(shinyjs)
# Canned Interactive EDA
require(ExPanDaR)
```
```{r}
# Data Ingest
KnightABS.df <- read_sav("Gallup Source Data/Knight Foundation 2019-2020 ABS Public Release Data.SAV")
```
```{r}
# sanity check
if (interactive()) {
KnightABS.df %>% ExPanD(df=., export_nb_option=TRUE)
}
```
```{r}
# Recode for R standards
## missing value codes
## value labels -> factors
kg.df <- KnightABS.df
kg.df %<>% to_factor(.,ordered=TRUE)
kg.df %<>% mutate(across(where(is.factor),function(x)fct_recode(x,NULL="No answer")))
kg.df %<>% mutate(across(where(is.factor),function(x)fct_recode(x,NULL="Undesignated")))
kg.df %<>% mutate(across(where(is.factor),function(x)fct_explicit_na(x,na_level="(Missing)")))
# to_factor drops var lables -- restore these
var_label(kg.df)<-var_label(KnightABS.df)
```
```{r}
# Cluster / of responses - factor analysis?
require(factoextra)
pca.res<-prcomp(kg.df %>% transmute(across(where(is.factor),as.numeric)),scale=TRUE,na.omit=TRUE)
fviz_eig(pca.res)
fviz_pca_var(pca.res, col.var = "contrib", gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"), repel = TRUE )
```
```{r}
# Map Visualization of trust in media
countytrust <-kg.df %>% transmute(fips=SAMPLE_FIPS,trust=Q5) %>% mutate(trust= fct_recode(trust,NULL="(Missing)")) %>% group_by(fips) %>% summarise(mintrust=min(trust,na.rm=TRUE),m25=quantile(trust,.25,type=1,na.rm=TRUE),mtrust=quantile(trust,.5,type=1,na.rm=TRUE),m75=quantile(trust,.75,type=1,na.rm=TRUE),maxtrust=max(trust,na.rm=TRUE),n=length(trust))
trustplot <- countytrust %>% plot_usmap(data=.,regions="counties",values="mtrust")
plot(trustplot)
#
#ggplotly(trustplot) # takes a long time -- ggplotly not so efficient with this?
# TODO: reproduce this in leaflet for more flexibility in mapping
```
```{r}
# Multivariate predictors of overall trust in media (Q5)
```