Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The MIT License (MIT)
- #
- # Copyright (c) 2019 Anamarija Lonza
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
- # to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
- # 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:
- #
- # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # 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,
- # 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.
- setwd("C:/Users/Nana/Desktop/ZA ANALIZU NA ENG/NOVO")
- source("libraries.r")
- itemNumber <- 58
- iterations <- 1000
- seeds <- c()
- #calculates spinglass analysis for a graph using a set seed
- calculate_spinglass_with_seed <- function(igraph)
- {
- seeds <- c(seeds, .Random.seed)
- spinglass <- spinglass.community(igraph)
- return(spinglass)
- }
- #returns a spinglass membership for a graph using a set seed
- membership_for_spinglass_with_seed <- function(igraph)
- {
- spinglass <- calculate_spinglass_with_seed(igraph)
- membership <- spinglass$membership
- return(membership)
- }
- #gives a matrix with the number of clusters obtained in each iteration
- create_groupings_matrix <- function(igraph)
- {
- groupingsMatrix <- matrix(NA, nrow = 1, ncol = iterations)
- for(i in 1:iterations)
- {
- membership <- membership_for_spinglass_with_seed(igraph)
- groupingsMatrix[1, i] <- max(membership)
- }
- return(groupingsMatrix)
- }
- #returns the number of clusters which shows up in the most iterations
- determine_most_common_grouping <- function(groupingsMatrix)
- {
- mean(as.vector(groupingsMatrix))
- max(as.vector(groupingsMatrix))
- min(as.vector(groupingsMatrix))
- mostCommonGrouping <- median(as.vector(groupingsMatrix))
- return(mostCommonGrouping)
- }
- #for all groupings - if a grouping is one of the most common groupings, add its index to list
- find_all_most_common_grouping_indexes <- function(groupingsMatrix, mostCommonGrouping)
- {
- mostCommonGroupingIndexes <- list()
- for(i in 1:ncol(groupingsMatrix))
- if(groupingsMatrix[1, i] == mostCommonGrouping)
- mostCommonGroupingIndexes <- c(mostCommonGroupingIndexes, i)
- return(mostCommonGroupingIndexes)
- }
- #makes a new matrix consisting only of spinglass solutions with the most common number of clusters
- #that we determined using the determine_most_common_grouping function
- create_matrix_of_all_solutions_with_most_common_grouping <- function(igraph, groupingsMatrix, mostCommonGrouping)
- {
- mostCommonGroupingIndexes <- find_all_most_common_grouping_indexes(groupingsMatrix, mostCommonGrouping)
- totalGroupings <- length(mostCommonGroupingIndexes)
- mostCommonGroupings <- matrix(NA, nrow = totalGroupings, ncol = itemNumber)
- count <- 1
- for (i in mostCommonGroupingIndexes)
- {
- set.seed(seeds[i])
- membership <- membership_for_spinglass_with_seed(igraph)
- mostCommonGroupings[count,] <- membership
- count <- count+1
- }
- return(mostCommonGroupings)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement