-
Notifications
You must be signed in to change notification settings - Fork 2
/
lse.Rmd
58 lines (47 loc) · 1.96 KB
/
lse.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
---
title: "Laplacian Spectral Embedding"
author: "Ronak Mehta"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{LSE}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, warning=FALSE, message=FALSE}
require(graphstats)
require(mclust)
require(ggplot2)
```
Here, we present the `lse` function, used for representing graphs in lower-dimensions.
## Stochastic Blockmodel via Random Dot-Product Graph
We will apply LSE to a core-periphery SBM constructed from an RDPG. We start with latent vectors [0.85, 0] and [0.3, 0.4]. These make edge probability within block 1 approximately 0.75 (the core), and other edge probabilities approximately 0.25. The Adjacency Matrix is plotted below.
```{r, fig.height=4.25, fig.width=5}
# Create latent vectors, and edge probability matrix.
block1 <- as.matrix(c(0.85, 0), nrow = 2)
block2 <- as.matrix(c(0.3, 0.4), nrow = 2)
block_probs <- matrix(c(t(block1) %*% block1,
t(block1) %*% block2,
t(block2) %*% block1,
t(block2) %*% block2), ncol = 2)
# Create SBM. Use higher edge probability if nodes are from same block.
set.seed(456)
n <- 100
block.sizes <- c(n/2, n/2)
blocks <- rep(c(1, 2), block.sizes)
g <- igraph::sample_sbm(n, block_probs, block.sizes)
gs.plot.plot_matrix(igraph::as_adjacency_matrix(g, sparse = FALSE),
legend.name="Connection",
ffactor = TRUE)
```
## Embedding the Graph
Now, we call the function to embed the Laplacian matrix in R^2. Returned is an nx2 matrix. By plotting the data, we observe that the core-periphery structure translates well to Euclidean space.
```{r, fig.height=4, fig.width=4}
# Embed graph into R^2.
dim <- 2
X <- lse(g, dim)
dat <- as.data.frame(X)
# Display.
p <- ggplot(dat) + geom_point(aes(x = V1, y = V2), color=blocks)
p + xlab("PC Score 1") + ylab("PC Score 2")
```