Nothing
## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
echo = TRUE,
message = FALSE,
warning = FALSE,
fig.width = 6, fig.height = 4)
set.seed(3052008) # set seed for reproducibility
## ----packages-----------------------------------------------------------------
library(missSBM)
library(igraph)
library(ggplot2)
library(magrittr)
## ----data---------------------------------------------------------------------
data("frenchblog2007", package = "missSBM")
frenchblog2007 <- delete_vertices(frenchblog2007, which(degree(frenchblog2007) == 0))
blog <- as_adjacency_matrix(frenchblog2007, sparse = FALSE)
party <- vertex_attr(frenchblog2007, "party")
table(party)
## ----fit-full-----------------------------------------------------------------
vBlocks <- 1:12
sbm_full <- estimateMissSBM(blog, vBlocks, "node", control = missSBM_param(trace = FALSE))
plot(sbm_full, "icl")
## ----blocks-full--------------------------------------------------------------
blocks_full <- sbm_full$bestModel$fittedSBM$memberships
table(party, block = blocks_full)
plot(sbm_full$bestModel, "expected", dimLabels = list(row = "blogs", col = "blogs"))
## ----sampling-----------------------------------------------------------------
block_prop <- sbm_full$bestModel$fittedSBM$blockProp
sampling_rate <- ifelse(block_prop < 0.1, 0.2, 0.8)
blog_obs <- observeNetwork(blog, "block-node", sampling_rate, clusters = blocks_full)
mean(is.na(blog_obs[upper.tri(blog_obs)])) # the fraction of missing dyads
## ----fit-partial--------------------------------------------------------------
control <- missSBM_param(trace = FALSE, iterates = 2)
sbm_node <- estimateMissSBM(blog_obs, vBlocks, "node", control = control)
sbm_block <- estimateMissSBM(blog_obs, vBlocks, "block-node", control = control)
## ----icl----------------------------------------------------------------------
rbind(
data.frame(Q = vBlocks, ICL = sbm_node$ICL, sampling = "node"),
data.frame(Q = vBlocks, ICL = sbm_block$ICL, sampling = "block-node")
) %>%
ggplot(aes(x = Q, y = ICL, color = sampling)) + geom_line() + geom_point() +
labs(x = "number of blocks", y = "ICL") + theme_bw()
c(node = min(sbm_node$ICL), "block-node" = min(sbm_block$ICL))
## ----ari----------------------------------------------------------------------
c(node = aricode::ARI(blocks_full, sbm_node$bestModel$fittedSBM$memberships),
"block-node" = aricode::ARI(blocks_full, sbm_block$bestModel$fittedSBM$memberships))
## ----sampling-parameters------------------------------------------------------
fit <- sbm_block$bestModel
estimated_rate <- coef(fit, "sampling")[fit$fittedSBM$memberships] # for each blog
true_rate <- sampling_rate[blocks_full]
tapply(estimated_rate, true_rate, mean)
## ----auc----------------------------------------------------------------------
auc <- function(truth, prediction) {
r <- rank(prediction)
n1 <- sum(truth == 1); n0 <- sum(truth == 0)
(sum(r[truth == 1]) - n1 * (n1 + 1) / 2) / (n1 * n0)
}
missing <- which(is.na(blog_obs) & upper.tri(blog_obs))
c(node = auc(blog[missing], predict(sbm_node$bestModel)[missing]),
"block-node" = auc(blog[missing], predict(sbm_block$bestModel)[missing]))
## ----covariates-data----------------------------------------------------------
subgraph <- induced_subgraph(frenchblog2007, party %in% c("right", "left"))
subgraph <- delete_vertices(subgraph, which(degree(subgraph) == 0))
left <- 1 * (vertex_attr(subgraph, "party") == "left")
adjacency_sub <- as_adjacency_matrix(subgraph, sparse = FALSE)
subgraph_obs <- observeNetwork(adjacency_sub, "covar-node", parameters = 3,
covariates = list(left), intercept = -1)
## an observed blog has all its dyads observed: the proportion of observed blogs, right and left
tapply(rowSums(is.na(subgraph_obs)) == 0, c("right", "left")[left + 1], mean)
## ----covariates-fit-----------------------------------------------------------
vBlocks_sub <- 1:6
sbm_cov_sampling <- estimateMissSBM(subgraph_obs, vBlocks_sub, "covar-node", covariates = list(left),
control = missSBM_param(trace = FALSE, useCov = FALSE))
sbm_cov_both <- estimateMissSBM(subgraph_obs, vBlocks_sub, "covar-node", covariates = list(left),
control = missSBM_param(trace = FALSE, useCov = TRUE))
c("in the sampling" = min(sbm_cov_sampling$ICL), "in the sampling and the SBM" = min(sbm_cov_both$ICL))
## ----covariates-coef----------------------------------------------------------
coef(sbm_cov_both$bestModel, "sampling")
coef(sbm_cov_both$bestModel, "covariates")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.