1

Is there an R function for generating an undirected graph with probability on edges? I've tried to generate a nonparanormal data set by constructing the adjacency matrix (precision matrix) [see appendix]. First of all, I should generate a random graph, which the of pairwise edges (i,j) follows the normal probability density function. the igraph package includes the sample_fitness and sample_degseq functions, which apply probabilities on the degree of each vertex, but not on the existence of edges between i and j vertices. Thank you all.

Liu, Lafferty and Wasserman (2009).

library(MASS)
library(Matrix)
library(philentropy)
library(qgraph)
library(igraph)
library(huge)
d=10
tmp=matrix(runif(2*d,0,0.5),d,2)
tmp=2*tmp
L=distance(tmp,method="euclidean")
s=0.125
prob=(1/sqrt(2*pi))*exp(-(L^2)/2*s)
prob=mat2vec(prob)
degs<-sample(0:(length(prob)-1),d,replace=TRUE,prob=prob)
for(i in 1:d){
if (degs[i]>4){degs[i]=4}
}
degs
if(sum(degs)%%2!=0){degs[1]<-degs[1]+1}
degs
g4 <-sample_degseq(degs,method="vl")
all(degree(g4)==degs)
plot(g4)
theta=as_adjacency_matrix(g4,type="both")
for(j in 1:d){
for(i in 1:d){
if(theta[i,j]==0)
omega[i,j]=0
else
omega[i,j]=0.245
}
}
diag(omega)=1
Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30

2 Answers2

0

You can use runif along with prob to generate your random network, e.g.,

adjmat <- prob
adjmat[] <- runif(length(prob))
g <- graph_from_adjacency_matrix(
  +(as.matrix(adjmat) <= as.matrix(prob)) - diag(d),
  mode = "undirected"
)

and the network with probabilistic edges are shown as, for example

enter image description here

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • The 'prob' is not an adjancency matrix, it's supposed to be the probability of the existence an edge between two nodes. The question is, first, how to generate a graph by probability on edges. Next, restrict the max degree of the graph to be four. Finally, covert the graph to an adjancency matrix. Thanks. – Hamid Aghabozorgi May 23 '21 at 15:55
  • @HamidAghabozorgi `prob` is not an adjacency matrix. I just use it with `runif` to see if the edge (i,j) should be assigned. If you run my code each time, you will see different adjacency between node, since `runif` gives a random edge assignment. – ThomasIsCoding May 23 '21 at 16:06
0

The first step is to create the required matrix. Source: https://arxiv.org/abs/math/0608017v1

Lets take as an example:

g <- sample_gnm(10, 20)
degree(g)

The next step is to remove random edges until all nodes have degree 4 or less.

g1 <- g
while (max(degree(g1)) > max_deg) {
  vvv <- V(g1)[which(degree(g1) > max_deg)]
  g1  <- g1 - sample(incident(g1, vvv), 1)
}
degree(g1)
dev.new(); plot(g2, layout=layout_in_circle)

Output:

[1] 2 6 3 7 4 5 2 1 5 5 - degrees g
[1] 1 3 3 2 4 4 4 3 2 2 - degrees g1
clp
  • 1,098
  • 5
  • 11