---
title: "Position"
author: "by James Hollway"
output: 
  learnr::tutorial:
    theme: journal
runtime: shiny_prerendered
---

```{r setup, include = FALSE}
library(learnr)
library(manynet)
library(migraph)
library(patchwork)
knitr::opts_chunk$set(echo = FALSE)
```


## Setting up

For this session, we're going to use the "ison_algebra" dataset included in the `{manynet}` package.
Do you remember how to call the data?
Can you find out some more information about it via its help file?

```{r data, exercise = TRUE, purl = FALSE}

```

```{r data-hint-1, purl = FALSE}
# Let's call and load the 'ison_algebra' dataset
data("ison_algebra", package = "manynet")
# Or you can retrieve like this:
ison_algebra <- manynet::ison_algebra
```

```{r data-hint-2, purl = FALSE}
# If you want to learn more about the 'ison_algebra' dataset, use the following function (below)
?manynet::ison_algebra
```

```{r data-solution}
data("ison_algebra", package = "manynet")
?manynet::ison_algebra
# If you want to see the network object, you can run the name of the object
# ison_algebra
# or print the code with brackets at the front and end of the code
# (ison_algebra <- manynet::ison_algebra)
```

We can see that the dataset is multiplex,
meaning that it contains several different types of ties: 
friendship (friends), social (social) and task interactions (tasks).

### Separating multiplex networks

As a multiplex network, 
there are actually three different types of ties in this network.
We can extract them and investigate them separately using `to_uniplex()`.
Within the parentheses, put the multiplex object's name,
and then as a second argument put the name of the tie attribute in quotation marks.
Once you have extracted all three networks,
graph them and add a descriptive title.

```{r separatingnets, exercise=TRUE, exercise.setup = "data", purl = FALSE}

```

```{r separatingnets-hint-1, purl = FALSE}
# Here's the basic idea/code syntax you will need to extract each type of network
# You will want to replace
____ <- to_uniplex(ison_algebra, _____)
```

```{r separatingnets-hint-4, purl = FALSE}
# Now, let's compare the each attribute's graph, side-by-side by using "+"
# Note: using "/" after each graph will order them vertically; however, it might not be best way
# See for example:
gfriend <- autographr(friends) + ggtitle("Friendship")
gfriend + gsocial + gtask
```

```{r separatingnets-solution}
friends <- to_uniplex(ison_algebra, "friends")
gfriend <- autographr(friends) + ggtitle("Friendship")

social <- to_uniplex(ison_algebra, "social")
gsocial <- autographr(social) + ggtitle("Social")

tasks <- to_uniplex(ison_algebra, "tasks")
gtask <- autographr(tasks) + ggtitle("Task")

gfriend + gsocial + gtask
```

Note also that these are weighted networks.
`autographr()` automatically recognises these different weights and plots them.

```{r strongties-qa, echo=FALSE, purl = FALSE}
question("If we interpret ties with higher weights as strong ties, and lesser weights as weak ties, then, according to network theory, where would we expect novel information to come from?",
         answer("Weak ties",
                correct = TRUE,
                message = learnr::random_praise()),
         answer("Strong ties",
                message = learnr::random_encouragement()),
         answer("Isolates",
                message = learnr::random_encouragement()),
         answer("Highest degree nodes",
                message = learnr::random_encouragement()),
  random_answer_order = TRUE,
  allow_retry = TRUE
)
```

## Structural Holes and Constraint

Our first question for this network, is where innovation and creative ideas
might be expected to appear.

```{r structinnov-qa, echo=FALSE, purl = FALSE}
question("Which network concepts are associated with innovation?",
         answer("Structural holes",
                correct = TRUE,
                message = learnr::random_praise()),
         answer("Structural folds",
                correct = TRUE),
         answer("Structural balance",
                message = learnr::random_encouragement()),
         answer("Structural equivalence",
                message = learnr::random_encouragement()),
         answer("Structuralism",
                message = learnr::random_encouragement()),
  random_answer_order = TRUE,
  allow_retry = TRUE
)
```

### Measuring structural holes

