-
Notifications
You must be signed in to change notification settings - Fork 1
/
FCM Aggregation Using Example Data - For Repository.R
1394 lines (998 loc) · 53.5 KB
/
FCM Aggregation Using Example Data - For Repository.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Set working directory (should have the example data file in that same folder)
setwd("~/FCM Code")
## Set the packages, if you have not already downloaded them, do so like the example below:
#install.packages("igraph")
library(rJava)
library(openxlsx)
library(xlsx)
library(igraph)
library(tnet)
#### PREPARE THE DATA ####
## Import the raw data - replace with your file name
## We basically just need a bit of information before we properly import the data
rawdata <- loadWorkbook("Example Random FCM Maps.xlsx")
## Extract the sheet names - can be numeric or characters, I use this to track participant IDs
sheetnames <- as.vector(getSheetNames("Example Random FCM Maps.xlsx")) #this function uses the package openxlsx
## Set the number of sheets (number of participants/maps)
nsheets <- as.numeric(length(sheetnames))
#### The import function converts the raw data into a matrix in R. X is a vector of the sheetnames and Y is the data file (xlsx of FCMs)
## I generated the example data to have zeros (no edge) in the adjacency matrices but if you're importing directly from Mental Modeler it will have blank cells.
## That's totally fine and the import function will manage that!
importfxn <- function(x, y){
sheet <- read.xlsx(y,sheetName = x)
dims <- dim(sheet)
mat <- as.matrix(sheet[1:dims[1],2:dims[2]], rownames.force=TRUE)
rownames(mat) <- as.list(sheet[,1])
colnames(mat) <- as.list(sheet[,1])
mat[is.na(mat)] = 0
print(mat)
}
### Now we use the import function to read the xlsx and create a list of matrix data
matrix_data <- lapply(X=sheetnames, FUN=importfxn, y="Example Random FCM Maps.xlsx")
names(matrix_data) <- sheetnames
#### DESCRIPTIVE DATA ####
## To determine the descriptive data for each individual map, we'll use a pretty long function and apply it to all of them
descriptivedatafxn_singlegraph <- function(matrixdata){
#read in the graph
testgraph <- graph_from_adjacency_matrix(matrixdata, mode ="directed", weighted=T )
#want one without the negative connections for centrality
abstestgraph <- testgraph
E(abstestgraph)$weight <- abs(E(testgraph)$weight)
# Concepts
nconcepts <- as.numeric(length(V(testgraph)))
conceptnames <- names(V(testgraph))
# Edges
nedges <- as.numeric(length(E(testgraph)))
edges <- as_edgelist(testgraph)
weights <- E(testgraph)$weight
edgedata <- cbind(edges, weights)
colnames(edgedata)[1] <- "from"
colnames(edgedata)[2] <- "to"
edgetype <- numeric(nedges)
for (i in 1:nedges){
if (weights[i]>0){
edgetype[i] <- "positive"
}
else {
edgetype[i] <- "negative"
}
}
testgraph <- set_edge_attr(testgraph, name = "sign", value=edgetype)
# Type of Concepts
nodetype <- numeric(nconcepts)
for (i in 1:nconcepts){
if ((any(edgedata[,1] == conceptnames[i])) == FALSE)
{nodetype[i] <- "Receiver"}
if ((any(edgedata[,1] == conceptnames[i])) == TRUE & (any(edgedata[,2] == conceptnames[i])) == TRUE)
{nodetype[i] <- "Ordinary"}
if ((any(edgedata[,1] == conceptnames[i])) == TRUE & (any(edgedata[,2] == conceptnames[i])) == FALSE)
{nodetype[i] <- "Driver"}
}
summarytypes <- cbind(sum(nodetype=="Receiver"), sum(nodetype=="Driver"), sum(nodetype=="Ordinary"))
colnames(summarytypes) <- c("Receiver", "Driver", "Ordinary")
testgraph <- set.vertex.attribute(testgraph, "type", value=nodetype)
# Centrality of Concepts
degreecent <- degree(abstestgraph, mode = "all")
indegreecent <- degree(abstestgraph, mode = "in")
outdegreecent <- degree(abstestgraph, mode = "out")
normdegreecent <- degree(abstestgraph, mode = "all", normalized = T)
normindegreecent <- degree(abstestgraph, mode = "in", normalized = T)
normoutdegreecent <- degree(abstestgraph, mode = "out", normalized = T)
degcentdist <- degree.distribution(abstestgraph, mode="all")
outdegdist <- degree.distribution(abstestgraph, mode="out")
indegdist <- degree.distribution(abstestgraph, mode="in")
absmatrix <- abs(matrixdata)
tnet_mat <- as.tnet(absmatrix, type="weighted one-mode tnet")
degree_out <- degree_w(tnet_mat, type= "out")
degree_in <- degree_w(tnet_mat, type= "in")
absweightedcent <- numeric(length=as.numeric(length(degree_out[,"output"])))
for (i in 1:as.numeric(length(degree_out[,"output"]))){
absweightedcent[i] <- sum(degree_out[i,"output"], degree_in[i,"output"])
}
closenesscent <- closeness(abstestgraph, mode = "all", weights = abs(E(testgraph)$weight))
betweennesscent <- betweenness(abstestgraph, weights = abs(E(testgraph)$weight))
attributelist <- c("absolute weighted degree centrality" , "degree centrality", "indegree centrality", "outdegree centrality", "normalized degree centrality", "normalized indegree centrality", "normalized outdegree centrality", "closeness centrality", "betweenness centrality")
centlist <- cbind(absweightedcent, degreecent, indegreecent, outdegreecent, normdegreecent, normindegreecent, normoutdegreecent, closenesscent, betweennesscent)
for (i in 1:as.numeric(length(attributelist))){
testgraph <- set_vertex_attr(graph=testgraph, name = attributelist[i], value= centlist[,i])
}
# Density
density <- edge_density(testgraph)
# C/N
CN <- nedges/nconcepts
# Complexity
complexity <- 0
if (summarytypes[1,1] != 0 & summarytypes[1,2] != 0) {
complexity <- as.numeric(summarytypes[1,1]/summarytypes[1,2])
} else {complexity <- 0}
# putting it all together
graphattributelist <- c("Number of Components", "Number of Connections", "Summary of Component Types", "Degree Centrality Distribution", "Outdegree Centrality Distribution", "Indegree Centrality Distribution", "Density", "Connections per Component", "Complexity", "Edge List" )
graphlist <- list(nconcepts, nedges, summarytypes, degcentdist, outdegdist, indegdist, density, CN, complexity, edgedata)
for (i in 1:as.numeric(length(graphattributelist))){
testgraph <- set_graph_attr(graph=testgraph, name = graphattributelist[i], value= graphlist[[i]])
}
print(testgraph)
}
### Run the function
descripdatalist_individuals <- lapply(X=matrix_data, FUN = descriptivedatafxn_singlegraph)
# If there are warnings about closeness centrality that is okay, the closeness centrality metric may just not being a great fit for your data
names(descripdatalist_individuals) <- sheetnames
#### Individual Map Descriptive Data ####
individualmapdata_fxn <- function(descripdatalist){
sheetnames <- names(descripdatalist)
densitylist <- vector(mode="numeric", length=as.numeric(length(sheetnames))) # Density
numcomplist <- vector(mode="numeric", length=as.numeric(length(sheetnames))) # Number of Components
numconnlist <- vector(mode="numeric", length=as.numeric(length(sheetnames))) # Number of Connections
CNlist <- vector(mode="numeric", length=as.numeric(length(sheetnames))) # Connections per Component
complexitylist <- vector(mode="numeric", length=as.numeric(length(sheetnames))) # Complexity
for (i in 1:as.numeric(length(sheetnames))){
densitylist[i] <- get.graph.attribute(descripdatalist[[i]], name = "Density")
numcomplist[i] <- get.graph.attribute(descripdatalist[[i]], name = "Number of Components")
numconnlist[i] <- get.graph.attribute(descripdatalist[[i]], name = "Number of Connections")
CNlist[i] <- get.graph.attribute(descripdatalist[[i]], name = "Connections per Component")
complexitylist[i] <- get.graph.attribute(descripdatalist[[i]], name = "Complexity")
}
## Export Individual Descriptive Data
descriptivedata_types <- list(sheetnames, densitylist, numcomplist, numconnlist, CNlist, complexitylist)
individualmapdata <- matrix(data=NA, nrow=as.numeric(length(sheetnames)), ncol=6)
for (i in 1:6){
individualmapdata[,i] <- descriptivedata_types[[i]]
}
colnames(individualmapdata) <- c("ID", "Density", "Number of Components", "Number of Connections", "CN", "Complexity")
print(individualmapdata)
}
individualmapdata_individuals <- individualmapdata_fxn(descripdatalist_individuals)
### Write the File ###
write.xlsx(individualmapdata_individuals, file="Example Data - Individual Map Data.xlsx", row.names = F)
#### Group Descriptive Data ####
### First set the groups by creating vectors of the map IDs/names <- make sure its the name! We'll use this as character, not numeric data
## For this example, I'll create three random groups
one <- c(1,3,4,6,9,11,12,15,24,25)
two <- c(2,7,10,16,17,20,22,26,28,30)
three <- c(5,8,13,14,18,19,21,23,27,29)
## ^^This info could also be in an excel file that you read in.
groupnames_expertise <- c("Group One", "Group Two", "Group Three")
grouplist_expertise <- list(one, two, three)
names(grouplist_expertise) <- groupnames_expertise
detach("package:xlsx", unload = TRUE)
groupmapdata_fxn <- function(individualmapdata, groupnames, grouplist){
## Then we'll create a workbook for each of the descriptive data types
descrip_types <- c("Density", "Number of Components", "Number of Connections", "CN", "Complexity")
datacompwb <- createWorkbook()
for (i in 1:as.numeric(length(descrip_types))){
addWorksheet(wb=datacompwb, sheetName=descrip_types[i])
}
### for each group and each metric we'll calculate the number of participants in each group, and the minimum value, maximum value, mean, and standard deviation for the given metric
datacomp_colnames <- c("count", "min", "max", "mean", "std") # set the names of each measure
datacomp_rownames <- groupnames # names of each group
datacomp_groups <- grouplist #vector of each group of participants
names(datacomp_groups) <- datacomp_rownames
changed.data <- datacomp_groups # make a list to be altered
## Pull the metric data for each participant within the group
for (j in 1:as.numeric(length(datacomp_groups))){
changed.data[[j]] <- individualmapdata[individualmapdata[,1] %in% datacomp_groups[[j]] == TRUE,]
}
## Do the calculations for min, max, mean, and standard deviation and fill the workbook
for (i in 1:as.numeric(length(descrip_types))){
datacomp <- matrix(nrow=as.numeric(length(datacomp_rownames)), ncol = as.numeric(length(datacomp_colnames)))
colnames(datacomp) <- datacomp_colnames
row.names(datacomp) <- datacomp_rownames
for (r in 1:as.numeric(length(datacomp_groups))){
if (as.numeric(length(datacomp_groups[[r]])) == 1) {
current <- t(as.matrix(changed.data[[r]]))
hold <- as.numeric(current[,descrip_types[i]])
}
else {
hold <- as.numeric(changed.data[[r]][,descrip_types[i]])
}
datacomp[r,] <- c(as.numeric(length(hold)), min(hold), max(hold), mean(hold), sd(hold))
}
writeData(wb=datacompwb, sheet = descrip_types[i], x = datacomp, colNames = T, rowNames = T)
}
print(datacompwb)
}
groupdata_expertise <- groupmapdata_fxn(individualmapdata_individuals, groupnames_expertise, grouplist_expertise)
saveWorkbook(groupdata_expertise, "Example Data - Descriptive Data by Expertise Group.xlsx")
#### COMPONENTS ####
## There are several pieces of information about components that could be useful
## List of components within each map
conceptlist <- lapply(X=1:as.numeric(length(sheetnames)), FUN = function(x) {
get.vertex.attribute(descripdatalist_individuals[[x]], name = "name")
})
names(conceptlist) <- sheetnames
# Determining the unique concepts
allconcepts <- unlist(conceptlist)
uniqueconcepts <- unique(allconcepts)
### Make an excel sheet of all the unique concepts within the maps
write.xlsx(uniqueconcepts, "Unique Concepts.xlsx")
### Compare the frequency of concepts between groups
components_fxn <- function(descripdatalist, groupnames, grouplist){
datacomp_groups <- grouplist
sheetnames <- names(descripdatalist)
frequency_types <- c("Net", "Proportion of Component", "Proportion of Group")
componentwb <- createWorkbook()
for (i in 1:as.numeric(length(frequency_types))){
addWorksheet(wb=componentwb, sheetName=frequency_types[i])
}
## Net Count - just the number of participants in each group that mentioned each concept
netconcepts <- matrix(data=NA, nrow=as.numeric(length(uniqueconcepts)), ncol=(2+as.numeric(length(datacomp_groups))))
netconcepts[,1] <- uniqueconcepts
colnames(netconcepts) <- c("Component", "Count", groupnames)
for (i in 1:as.numeric(length(uniqueconcepts))){
netconcepts[i,2] <- sum(allconcepts == netconcepts[i,1])
}
for (j in 1:as.numeric(length(datacomp_groups))){
pull <- which(as.numeric(names(conceptlist)) %in% datacomp_groups[[j]])
newlist <- vector(mode = "list", length = as.numeric(length(pull)))
for (i in 1:as.numeric(length(pull))){
newlist[[i]] <- conceptlist[[pull[i]]]
}
concepts <- unlist(newlist)
for (r in 1:as.numeric(length(uniqueconcepts)))
netconcepts[r,j+2] <- sum(concepts == netconcepts[r,1])
}
writeData(wb=componentwb, sheet = frequency_types[1], x = netconcepts, colNames = T, rowNames = F)
## Proportion of Component - how each component is represented across groups, so the rows will sum to 1
fullconcepts <- matrix(data=NA, nrow=as.numeric(length(uniqueconcepts)), ncol=(2+as.numeric(length(datacomp_groups))))
fullconcepts[,1] <- uniqueconcepts
colnames(fullconcepts) <- c("Component", "Count", groupnames)
for (i in 1:as.numeric(length(uniqueconcepts))){
fullconcepts[i,2] <- sum(allconcepts == fullconcepts[i,1])
}
for (j in 1:as.numeric(length(datacomp_groups))){
pull <- which(as.numeric(names(conceptlist)) %in% datacomp_groups[[j]])
newlist <- vector(mode = "list", length = as.numeric(length(pull)))
for (i in 1:as.numeric(length(pull))){
newlist[[i]] <- conceptlist[[pull[i]]]
}
concepts <- unlist(newlist)
for (r in 1:as.numeric(length(uniqueconcepts)))
fullconcepts[r,j+2] <- (sum(concepts == fullconcepts[r,1]))/as.numeric(fullconcepts[r,2])
}
writeData(wb=componentwb, sheet = frequency_types[2], x = fullconcepts, colNames = T, rowNames = F)
## Proportion of Group - distribution of concepts within the group, so columns will sum to 1
fullconcepts_proportion <- matrix(data=NA, nrow=as.numeric(length(uniqueconcepts)), ncol=(2+as.numeric(length(datacomp_groups))))
fullconcepts_proportion[,1] <- uniqueconcepts
colnames(fullconcepts_proportion) <- c("Component", "Count", groupnames)
for (i in 1:as.numeric(length(uniqueconcepts))){
fullconcepts_proportion[i,2] <- sum(allconcepts == fullconcepts_proportion[i,1])
}
for (j in 1:as.numeric(length(datacomp_groups))){
pull <- which(as.numeric(names(conceptlist)) %in% datacomp_groups[[j]])
newlist <- vector(mode = "list", length = as.numeric(length(pull)))
for (i in 1:as.numeric(length(pull))){
newlist[[i]] <- conceptlist[[pull[i]]]
}
concepts <- unlist(newlist)
for (r in 1:as.numeric(length(uniqueconcepts)))
fullconcepts_proportion[r,j+2] <- (sum(concepts == fullconcepts_proportion[r,1]))/as.numeric(length(datacomp_groups[[j]]))
}
writeData(wb=componentwb, sheet = frequency_types[3], x = fullconcepts_proportion, colNames = T, rowNames = F)
print(componentwb)
}
components_expertise <- components_fxn(descripdatalist_individuals, groupnames_expertise, grouplist_expertise)
saveWorkbook(components_expertise, "Example Data - Concepts and Frequency by Expertise Group.xlsx")
#### NODE CENTRALITY ####
# input data should be your list of all the data from the descriptive data function (graph objects)
# inputconcepts should be a vector/list of all the final concepts there are
nodedataextractionfxn <- function(inputdata, inputconcepts, datatype){
rownum <- as.numeric(length(inputconcepts))
colnum <- as.numeric(length(inputdata))
indegreedata <- matrix(data=NA, nrow=(rownum), ncol=(colnum+2))
indegreedata[1:(rownum),1] <- inputconcepts
colnames(indegreedata) <- c("Concepts", names(inputdata), "Mean")
for (j in 1:colnum){
same <- match(get.vertex.attribute(inputdata[[j]], name = "name"), indegreedata[,1])
numvals <- as.numeric(length(same))
for (i in 1:numvals){
indegreedata[same[i], (j+1)] <- get.vertex.attribute(inputdata[[j]], name = datatype)[i]
}
}
for (r in 1:rownum){
indegreedata[r, (colnum+2)] <- mean(as.numeric(indegreedata[r,2:(colnum+1)]), na.rm = TRUE)
}
print(indegreedata)
}
nodecentrality_fxn <- function(descripdatalist, groupnames, grouplist){
datacomp_groups <- grouplist
datacomp_rownames <- groupnames
sheetnames <- names(descripdatalist)
## Now we'll use that funciton for all the different kinds of centrality
absweightedcollected <- nodedataextractionfxn(descripdatalist, uniqueconcepts, "absolute weighted degree centrality")
indegreecollected <- nodedataextractionfxn(descripdatalist, uniqueconcepts, "indegree centrality")
outdegreecollected <- nodedataextractionfxn(descripdatalist, uniqueconcepts, "outdegree centrality")
degreecentcollected <- nodedataextractionfxn(descripdatalist, uniqueconcepts, "degree centrality")
normindegreecollected <- nodedataextractionfxn(descripdatalist, uniqueconcepts, "normalized indegree centrality")
normoutdegreecollected <- nodedataextractionfxn(descripdatalist, uniqueconcepts, "normalized outdegree centrality")
normdegreecentcollected <- nodedataextractionfxn(descripdatalist, uniqueconcepts, "normalized degree centrality")
closenesscentcollected <- nodedataextractionfxn(descripdatalist, uniqueconcepts, "closeness centrality")
betweennesscentcollected <- nodedataextractionfxn(descripdatalist, uniqueconcepts, "betweenness centrality")
### Exporting centrality data
centwb <- createWorkbook()
centralityattributelist <- c("abs weighted degree centrality", "degree centrality", "indegree centrality", "outdegree centrality", "normalized degree centrality", "normalized indegree centrality", "normalized outdegree centrality", "closeness centrality", "betweenness centrality")
addWorksheet(centwb, sheetName = "Summary")
for (i in 1:as.numeric(length(centralityattributelist))){
addWorksheet(wb=centwb, sheetName=centralityattributelist[i])
}
allcentdata <- list(absweightedcollected, degreecentcollected, indegreecollected, outdegreecollected, normdegreecentcollected, normindegreecollected, normoutdegreecollected, closenesscentcollected, betweennesscentcollected)
names(allcentdata) <- centralityattributelist
for (i in 1:as.numeric(length(centralityattributelist))){
writeData(wb=centwb, sheet = centralityattributelist[i], x= allcentdata[[i]])
}
## Now we'll make a summary page that compares by group
testcolnames <- unlist(lapply(X=centralityattributelist, FUN=paste, sep= " - ", datacomp_rownames))
summarycentdata <- matrix(ncol=as.numeric(length(testcolnames)), nrow = as.numeric(length(uniqueconcepts)))
row.names(summarycentdata) <- uniqueconcepts
colnames(summarycentdata) <- testcolnames
for (j in 1:as.numeric(length(centralityattributelist))) {
for (r in 1:as.numeric(length(datacomp_groups))) {
if (as.numeric(length(datacomp_groups[[r]] == 1))) {
test <- (as.matrix(allcentdata[[j]][, which(colnames(allcentdata[[j]]) %in% datacomp_groups[[r]] == T)]))
}
else {
test <- allcentdata[[j]][, which(colnames(allcentdata[[j]]) %in% datacomp_groups[[r]] == T)]
}
num.test <- apply(test, 2, as.numeric)
means <- rowMeans(num.test, na.rm=T)
summarycentdata[, (j-1)*as.numeric(length(datacomp_rownames))+r] <- means
}
}
writeData(wb=centwb, sheet = "Summary", x= summarycentdata, colNames = T, rowNames = T)
print(centwb)
}
centrality_expertise <- nodecentrality_fxn(descripdatalist_individuals, groupnames_expertise, grouplist_expertise)
## Write File ##
saveWorkbook(centrality_expertise, "Example Data - Node Centrality Data By Expertise Group.xlsx")
#### NODE TYPE ####
# Comparing whether nodes are Ordinary, Receivers, or Drivers
nodeTYPEextractionfxn <- function(inputdata, inputconcepts){
rownum <- as.numeric(length(inputconcepts))
colnum <- as.numeric(length(inputdata))
indegreedata <- matrix(data=NA, nrow=(rownum), ncol=(colnum+4))
indegreedata[1:(rownum),1] <- inputconcepts
colnames(indegreedata) <- c("Concepts", names(inputdata), "Ordinary", "Receiver", "Driver")
for (j in 1:colnum){
same <- match(get.vertex.attribute(inputdata[[j]], name = "name"), indegreedata[,1])
numvals <- as.numeric(length(same))
for (i in 1:numvals){
indegreedata[same[i], (j+1)] <- get.vertex.attribute(inputdata[[j]], name = "type")[i]
}
}
indegreedata[is.na(indegreedata)] <- "NA"
for (r in 1:rownum){
indegreedata[r, (colnum+2)] <- (sum(indegreedata[r, 1:colnum+1] == "Ordinary"))
indegreedata[r, (colnum+3)] <- (sum(indegreedata[r, 1:colnum+1] == "Receiver"))
indegreedata[r, (colnum+4)] <- (sum(indegreedata[r, 1:colnum+1] == "Driver"))
}
print(indegreedata)
}
nodetype_fxn <- function(descripdatalist, groupnames, grouplist){
datacomp_groups <- grouplist
datacomp_rownames <- groupnames
sheetnames <- names(descripdatalist)
typewb <- createWorkbook()
addWorksheet(typewb, sheetName = "Node Types")
addWorksheet(typewb, sheetName = "Avg Percent by Group")
addWorksheet(typewb, sheetName = "Group Summary")
nodetypescollected <- nodeTYPEextractionfxn(descripdatalist, uniqueconcepts)
nodetypescollected_percent <- nodetypescollected
typesums <- vector(mode="numeric", length=as.numeric(length(uniqueconcepts)))
ncols_nodetypes <- as.numeric(ncol(nodetypescollected_percent))
for (i in 1:as.numeric(length(uniqueconcepts))){
typesums[i] <- sum(as.numeric(nodetypescollected[i,(ncols_nodetypes-2):ncols_nodetypes]))
nodetypescollected_percent[i,ncols_nodetypes-2] <- (as.numeric(nodetypescollected[i,ncols_nodetypes-2]))/typesums[i]
nodetypescollected_percent[i,ncols_nodetypes-1] <- (as.numeric(nodetypescollected[i,ncols_nodetypes-1]))/typesums[i]
nodetypescollected_percent[i,ncols_nodetypes] <- (as.numeric(nodetypescollected[i,ncols_nodetypes]))/typesums[i]
}
finalnodedata <- cbind(nodetypescollected, nodetypescollected_percent[,(ncol(nodetypescollected_percent) - 2):ncol(nodetypescollected_percent)])
writeData(wb=typewb, sheet = "Node Types", x= finalnodedata, colNames = T, rowNames = F)
## Average percent by group
typecolnames <- unlist(lapply(X=c("Ordinary", "Receiver", "Driver"), FUN=paste, sep= " - ", datacomp_rownames))
percentmat <- matrix(nrow=as.numeric(length(uniqueconcepts)), ncol = as.numeric(length(typecolnames)))
colnames(percentmat) <- typecolnames
rownames(percentmat) <- uniqueconcepts
for (r in 1:as.numeric(length(datacomp_groups))) {
if (as.numeric(length(datacomp_groups[[r]] == 1))) {
test <- (as.matrix(nodetypescollected[, which(colnames(nodetypescollected) %in% datacomp_groups[[r]] == T)]))
}
else {
test <- nodetypescollected[, which(colnames(nodetypescollected) %in% datacomp_groups[[r]] == T)]
}
sumord <- vector(mode = "numeric", length=as.numeric(length(uniqueconcepts)))
sumrec <- vector(mode = "numeric", length=as.numeric(length(uniqueconcepts)))
sumdriv <- vector(mode = "numeric", length=as.numeric(length(uniqueconcepts)))
for (i in 1:as.numeric(length(uniqueconcepts))){
sumord[i] <- (sum(test[i,] == "Ordinary"))/as.numeric(length(datacomp_groups[[r]]))
sumrec[i] <- (sum(test[i,] == "Receiver"))/as.numeric(length(datacomp_groups[[r]]))
sumdriv[i] <- (sum(test[i,] == "Driver"))/as.numeric(length(datacomp_groups[[r]]))
}
for (j in 1:as.numeric(length(uniqueconcepts))) {
percentmat[j,1+(r-1)*3] <- mean(sumord[j])
percentmat[j,2+(r-1)*3] <- mean(sumrec[j])
percentmat[j,3+(r-1)*3] <- mean(sumdriv[j])
}
}
writeData(wb=typewb, sheet = "Avg Percent by Group", x= percentmat, colNames = T, rowNames = T)
## Summary by Group
summarymat <- matrix(nrow=as.numeric(length(datacomp_groups)), ncol = 6)
colnames(summarymat) <- c("Ordinary - Norm", "Receiver - Norm", "Driver - Norm", "Ordinary - Percent", "Receiver - Percent", "Driver - Percent")
rownames(summarymat) <- datacomp_rownames
for (r in 1:as.numeric(length(datacomp_groups))) {
if (as.numeric(length(datacomp_groups[[r]] == 1))) {
test <- (as.matrix(nodetypescollected[, which(colnames(nodetypescollected) %in% datacomp_groups[[r]] == T)]))
}
else {
test <- nodetypescollected[, which(colnames(nodetypescollected) %in% datacomp_groups[[r]] == T)]
test <- nodetypescollected[, which(colnames(nodetypescollected) %in% datacomp_groups[[3]] == T)]
}
sumord <- vector(mode = "numeric", length=as.numeric(length(uniqueconcepts)))
sumrec <- vector(mode = "numeric", length=as.numeric(length(uniqueconcepts)))
sumdriv <- vector(mode = "numeric", length=as.numeric(length(uniqueconcepts)))
for (i in 1:as.numeric(length(uniqueconcepts))){
sumord[i] <- (sum(test[i,] == "Ordinary"))
sumrec[i] <- (sum(test[i,] == "Receiver"))
sumdriv[i] <- (sum(test[i,] == "Driver"))
}
total <- sum(sumord, sumrec, sumdriv)
summarymat[r,1] <- sum(sumord)/as.numeric(length(datacomp_groups[[r]]))
summarymat[r,2] <- sum(sumrec)/as.numeric(length(datacomp_groups[[r]]))
summarymat[r,3] <- sum(sumdriv)/as.numeric(length(datacomp_groups[[r]]))
summarymat[r,4] <- sum(sumord)/total
summarymat[r,5] <- sum(sumrec)/total
summarymat[r,6] <- sum(sumdriv)/total
}
writeData(wb=typewb, sheet = "Group Summary", x= summarymat, colNames = T, rowNames = T)
print(typewb)
}
nodetype_expertise <- nodetype_fxn(descripdatalist_individuals, groupnames_expertise, grouplist_expertise)
saveWorkbook(nodetype_expertise, "Example Data - Node Type Data By Expertise Group.xlsx")
#### EDGES ####
edges_fxn <- function(descripdatalist){
sheetnames <- names(descripdatalist)
# List of Edges
edgelists <- list()
for (i in 1:as.numeric(length(sheetnames))){
edgelists[[i]] <- get.graph.attribute(descripdatalist[[i]], name = "Edge List")
}
names(edgelists) <- sheetnames
#Export edge data
alledges <- do.call(rbind, Map(data.frame, edgelists))
edgerownames <- as.numeric(rownames(alledges))
ID <- trunc(edgerownames, digits=0)
alledgedata <- cbind(ID, alledges)
print(alledgedata)
}
edgedata_individualmaps <- edges_fxn(descripdatalist_individuals)
## You might not need this information, I used "Text Edges" for transcript coding
edgewb_fxn <- function(edgedata){
edgewb <- createWorkbook()
addWorksheet(edgewb, sheetName = "All Edges with IDs")
addWorksheet(edgewb, sheetName = "Edge List")
addWorksheet(edgewb, sheetName = "All Text Edges")
addWorksheet(edgewb, sheetName = "Text Edges with Weights")
alledges <- edgedata[,-1]
writeData(wb=edgewb, sheet = "All Edges with IDs", x= edgedata, colNames = T, rowNames = T)
writeData(wb=edgewb, sheet = "Edge List", x= alledges, colNames = T, rowNames = T)
textonlyedges <- paste(alledges[,1], "TO", alledges[,2])
textonlyedges_weights <- cbind(textonlyedges, edgedata[,4])
writeData(wb=edgewb, sheet = "All Text Edges", x= textonlyedges, colNames = T, rowNames = T)
writeData(wb=edgewb, sheet = "Text Edges with Weights", x= textonlyedges_weights, colNames = T, rowNames = T)
print(edgewb)
}
edgewb_individualmaps <- edgewb_fxn(edgedata_individualmaps)
## Write File ##
saveWorkbook(edgewb_individualmaps, "Example Data - Edge Data for Individual Maps.xlsx")
#### AGGREGATION INTO GROUPS ####
## To aggregate groups we want to use the means of the connections
### There are a few base functions we'll use in a larger analysis function
sheetgroupingfxn <- function(y, r){
groupmats <- lapply(X=y, function(x){r[r$ID == x,]})
names(groupmats) <- y
print(groupmats)
}
groupconndatafxn_wozero <- function(groupedgedata){
nsheets <- as.numeric(length(groupedgedata))
allfromedges <- unlist(lapply(X=1:nsheets, function(x){groupedgedata[[x]]$from}))
alltoedges <- unlist(lapply(X=1:nsheets, function(x){groupedgedata[[x]]$to}))
allweights <- unlist(lapply(X=1:nsheets, function(x){groupedgedata[[x]]$weights}))
allconns <- paste(allfromedges, "TO", alltoedges)
conns_weights <- cbind(allconns, allweights)
uniqueconns <- unique(allconns)
conndata <- matrix(ncol = 6, nrow = as.numeric(length(uniqueconns)))
conndata[,1] <- uniqueconns
colnames(conndata) <- c("Edge", "Min", "Max", "Mean", "StDev", "Count")
for (i in 1:as.numeric(length(uniqueconns))) {
hold <- as.numeric(conns_weights[(conns_weights[,1] %in% uniqueconns[i] == TRUE), 2])
conndata[i,2:6] <- c(max(hold), min(hold), mean(hold), sd(hold), as.numeric(length(hold)))
}
print(conndata)
}
conndatafxn_agg_nozeros <- function(edgedata, grouplist){
nedges <- as.numeric(length(edgedata$from))
fromedges <- unlist(lapply(X=1:nedges, function(x){edgedata$from}))
toedges <- unlist(lapply(X=1:nedges, function(x){edgedata$to}))
weights <- unlist(lapply(X=1:nedges, function(x){edgedata$weights}))
conns <- paste(fromedges, "TO", toedges)
finaluniqueconns <- unique(conns)
conndata <- matrix(nrow=as.numeric(length(finaluniqueconns)), ncol=as.numeric(length(grouplist))+2)
colnames(conndata) <- c("from", "to", names(grouplist))
connedgelist <- strsplit(finaluniqueconns, " TO ")
for (i in 1:as.numeric(length(connedgelist))){
conndata[i,1] <- connedgelist[[i]][1]
conndata[i,2] <- connedgelist[[i]][2]
}
for (r in 1:as.numeric(length(grouplist))){
groupedgedata <- lapply(X=grouplist[[r]], function(x){edgedata[edgedata$ID == x,]})
names(groupedgedata) <- grouplist[[r]]
nsheets <- as.numeric(length(grouplist[[r]]))
allfromedges <- unlist(lapply(X=1:nsheets, function(x){groupedgedata[[x]]$from}))
alltoedges <- unlist(lapply(X=1:nsheets, function(x){groupedgedata[[x]]$to}))
allweights <- unlist(lapply(X=1:nsheets, function(x){groupedgedata[[x]]$weights}))
allconns <- paste(allfromedges, "TO", alltoedges)
conns_weights <- cbind(allconns, allweights)
for (t in 1:as.numeric(length(finaluniqueconns))) {
hold <- as.numeric(conns_weights[(conns_weights[,1] %in% finaluniqueconns[t] == TRUE), 2])
conndata[t,2+r] <- mean(as.numeric(hold))
}
}
print(conndata)
}
### Combined into one function
analysis.megafxn <- function(matrix_data, grouplist, edgedata){
ngroups <- as.numeric(length(grouplist))
expertisegroups <- names(grouplist)
alledgedata <- edgedata
#### AGGREGATION INTO GROUPS ####
group_edges <- vector("list", ngroups)
for (i in 1:ngroups){
group_edges[[i]] <- sheetgroupingfxn(grouplist[[i]], alledgedata)
}
## Then use that edge data to calculate some information
#### Determines max, min, mean, standard deviation, and number of mentions
#### ONLY includes edges that exist, no zeros for no edges
group_conndata_list <- vector("list", ngroups)
for (i in 1:ngroups){
group_conndata_list[[i]] <- groupconndatafxn_wozero(group_edges[[i]])
}
## Now a comparison across groups
conndata_groups <- conndatafxn_agg_nozeros(alledgedata, grouplist)
conndata_groups[which(conndata_groups == "NaN")] <- 0
medians <- vector(mode = "numeric", length = nrow(conndata_groups))
for (i in 1:nrow(conndata_groups)) {
medians[i] <- median(as.numeric(conndata_groups[i,3:(2+as.numeric(length(grouplist)))]))
}
conndata_groups_withagg <- cbind(conndata_groups, medians)
## First make edgelists of just the means per group & median of metamodel
edgelists_groups <- vector("list", length=(as.numeric(length(grouplist))+1))
names(edgelists_groups) <- c(expertisegroups, "Metamodel")
for (i in 1:as.numeric(length(edgelists_groups))){
edgelists_groups[[i]] <- cbind(conndata_groups_withagg[,1], conndata_groups_withagg[,2], conndata_groups_withagg[,2+i])
edgelists_groups[[i]] <- edgelists_groups[[i]][edgelists_groups[[i]][,3] !=0,]
colnames(edgelists_groups[[i]]) <- c("from", "to", "weights")
}
## Now we can make graphs to mess with in R and adjacency matrices for MentalModeler
# Graph
graphs_groups <- vector("list", length=(as.numeric(length(grouplist))+1))
names(graphs_groups) <- c(names(grouplist), "Metamodel")
for (i in 1:as.numeric(length(graphs_groups))){
graphs_groups[[i]] <- graph_from_edgelist(edgelists_groups[[i]][,1:2], directed=T)
E(graphs_groups[[i]])$weight <- as.numeric(edgelists_groups[[i]][,3])
}
print(graphs_groups)
}
## Run the function - the output will be aggregated group graphs and a metamodel graph as list objects
expertisegroups <- analysis.megafxn(matrix_data, grouplist_expertise, edgedata_individualmaps)
#### PCA ####
library(factoextra)
library(cluster)
### Read in the categorization
themes <- read.xlsx("Example Random Categorization.xlsx")
### Fxns we need
nodedataextractionfxn <- function(inputdata, inputconcepts, datatype){
rownum <- as.numeric(length(inputconcepts))
colnum <- as.numeric(length(inputdata))
indegreedata <- matrix(data=NA, nrow=(rownum), ncol=(colnum+2))
indegreedata[1:(rownum),1] <- inputconcepts
colnames(indegreedata) <- c("Concepts", names(inputdata), "Mean")
for (j in 1:colnum){
same <- match(get.vertex.attribute(inputdata[[j]], name = "name"), indegreedata[,1])
numvals <- as.numeric(length(same))
for (i in 1:numvals){
indegreedata[same[i], (j+1)] <- get.vertex.attribute(inputdata[[j]], name = datatype)[i]
}
}
for (r in 1:rownum){
indegreedata[r, (colnum+2)] <- mean(as.numeric(indegreedata[r,2:(colnum+1)]), na.rm = TRUE)
}
print(indegreedata)
}
#### Two Big PCA Functions ####
pca.dataprep <- function(descripdatalist, themes.data){
uniquethemes <- unique(themes.data$Category)
cent_all_themes <- matrix(ncol=nsheets, nrow=as.numeric(length(uniquethemes)))
row.names(cent_all_themes) <- uniquethemes
colnames(cent_all_themes) <- sheetnames
### Get a list of unique concepts
conceptlist <- lapply(X=1:as.numeric(length(sheetnames)), FUN = function(x) {
get.vertex.attribute(descripdatalist[[x]], name = "name")
})
names(conceptlist) <- sheetnames
allconcepts <- unlist(conceptlist)
uniqueconcepts <- unique(allconcepts)
##### Centrality Calculation
absweightedcollected_rounded <- nodedataextractionfxn(descripdatalist, uniqueconcepts, "absolute weighted degree centrality")
### Calc
for (j in 1:nsheets){
sumcent <- sum(as.numeric(absweightedcollected_rounded[,1+j]), na.rm=T)
for (i in 1:as.numeric(length(uniquethemes))) {
hold <- themes.data[which(themes.data$Category == uniquethemes[i]),1]
vals <- as.numeric(absweightedcollected_rounded[which(absweightedcollected_rounded[,1] %in% hold == T),1+j])
add <- sum(vals, na.rm = T)
cent_all_themes[i,j] <- add/sumcent
}
}
## Transpose: t(x)
transposed.data <- t(cent_all_themes)
raw.data <- transposed.data[,1:as.numeric(ncol(transposed.data))]
row.names(raw.data) <- sheetnames
print(raw.data)
}
pca.groups <- function(raw.data, num.clust, alledgedata, sheetnames){
raw.pca <- prcomp(raw.data, scale. = T)
rank.pca <- as.numeric(length(which(get_eig(raw.pca)$eigenvalue >= 1)))
pca.dim <- prcomp(raw.data, scale=T, rank. = rank.pca)
pca.dim.ind <- get_pca_ind(pca.dim)
ward.pca <- agnes(pca.dim.ind$coord, method="ward")
pltree(ward.pca, cex = 0.6, hang = -1, main = "Dendrogram")
clust.raw <- cutree(ward.pca, k = num.clust)
groups.raw <- cbind(sheetnames, clust.raw)
pca.grouplist <- vector("list", num.clust)
for (i in 1:num.clust){
pca.grouplist[[i]] <- groups.raw[which(groups.raw[,2] == i),1]
names(pca.grouplist)[i] <- i
}
print(pca.grouplist)
conndata.pca <- conndatafxn_agg_nozeros(alledgedata, pca.grouplist)
conndata.pca.zeros <- conndata.pca
conndata.pca.zeros[which(conndata.pca.zeros == "NaN")] <- 0
medians.pca <- vector(mode = "numeric", length = nrow(conndata.pca.zeros))
for (i in 1:nrow(conndata.pca.zeros)) {
medians.pca[i] <- median(as.numeric(conndata.pca.zeros[i,3:(2+as.numeric(length(pca.grouplist)))]))
}
conndata.pca.withagg <- cbind(conndata.pca.zeros, medians.pca)
#edge lists
edgelists_groups <- vector("list", length=num.clust+1)
names(edgelists_groups) <- c(1:num.clust, "Metamodel")
for (i in 1:as.numeric(length(edgelists_groups))){
edgelists_groups[[i]] <- cbind(conndata.pca.withagg[,1], conndata.pca.withagg[,2], conndata.pca.withagg[,2+i])
edgelists_groups[[i]] <- edgelists_groups[[i]][edgelists_groups[[i]][,3] !=0,]
colnames(edgelists_groups[[i]]) <- c("from", "to", "weights")
}
## Now we can make graphs to mess with in R and adjacency matrices for MentalModelers
# Graph
graphs_groups <- vector("list", length=(num.clust+1))
names(graphs_groups) <- c(1:num.clust, "Metamodel")
for (i in 1:as.numeric(length(graphs_groups))){
graphs_groups[[i]] <- graph_from_edgelist(edgelists_groups[[i]][,1:2], directed=T)
E(graphs_groups[[i]])$weight <- as.numeric(edgelists_groups[[i]][,3])
}
print(graphs_groups)
}
## Run it
pca.data <- pca.dataprep(descripdatalist_individuals, themes)
pca.clustered <- pca.groups(pca.data, 4, edgedata_individualmaps, sheetnames) ## Use your best guess for the number of clusters the first time you run this, then check out the dendrogram to really decide
#### Loadings, if you need them
forloadings.pca <- prcomp(pca.data, scale. = T)
rank.pca <- as.numeric(length(which(get_eig(forloadings.pca)$eigenvalue >= 1)))
pca.dim <- prcomp(pca.data, scale=T, rank. = rank.pca)
loadings <- pca.dim$rotation
write.csv(loadings, "Example Data - PCA Loadings.csv")
#### CONVERSION TO ADJACENCY MATRICIES for Mental Modeler ####
make.adjmats.fxn <- function(graphnames, graphs_groups){
adjmat_groups <- vector("list", length=(as.numeric(length(graphnames))))
names(adjmat_groups) <- graphnames
adjmat_names <- graphnames
for (i in 1:as.numeric(length(adjmat_groups))){
adjmat_groups[[i]] <- get.adjacency(graphs_groups[[i]], sparse = FALSE, attr="weight")
write.csv(adjmat_groups[[i]], file=paste0(adjmat_names[i], "_Adjmat", ".csv"))
}
}
expertise_graphnames <- c(paste("Expertise", groupnames_expertise, sep = " - "), "Expertise - Metamodel")
make.adjmats.fxn(expertise_graphnames, expertisegroups)
pca_graphnames <- paste("PCA", names(pca.clustered), sep = " - ")
make.adjmats.fxn(pca_graphnames, pca.clustered)
#### PCA Group Data ####
## We can pull how the maps were grouped during the PCA using print out of the pca.clustered function, or pull the pieces out to hold it as vectors
pca.one <- c(1,2,7,8,13,17,26,30)
pca.two <- c(3,10,11,14,15,19,20,22,23,25)
pca.three <- c(4,6,9,16,18,28)
pca.four <- c(5,12,21,24,27,29)
### Then any of the processes done for the expertise data can be re-run, I'll do the descriptive data just as an example
## We'll create a workbook for each of the descriptive data types
groupnames_PCA <- c("PCA One", "PCA Two", "PCA Three", "PCA Four") # names of each group
grouplist_PCA <- list(pca.one, pca.two, pca.three, pca.four) #vector of each group of participants
names(grouplist_PCA ) <- groupnames_PCA
groupdata_PCA <- groupmapdata_fxn(individualmapdata_individuals, groupnames_PCA, grouplist_PCA)
saveWorkbook(groupdata_PCA, "Example Data - Descriptive Data by PCA Group.xlsx")