Skip to content

Commit

Permalink
Merge pull request #277 from snlab-ch/develop
Browse files Browse the repository at this point in the history
v1.1.2
  • Loading branch information
jhollway authored Oct 18, 2023
2 parents 63312ff + c148787 commit 9708d02
Show file tree
Hide file tree
Showing 18 changed files with 2,263 additions and 1,804 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: migraph
Title: Multimodal Network Analysis and More
Version: 1.1.1
Date: 2023-10-11
Version: 1.1.2
Date: 2023-10-18
Description: A set of tools for analysing multimodal networks.
It includes functions for measuring
centrality, centralization, cohesion, closure, constraint and diversity,
Expand All @@ -12,7 +12,7 @@ Description: A set of tools for analysing multimodal networks.
Built on the 'manynet' package, all functions operate with matrices, edge lists,
and 'igraph', 'network', and 'tidygraph' objects,
and on one-mode, two-mode (bipartite), and sometimes three-mode networks.
URL: https://github.com/snlab-ch/migraph
URL: https://snlab-ch.github.io/migraph/
BugReports: https://github.com/snlab-ch/migraph/issues
License: MIT + file LICENSE
Language: en-GB
Expand Down
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# migraph 1.1.2

2023-10-11

## Package

- Added more code annotations in 'tutorial4' (Community)
- Elaborated 'tutorial5' (was named Equivalence, now Position)
- Chunks in tutorials that are incremental now hidden upon extraction using `purl = FALSE` argument

## Members

- Improved printing of node_members and node_measures objects

# migraph 1.1.1

2023-10-11
Expand Down
3 changes: 2 additions & 1 deletion R/class_measures.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
make_node_measure <- function(out, .data) {
if(manynet::is_labelled(.data)) names(out) <- manynet::node_names(.data)
class(out) <- c("node_measure", class(out))
attr(out, "mode") <- manynet::node_mode(.data)
out
Expand Down Expand Up @@ -154,6 +155,6 @@ print_tblvec <- function(y, names){
print(body)
cat(pillar::style_subtle(paste("# ... with",
setup$extra_cols_total,
"more from this nodeset in the vector.")))
"more values from this nodeset not printed but in the vector.")))
} else print(body)
}
8 changes: 5 additions & 3 deletions R/class_members.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
make_node_member <- function(out, .data) {
if(manynet::is_labelled(.data)) names(out) <- manynet::node_names(.data)
class(out) <- c("node_member", class(out))
attr(out, "mode") <- manynet::node_mode(.data)
out
Expand All @@ -24,13 +25,14 @@ print.node_member <- function(x, ...,
}
} else {
for (i in names(table(x))) {
if (i == names(table(x))[1]) cat(i, "\n")
else cat("\n", i, "\n")
if (i == names(table(x))[1]) cat("Class ", i, ":", sep = "")
else cat("Class ", i, ":", sep = "")
if (!is.null(names(x)))
y <- paste(names(x[x == i]), collapse = ", ")
else
y <- paste(which(x == i), collapse = ", ")
cat(" ", y)
cat(" ", y)
if (i != names(table(x))[length(table(x))]) cat("\n")
}
}
}
Expand Down
1 change: 0 additions & 1 deletion R/member_community.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ node_kernighanlin <- function(.data){

# extract names of vertices in each group after swaps
out <- ifelse(manynet::node_names(.data) %in% g1.newnames, 1, 2)
if(manynet::is_labelled(.data)) names(out) <- manynet::node_names(.data)
make_node_member(out, .data)
}

Expand Down
50 changes: 28 additions & 22 deletions inst/tutorials/tutorial3/centrality.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@ We can create a two-mode version of the dataset
by renaming the nodal attribute "twomode_type" to just "type".
Let's begin by graphing these datasets using `manynet::autographr()`.

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

```{r coercion-hint-1}
```{r coercion-hint-1, purl = FALSE}
# Let's graph the one-mode version
autographr(____)
```

```{r coercion-hint-2}
```{r coercion-hint-2, purl = FALSE}
# Now, let's create a two-mode version 'ison_brandes2' and graph it.
ison_brandes2 <- ison_brandes %>% rename(type = twomode_type)
autographr(____)
```

```{r coercion-solution}
```{r coercion-solution, purl = FALSE}
# plot the one-mode version
autographr(ison_brandes)
ison_brandes2 <- ison_brandes %>% rename(type = twomode_type)
# plot the two-mode version
autographr(ison_brandes2)
```

Expand All @@ -52,17 +54,18 @@ even if it's just pretend.
Luckily, `{manynet}` has a function for this.
This makes plotting the network just a wee bit more accessible and interpretable:

```{r addingnames, exercise = TRUE}
```{r addingnames, exercise = TRUE, purl = FALSE}
ison_brandes <- to_named(ison_brandes)
```

```{r addingnames-hint-1}
```{r addingnames-hint-1, purl = FALSE}
# Now, let's graph using the object names: "ison_brandes"
autographr(____)
```

```{r addingnames-solution}
ison_brandes <- to_named(ison_brandes)
# plot network with names
autographr(ison_brandes)
```

Expand All @@ -74,31 +77,32 @@ as they are assigned randomly from a pool of (American) first names.
Let's start with calculating degree, as it is easy to calculate yourself.
Just sum the rows or columns of the matrix!

```{r degreesum, exercise = TRUE, exercise.setup = "addingnames"}
```{r degreesum, exercise = TRUE, exercise.setup = "addingnames", purl = FALSE}
```