```{r shmeasures-qa, echo=FALSE, purl = FALSE}
question("There are a number of measures that might be used to approximate the concept of structural holes. Select all that apply.",
         answer("Constraint",
                correct = TRUE,
                message = learnr::random_praise()),
         answer("Effective size",
                correct = TRUE),
         answer("Bridges",
                correct = TRUE),
         answer("Redundancy",
                correct = TRUE),
         answer("Efficiency",
                correct = TRUE),
         answer("Hierarchy",
                correct = TRUE),
  random_answer_order = TRUE,
  allow_retry = TRUE
)
```

Let's take a look at which actors are least _constrained_ 
by their position in the *task* network to begin with.
`{migraph}` makes this easy enough with the `node_constraint()` function.

```{r objects-setup, purl=FALSE}
alge <- to_named(ison_algebra)
friends <- to_uniplex(alge, "friends")
social <- to_uniplex(alge, "social")
tasks <- to_uniplex(alge, "tasks")
```

```{r constraint, exercise = TRUE, exercise.setup = "objects-setup", purl = FALSE}

```

```{r constraint-hint, purl = FALSE}
node_constraint(____)
# Don't forget we want to look at which actors are least constrained by their position in the 'tasks' network
```

```{r constraint-solution}
node_constraint(tasks)
```

This function returns a vector of constraint scores that can range between 0 and 1.
Let's graph the network again, sizing the nodes according to this score.
We can also identify the node with the minimum constraint score using `node_is_min()`.

```{r constraintplot, exercise=TRUE, exercise.setup = "objects-setup", purl = FALSE}

```

```{r constraintplot-hint-1, purl = FALSE}
tasks <- tasks %>% 
  mutate(constraint = node_constraint(____),
         low_constraint = node_is_min(node_constraint(____)))

# Don't forget, we are still looking at the 'tasks' network
```

```{r constraintplot-hint-3, purl = FALSE}
# Now, let's graph the network
# Note 1: we are looking at the 'tasks' network
# Note 2: we are interested in the actors 'least constrained' by their position

autographr(____, node_color = "____")
```

```{r constraintplot-hint-4, purl = FALSE}
autographr(tasks, node_size = "constraint", node_color = "low_constraint")
```

```{r constraintplot-solution}
tasks <- tasks %>% 
  mutate(constraint = node_constraint(tasks), 
         low_constraint = node_is_min(node_constraint(tasks)))
autographr(tasks, node_size = "constraint", node_color = "low_constraint")
```

Why minimum? Because constraint measures how well connected each node's partners are,
with the implication that having few partners that are already connected to each other puts a node in an advantageous position to identify and share novel solutions to problems.
So what can we learn from this plot
about where innovation might occur within this network?

## Structural Equivalence

Next we might ask ourselves what (other) roles there are in the network?
We want to know who plays what role in this algebra class.
Let us begin with structural equivalence.

```{r equiv-qa, echo=FALSE, purl = FALSE}
question("Structural equivalence means identifying classes of nodes with...",
         answer("same/similar tie partners.",
                correct = TRUE,
                message = learnr::random_praise()),
         answer("same/similar pattern of ties.",
                message = "This is the definition for regular equivalence."),
         answer("same/similar distance from all others.",
                message = "This is the definition for automorphic equivalence.")
)
```

We're going to identify structurally equivalent positions
across all the data that we have, including 'task', 'social', and 'friend' ties.
So that is, we are using the multiplex `ison_algebra` dataset again and not
a uniplex subgraph thereof.

### Finding structurally equivalent classes

In `{migraph}`, finding how the nodes of a network can be partitioned
into structurally equivalent classes can be as easy as:

```{r find-se, exercise = TRUE, exercise.setup = "data"}
node_structural_equivalence(ison_algebra)

ison_algebra %>% 
  mutate(se = node_structural_equivalence(ison_algebra)) %>% 
  autographr(node_color = "se")
```

But actually, a lot is going on behind the scenes here that we can unpack.
Understanding what is going on behind the scenes is important for understanding
how these classes are identified and how to interpret them.

### Step one: starting with a census

All equivalence classes are based on nodes' similarity across some profile of motifs.
In `{migraph}`, we call these motif *censuses*.
Any kind of census can be used, and `{migraph}` includes a few options,
but `node_structural_equivalence()` is based off of the census of all the nodes' ties,
both outgoing and incoming ties, to characterise their relationships to tie partners.

