set.seed(123)
# GENERATE DATA
# Sample size
N <- 50
n <- 30
# ARD parameters
genzeta <- 1
mu <- -1.35
sigma <- 0.37
K <- 12 # number of traits
P <- 3 # Sphere dimension
# Generate z (spherical coordinates)
genz <- rvMF(N,rep(0,P))
# Genetate nu from a Normal distribution with parameters mu and sigma (The gregariousness)
gennu <- rnorm(N,mu,sigma)
# compute degrees
gend <- N*exp(gennu)*exp(mu+0.5*sigma^2)*exp(logCpvMF(P,0) - logCpvMF(P,genzeta))
# Link probabilities
Probabilities <- sim.dnetwork(gennu,gend,genzeta,genz)
# Adjacency matrix
G <- sim.network(Probabilities)
# Generate vk, the trait location
genv <- rvMF(K,rep(0,P))
# set fixed some vk distant
genv[1,] <- c(1,0,0)
genv[2,] <- c(0,1,0)
genv[3,] <- c(0,0,1)
# eta, the intensity parameter
geneta <-abs(rnorm(K,2,1))
# Build traits matrix
densityatz <- matrix(0,N,K)
for(k in 1:K){
densityatz[,k] <- dvMF(genz,genv[k,]*geneta[k])
}
trait <- matrix(0,N,K)
NK <- floor(runif(K, 0.8, 0.95)*colSums(densityatz)/apply(densityatz, 2, max))
for (k in 1:K) {
trait[,k] <- rbinom(N, 1, NK[k]*densityatz[,k]/sum(densityatz[,k]))
}
# print a percentage of people having a trait
colSums(trait)*100/N
# Build ARD
ARD <- G %*% trait
# generate b
genb <- numeric(K)
for(k in 1:K){
genb[k] <- sum(G[,trait[,k]==1])/sum(G)
}
############ ARD Posterior distribution ###################
# EXAMPLE 1: ARD observed for the entire population
# initialization
d0 <- exp(rnorm(N)); b0 <- exp(rnorm(K)); eta0 <- rep(1,K);
zeta0 <- 1; z0 <- matrix(rvMF(N,rep(0,P)),N); v0 <- matrix(rvMF(K,rep(0,P)),K)
# We need to fix some of the vk and bk for identification (see Breza et al. (2020) for details).
vfixcolumn <- 1:6
bfixcolumn <- c(3, 5)
b0[bfixcolumn] <- genb[bfixcolumn]
v0[vfixcolumn,] <- genv[vfixcolumn,]
start <- list("z" = z0, "v" = v0, "d" = d0, "b" = b0, "eta" = eta0, "zeta" = zeta0)
# # MCMC ARD
# REMOVE COMMENTS TO RUN THE REST OF THE CODE.
# mcmcARD USES Rcpp FUNCTIONS IN PARALLEL TO BE FAST AND CRAN POLICY DOES
# NOT ALLOW CODE IN PARALLEL. FOR THIS REASON WE PUT THE REST OF THE CODE IN
# PARALLEL SO THAT CRAN ACCEPTS THE PACKAGE.
# out <- mcmcARD(Y = ARD, traitARD = trait, start = start, fixv = vfixcolumn,
# consb = bfixcolumn, iteration = 500)
#
# # fit network distribution
# dist <- fit.dnetwork(out)
#
# plot(rowSums(dist$dnetwork), gend)
# abline(0, 1, col = "red")
#
# # EXAMPLE 2: ARD observed for a sample of the population
# # observed sample
# selectARD <- sort(sample(1:N, n, FALSE))
# traitard <- trait[selectARD,]
# ARD <- ARD[selectARD,]
# logicalARD <- (1:N) %in% selectARD
#
# # initianalization
# d0 <- exp(rnorm(n)); b0 <- exp(rnorm(K)); eta0 <- rep(1,K);
# zeta0 <- 1; z0 <- matrix(rvMF(n,rep(0,P)),n); v0 <- matrix(rvMF(K,rep(0,P)),K)
#
# # We need to fix some of the vk and bk for identification (see Breza et al. (2020) for details).
# vfixcolumn <- 1:6
# bfixcolumn <- c(3, 5)
# b0[bfixcolumn] <- genb[bfixcolumn]
# v0[vfixcolumn,] <- genv[vfixcolumn,]
#
# start <- list("z" = z0, "v" = v0, "d" = d0, "b" = b0, "eta" = eta0, "zeta" = zeta0)
# # MCMC ARD
# REMOVE COMMENTS TO RUN THE REST OF THE CODE.
# mcmcARD USES Rcpp FUNCTIONS IN PARALLEL TO BE FAST AND CRAN POLICY DOES
# NOT ALLOW CODE IN PARALLEL. FOR THIS REASON WE PUT THE REST OF THE CODE IN
# PARALLEL SO THAT CRAN ACCEPTS THE PACKAGE.
# out <- mcmcARD(Y = ARD, traitARD = traitard, start = start, fixv = vfixcolumn,
# consb = bfixcolumn, iteration = 500)
#
# # fit network distribution
# dist <- fit.dnetwork(out, X = trait, obsARD = logicalARD, m = 1)
#
# library(ggplot2)
# ggplot(data.frame("etimated.degree" = dist$degree,
# "true.degree" = gend,
# "observed" = ifelse(logicalARD, TRUE, FALSE)),
# aes(x = etimated.degree, y = true.degree, colour = observed)) +
# geom_point()
Run the code above in your browser using DataLab