```{r degreesum-hint-1}
```{r degreesum-hint-1, purl = FALSE}
# We can calculate degree centrality like this:
(mat <- as_matrix(ison_brandes))
(degrees <- rowSums(mat))
rowSums(mat) == colSums(mat)
```

```{r degreesum-hint-2}
```{r degreesum-hint-2, purl = FALSE}
# Or by using a built in command in migraph like this:
node_degree(ison_brandes, normalized = FALSE)
```

```{r degreesum-solution}
# manually calculate degree centrality
mat <- as_matrix(ison_brandes)
degrees <- rowSums(mat)
rowSums(mat) == colSums(mat)
# You can also just use a built in command in migraph though:
node_degree(ison_brandes, normalized = FALSE)
```

```{r degreesum-Q, echo=FALSE}
```{r degreesum-Q, echo=FALSE, purl = FALSE}
question("Are the row sums the same as the column sums?",
answer("Yes",
correct = TRUE,
Expand All @@ -112,11 +116,12 @@ Often we are interested in the distribution of (degree) centrality in a network.
`{migraph}` offers a way to get a pretty good first look at this distribution,
though there are more elaborate ways to do this in base and grid graphics.

```{r distrib, exercise = TRUE, exercise.setup = "addingnames"}
```{r distrib, exercise = TRUE, exercise.setup = "addingnames", purl = FALSE}
```

```{r distrib-solution}
# distribution of degree centrality scores of nodes
plot(node_degree(ison_brandes))
```

Expand All @@ -131,23 +136,23 @@ Fortunately, we can use functions from `{migraph}` to help calculate the
betweenness, closeness, and eigenvector centralities for each node in the network.
Let's collect the vectors of these centralities for the `ison_brandes` dataset:

```{r micent, exercise = TRUE, exercise.setup = "addingnames"}
```{r micent, exercise = TRUE, exercise.setup = "addingnames", purl = FALSE}
```

```{r micent-hint-1}
```{r micent-hint-1, purl = FALSE}
# Use the node_betweenness() function to calculate the
# betweenness centralities of nodes in a network
node_betweenness(ison_brandes)
```

```{r micent-hint-2}
```{r micent-hint-2, purl = FALSE}
# Use the node_closeness() function to calculate the
# closeness centrality of nodes in a network
node_closeness(ison_brandes)
```

```{r micent-hint-3}
```{r micent-hint-3, purl = FALSE}
# Use the node_eigenvector() function to calculate
# the eigenvector centrality of nodes in a network
node_eigenvector(ison_brandes)
Expand Down Expand Up @@ -186,11 +191,12 @@ e.g. `node_is_max()` or `tie_is_min()`.
By passing this attribute to the `autographr()` argument "node_color"
we can highlight which node or nodes hold the maximum score in red.

```{r ggid, exercise = TRUE, exercise.setup = "addingnames"}
```{r ggid, exercise = TRUE, exercise.setup = "addingnames", purl = FALSE}
```

```{r ggid-solution}
# plot the network, highlighting the node with the highest centrality score with a different colour
ison_brandes %>%
add_node_attribute("color", node_is_max(node_degree(ison_brandes))) %>%
autographr(node_color = "color")
Expand All @@ -211,7 +217,7 @@ ison_brandes %>%
How neat! Try it with the two-mode version.
What can you see?

```{r ggid_twomode, exercise = TRUE}
```{r ggid_twomode, exercise = TRUE, purl = FALSE}
# Instead of "ison_brandes", use "ison_brandes2"
```
Expand All @@ -234,7 +240,7 @@ ison_brandes2 %>%
autographr(node_color = "color")
```

```{r brandes2quiz}
```{r brandes2quiz, purl = FALSE}
question("Select all that are true for the two-mode Brandes network.",
answer("Only one node is selected in each plot."),
answer("The maximum degree square has a higher degree than the maximum degree circle(s).",
Expand All @@ -251,7 +257,7 @@ Here we are no longer interested in the level of the node,
but in the level of the whole network,
so the syntax replaces `node_` with `network_`:

```{r centzn, exercise = TRUE, exercise.setup = "addingnames"}
```{r centzn, exercise = TRUE, exercise.setup = "addingnames", purl = FALSE}
```

Expand All @@ -277,7 +283,7 @@ What if we want to have a single image/figure with multiple plots?
This can be a little tricky with gg-based plots,
but fortunately the `{patchwork}` package is here to help.

```{r multiplot, exercise = TRUE, exercise.setup = "addingnames"}
```{r multiplot, exercise = TRUE, exercise.setup = "addingnames", purl = FALSE}
```

Expand All @@ -304,7 +310,7 @@ ge <- autographr(ison_brandes, node_color = "eigenvector") +
```


```{r centzdq}
```{r centzdq, purl = FALSE}
question("How centralized is the ison_brandes network? Select all that apply.",
answer("It is more degree centralised than betweenness centralised.",
message = "Degree centralisation is at 0.18 for this network whereas betweenness centralisation is at 0.32. In other words, the network is better characterised as having 1 or 2 nodes lying on the shortest paths between others than one where 1 or 2 nodes have many more ties than the others."),
Expand All @@ -317,7 +323,7 @@ question("How centralized is the ison_brandes network? Select all that apply.",
allow_retry = TRUE)
```

```{r centvcent, echo=FALSE}
```{r centvcent, echo=FALSE, purl = FALSE}
question("What is the difference between centrality and centralisation according to the literature?",
answer("Centrality is for nodes and centralisation is for networks",
correct = TRUE),
Expand Down
Loading

0 comments on commit 9708d02

Please sign in to comment.