1 2context("authority_score") 3 4test_that("authority score works", { 5 library(igraph) 6 7 ashs <- function(graph, as=TRUE) { 8 mscale <- function(x) { 9 if (sd(x)!=0) { x <- scale(x) } 10 if (x[1] < 0) { x <- -x } 11 x 12 } 13 A <- as_adj(graph, sparse=FALSE) 14 if (as) { 15 s1 <- eigen(t(A) %*% A)$vectors[,1] 16 s2 <- authority_score(graph)$vector 17 } else { 18 s1 <- eigen(A %*% t(A))$vectors[,1] 19 s2 <- hub_score(graph)$vector 20 } 21 expect_that(mscale(s1), is_equivalent_to(mscale(s2))) 22 } 23 24 g1 <- sample_pa(100, m=10) 25 ashs(g1) 26 ashs(g1, as=FALSE) 27 28 g2 <- sample_gnp(100, 2/100) 29 ashs(g2) 30 ashs(g2, as=FALSE) 31}) 32 33test_that("authority scores of a ring are all one", { 34 library(igraph) 35 g3 <- make_ring(100) 36 expect_that(authority_score(g3)$vector, equals(rep(1, vcount(g3)))) 37 expect_that(hub_score(g3)$vector, equals(rep(1, vcount(g3)))) 38}) 39 40test_that("authority_score survives stress test", { 41 42 skip_on_cran() 43 44 set.seed(42) 45 46 is.principal <- function(M, lambda) { 47 expect_that(eigen(M)$values[1], equals(lambda)) 48 } 49 50 is.ev <- function(M, v, lambda) { 51 expect_that(as.vector(M %*% v), equals(lambda * v)) 52 } 53 54 is.good <- function(M, v, lambda) { 55 is.principal(M, lambda) 56 is.ev(M, v, lambda) 57 } 58 59 for (i in 1:100) { 60 G <- sample_gnm(10, sample(1:20, 1)) 61 as <- authority_score(G) 62 M <- as_adj(G, sparse = FALSE) 63 is.good(t(M) %*% M, as$vector, as$value) 64 } 65 66 for (i in 1:100) { 67 G <- sample_gnm(10, sample(1:20, 1)) 68 hs <- hub_score(G) 69 M <- as_adj(G, sparse = FALSE) 70 is.good(M %*% t(M), hs$vector, hs$value) 71 } 72}) 73