Skip to content

Commit

Permalink
version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
albertoarcagni authored and cran-robot committed Aug 11, 2023
0 parents commit ded6071
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 0 deletions.
19 changes: 19 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Package: HOasso
Type: Package
Title: Higher Order Assortativity for Complex Networks
Version: 1.0.0
Date: 2023-07-21
Authors@R: c(person("Alberto", "Arcagni", role = c("aut", "cre"), email = "alberto.arcagni@uniroma1.it"), person("Roy", "Cerqueti", role = c("aut")), person("Rosanna", "Grassi", role = c("aut")))
Author: Alberto Arcagni [aut, cre],
Roy Cerqueti [aut],
Rosanna Grassi [aut]
Description: Allows to evaluate Higher Order Assortativity of complex networks defined through objects of class 'igraph' from the package of the same name. The package returns a result also for directed and weighted graphs. References, Arcagni, A., Grassi, R., Stefani, S., & Torriero, A. (2017) <doi:10.1016/j.ejor.2017.04.028> Arcagni, A., Grassi, R., Stefani, S., & Torriero, A. (2021) <doi:10.1016/j.jbusres.2019.10.008> Arcagni, A., Cerqueti, R., & Grassi, R. (2023) <doi:10.48550/arXiv.2304.01737>.
Depends: igraph, Rdpack
RdMacros: Rdpack
License: GPL (>= 2)
Encoding: UTF-8
NeedsCompilation: no
Packaged: 2023-08-10 10:04:48 UTC; Alberto
Maintainer: Alberto Arcagni <alberto.arcagni@uniroma1.it>
Repository: CRAN
Date/Publication: 2023-08-11 08:10:02 UTC
6 changes: 6 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
53cef403a3435cf508be10b7bf061a4f *DESCRIPTION
740a27e00bcbc56143f5897b0f897cf2 *NAMESPACE
cb5dde28544d0c2c5b72b3589eb9fb28 *R/HOasso.R
d24568214203b863f387622b7c2db5f7 *build/partial.rdb
88faf3510a569a1df58dd341d105b5c1 *inst/REFERENCES.bib
9a60794edffbdbc41b2de921d45c8479 *man/HOasso.Rd
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exportPattern("^[[:alpha:]]+")
import("igraph")
import("Rdpack")
95 changes: 95 additions & 0 deletions R/HOasso.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
HOasso <- function(
g,
h = 1,
weighted = is.weighted(g),
x = c("sout", "dout", "lout", "sin", "din", "lin"),
y = c("sin", "din", "lin", "sout", "dout", "lout")
) {

h <- round(max(h))

A <- W <- get.adjacency(g)
if (is.weighted(g))
W <- get.adjacency(g, attr = "weight")
din <- degree(g, mode = "in")
dout <- degree(g, mode = "out")
sin <- strength(g, mode = "in")
sout <- strength(g, mode = "out")
lin <- log(sin, 10)
lout <- log(sout, 10)

if (weighted) {
E <- W / sum(W)
} else {
E <- A / sum(A)
}

q0 <- rowSums(as.matrix(E))

Dinv <- 1 / q0
Dinv[is.infinite(Dinv)] <- 0
Dinv <- diag(Dinv)

P <- Dinv %*% E
Ph <- list(P)

if (h > 1)
for (i in 2:h)
Ph[[i]] <- Ph[[i - 1]] %*% P

Eh <- lapply(Ph, function(x) diag(q0) %*% x)

qh <- lapply(Eh, function(x) colSums(as.matrix(x)))

M <- list()
for (i in 1:h)
M[[i]] <- (Eh[[i]] - q0 %*% t(qh[[i]]))

x <- switch(x[1],
din = din,
dout = dout,
sin = sin,
sout = sout,
lin = lin,
lout = lout
)

y <- switch(y[1],
din = din,
dout = dout,
sin = sin,
sout = sout,
lin = lin,
lout = lout
)

num <- list()
for (i in 1:h)
num[[i]] <- t(x) %*% M[[i]] %*% y

Mx <- (diag(q0) - q0 %*% t(q0))

My <- list()
for (i in 1:h)
My[[i]] <- (diag(qh[[i]]) - qh[[i]] %*% t(qh[[i]]))

denX <- t(x) %*% Mx %*% x

denY <- list()
for (i in 1:h)
denY[[i]] <- t(y) %*% My[[i]] %*% y

res <- list()
for (i in 1:h)
res[[i]] <- as.numeric(num[[i]] / sqrt(denX * denY[[i]]))

res <- unlist(res)

sel <- abs(res) > 1
res[sel] <- NaN
if (any(sel, na.rm = TRUE)) {
warning(paste0("Error of approximation to zero of the covariance or a variance, ", sum(sel), "results forced to NaN"))
}

return(res)
}
Binary file added build/partial.rdb
Binary file not shown.
30 changes: 30 additions & 0 deletions inst/REFERENCES.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@article{arcagni2017higher,
title={Higher order assortativity in complex networks},
author={Arcagni, Alberto and Grassi, Rosanna and Stefani, Silvana and Torriero, Anna},
journal={European Journal of Operational Research},
volume={262},
number={2},
pages={708--719},
year={2017},
publisher={Elsevier},
doi = {10.1016/j.ejor.2017.04.028}
}
@article{arcagni2021extending,
title={Extending assortativity: An application to weighted social networks},
author={Arcagni, Alberto and Grassi, Rosanna and Stefani, Silvana and Torriero, Anna},
journal={Journal of Business Research},
volume={129},
pages={774--783},
year={2021},
publisher={Elsevier},
doi = {10.1016/j.jbusres.2019.10.008}
}
@article{arcagni2023higher,
title={Higher order assortativity for directed weighted networks and Markov chains},
author={Arcagni, Alberto and Cerqueti, Roy and Grassi, Rosanna},
journal={arXiv preprint arXiv:2304.01737},
year={2023},
doi = {10.48550/arXiv.2304.01737}
}
47 changes: 47 additions & 0 deletions man/HOasso.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
\name{HOasso}
\alias{HOasso}
\title{Evalutes Higer Order Assortativity of complex networks}
\usage{
HOasso(
g,
h = 1,
weighted = is.weighted(g),
x = c("sout", "dout", "lout", "sin", "din", "lin"),
y = c("sin", "din", "lin", "sout", "dout", "lout")
)
}
\arguments{
\item{g}{an object of class \code{igraph} with two columns, listing the dominances, by rows.}
\item{h}{an integer value, the function will evaluates the assortativity from the order 1 to the order \code{h}.}
\item{weighted}{\code{logical}, if to use the weighted matrix to create the trasnition probabilities.}
\item{x}{The first centrarlity measure, out-strength by default, see \code{details}.}
\item{y}{The second centrarlity measure, in-strength by default, see \code{details}.}
}
\value{A vector \code{h} long containing the assortativity measures from the order 1 to the order \code{h}.}
\description{
The function evalutes Higer Order Assortativity of complex networks represented by objects of class \code{igraph} from the package of the same name.
}
\details{
Arguments \code{x} and {y} are \code{character} objects and can assume values \code{"sout"}, \code{"dout"}, \code{"lout"}, \code{"sin"}, \code{"din"}, \code{"lin"} representing the out-strength, out-degree, out-log-strength, in-strength, in-degree, and in-log-strength respectively.

In case of undirected graphs in- and out- centrality measures are equal. In case of unweighted graphs the strength is equal to the degree.
}
\references{
\insertRef{arcagni2017higher}{HOasso}

\insertRef{arcagni2021extending}{HOasso}

\insertRef{arcagni2023higher}{HOasso}
}
\examples{
g <- graph_from_data_frame(data.frame(
from = c("i", "j", "j", "k", "l"),
to = c("k", "k", "l", "l", "i"),
weight = c( 10, 5, 2, 3, 2 )
))
E(g)$label <- E(g)$weight
plot(g, layout = layout_in_circle(g))
a <- HOasso(g, h = 10)
a
plot(a, type = "b", ylim = c(-1, 1), panel.first = abline(h = 0, lty = 2))
}

0 comments on commit ded6071

Please sign in to comment.