```{r construct-cor, exercise = TRUE, exercise.setup = "data", purl = FALSE}

```

```{r construct-cor-hint-1, purl = FALSE}
# Let's use the node_tie_census() function
# The function accepts an object such as a dataset
# Hint: Which dataset are we using in this tutorial?
node_tie_census(____)
```

```{r construct-cor-hint-2, purl = FALSE}
node_tie_census(ison_algebra)
```

```{r construct-cor-hint-3, purl = FALSE}
# Now, let's get the dimensions of an object via the dim() function
dim(node_tie_census(ison_algebra))
```

```{r construct-cor-solution}
node_tie_census(ison_algebra)
dim(node_tie_census(ison_algebra))
```

We can see that the result is a matrix of 16 rows
and 96 columns, 
because we want to catalogue or take a census of all the different incoming/outgoing partners 
our 16 nodes might have across these three networks.
Note also that the result is a weighted matrix; 
what would you do if you wanted it to be binary?

```{r construct-binary, exercise = TRUE, exercise.setup = "data", purl = FALSE}

```

```{r construct-binary-hint, purl = FALSE}
# we could convert the result using as.matrix, returning the ties 
as.matrix((node_tie_census(ison_algebra)>0)+0)

```

```{r construct-binary-solution}
# But it's easier to simplify the network by removing the classification into different types of ties.
# Note that this also reduces the total number of possible paths between nodes
ison_algebra %>%
  select_ties(-type) %>%
  node_tie_census()
```

Note that `node_tie_census()` does not need to be passed to `node_structural_equivalence()` ---
this is done automatically!
However, the more generic `node_equivalence()` is available and can be used with whichever tie census is desired.
Feel free to explore using some of the other censuses available in `{migraph}`,
though some common ones are already used in the other equivalence convenience functions,
e.g. `node_triad_census()` in `node_regular_equivalence()` 
and `node_path_census()` in `node_automorphic_equivalence()`.

### Step two: growing a tree of similarity

The next part takes this census and creates a dendrogram based on distance or dissimilarity among the nodes' census profiles.
This is all done internally within e.g. `node_structural_equivalence()`,
though there are two important parameters that can be set to obtain different results.

First, users can set the type of distance measure used.
For enthusiasts, this is passed on to `stats::dist()`, 
so that help page should be consulted for more details.
By default `"euclidean"` is used.

Second, we can also set the type of clustering algorithm employed.
By default, `{migraph}`'s equivalence functions use hierarchical clustering, `"hier"`,
but for compatibility and enthusiasts, we also offer `"concor"`,
which implements a CONCOR (CONvergence of CORrelations) algorithm.

We can see the difference from varying the clustering algorithm and/or distance
by plotting the dendrograms (hidden) in the output from `node_structural_equivalence()`:

```{r varyclust, exercise = TRUE, exercise.setup = "data"}
alge <- to_named(ison_algebra) # fake names to make comparison clearer
plot(node_structural_equivalence(alge, 
                                 cluster = "hier", distance = "euclidean"))

# changing the type of distance used
plot(node_structural_equivalence(alge, 
                                 cluster = "hier", distance = "manhattan"))

# changing the clustering algorithm
plot(node_structural_equivalence(alge, 
                                 cluster = "concor", distance = "euclidean"))
```

```{r scale-interp, echo = FALSE, purl = FALSE}
question("Do you see any differences?",
         answer("Yes", correct = TRUE, message = learnr::random_praise()),
         answer("No"),
        allow_retry = TRUE)
```

So plotting a `membership` vector from `{migraph}` returns a dendrogram
with the names of the nodes on the _y_-axis and the distance between them on the _x_-axis.
Using the census as material, the distances between the nodes
is used to create a dendrogram of (dis)similarity among the nodes.
Basically, as we move to the right, we're allowing for
more and more dissimilarity among those we cluster together.
A fork or branching point indicates the level of dissimilarity
at which those two or more nodes would be said to be equivalent.
Where two nodes' branches join/fork represents the maximum distance among all their leaves,
so more similar nodes' branches fork closer to the tree's canopy,
and less similar (groups of) nodes don't join until they form basically the trunk.

Note that with the results using the hierarchical clustering algorithm,
the distance directly affects the structure of the tree (and the results).

