-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5_projection.R
393 lines (322 loc) · 15.8 KB
/
5_projection.R
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
# ---- Clean up ----
rm(list = ls())
graphics.off()
# ---- Packages ----
library(ggplot2)
library(ggdendro)
library(tidyverse)
library(plyr)
library(ggrepel)
library(ggpubr)
library(gridExtra)
library(grid)
library(devtools)
library(readxl)
library(ExPosition)
library(InPosition)
library(ggplot2)
library(PTCA4CATA)
library(data4PCCAR)
library(factoextra)
library(dendextend)
library(stringr)
library(effectsize)
library(corrplot)
library(psych)
library(car)
library(dplyr)
func.dir <- ("./functions/")
sapply(paste0(func.dir,list.files(func.dir, pattern = "\\.[Rr]$")), source)
# ---- Read.results ----
load("Results/from2_Dat4Plot.rda")
# ---- Read data for projection ----
rockland <- read.csv("Data/rockland.cognitive.ses.onet.csv") # N = 996
rockland.filter <- rockland %>%
filter(
head_injury == 0,
stroke == 0,
bipolar == 0,
autism_asperger_pervasivedevdisorder == 0,
adhd == 0,
alzheimers == 0,
epilepsy == 0,
native_language == 1,
FSIQ > 70 , # N = 808
Job.Zone > 3,
!is.na(wasi_vci_comp),
!is.na(wasi_pri_comp)) # 507
rockland.filter$wasi_diff_comp <- rockland.filter$wasi_vci_comp - rockland.filter$wasi_pri_comp
rockland.filter$wasi_diff_grp <- ifelse(abs(rockland.filter$wasi_diff_comp) < 10, "Same",
ifelse(rockland.filter$wasi_diff_comp > 0, "VCI+", "PRI+"))
gnd.idx <- c("1" = "Male", "2" = "Female")
rockland.filter$sex_recode <- dplyr::recode(rockland.filter$sex_m1f2, !!!gnd.idx)
## Use wasi as design
wasi.grp.idx <- c("Same" = "grey60",
"VCI+" = "#F28E2B",
"PRI+" = "#B07AA1")
wasi.grp.col <- list()
wasi.grp.col$oc <- dplyr::recode(rockland.filter$wasi_diff_grp, !!!wasi.grp.idx) %>% as.matrix
wasi.grp.col$gc <- wasi.grp.idx %>% as.matrix
## Comparing Groups
#create new dataframe with only VCI+ and PRI+ people
wasi_2grp <- filter(rockland.filter, wasi_diff_grp != "Same")
#test if age if diff
t.test(age ~ wasi_diff_grp, wasi_2grp)
#test if gender is diff
chisq.test(wasi_2grp$sex_m1f2, wasi_2grp$wasi_diff_grp)
## Projection
load("Data/OccuPCA4proj_completeONET.rda")
Proj2Cognitive <- getProj(rockland.filter, onet.column = "onet", PCA.type = "cognitive", number.of.dimensions = 2)
## ============================= ##
## (project subject onto the occupation row space)
# data2plot <- rockland.filter[,c("dim1_45", "dim2_45")]
data2plot <- Proj2Cognitive[,c("Dimension.1", "Dimension.2")]
colnames(data2plot) <- c("Component 1", "Component 2")
proj.wasi.occu <- createFactorMap(data2plot,
axis1 = 2, axis2 = 1,
col.points = wasi.grp.col$oc,
alpha.points = 0.1,
cex = 5,
col.background = NULL,
alpha.axes = 0.5,
col.axes = "#42376B",
width.axes = 1,
title = "Project the Rockland data onto\nthe Job Zones 4-5 PCA space")
wasi.occu.boot <- Boot4Mean(data2plot, rockland.filter$wasi_diff_grp, niter = 1000)
# wasi.occu.boot.gnd <- Boot4Mean(data2plot, paste0(rockland.filter$wasi_diff_grp, rockland.filter$sex_recode), niter = 1000)
wasi.occu.boot.gnd <- Boot4Mean(data2plot, rockland.filter$sex_recode, niter = 1000)
proj.wasi.mean <- createFactorMap(wasi.occu.boot$GroupMeans[c(1,3),],
axis1 = 2, axis2 = 1,
col.points = wasi.grp.col$gc[rownames(wasi.occu.boot$GroupMeans[c(1,3),],),],
col.labels = wasi.grp.col$gc[rownames(wasi.occu.boot$GroupMeans[c(1,3),],),],
alpha.points = 1,
pch = 17,
cex = 8,
text.cex = 10)
# proj.wasi.mean.gnd <- createFactorMap(wasi.occu.boot.gnd$GroupMeans[c(1:2,5:6),],
# col.points = rep(wasi.grp.col$gc[rownames(wasi.occu.boot$GroupMeans)[c(1,3)],], each = 2),
# col.labels = rep(wasi.grp.col$gc[rownames(wasi.occu.boot$GroupMeans)[c(1,3)],], each = 2),
# alpha.points = 1,
# pch = 1,
# cex = 2,
# text.cex = 3,
# col.background = NULL,
# col.axes = "#42376B",
# width.axes = 1)
proj.wasi.mean.gnd <- createFactorMap(wasi.occu.boot.gnd$GroupMeans,
col.points = c("indianred3", "royalblue2"),
col.labels = c("indianred3", "royalblue2"),
alpha.points = 1,
pch = 1,
cex = 2,
text.cex = 3,
col.background = NULL,
col.axes = "#42376B",
width.axes = 1)
wasi.mean.ci <- MakeCIEllipses(wasi.occu.boot$BootCube[c(1,3),,],
axis1 = 1, axis2 = 2,
names.of.factors = paste0("Component ", c(1:2)),
col = wasi.grp.col$gc[rownames(wasi.occu.boot$BootCube[c(1,3),,]),],
alpha.ellipse = 0.1,
line.size = 0.7)
# wasi.mean.gnd.ci <- MakeCIEllipses(wasi.occu.boot.gnd$BootCube[c(1:2,5:6),,],
# axis1 = 2, axis2 = 1,
# names.of.factors = paste0("Component ", c(1:2)),
# col = rep(wasi.grp.col$gc[rownames(wasi.occu.boot$BootCube[c(1,3),,]),], each = 2),
# alpha.ellipse = 0.1,
# line.size = 0.7)
wasi.mean.gnd.ci <- MakeCIEllipses(wasi.occu.boot.gnd$BootCube,
axis1 = 2, axis2 = 1,
names.of.factors = paste0("Component ", c(1:2)),
col = c("indianred3", "royalblue2"),
alpha.ellipse = 0.1,
line.size = 0.7)
Rockland.cog <- proj.wasi.occu$zeMap_background + proj.wasi.occu$zeMap_dots + wasi.mean.ci +
proj.wasi.mean$zeMap_dots + proj.wasi.mean$zeMap_text +
xlab("Component 2\nHealth vs. Computational Science") +
ylab("Componnet 1\nSTEM vs. Social Science and Humanities") +
theme(plot.title = element_text(size = 30),
axis.title = element_text(size = 30),
plot.margin = unit(c(1,1,1,1), "mm"),
text = element_text(size = 30))
Rockland.cog %<>%
arrangeGrob(top = textGrob(expression(bold("B")), x = unit(0, "npc"), y = unit(1, "npc"), just=c("left","top"), gp=gpar(col="black", fontsize=40)))
proj.wasi.occu$zeMap_background + wasi.mean.gnd.ci +
proj.wasi.mean.gnd$zeMap_dots + proj.wasi.mean.gnd$zeMap_text
proj.wasi.mean.gnd$zeMap_background + proj.wasi.occu$zeMap_dots + wasi.mean.ci + #wasi.mean.gnd.ci +
proj.wasi.mean$zeMap_dots + proj.wasi.mean$zeMap_text
## Univariate Analyses
rockland.filter.new <- rockland.filter
rockland.filter.new[,c("dim1_45", "dim2_45")] <- data2plot
# write_csv(rockland.filter.new, "/Users/moriah/Dropbox/rockland.csv")
#LM predicting dim1 (STEM)
model_VCI_PRI_STEM = lm(dim1_45 ~ sex_m1f2 + wasi_vci_comp + wasi_pri_comp, data = rockland.filter.new)
model_VCI_PRI_STEM %>% summary()
model_VCI_PRI_STEM %>% effectsize()
model_VCI_PRI_STEM %>% eta_squared()
model_VCI_PRI_STEM %>% vif()
model_VCI_PRI_x_STEM = lm(dim1_45 ~ sex_m1f2*(wasi_vci_comp + wasi_pri_comp), data = rockland.filter.new)
model_VCI_PRI_x_STEM %>% summary()
model_VCI_PRI_x_STEM %>% effectsize()
model_VCI_PRI_x_STEM %>% eta_squared()
model_VCI_PRI_x_STEM %>% vif()
#bivariate correlations
cor(rockland.filter.new$dim1_45, rockland.filter.new$wasi_vci_comp, method = "pearson")
cor.test(rockland.filter.new$dim1_45, rockland.filter.new$wasi_pri_comp, method = "pearson")
#NEED TO COMPARE CORRELATIONS
rpri_vci <- cor(rockland.filter.new$wasi_vci_comp, rockland.filter.new$wasi_pri_comp, method = "pearson")
rdim1_vci <- cor(rockland.filter.new$dim1_45, rockland.filter.new$wasi_vci_comp, method = "pearson")
rdim1_pri <- cor(rockland.filter.new$dim1_45, rockland.filter.new$wasi_pri_comp, method = "pearson")
rpri_vci <- cor(rockland.filter.new$wasi_vci_comp, rockland.filter.new$wasi_pri_comp, method = "pearson")
(test <- r.test(470, r12 = rdim1_vci, r13 = rdim1_pri, r23 = rpri_vci))
# test %>% summary()
#Correlation of difference score
cor.test(rockland.filter.new$dim1_45, rockland.filter.new$wasi_diff_comp)
model_diff_STEM <- lm(dim1_45 ~ sex_m1f2 + wasi_diff_comp, data = rockland.filter.new)
model_diff_STEM %>% summary()
model_diff_STEM %>% effectsize()
model_diff_STEM %>% eta_squared()
model_diff_STEM %>% vif()
model_diff_x_STEM <- lm(dim1_45 ~ sex_m1f2*wasi_diff_comp, data = rockland.filter.new)
model_diff_x_STEM %>% summary()
model_diff_x_STEM %>% effectsize()
model_diff_x_STEM %>% eta_squared()
model_diff_x_STEM %>% vif()
# Scatterplot of VCI and PRI --> Dim1
scatter_dim1_wasi <-
rockland.filter.new %>%
select(dim1_45, wasi_vci_comp, wasi_pri_comp) %>%
reshape2::melt(id = "dim1_45", measured = c("wasi_vci_comp", "wasi_pri_comp")) %>%
ggplot(aes(x = value, y = dim1_45, color = variable)) +
geom_point(alpha = .1, position = "jitter", size = 5) +
geom_smooth(method = "lm", size = 3) +
stat_cor(method = "pearson", label.x = 65, label.y = c(0, -7), size = 10, show.legend = FALSE) +
theme_bw() +
labs(x = "WASI Composite Score", y = "Component 1\nSTEM vs. Social Science and Humanities") +
scale_colour_manual(name = "WASI Domain",
values = c("#F28E2B","#B07AA1"),
labels = c("VCI", "PRI")) +
theme(legend.position = "bottom",
legend.text = element_text(size = 30),
plot.title = element_text(size = 30),
axis.title = element_text(size = 30),
plot.margin = unit(c(1,1,1,1), "mm"),
text = element_text(size = 30))
scatter_dim1_wasi
scatter_dim1_wasi %<>%
arrangeGrob(top = textGrob(expression(bold("C")), x = unit(0, "npc"), y = unit(1, "npc"), just=c("left","top"), gp=gpar(col="black", fontsize=40)))
# Scatterplot of VCI>PRI --> Dim1
scatter_dim1_diff_wasi <-
rockland.filter.new %>%
select(dim1_45, wasi_diff_comp) %>%
ggplot(aes(x = wasi_diff_comp, y = dim1_45)) +
geom_point(alpha = .1, position = "jitter", size = 5) +
geom_smooth(method = "lm", size = 3, color = "royalblue3") +
stat_cor(method = "pearson", size = 10, show.legend = FALSE, label.y = 5) +
theme_bw() +
labs(x = "WASI Difference Score\n(VCI-PRI)", y = "Component 1\nSTEM vs. Social Science and Humanities") +
theme(legend.position = "bottom",
legend.text = element_text(size = 30),
plot.title = element_text(size = 30),
axis.title = element_text(size = 30),
plot.margin = unit(c(1,5,1,1), "mm"),
text = element_text(size = 30))
scatter_dim1_diff_wasi
scatter_dim1_diff_wasi %<>%
arrangeGrob(top = textGrob(expression(bold("D")), x = unit(0, "npc"), y = unit(1, "npc"), just=c("left","top"), gp=gpar(col="black", fontsize=40)))
## Histogram
rockland.filter.new <- within(rockland.filter.new,
rowclust_45 <- factor(rowclust_45,
levels= c("SocSci", "BusGovt", "AltThrpy",
"MedGen", "SalesLog", "CompSci",
"MedSpec", "SciMaths", "Enviro", "Engi")))
rockland.table <- rockland.filter.new %>% select(wasi_diff_grp, rowclust_45) %>% table
rockland.freq <- (rockland.table/rowSums(rockland.table))
rock.hist <- rockland.freq %>% as.data.frame %>% filter(wasi_diff_grp != "Same") %>%
ggplot(aes(x = rowclust_45, y = Freq, fill = wasi_diff_grp)) +
geom_bar(position = "dodge", stat = "identity") +
scale_fill_manual(values = wasi.grp.col$gc[c(3,2)], name = "Cognitive groups") +
theme_bw() +
xlab("Occupation clusters") +
ylab("Proportion of individuals in each group\nby row clusters") +
theme(legend.position = "bottom",
legend.text = element_text(size = 30),
plot.title = element_text(size = 30),
axis.title = element_text(size = 30),
axis.text.x = element_text(size = 30, angle = 90, hjust = 1, vjust = 0.5, color = occu.clust$jz45$col$gc[levels(rockland.filter.new$rowclust_45),]),
plot.margin = unit(c(10,1,1,1), "mm"),
text = element_text(size = 30))
rock.hist %<>%
arrangeGrob(top = textGrob(expression(bold("A")), x = unit(0, "npc"), y = unit(1, "npc"), just=c("left","top"), gp=gpar(col="black", fontsize=40)))
#Correlations
#set-up df
df.corr <- rockland.filter.new %>%
select("age", "highest_grade", "dim1_45", "dim2_45", "Job.Zone",
"wasi_vocab_tscore", "wasi_similarities_tscore", "wasi_matrix_tscore", "wasi_blockdesign_tscore",
"wiat_numop_standard", "wiat_wordreading_standard", "wiat_spelling_standard")
df.corr<- df.corr %>%
dplyr::rename("edu" = "highest_grade",
"jobzone" = "Job.Zone",
"vocab" = "wasi_vocab_tscore",
"similarities" = "wasi_similarities_tscore",
"matrix" = "wasi_matrix_tscore",
"block" = "wasi_blockdesign_tscore",
"math" = "wiat_numop_standard",
"reading" = "wiat_wordreading_standard",
"spelling" = "wiat_spelling_standard",
"Dim1:STEM" = "dim1_45",
"Dim2:Numeric_Health" = "dim2_45")
#df.reg.5 <- df.reg %>%
# filter(jobzone == 5)
#create dfs for correlation matrices jz45
df.corr <- df.corr %>%
select("jobzone", "edu", "Dim1:STEM", "Dim2:Numeric_Health",
"similarities", "vocab", "matrix", "block") %>%
as.matrix()
corr = cor(df.corr)
corrplot(corr, method = "shade", addCoef.col = "black",
type = "lower", title = "Rockland Correlations")
## Correlation circle
# rockland.filter.45 <- rockland.filter.new %>% filter(Job.Zone > 3)
#
# (cor.loadings.all <- cor(rockland.filter.new[,c("wasi_vci_comp", "wasi_pri_comp")],
# rockland.filter.new[,c("dim1_45","dim2_45")]))
# (cor.loadings.45 <- cor(rockland.filter.45[,c("wasi_vci_comp", "wasi_pri_comp")],
# rockland.filter.45[,c("dim1_45","dim2_45")]))
#
# col4J <- c(wasi.grp.col$gc[2,], wasi.grp.col$gc[3,])
# ## (project variables onto the components)
# jolie.ggplot.J.Q <- createFactorMap(
# cor.loadings.all,
# col.points = col4J,
# col.labels = col4J,
# constraints = list(minx = -1, miny = -1,
# maxx = 1, maxy = 1) )
# arrows.Q <- addArrows(cor.loadings.all, color = col4J)
# b3.jolieggMap.J.Q <- jolie.ggplot.J.Q$zeMap_background +
# jolie.ggplot.J.Q$zeMap_text +
# arrows.Q +
# addCircleOfCor()
# b3.jolieggMap.J.Q
#
# jolie.ggplot.J.Q45 <- createFactorMap(
# cor.loadings.45,
# col.points = col4J,
# col.labels = col4J,
# constraints = list(minx = -1, miny = -1,
# maxx = 1, maxy = 1) )
# arrows.Q45 <- addArrows(cor.loadings.45, color = col4J)
# b3.jolieggMap.J.Q45 <- jolie.ggplot.J.Q45$zeMap_background +
# jolie.ggplot.J.Q45$zeMap_text +
# arrows.Q45 +
# addCircleOfCor()
# b3.jolieggMap.J.Q45
## Output figures ====
png(filename = "Figure6_600dpi.png", width = 60, height = 60, units = "cm", bg = "white",res = 600)
grid.arrange(grobs = list(rock.hist, Rockland.cog, scatter_dim1_wasi, scatter_dim1_diff_wasi),
widths = c(1, 0.1, 1),
heights = c(1,1),
layout_matrix = rbind(c(1,NA,2),
c(3, NA, 4))) +
theme(plot.margin = margin(2,2,2,2, "mm"))
dev.off()