-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedpolish-survey.Rmd
215 lines (159 loc) · 4.04 KB
/
medpolish-survey.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
---
title: "medpolish-survey"
author: "John D. Smith"
date: '2022-03-21'
output: md_document
---
## Libraries
```{r setup, include=FALSE}
library(tidyverse)
library(grid)
library(ggplotify)
library(cowplot)
library(mpx)
theme_set(theme_light())
```
```{r}
survey_responses <- read_csv(
"generation,n,response
1970s,32,Strongly Disagree
1970s,32,Disagree
1970s,111,Neutral
1970s,64,Agree
1970s,17,Strongly Agree
1980s,23,Strongly Disagree
1980s,19,Disagree
1980s,83,Neutral
1980s,54,Agree
1980s,11,Strongly Agree
1990s,16,Strongly Disagree
1990s,31,Disagree
1990s,104,Neutral
1990s,52,Agree
1990s,12,Strongly Agree
2000-2007,18,Strongly Disagree
2000-2007,20,Disagree
2000-2007,95,Neutral
2000-2007,61,Agree
2000-2007,21,Strongly Agree
2008-2012,16,Strongly Disagree
2008-2012,13,Disagree
2008-2012,90,Neutral
2008-2012,43,Agree
2008-2012,10,Strongly Agree
2013-2017,13,Strongly Disagree
2013-2017,18,Disagree
2013-2017,94,Neutral
2013-2017,44,Agree
2013-2017,11,Strongly Agree
Since 2018,1,Strongly Disagree
Since 2018,4,Disagree
Since 2018,40,Neutral
Since 2018,9,Agree
Since 2018,6,Strongly Agree") %>%
group_by(generation) %>%
mutate(percent = 100 * (n / sum(n))) %>%
ungroup()
```
pivot_wider does the trick...
```{r}
get_array <- function(df, measure, row_name, col_name) {
my_df <- df
my_measure <- my_df %>% select({{measure}}) %>% unlist()
my_row_name <- my_df %>% select({{row_name}}) %>% unique() %>% unlist()
my_col_name <- my_df %>% select({{col_name}}) %>% unique() %>% unlist()
my_row_length <- my_row_name %>% unlist() %>% length()
my_col_length <- my_col_name %>% unlist() %>% length()
my_array <- array(my_measure, c(my_row_length, my_col_length),
dimnames = list(my_row_name, my_col_name))
}
x <- get_array(survey_responses, n, generation, response)
x
str(x)
```
var <- rlang::enquo(var)
data %>%
}
1 summarise(mean = mean(!!var))
```{r}
counts <- array(survey_responses$n, c(7,5),
dimnames = list(unique(survey_responses$generation),
unique(survey_responses$response)))
percents <- array(survey_responses$percent, c(7,5),
dimnames = list(unique(survey_responses$generation),
unique(survey_responses$response)))
```
## Techniques described by Hadley Wickham
Hadley Wickham, R for Data Science chapter on Exploratory Data Analysis: <https://r4ds.had.co.nz/exploratory-data-analysis.html>
### look at COUNTS
#### Dot size
```{r}
survey_responses %>% ggplot(aes(generation, y = response)) +
geom_point(aes(size = n)) +
coord_flip()
```
#### Heat map
```{r}
survey_responses %>% ggplot(aes(generation, y = response)) +
geom_tile(mapping = aes(fill = n)) +
coord_flip()
```
### Look at PERCENTS
#### Dot Size
```{r}
survey_responses %>% ggplot(aes(generation, y = response)) +
geom_point(aes(size = percent)) +
coord_flip()
```
#### Heatmap
```{r}
survey_responses %>% ggplot(aes(generation, y = response)) +
geom_tile(mapping = aes(fill = percent)) +
coord_flip()
```
## Save medpolish output in a list and demonstrate basic additivity plot
### Look at counts
## Augment *medpolish* output to produce an augmented plot
Aiming to reproduce the plot on p 176
```{r}
lpc_out <- mp_fit(long_df = survey_responses,
measure = percent,
row_name = generation,
col_name = response)
```
### additivity plot
```{r}
plot(lpc_out)
```
### Graph the median polish fit
```{r}
percent_plot <- mp_plot(lpc_out)
percent_plot
```
### Rotate the fit plot
```{r}
vp <- mp_rotate_plot(percent_plot)
pushViewport(vp)
```
## Save medpolish output in a list and demonstrate basic additivity plot
### Look at percents
```{r}
lpp <- medpolish(percents)
lpp
plot(lpp)
```
## Augment *medpolish* output to produce an augmented plot
Aiming to reproduce the plot on p 176
```{r}
lpp_out <- mp_fit(lpp)
```
### Graph the median polish fit
```{r}
count_plot <- mp_plot(lpp_out)
count_plot
```
### Rotate the fit plot
```{r}
vp <- mp_rotate_plot(count_plot)
pushViewport(vp)
```