First we need to look to see if there is homophily by district, male, trust and efficacy each separately. We can see that each graph is very dense.

library(knitr)
library(tidyverse)
library(janitor)
library(skimr)
library(ggraph)
library(igraph)
library(tidygraph)
library(readxl)
library(network)
library(ergm)
library(kableExtra)
library(texreg)

#read in node data
leader_nodes <- read_excel("data/School Leaders Data Chapter 9_e.xlsx", 
                           col_types = c("text", "numeric", "numeric", "numeric", "numeric")) |>
  clean_names()

#read in matrix to create edges.

leader_matrix <-  read_excel("data/School Leaders Data Chapter 9_d.xlsx", col_names = FALSE)
#convert to a matrix object
leader_matrix <- leader_matrix |>
  as.matrix()

#ichotomize the network, assign" 0's to all values in our matrix that are less than or equal to 2, and 1's to all values that are greater the or equal to 3

leader_matrix[leader_matrix <= 2] <- 0

leader_matrix[leader_matrix >= 3] <- 1

#add row and column names
rownames(leader_matrix) <- leader_nodes$id

colnames(leader_matrix) <- leader_nodes$id

# convert to a igraph object to extract edge data
adjacency_matrix <- graph.adjacency(leader_matrix,
                                    diag = FALSE)

#create edge data
leader_edges <- get.data.frame(adjacency_matrix) |>
  mutate(from = as.character(from)) |>
  mutate(to = as.character(to))

#combine data to a network object
leader_network <- as.network(leader_edges,
                             vertices = leader_nodes)

#inspect data for homophily
attach(leader_network)
par(mfrow=c(2,2))
plot(leader_network, vertex.cols="district", main ="homophily on district")
plot(leader_network, vertex.cols="male", main ="homophily on male")
plot(leader_network, vertex.cols="efficacy", main ="homophily on efficacy")
plot(leader_network, vertex.cols="trust", main ="homophily on trust")

m8 <- as.formula(leader_network ~ 
                   nodeifactor('district_site') + # those making connections in the district
                   nodeofactor('district_site') + # making connections out of the district
                   nodematch('male') + nodeifactor('male') +
                    absdiff('efficacy') +
                    absdiff('trust') +
                    mutual + 
                    edges)

# add summary at the same time
fit8 <- ergm(m8, control = control.ergm(seed = 12345))
library(btergm)
library(kableExtra)
alldyads <- edgeprob(fit8)
allnames <- matrix(data = names(alldyads), ncol = 1)
colnames(allnames) <- "Variable name"

Here we are inspecting whether gender attribute predicts confidential exchanges between school leaders. By taking a closer look at how two males or not affects probability of the confidential exchange. We can see the marginal effects with The absolute diffirential of trust on the y axis and the probability on the y axis. The negative correlation of between more dissimilar trust behaviors and the probability of ties being made.

ggplot(alldyads, 
       aes(x = absdiff.trust, 
           y = probability, 
           color = factor(nodematch.male))) + 
  xlab("Trust") + ylab("Probability of Tie") +
  ggtitle("Does gender predict confidential exchanges? ") +
  geom_point() +
  geom_smooth(method = 'lm')

```