Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c) 2019 Anamarija Lonza
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  6. # to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. # and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. #
  9. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  10. #
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  13. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  14.  
  15. setwd("C:/Users/Nana/Desktop/ZA ANALIZU NA ENG/NOVO")
  16.  
  17.  
  18. source("libraries.r")
  19.  
  20. itemNumber <- 58
  21. iterations <- 1000
  22. seeds <- c()
  23.  
  24. #calculates spinglass analysis for a graph using a set seed
  25. calculate_spinglass_with_seed <- function(igraph)
  26. {
  27. seeds <- c(seeds, .Random.seed)
  28. spinglass <- spinglass.community(igraph)
  29. return(spinglass)
  30. }
  31.  
  32. #returns a spinglass membership for a graph using a set seed
  33. membership_for_spinglass_with_seed <- function(igraph)
  34. {
  35. spinglass <- calculate_spinglass_with_seed(igraph)
  36. membership <- spinglass$membership
  37. return(membership)
  38. }
  39.  
  40. #gives a matrix with the number of clusters obtained in each iteration
  41. create_groupings_matrix <- function(igraph)
  42. {
  43. groupingsMatrix <- matrix(NA, nrow = 1, ncol = iterations)
  44.  
  45. for(i in 1:iterations)
  46. {
  47. membership <- membership_for_spinglass_with_seed(igraph)
  48. groupingsMatrix[1, i] <- max(membership)
  49. }
  50.  
  51. return(groupingsMatrix)
  52. }
  53.  
  54. #returns the number of clusters which shows up in the most iterations
  55. determine_most_common_grouping <- function(groupingsMatrix)
  56. {
  57. mean(as.vector(groupingsMatrix))
  58. max(as.vector(groupingsMatrix))
  59. min(as.vector(groupingsMatrix))
  60.  
  61. mostCommonGrouping <- median(as.vector(groupingsMatrix))
  62. return(mostCommonGrouping)
  63. }
  64.  
  65. #for all groupings - if a grouping is one of the most common groupings, add its index to list
  66. find_all_most_common_grouping_indexes <- function(groupingsMatrix, mostCommonGrouping)
  67. {
  68. mostCommonGroupingIndexes <- list()
  69.  
  70. for(i in 1:ncol(groupingsMatrix))
  71. if(groupingsMatrix[1, i] == mostCommonGrouping)
  72. mostCommonGroupingIndexes <- c(mostCommonGroupingIndexes, i)
  73.  
  74. return(mostCommonGroupingIndexes)
  75. }
  76.  
  77. #makes a new matrix consisting only of spinglass solutions with the most common number of clusters
  78. #that we determined using the determine_most_common_grouping function
  79. create_matrix_of_all_solutions_with_most_common_grouping <- function(igraph, groupingsMatrix, mostCommonGrouping)
  80. {
  81. mostCommonGroupingIndexes <- find_all_most_common_grouping_indexes(groupingsMatrix, mostCommonGrouping)
  82.  
  83. totalGroupings <- length(mostCommonGroupingIndexes)
  84.  
  85. mostCommonGroupings <- matrix(NA, nrow = totalGroupings, ncol = itemNumber)
  86. count <- 1
  87.  
  88. for (i in mostCommonGroupingIndexes)
  89. {
  90. set.seed(seeds[i])
  91. membership <- membership_for_spinglass_with_seed(igraph)
  92.  
  93. mostCommonGroupings[count,] <- membership
  94. count <- count+1
  95. }
  96.  
  97. return(mostCommonGroupings)
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement