# \donttest{
# note: iter = 250 for demonstrative purposes
# data
Y <- bfi
#############################
######### global ############
#############################
# males
Ym <- subset(Y, gender == 1,
select = - c(gender, education))
# females
Yf <- subset(Y, gender == 2,
select = - c(gender, education))
global_test <- ggm_compare_ppc(Ym, Yf,
iter = 250)
global_test
#############################
###### custom function ######
#############################
# example 1
# maximum difference van Borkulo et al. (2017)
f <- function(Yg1, Yg2){
# remove NA
x <- na.omit(Yg1)
y <- na.omit(Yg2)
# nodes
p <- ncol(Yg1)
# identity matrix
I_p <- diag(p)
# partial correlations
pcor_1 <- -(cov2cor(solve(cor(x))) - I_p)
pcor_2 <- -(cov2cor(solve(cor(y))) - I_p)
# max difference
max(abs((pcor_1[upper.tri(I_p)] - pcor_2[upper.tri(I_p)])))
}
# observed difference
obs <- f(Ym, Yf)
global_max <- ggm_compare_ppc(Ym, Yf,
iter = 250,
FUN = f,
custom_obs = obs,
progress = FALSE)
global_max
# example 2
# Hamming distance (squared error for adjacency)
f <- function(Yg1, Yg2){
# remove NA
x <- na.omit(Yg1)
y <- na.omit(Yg2)
# nodes
p <- ncol(x)
# identity matrix
I_p <- diag(p)
fit1 <- estimate(x, analytic = TRUE)
fit2 <- estimate(y, analytic = TRUE)
sel1 <- select(fit1)
sel2 <- select(fit2)
sum((sel1$adj[upper.tri(I_p)] - sel2$adj[upper.tri(I_p)])^2)
}
# observed difference
obs <- f(Ym, Yf)
global_hd <- ggm_compare_ppc(Ym, Yf,
iter = 250,
FUN = f,
custom_obs = obs,
progress = FALSE)
global_hd
#############################
######## nodewise ##########
#############################
nodewise <- ggm_compare_ppc(Ym, Yf, iter = 250,
test = "nodewise")
nodewise
# }
Run the code above in your browser using DataLab