The CONCOR dendrogram operates a bit differently to hierarchical clustering though.
Instead it represents how converging correlations repeatedly bifurcate 
the nodes into one of two partitions.
As such the 'distance' is really just the (inverse) number of steps
of bifurcations until nodes belong to the same class.

### Step three: identifying the number of clusters

Another bit of information represented in the dendrogram
is where the tree should be cut (the dashed red line) and
how the nodes are assigned to the branches (clusters) present at that cut-point.

But where does this red line come from?
Or, more technically, how do we identify the number of clusters
into which to assign nodes?

`{migraph}` includes several different ways of establishing `k`,
or the number of clusters.
Remember, the further to the right the red line is 
(the lower on the tree the cut point is)
the more dissimilar we're allowing nodes in the same cluster to be.
We could set this ourselves by just passing `k` an integer.

```{r k-discrete, exercise = TRUE, exercise.setup = "varyclust"}
plot(node_structural_equivalence(alge, k = 2))
```

But we're really just guessing. Maybe 2 is not the best `k`?
To establish what the best `k` is for this clustering exercise, 
we need to iterate through a number of potential `k`
and consider their fitness by some metric.
There are a couple of options here.

One is to consider, for each `k`, 
how correlated this partition is with the observed network.
When there is one cluster for each vertex in the network, 
cell values will be identical to the observed correlation matrix, 
and when there is one cluster for the whole network, 
the values will all be equal to the average correlation 
across the observed matrix.
So the correlations in each by-cluster matrix are correlated with the observed 
correlation matrix to see how well each by-cluster matrix fits the data.

Of course, the perfect partition would then be 
where all nodes are in their own cluster,
which is hardly 'clustering' at all.
Also, increasing `k` will always improve the correlation.
But if one were to plot these correlations as a line graph,
then we might expect there to be a relatively rapid increase
in correlation as we move from, for example, 3 clusters to 4 clusters,
but a relatively small increase from, for example, 13 clusters to 14 clusters.
By identifying the inflection point in this line graph,
`{migraph}` selects a number of clusters that represents a trade-off
between fit and parsimony.
This is the `k = "elbow"` method.

The other option is to evaluate a candidate for `k` based
not on correlation but on a metric of 
how similar each node in a cluster is to others in its cluster
_and_ how dissimilar each node is to those in a neighbouring cluster.
When averaged over all nodes and all clusters, 
this provides a 'silhouette coefficient' for a candidate of `k`.
Choosing the number of clusters that maximizes this coefficient,
which is what `k = "silhouette"` does,
can return a somewhat different result to the elbow method.
See what we have here, with all other arguments held the same:

```{r elbowsil, exercise = TRUE, exercise.setup = "varyclust"}
plot(node_structural_equivalence(alge, 
                                 k = "elbow"))
plot(node_structural_equivalence(alge, 
                                 k = "silhouette"))
```

Ok, so it looks like the elbow method returns `k == 3` as a good trade-off
between fit and parsimony.
The silhouette method, by contrast, sees `k == 4` as maximising cluster similarity
and dissimilarity.
Either is probably fine here, 
and there is much debate around how to select the number of clusters anyway.
However, the silhouette method seems to do a better job of identifying how unique
the 16th node is.
The silhouette method is also the default in `{migraph}`.

Note that there is a somewhat hidden parameter here, `range`.
Since testing across all possible numbers of clusters can get 
computationally expensive (not to mention uninterpretable) for large networks,
`{migraph}` only considers up to 8 clusters by default.
This however can be modified to be higher or lower, e.g. `range = 16`.

Finally, one last option is `k = "strict"`,
which only assigns nodes to the same partition 
if there is zero distance between them.
This is quick and rigorous solution,
however oftentimes this misses the point in finding clusters of nodes that, 
despite some variation, can be considered as similar on some dimension.

```{r strict, exercise = TRUE, exercise.setup = "varyclust"}
plot(node_structural_equivalence(alge, k = "strict"))
```

Here for example, no two nodes have precisely the same tie-profile,
otherwise their branches would join/fork at a distance of 0.
As such, `k = "strict"` partitions the network into 16 clusters.
Where networks have a number of nodes with strictly the same profiles,
such a k-selection method might be helpful to recognise those in exactly the same structural position,
but here it essentially just reports nodes' identity.

