R/tests/change_stat_test.R

Defines functions change_stat_test

#Testing that the lolog_change_stat_R function and the lolog_change_stat_cpp function 
#give the same results, and measure the difference in speed

library(Blolog)
library(statnet)
library(lolog)
library(coda)
library(doParallel)
library(ggplot2)
require(bootlog)
library(microbenchmark)

setwd("C:/Users/Duncan/Documents/Academics/UCLA_Academics/Networks/LOLOG_Cateloging/1_16_KapfererTailors")
load("processed_data.RData")
net <- net_1
n <- get.network.attribute(net_1,"n")
e <- choose(n,2)

formula_rhs <- "edges+triangles"

change_stat_test <- function(change_stat_function_1,change_stat_function_2,times = 10){
  
  s <- sample(1:e,e)
  change_stat_1 <- change_stat_function_1(net,s,formula_rhs)
  change_stat_2 <- change_stat_function_2(net,s,formula_rhs)
  
  same = mapply(change_stat_1,change_stat_2,FUN = function(x,y){
    (sum(x==y) == length(x))
  })
  
  same = (sum(same) ==length(same))
  if(same){
    print("Both functions return the same change stats")
  }else{"Different Change Stats"}
  
  t_1 <- microbenchmark(change_stat_1 <- change_stat_function_1(net,s,formula_rhs),times = times)
  t_2 <- microbenchmark(change_stat_2 <- change_stat_function_2(net,s,formula_rhs),times = times)
  
  print(paste("Method 2 is ",summary(t_1)$mean/summary(t_2)$mean, " times faster than method 1",sep=""))
 return()
}

change_stat_test(lolog_change_stats_R,lolog_change_stats_CPP,times = 50)
duncan-clark/Blolog documentation built on June 22, 2022, 7:57 a.m.