R/compute_centr_year.R

#' Compute eigenvector centrality up to and including a given year
#'
#' @param .graph .graph a tbl_graph object which is a wrapper around an igraph object.
#' In the context of this paper, the nodes are exhibition venues. These are linked by
#' exhibition participations of artists that connect exhibition places if an artists'
#' artwork were later shown in another exhibition venue.
#' @param .yr_centr_computed year of interest
#' @param .lower_boundary_yr a 4 digit number. First, .lower_boundary_yr is used to exclude \emph{edges} created by exhibition
#' participations that took place before before a given year. Removing edges from the
#' complete network can be used to measure the eigenvector centrality of nodes in a
#' specific time period. For example, is the Musée d'Art Moderne (Paris) which was
#' founded earlier than most other exhibition places not only central relative to the
#' network of 1900-2015, but also in 1980-1990?
#' Second, .lower_boundary_yr is used to exclude \emph{nodes} that show no exhibition
#' activity before a given year (first_exh_yr <= .lower_boundary_yr). 1980, for example,
#' will remove exhibition places which organised their first exhibition only in
#' @param .only_active .only_active remove ties toward exhibition venues that no longer show exhibition
#' activity. This is in line with the fact that those venues can no longer be an artist's
#' career incident.
#' @param .remove_loops if TRUE loops will be removed from the graph before computing
#' eigenvector centrality
#' @param .output_as_df return a dataframe
#'
#' @return a data frame of nodes with their eigenvector centrality
#' and the given reference years
#' @export
#' @examples
#' g <- create_example_graph()
#'
#' # compute eigenvector centrality for complete network
#' compute_centr_year(g, 2002)
#' # compute centrality for the network from 2000 up to and including 2001
#' compute_centr_year(g, 2001, .lower_boundary_yr = 2000)
compute_centr_year <- function(.graph, .yr_centr_computed, .lower_boundary_yr,
                               .only_active_place = F,
                               .only_n_past_exh = NULL,
                               .output_as_nodes_df = F,
                               .set_centrality_NA_if_inactive = T
                               ){

  # .yr_centr_computed <- 1990
  # .lower_boundary_yr <- 1900
  # .graph <- galsimple

  # SUBGRAPH
  # all edges will be taken into account that are between lower boundary and year of interest
  subgraph <- create_subgraph_year(.graph = .graph,
                                   .lower_boundary_yr = .lower_boundary_yr,
                                   .yr_of_interest = .yr_centr_computed,
                                   .only_n_past_exh = .only_n_past_exh,
                                   .output_as_edge_df = F)


  # COLUMN NAMES
  # name of column that contains a node's eigenvector centrality in a given year
  column_name_eigen <- paste0('eigen_', .yr_centr_computed, '_', .yr_centr_computed - .lower_boundary_yr)

  # name of column that contains a node's rank according to its eigenvector centrality
  # column_name_eigen_rank <- paste0('rank_', .yr_centr_computed, '_', .yr_centr_computed - .lower_boundary_yr)



  # NODES DATA FRAME
  output <- subgraph %>%

    tidygraph::activate(nodes) %>%

    # calculate eigenvector centrality of exhibitions venues per year
    tidygraph::mutate(!!column_name_eigen      := tidygraph::centrality_eigen(directed = F)
                      #,!!column_name_eigen_rank := .data[[column_name_eigen]] %>% rank()
                      ) %>%
    tidygraph::select(name, last_exh_yr, !! column_name_eigen#, !!column_name_eigen_rank
                      ) %>%
    tidygraph::mutate(name = as.integer(name))

  if(.set_centrality_NA_if_inactive == T){
    output <- output %>%
      activate(nodes) %>%
      tidygraph::mutate(!!column_name_eigen := ifelse(.yr_centr_computed > last_exh_yr,
                                                      NA, !!as.name(column_name_eigen))) %>%
      select(- last_exh_yr)
  }


  if(.output_as_nodes_df == T){

    output <- output %>%
      igraph::as_data_frame(what = "vertices") %>%
      dplyr::mutate(name = as.integer(name))

  }


  return(output)

}
Framus94/HierarchiesAndCareers documentation built on June 5, 2019, 8:52 a.m.