## Blockmodelling

### Summarising profiles

Ok, so now we have a result from establishing nodes' membership in structurally equivalent classes.
We can graph this of course, as above:

```{r strplot, exercise = TRUE, exercise.setup = "varyclust"}
alge %>% 
  mutate(se = node_structural_equivalence(alge)) %>% 
  autographr(node_color = "se")
```

While this plot adds the structurally equivalent classes information to our earlier graph,
it doesn't really help us understand how the classes relate.
That is, we might be less interested in how the individuals in the different classes relate, and more interested in how the different classes relate in aggregate.

One option that can be useful for characterising what
the profile of ties (partners) is for each position/equivalence class
is to use `summary()`.
It summarises some census result by a partition (equivalence/membership) assignment.
By default it takes the average of ties (values),
but this can be tweaked by assigning some other summary statistic as `FUN = `.

```{r summ, exercise = TRUE, exercise.setup = "strplot", purl = FALSE}

```

```{r summ-hint, purl = FALSE}
# Let's wrap node_tie_census inside the summary() function
# and pass it a membership result
summary(node_tie_census(____),
        membership = ____)
```

```{r summ-solution}
summary(node_tie_census(alge),
        membership = node_structural_equivalence(alge))
```

This node census produces 96 columns, 
$16 \text{nodes} * 2 \text{directions} * 3 \text{edge types}$,
it takes a bit to look through what varies between the different classes
as 'blocked'.
But only four rows (the four structurally equivalent classes, according to the default).

Another way to do this is to plot the blockmodel as a whole.
Passing the `plot()` function an adjacency/incidence matrix
along with a membership vector allows the matrix to be sorted and framed
(without the membership vector, just the adjacency/incidence matrix is plotted):

```{r block, exercise = TRUE, exercise.setup = "strplot", purl = FALSE}

```

```{r block-hint, purl = FALSE}
# Let's plot the blockmodel using the plot() function we used for the dendrograms
# Instead of node_tie_census() let's us as_matrix()

plot(as_matrix(____),
     membership = ____)
```

```{r block-solution}
# plot the blockmodel for the whole network
plot(as_matrix(alge),
     membership = node_structural_equivalence(alge))

# plot the blockmodel for the friends, tasks, and social networks separately
plot(as_matrix(friends),
     membership = node_structural_equivalence(alge)) +
plot(as_matrix(tasks),
     membership = node_structural_equivalence(alge)) +
plot(as_matrix(social),
     membership = node_structural_equivalence(alge))
```

By passing the membership argument our structural equivalence results,
the matrix is re-sorted to cluster or 'block' nodes from the same class together.
This can help us interpret the general relationships between classes.
For example, when we plot the friends, tasks, and social networks using the structural equivalence results,
we might characterise them like so:

- The first group work together only in reciprocal pairs on tasks,
preferring to approach the nerd but also those of the other two roles.
While they hang out with each other socially quite a bit, friendship from groups 2 and 3 are preferred.
- The second group also work together only in reciprocal pairs,
preferring to work collaboratively with group 1 or also the nerd.
They also tend to count those from group 1 as friends,
and hang out with everyone else but themselves.
- The third group will work with either some in group 1 and 3, or 2,
but again prefer the nerd for task advice.
They are pretty good friends with each other though,
and pretty happy to socialise with everyone.
- The nerd is a loner, no friends,
but everyone hangs out with them for task advice.

### Reduced graph

Lastly, we can consider how _classes_ of nodes relate to one another in a blockmodel.
Let's use the 4-cluster solution on the valued network (though binary is possible too)
to create a _reduced graph_.
A reduced graph is a transformation of a network such that 
the nodes are no longer the individual nodes but the groups of one or more nodes as a class,
and the ties between these blocked nodes can represent the sum or average tie between these classes.
Of course, this means that there can be self-ties or loops,
because even if the original network was simple (not complex), 
any within-class ties will end up becoming loops and thus the network will be complex.

```{r structblock, exercise = TRUE, exercise.setup = "varyclust", warning=FALSE}
(bm <- to_blocks(alge, node_structural_equivalence(alge)))

bm <- bm %>% as_tidygraph %>% 
  mutate(name = c("Freaks", "Squares", "Nerds", "Geek"))
autographr(bm)
```
