## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----inst, eval = FALSE-------------------------------------------------------
# install.packages("netmem")

## ----setup--------------------------------------------------------------------
library(netmem)

## ----data---------------------------------------------------------------------
data(campnet)
A <- campnet$network
gender <- campnet$attributes$gender # 1 = woman, 2 = man
role <- campnet$attributes$role # 1 = participant, 2 = instructor

matrix_report(A)

## ----underlying---------------------------------------------------------------
U <- pmax(A, t(A))

## ----describe-----------------------------------------------------------------
gen_density(A)
recip_coef(A)
trans_coef(A)
geo_summary(A)

## ----census-------------------------------------------------------------------
dyadic_census(A)
triad_uman(A)

## ----edgelist-----------------------------------------------------------------
E <- matrix_to_edgelist(A, digraph = TRUE)
head(E)
identical(edgelist_to_matrix(E, label = rownames(A)), A)

## ----centrality---------------------------------------------------------------
centrality <- data.frame(
  indegree = gen_degree(A, type = "in"),
  closeness = closeness_centrality(A, type = "in", harmonic = TRUE),
  betweenness = betweenness_centrality(A),
  eigenvector = eigenvector_centrality(A)$vector,
  pagerank = page_rank_centrality(A)
)
round(centrality, 2)

## ----centralization-----------------------------------------------------------
centrality_centralization(A, measure = "degree", digraph = TRUE, type = "in")$centralization
centrality_centralization(A, measure = "betweenness", digraph = TRUE)$centralization

## ----cohesion-----------------------------------------------------------------
components_id(A, mode = "weak")$size
k_core(U)
clique_max(U, min = 3)

## ----communities--------------------------------------------------------------
set.seed(18)
communities <- leiden(U)
communities$modularity
table(community = communities$partition, gender = gender)
table(community = communities$partition, role = role)

## ----homophily----------------------------------------------------------------
mix_matrix(A, gender)
ei_index(A, att = gender)
block_density(A, gender)

## ----core---------------------------------------------------------------------
set.seed(18)
core_periphery(U)[c("core", "periphery")]

## ----holes--------------------------------------------------------------------
round(structural_holes(A), 2)

## ----constraint---------------------------------------------------------------
eb_constraint(A, ego = "HOLLY", digraph = TRUE)

## ----cug----------------------------------------------------------------------
set.seed(18)
transitivity <- cug_test(A, trans_coef, cmode = "edges", reps = 500)
transitivity[c("observed", "mean", "p_greater")]

## ----qap----------------------------------------------------------------------
same_gender <- outer(gender, gender, "==") * 1
dimnames(same_gender) <- dimnames(A)

set.seed(18)
homophily <- qap_cor(A, same_gender, reps = 500)
homophily[c("correlation", "p_greater")]

