-
Notifications
You must be signed in to change notification settings - Fork 0
/
040_clustering_software_with_profiles.Rmd
240 lines (130 loc) · 5.1 KB
/
040_clustering_software_with_profiles.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
---
title: "3_Using_Latent_Profiles_for_Software_Aggregation.Rmd"
mainfont: DejaVu Sans
output:
pdf_document:
latex_engine: xelatex
keep_tex: true
html_document: default
word_document: default
font-family: Times New Roman
---
```{r}
source('010_data_preprocessing.R')
library(tidyverse)
library(magrittr)
library(tidyLPA)
library(mclust)
library(forcats)
library(hrbrthemes)
library(knitr)
library(kableExtra)
library(graphics)
library(PMCMR)
library(gridExtra)
source("./functions.R")
```
Extracting profile and prob associated with the person
```{r}
profiles = m3 %>%
dplyr::select(RecordNo, profile, posterior_prob)
```
```{r}
jobtitleanddescription = df_clean[, c("RecordNo", "Q18_1_open", "Q18a", "Occupation_DISCO", "Occupation")]
merged = merge(x = software_cleaned, y = jobtitleanddescription, by= "RecordNo") %>%
select(-a, -Q18_1_open, -Q18a, -Occupation_DISCO)
merged = merged[!is.na(merged$a2),] %>% # removing NA's from the a column
dplyr::rename(a = a2)
merged$RecordNo %<>% as.numeric()
profiles$RecordNo %<>% as.numeric()
merged %>% View()
```
TODO: make the same trick with education instead/with profession
```{r}
education_df = df_clean[, c("RecordNo", "profile_education")]
education_df$RecordNo %<>% as.numeric()
education_software = inner_join(software_cleaned, education_df, by= "RecordNo")
education_software = education_software[!is.na(education_software$a2),] %>% # removing NA's from the a column
dplyr::select(-a) %>%
dplyr::rename(a = a2)
education_software %>% View
```
Checking for NAs
```{r}
anti_join(merged, profiles, by = "RecordNo")
merged_with_profiles = inner_join(merged, profiles, by = "RecordNo")
merged_with_profiles %>% View
```
Summing probabilities from respondents to software
UPD: Doesn't seem to be promising
Finally, occupation + profiles per software for mapping
```{r}
profiling_dist = merged_with_profiles %>%
dplyr::select(profile, a) %>%
gather(key, val, profile) %>% # dplyr::mutate(val_weighted = posterior_prob*as.numeric(val)) %>%
group_by(a, val) %>%
tally() %>%
spread(val, n, fill = 0) # %>% View()
# checking for merging
# anti_join(pre_dist, profiling_dist, by = "a")
occupation_profiling_dist = inner_join(pre_dist, profiling_dist, by = "a")
# Normalizing Frequencies
occupation_profiling_dist[,-1] = apply(X = occupation_profiling_dist[,-1],
2,
FUN = function(x) scale(x, center = TRUE, scale = TRUE))
occupation_profiling_dist %>% View
```
# occupation + profiles per software for mapping
## Only 200 most popular software items
```{r}
profiling_dist_200 = merged_with_profiles %>%
dplyr::filter(a %in% software_aggregated$a2[1:200]) %>%
dplyr::select(profile, a) %>%
gather(key, val, profile) %>% # dplyr::mutate(val_weighted = posterior_prob*as.numeric(val)) %>%
group_by(a, val) %>%
tally() %>%
spread(val, n, fill = 0) # %>% View()
# checking for merging
# anti_join(pre_dist, profiling_dist, by = "a")
occupation_profiling_dist_200 = inner_join(pre_dist, profiling_dist_200, by = "a")
# Normalizing Frequencies
occupation_profiling_dist_200[,-1] = apply(X = occupation_profiling_dist[,-1],
2,
FUN = function(x) scale(x, center = TRUE, scale = TRUE))
occupation_profiling_dist %>% View
```
WAITING: Function to got predefined number of software labels for each cluster
```{r}
# based on number of appearences defined in software_aggregated df
# common case
# labels_to_select = occupation_profiling_dist$a[occupation_profiling_dist$a %in% software_aggregated$a2[1:60]]
occupation_profiling_dist$cluster = distance_clustering(occupation_profiling_dist, "canberra", 6)
for_labels = inner_join(software_aggregated %>% dplyr::select(a = a2, n), occupation_profiling_dist, by = "a")
# occupation_profiling_dist %>% View
labels_to_select = for_labels %>%
group_by(cluster) %>%
top_n(n = 10, wt = n)
```
```{r}
occupation_profiling_dist_200$cluster = distance_clustering(occupation_profiling_dist_200, "canberra", 6)
for_labels_200 = inner_join(software_aggregated %>% dplyr::select(a = a2, n), occupation_profiling_dist_200, by = "a")
# occupation_profiling_dist %>% View
labels_to_select_200 = for_labels_200 %>%
group_by(cluster) %>%
top_n(n = 10, wt = n)
labels_to_select_200 %>% View
```
# Mapping all the software items
```{r}
# "euclidean" 'maximum' 'manhattan' 'minkowski' 'canberra' 'binary'
with(dev.new(), mds_n_plot(occupation_profiling_dist, "canberra", labels_to_select, 6))
with(dev.new(), mds_n_plot(occupation_profiling_dist, "euclidean", labels_to_select, 6))
with(dev.new(), mds_n_plot(occupation_profiling_dist, "manhattan", labels_to_select, 6))
with(dev.new(), mds_n_plot(occupation_profiling_dist, "minkowski", labels_to_select, 6))
with(dev.new(), mds_n_plot(occupation_profiling_dist, "maximum", labels_to_select, 6))
```
# Mapping only top-200
```{r}
## using only profiles
with(dev.new(), plot_mds(occupation_profiling_dist_200 %>% ungroup() %>% dplyr::select(a, groups, '1', '2', '3', '4','5', '6', cluster), "canberra", labels_to_select_200, 6))
```